package com.willalp.web.controller.clockingin;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.willalp.clockingin.domain.HsPnsShift;
import com.willalp.clockingin.service.IHsPnsShiftService;
import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.core.page.TableDataInfo;
import com.willalp.common.enums.BusinessType;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 考勤班次Controller
 *
 * @author willalp
 * @date 2022-04-26
 */
@RestController
@RequestMapping("/clockingin/shift")
public class HsPnsShiftController extends BaseController
{
    @Resource
    private IHsPnsShiftService hsPnsShiftService;

    /**
     * 查询考勤班次列表
     */
    @PreAuthorize("@ss.hasPermi('clockingin:shift:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsPnsShift hsPnsShift)
    {
        startPage();
        List<HsPnsShift> list = hsPnsShiftService.selectHsPnsShiftList(hsPnsShift);
        return getDataTable(list);
    }

    /**
     * 导出考勤班次列表
     */
    @PreAuthorize("@ss.hasPermi('clockingin:shift:export')")
    @Log(title = "考勤班次", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsPnsShift hsPnsShift)
    {
        List<HsPnsShift> list = hsPnsShiftService.selectHsPnsShiftList(hsPnsShift);
        ExcelUtil<HsPnsShift> util = new ExcelUtil<HsPnsShift>(HsPnsShift.class);
        return util.exportExcel(list, "考勤班次数据");
    }

    /**
     * 获取考勤班次详细信息
     */
    @PreAuthorize("@ss.hasPermi('clockingin:shift:query')")
    @GetMapping(value = "/{shiftId}")
    public AjaxResult getInfo(@PathVariable("shiftId") String shiftId)
    {
        return AjaxResult.success(hsPnsShiftService.getById(shiftId));
    }

    /**
     * 新增考勤班次
     */
    @PreAuthorize("@ss.hasPermi('clockingin:shift:add')")
    @Log(title = "考勤班次", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsPnsShift hsPnsShift)
    {
        while (true) {
            String idStr = IdWorker.getIdStr();
            HsPnsShift byId = hsPnsShiftService.getById(Long.valueOf(idStr));
            if (null == byId) {
                hsPnsShift.setShiftId(idStr);
                break;
            }
        }
        hsPnsShift.setCreateBy(SecurityUtils.getUsername());
        hsPnsShift.setCreateTime(DateUtils.getNowDate());
        return toAjax(hsPnsShiftService.save(hsPnsShift));
    }

    /**
     * 修改考勤班次
     */
    @PreAuthorize("@ss.hasPermi('clockingin:shift:edit')")
    @Log(title = "考勤班次", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsPnsShift hsPnsShift)
    {
        hsPnsShift.setUpdateBy(SecurityUtils.getUsername());
        hsPnsShift.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsPnsShiftService.updateById(hsPnsShift));
    }

    /**
     * 删除考勤班次
     */
    @PreAuthorize("@ss.hasPermi('clockingin:shift:remove')")
    @Log(title = "考勤班次", businessType = BusinessType.DELETE)
	@DeleteMapping("/{shiftIds}")
    public AjaxResult remove(@PathVariable String[] shiftIds)
    {
        return toAjax(hsPnsShiftService.removeByIds(Arrays.asList(shiftIds)));
    }
}
