package com.ruoyi.project.plant.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.project.plant.domain.TStaffmgr;
import com.ruoyi.project.plant.domain.TStaffmgrRecord;
import com.ruoyi.project.plant.service.ITStaffmgrRecordService;
import com.ruoyi.project.plant.service.ITStaffmgrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 人员操作记录Controller
 *
 * @author ruoyi
 * @date 2023-07-27
 */
@RestController
@RequestMapping("/plant/record")
public class TStaffmgrRecordController extends BaseController {
    @Autowired
    private ITStaffmgrRecordService tStaffmgrRecordService;
    @Autowired
    private ITStaffmgrService tStaffmgrService;

    /**
     * 查询人员操作记录列表
     */
    @PreAuthorize("@ss.hasPermi('plant:record:list')")
    @GetMapping("/list")
    public AjaxResult list(TStaffmgrRecord tStaffmgrRecord) {
        return AjaxResult.success(tStaffmgrRecordService.selectTStaffmgrRecordList(tStaffmgrRecord));
    }


    /**
     * 导出人员操作记录列表
     */
    @PreAuthorize("@ss.hasPermi('plant:record:export')")
    @Log(title = "人员操作记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TStaffmgrRecord tStaffmgrRecord) {
        List<TStaffmgrRecord> list = tStaffmgrRecordService.selectTStaffmgrRecordList(tStaffmgrRecord);
        ExcelUtil<TStaffmgrRecord> util = new ExcelUtil<TStaffmgrRecord>(TStaffmgrRecord.class);
        util.exportExcel(list, "人员操作记录数据");
    }

    /**
     * 获取人员操作记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('plant:record:query')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId) {
        return AjaxResult.success(tStaffmgrRecordService.selectTStaffmgrRecordByRecordId(recordId));
    }

    /**
     * 新增人员操作记录
     */
    @PreAuthorize("@ss.hasPermi('plant:record:add')")
    @Log(title = "人员操作记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TStaffmgrRecord tStaffmgrRecord) {
        if ("调班".equals(tStaffmgrRecord.getType())) {
            TStaffmgr tStaffmgr = new TStaffmgr();
            tStaffmgr.setId(tStaffmgrRecord.getStaffmgrId());
            tStaffmgr.setTeam(tStaffmgrRecord.getChange());
            tStaffmgr.setUpdatedate(new Date());
            tStaffmgr.setUpdaterCode(getUserId().toString());
            tStaffmgrService.updateTStaffmgr(tStaffmgr);
        }else if ("调岗".equals(tStaffmgrRecord.getType())) {
            TStaffmgr tStaffmgr = new TStaffmgr();
            tStaffmgr.setId(tStaffmgrRecord.getStaffmgrId());
            tStaffmgr.setActualpost(tStaffmgrRecord.getChange());
            tStaffmgr.setUpdatedate(new Date());
            tStaffmgr.setUpdaterCode(getUserId().toString());
            tStaffmgrService.updateTStaffmgr(tStaffmgr);
        }
        return toAjax(tStaffmgrRecordService.insertTStaffmgrRecord(tStaffmgrRecord));
    }

    /**
     * 修改人员操作记录
     */
    @PreAuthorize("@ss.hasPermi('plant:record:edit')")
    @Log(title = "人员操作记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TStaffmgrRecord tStaffmgrRecord) {
        if ("调班".equals(tStaffmgrRecord.getType())) {
            TStaffmgr tStaffmgr = new TStaffmgr();
            tStaffmgr.setId(tStaffmgrRecord.getStaffmgrId());
            tStaffmgr.setTeam(tStaffmgrRecord.getChange());
            tStaffmgr.setUpdatedate(new Date());
            tStaffmgr.setUpdaterCode(getUserId().toString());
            tStaffmgrService.updateTStaffmgr(tStaffmgr);
        }else if ("调岗".equals(tStaffmgrRecord.getType())) {
            TStaffmgr tStaffmgr = new TStaffmgr();
            tStaffmgr.setId(tStaffmgrRecord.getStaffmgrId());
            tStaffmgr.setActualpost(tStaffmgrRecord.getChange());
            tStaffmgr.setUpdatedate(new Date());
            tStaffmgr.setUpdaterCode(getUserId().toString());
            tStaffmgrService.updateTStaffmgr(tStaffmgr);
        }
        return toAjax(tStaffmgrRecordService.updateTStaffmgrRecord(tStaffmgrRecord));
    }

    /**
     * 删除人员操作记录
     */
    @PreAuthorize("@ss.hasPermi('plant:record:remove')")
    @Log(title = "人员操作记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds) {
        return toAjax(tStaffmgrRecordService.deleteTStaffmgrRecordByRecordIds(recordIds));
    }
}
