package com.ruoyi.project.training.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.project.plant.domain.TStaffmgr;
import com.ruoyi.project.plant.service.ITStaffmgrService;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.service.ISysDictTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.training.domain.TWorklicenseCertificate;
import com.ruoyi.project.training.service.ITWorklicenseCertificateService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 上岗证/作业证书一览Controller
 *
 * @author ssy
 * @date 2024-03-04
 */
@RestController
@RequestMapping("/training/worklicensecertificate")
public class TWorklicenseCertificateController extends BaseController
{
    @Autowired
    private ITWorklicenseCertificateService tWorklicenseCertificateService;

    @Autowired
    private ITStaffmgrService staffmgrService;

    @Autowired
    private ISysDictTypeService iSysDictTypeService;

    /**
     * 同步人员数据
     */
    @PreAuthorize("@ss.hasPermi('training:worklicensecertificate:add')")
    @GetMapping("/syncStaff")
    public AjaxResult syncStaff()
    {
        //字典查询
        List<SysDictData> teamDivideDict = iSysDictTypeService.selectDictDataByType("TEAM_DIVIDE");
        List<SysDictData> actualPostDict = iSysDictTypeService.selectDictDataByType("ACTUALPOST");
        String teamValueA = "";
        String teamValueB = "";
        String teamValueC = "";
        String teamValueD = "";
        for (SysDictData sysDictData : teamDivideDict) {
            if (sysDictData.getDictLabel().equals("A")) {
                teamValueA = sysDictData.getDictValue();
            }
            if (sysDictData.getDictLabel().equals("B")) {
                teamValueB = sysDictData.getDictValue();
            }
            if (sysDictData.getDictLabel().equals("C")) {
                teamValueC = sysDictData.getDictValue();
            }
            if (sysDictData.getDictLabel().equals("D")) {
                teamValueD = sysDictData.getDictValue();
            }
        }
        // 员工表
        List<TStaffmgr> tStaffmgrs = new ArrayList<TStaffmgr>();
        List<TStaffmgr> tStaffmgrAll = staffmgrService.selectTStaffmgrList(new TStaffmgr());
        for (TStaffmgr tStaffmgr : tStaffmgrAll) {
            String team = tStaffmgr.getTeam();
            if (teamValueA.equals(team) || teamValueB.equals(team) || teamValueC.equals(team) || teamValueD.equals(team)) {
                tStaffmgrs.add(tStaffmgr);
            }
        }
        // 上岗证/作业证书表
        List<TWorklicenseCertificate> tWorklicenseCertificates = tWorklicenseCertificateService.selectTWorklicenseCertificateList(new TWorklicenseCertificate());
        // 新增列表
        List<TWorklicenseCertificate> tWorklicenseCertificatesAdd = new ArrayList<TWorklicenseCertificate>();
        // 对比两张表
        for (TStaffmgr tStaffmgr : tStaffmgrs) {
            String staffid = tStaffmgr.getStaffid();
            boolean isAddFlag = false;
            for (TWorklicenseCertificate tWorklicenseCertificate : tWorklicenseCertificates) {
                String employeeid = tWorklicenseCertificate.getEmployeeid();
                if (employeeid.equals(staffid)) {
                    isAddFlag = true;
                }
            }
            if (!isAddFlag) {
                TWorklicenseCertificate cert = new TWorklicenseCertificate();
                cert.setDeptId(tStaffmgr.getDeptId());
                cert.setName(tStaffmgr.getName());
                cert.setEmployeeid(tStaffmgr.getStaffid());
                for (SysDictData sysDictData : teamDivideDict) {
                    if (sysDictData.getDictValue().equals(tStaffmgr.getTeam())) {
                        cert.setClasses(sysDictData.getDictLabel());
                    }
                }
                for (SysDictData sysDictData : actualPostDict) {
                    if (sysDictData.getDictValue().equals(tStaffmgr.getActualpost())) {
                        cert.setPost(sysDictData.getDictLabel());
                    }
                }
                tWorklicenseCertificatesAdd.add(cert);
            }
        }
        for (TWorklicenseCertificate tWorklicenseCertificate : tWorklicenseCertificatesAdd) {
            tWorklicenseCertificateService.insertTWorklicenseCertificate(tWorklicenseCertificate);
        }
        return AjaxResult.success();
    }

