package com.ruoyi.project.production.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.plant.domain.TStaffmgr;
import com.ruoyi.project.plant.service.ITStaffmgrService;
import com.ruoyi.project.production.domain.TPpePerson;
import com.ruoyi.project.production.service.ITPpePersonService;
import com.ruoyi.project.system.service.ISysUserService;
import com.ruoyi.project.training.domain.TWorklicense;
import com.ruoyi.project.training.service.ITWorklicenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 员工PPE检查Controller
 *
 * @author ssy
 * @date 2023-11-28
 */
@RestController
@RequestMapping("/production/person")
public class TPpePersonController extends BaseController {
    @Autowired
    private ITPpePersonService tPpePersonService;

    @Autowired
    private ITWorklicenseService tWorklicenseService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ITStaffmgrService tStaffmgrService;

    /**
     * 查询员工PPE检查列表
     */
    @PreAuthorize("@ss.hasPermi('production:person:list')")
    @GetMapping("/list")
    public TableDataInfo list(TPpePerson tPpePerson) {
        startPage();
        List<TPpePerson> list = tPpePersonService.selectTPpePersonList(tPpePerson);
        return getDataTable(list);
    }

    /**
     * 查询员工PPE检查列表
     */
    @PreAuthorize("@ss.hasPermi('production:person:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll(TPpePerson tPpePerson) {
        return AjaxResult.success(tPpePersonService.selectTPpePersonList(tPpePerson));
    }

    /**
     * 导出员工PPE检查列表
     */
    @PreAuthorize("@ss.hasPermi('production:person:export')")
    @Log(title = "员工PPE检查", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TPpePerson tPpePerson) {
        List<TPpePerson> list = tPpePersonService.selectTPpePersonList(tPpePerson);
        ExcelUtil<TPpePerson> util = new ExcelUtil<TPpePerson>(TPpePerson.class);
        return util.exportExcel(list, "person");
    }

    /**
     * 获取员工PPE检查详细信息
     */
    @PreAuthorize("@ss.hasPermi('production:person:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tPpePersonService.selectTPpePersonById(id));
    }

    /**
     * 新增员工PPE检查
     */
    @PreAuthorize("@ss.hasPermi('production:person:add')")
    @Log(title = "员工PPE检查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TPpePerson tPpePerson) {
        TStaffmgr tStaffmgr = new TStaffmgr();
        tStaffmgr.setUnits("10");
        for (TStaffmgr staffmgr : tStaffmgrService.selectTStaffmgrList(tStaffmgr)) {
            TPpePerson person = new TPpePerson();
            person.setEmployeeid(staffmgr.getStaffid());
            person.setPlantCode(staffmgr.getPlantCode());
            person.setName(staffmgr.getName());
            person.setClasses(staffmgr.getTeam());
            person.setSex(staffmgr.getSex());
            person.setBoiler("0");
            person.setCracking("0");
            person.setHotarea("0");
            person.setColdarea("0");
            person.setDeptId(staffmgr.getDeptId());
            if ("36".equals(staffmgr.getActualpost())) {
                person.setOperator("1");
            } else {
                person.setOperator("0");
            }
            insertOrUpdate(person);
        }
        for (TWorklicense tWorklicense : tWorklicenseService.selectTWorklicenseList(new TWorklicense())) {
            try {
                TPpePerson person = new TPpePerson();
                person.setEmployeeid(tWorklicense.getEmployeeid());
                person.setPlantCode(tWorklicense.getPlantCode());
                person.setName(tWorklicense.getName());
                TStaffmgr staffmgr = tStaffmgrService.selectTStaffmgrByStaffId(tWorklicense.getEmployeeid());
                if (staffmgr != null)
                    person.setSex(staffmgr.getSex());
                person.setBoiler(tWorklicense.getBoiler());
                person.setCracking(tWorklicense.getCracking());
                person.setHotarea(tWorklicense.getHotarea());
                person.setColdarea(tWorklicense.getColdarea());
                person.setDeptId(tWorklicense.getDeptId());
                person.setOperator("0");
                TPpePerson ppePerson = tPpePersonService.selectTPpePersonByEmployeeid(person.getEmployeeid());
                if (ppePerson != null) {
                    person.setId(ppePerson.getId());
                    person.setUpdatedate(new Date());
                    person.setUpdaterCode(getUserId().toString());
                    tPpePersonService.updateTPpePerson(person);
                }
            } catch (Exception e) {
                logger.info("================================:{}",tWorklicense.getEmployeeid());
                e.printStackTrace();
            }
        }
        tPpePersonService.deleteTPpePersonByShiftmgr();
        return AjaxResult.success();
    }

    private void insertOrUpdate(TPpePerson person) {
        TPpePerson ppePerson = tPpePersonService.selectTPpePersonByEmployeeid(person.getEmployeeid());
        if (ppePerson != null) {
            person.setId(ppePerson.getId());
            person.setUpdatedate(new Date());
            person.setUpdaterCode(getUserId().toString());
            tPpePersonService.updateTPpePerson(person);
        } else {
            person.setCreaterCode(getUserId().toString());
            person.setCreatedate(new Date());
            tPpePersonService.insertTPpePerson(person);
        }
    }

    /**
     * 修改员工PPE检查
     */
    @PreAuthorize("@ss.hasPermi('production:person:edit')")
    @Log(title = "员工PPE检查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TPpePerson tPpePerson) {
        return toAjax(tPpePersonService.updateTPpePerson(tPpePerson));
    }

    /**
     * 删除员工PPE检查
     */
    @PreAuthorize("@ss.hasPermi('production:person:remove')")
    @Log(title = "员工PPE检查", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tPpePersonService.deleteTPpePersonByIds(ids));
    }
}
