package com.jjt.emp.controller;

import com.jjt.common.annotation.Log;
import com.jjt.common.core.controller.BaseController;
import com.jjt.common.core.domain.AjaxResult;
import com.jjt.common.core.page.TableDataInfo;
import com.jjt.common.enums.BusinessType;
import com.jjt.common.utils.DateUtils;
import com.jjt.common.utils.StringUtils;
import com.jjt.common.utils.poi.ExcelUtil;
import com.jjt.emp.domain.TwinEmp;
import com.jjt.emp.service.ITwinEmpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 员工排班Controller
 *
 * @author wukai
 * @date 2025-01-18
 */
@Api(tags = "员工排班")
@RestController
@RequestMapping("/emp/emp")
public class TwinEmpController extends BaseController {
    @Resource
    private ITwinEmpService twinEmpService;

    /**
     * 查询员工排班列表
     */
    @ApiOperation("查询员工排班列表")
    //@PreAuthorize("@ss.hasPermi('emp:emp:list')")
    @GetMapping("/list")
    public TableDataInfo list(TwinEmp twinEmp) {
        startPage();
        List<TwinEmp> list = twinEmpService.selectTwinEmpList(twinEmp);
        return getDataTable(list);
    }

    /**
     * 导出员工排班列表
     */
    @ApiOperation("导出员工排班列表")
    //@PreAuthorize("@ss.hasPermi('emp:emp:export')")
    @Log(title = "员工排班", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TwinEmp twinEmp) {
        List<TwinEmp> list = twinEmpService.selectTwinEmpList(twinEmp);
        ExcelUtil<TwinEmp> util = new ExcelUtil<TwinEmp>(TwinEmp.class);
        util.exportExcel(response, list, "员工排班数据");
    }

    /**
     * 获取员工排班详细信息
     */
    @ApiOperation("获取员工排班详细信息")
    //@PreAuthorize("@ss.hasPermi('emp:emp:query')")
    @GetMapping(value = "/{empId}")
    public AjaxResult getInfo(@PathVariable("empId") Long empId) {
        return success(twinEmpService.selectTwinEmpByEmpId(empId));
    }

    /**
     * 新增员工排班
     */
    @ApiOperation("新增员工排班")
    //@PreAuthorize("@ss.hasPermi('emp:emp:add')")
    @Log(title = "员工排班", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TwinEmp twinEmp) {
        TwinEmp search = new TwinEmp();
        search.setEmpDate(twinEmp.getEmpDate());
        List list = twinEmpService.selectTwinEmpList(search);
        if (list.size() > 0) {
            return AjaxResult.error("已经有\"" + DateUtils.parseDateToStr(twinEmp.getEmpDate()) + "\"排班，请到对应排班日期修改");
        } else {
            if (StringUtils.isNull(twinEmp.getTwinEmpDetailListA()) || StringUtils.isNull(twinEmp.getTwinEmpDetailListB())) {
                return AjaxResult.error("AB班员工排班明细不能为空");
            } else {
                return toAjax(twinEmpService.insertTwinEmp(twinEmp));
            }
        }
    }

    /**
     * 修改员工排班
     */
    @ApiOperation("修改员工排班")
    //@PreAuthorize("@ss.hasPermi('emp:emp:edit')")
    @Log(title = "员工排班", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TwinEmp twinEmp) {
        return toAjax(twinEmpService.updateTwinEmp(twinEmp));
    }

    /**
     * 删除员工排班
     */
    @ApiOperation("删除员工排班")
    //@PreAuthorize("@ss.hasPermi('emp:emp:remove')")
    @Log(title = "员工排班", businessType = BusinessType.DELETE)
    @DeleteMapping("/{empIds}")
    public AjaxResult remove(@PathVariable Long[] empIds) {
        return toAjax(twinEmpService.deleteTwinEmpByEmpIds(empIds));
    }
}