    /**
     * 查询上岗证/作业证书一览列表
     */
    @PreAuthorize("@ss.hasPermi('training:worklicensecertificate:list')")
    @GetMapping("/list")
    public TableDataInfo list(TWorklicenseCertificate tWorklicenseCertificate)
    {
        startPage();
        List<TWorklicenseCertificate> list = tWorklicenseCertificateService.selectTWorklicenseCertificateList(tWorklicenseCertificate);
        for (TWorklicenseCertificate worklicenseCertificate : list) {
            String cracking = worklicenseCertificate.getCracking();
            String hotarea = worklicenseCertificate.getHotarea();
            String coldarea = worklicenseCertificate.getColdarea();
            String aromatic = worklicenseCertificate.getAromatic();
            // 是否高亮
            worklicenseCertificate.setIsHighlight(0);
            // 岗位-作业证书矩阵
            // 裂解(裂解/SUB)岗位:特种设备管理(压力容器/管道操作证书);危险化学品作业-裂解(裂化)工艺;锅炉操作证。
            // 热区(压缩)岗位:特种设备管理(压力容器/管道操作证书);危险化学品作业-加氢工艺。
            // 冷区(分离)岗位:特种设备管理(压力容器/管道操作证书);危险化学品作业-加氢工艺;特种作业-制冷与空调(指定人员)。
            // 芳烃(PGU&AEU)岗位:特种设备管理(压力容器/管道操作证书);危险化学品作业-加氢工艺。
            if ("1".equals(cracking)) {
                if (worklicenseCertificate.getContainer() == null
                        || worklicenseCertificate.getPipe() == null
                        || worklicenseCertificate.getCrackingid() == null
                        || worklicenseCertificate.getBoilerid() == null) {
                    worklicenseCertificate.setIsHighlight(1);
                }
            }
            if ("1".equals(hotarea)) {
                if (worklicenseCertificate.getContainer() == null
                        || worklicenseCertificate.getPipe() == null
                        || worklicenseCertificate.getCrackingid() == null) {
                    worklicenseCertificate.setIsHighlight(1);
                }
            }
            if ("1".equals(coldarea)) {
                if (worklicenseCertificate.getContainer() == null
                        || worklicenseCertificate.getPipe() == null
                        || worklicenseCertificate.getCrackingid() == null
                        || worklicenseCertificate.getRefrigeration() == null) {
                    worklicenseCertificate.setIsHighlight(1);
                }
            }
            if ("1".equals(aromatic)) {
                if (worklicenseCertificate.getContainer() == null
                        || worklicenseCertificate.getPipe() == null
                        || worklicenseCertificate.getCrackingid() == null) {
                    worklicenseCertificate.setIsHighlight(1);
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出上岗证/作业证书一览列表
     */
    @PreAuthorize("@ss.hasPermi('training:worklicensecertificate:export')")
    @Log(title = "上岗证/作业证书一览", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TWorklicenseCertificate tWorklicenseCertificate)
    {
        List<TWorklicenseCertificate> list = tWorklicenseCertificateService.selectTWorklicenseCertificateList(tWorklicenseCertificate);
        ExcelUtil<TWorklicenseCertificate> util = new ExcelUtil<TWorklicenseCertificate>(TWorklicenseCertificate.class);
        return util.exportExcel(list, "worklicensecertificate");
    }

    /**
     * 获取上岗证/作业证书一览详细信息
     */
    @PreAuthorize("@ss.hasPermi('training:worklicensecertificate:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tWorklicenseCertificateService.selectTWorklicenseCertificateById(id));
    }

    /**
     * 新增上岗证/作业证书一览
     */
    @PreAuthorize("@ss.hasPermi('training:worklicensecertificate:add')")
    @Log(title = "上岗证/作业证书一览", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TWorklicenseCertificate tWorklicenseCertificate)
    {
        return toAjax(tWorklicenseCertificateService.insertTWorklicenseCertificate(tWorklicenseCertificate));
    }

    /**
     * 修改上岗证/作业证书一览
     */
    @PreAuthorize("@ss.hasPermi('training:worklicensecertificate:edit')")
    @Log(title = "上岗证/作业证书一览", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TWorklicenseCertificate tWorklicenseCertificate)
    {
        return toAjax(tWorklicenseCertificateService.updateTWorklicenseCertificate(tWorklicenseCertificate));
    }

    /**
     * 删除上岗证/作业证书一览
     */
    @PreAuthorize("@ss.hasPermi('training:worklicensecertificate:remove')")
    @Log(title = "上岗证/作业证书一览", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tWorklicenseCertificateService.deleteTWorklicenseCertificateByIds(ids));
    }
}
