package com.ruoyi.project.production.controller;

import com.ruoyi.common.utils.StringUtils;
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.production.domain.*;
import com.ruoyi.project.production.service.ITPpeMatrixService;
import com.ruoyi.project.production.service.ITPpePersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * ppe矩阵管理Controller
 *
 * @author ssy
 * @date 2023-12-11
 */
@RestController
@RequestMapping("/production/matrix")
public class TPpeMatrixController extends BaseController {
    @Autowired
    private ITPpeMatrixService tPpeMatrixService;

    @Autowired
    private ITPpePersonService tPpePersonService;

    /**
     * 查询ppe矩阵管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TPpeMatrix tPpeMatrix) {
        startPage();
        List<TPpeMatrix> list = tPpeMatrixService.selectTPpeMatrixList(tPpeMatrix);
        return getDataTable(list);
    }

    @GetMapping("/listByPost")
    public AjaxResult listByPost(TPpeMain tPpeMain) {
        TPpePerson person = tPpePersonService.selectTPpePersonByEmployeeid(tPpeMain.getStaffId());
        TPpeMain main = new TPpeMain();
        List<TPpeWardrobe> wardrobes = new ArrayList<>();
        TPpeWardrobe tPpeWardrobe = new TPpeWardrobe();
        tPpeWardrobe.setPutAsRequired("是");
        tPpeWardrobe.setNoFood("是");
        tPpeWardrobe.setNoPollution("是");
        wardrobes.add(tPpeWardrobe);
        main.setWardrobes(wardrobes);
        String post = "";
        if ("1".equals(person.getSex())) {
            return AjaxResult.success(main);
        }
        Set<String> checkItems = new HashSet<>();
        TPpeMatrix tPpeMatrix = new TPpeMatrix();
        if ("1".equals(person.getBoiler())) {
            tPpeMatrix.setUsePost("裂解");
            post += "裂解、";
            for (TPpeMatrix ppeMatrix : tPpeMatrixService.selectTPpeMatrixList(tPpeMatrix)) {
                checkItems.add(ppeMatrix.getPpeType());
            }
        }
        if ("1".equals(person.getCracking())) {
            tPpeMatrix.setUsePost("热区");
            post += "热区、";
            for (TPpeMatrix ppeMatrix : tPpeMatrixService.selectTPpeMatrixList(tPpeMatrix)) {
                if (StringUtils.isNotEmpty(ppeMatrix.getAssigner()) && ppeMatrix.getAssigner().contains(tPpeMain.getStaffId()) && ppeMatrix.getPpeType().contains("指定人")) {
                    checkItems.add(ppeMatrix.getPpeType());
                }else if(!ppeMatrix.getPpeType().contains("指定人")){
                    checkItems.add(ppeMatrix.getPpeType());
                }

            }
        }
        if ("1".equals(person.getHotarea())) {
            tPpeMatrix.setUsePost("冷区");
            post += "冷区、";
            for (TPpeMatrix ppeMatrix : tPpeMatrixService.selectTPpeMatrixList(tPpeMatrix)) {
                checkItems.add(ppeMatrix.getPpeType());
            }
        }
        if ("1".equals(person.getColdarea())) {
            tPpeMatrix.setUsePost("PGU/AEU");
            post += "PGU/AEU、";
            for (TPpeMatrix ppeMatrix : tPpeMatrixService.selectTPpeMatrixList(tPpeMatrix)) {
                checkItems.add(ppeMatrix.getPpeType());
            }
        }
        if ("1".equals(person.getOperator())) {
            tPpeMatrix.setUsePost("白班操作员");
            post += "白班操作员、";
            for (TPpeMatrix ppeMatrix : tPpeMatrixService.selectTPpeMatrixList(tPpeMatrix)) {
                checkItems.add(ppeMatrix.getPpeType());
            }
        }
        if (post.endsWith("、")) {
            post = post.substring(0, post.length() - 1);
        }
        List<TPpeStaffLocker> staffLockers = new ArrayList<>();
        List<TPpeCmldefLocker> cmldefLockers = new ArrayList<>();
        for (String checkItem : checkItems) {
            if (checkItem.contains("防化服") || checkItem.contains("防化靴")) {
                TPpeCmldefLocker tPpeCmldefLocker = new TPpeCmldefLocker();
                tPpeCmldefLocker.setIsEquip("是");
                tPpeCmldefLocker.setIsPollution("是");
                tPpeCmldefLocker.setIsIdentification("是");
                tPpeCmldefLocker.setPutAsRequired("是");
                tPpeCmldefLocker.setPpeType(checkItem);
                tPpeCmldefLocker.setUsePost(post);
                cmldefLockers.add(tPpeCmldefLocker);
            } else {
                TPpeStaffLocker tPpeStaffLocker = new TPpeStaffLocker();
                tPpeStaffLocker.setIsEquip("是");
                tPpeStaffLocker.setIsPollution("是");
                tPpeStaffLocker.setIsIdentification("是");
                tPpeStaffLocker.setPutAsRequired("是");
                tPpeStaffLocker.setIsRequire("是");
                tPpeStaffLocker.setIsValidity("是");
                tPpeStaffLocker.setPpeType(checkItem);
                tPpeStaffLocker.setUsePost(post);
                staffLockers.add(tPpeStaffLocker);
            }
        }
        main.setStaffLockers(staffLockers);
        main.setCmldefLockers(cmldefLockers);
        return AjaxResult.success(main);
    }

    /**
     * 导出ppe矩阵管理列表
     */
    @Log(title = "ppe矩阵管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TPpeMatrix tPpeMatrix) {
        List<TPpeMatrix> list = tPpeMatrixService.selectTPpeMatrixList(tPpeMatrix);
        ExcelUtil<TPpeMatrix> util = new ExcelUtil<TPpeMatrix>(TPpeMatrix.class);
        return util.exportExcel(list, "matrix");
    }

    /**
     * 获取ppe矩阵管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tPpeMatrixService.selectTPpeMatrixById(id));
    }

    /**
     * 新增ppe矩阵管理
     */
    @Log(title = "ppe矩阵管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TPpeMatrix tPpeMatrix) {
        return toAjax(tPpeMatrixService.insertTPpeMatrix(tPpeMatrix));
    }

    /**
     * 修改ppe矩阵管理
     */
    @Log(title = "ppe矩阵管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TPpeMatrix tPpeMatrix) {
        return toAjax(tPpeMatrixService.updateTPpeMatrix(tPpeMatrix));
    }

    /**
     * 删除ppe矩阵管理
     */
    @Log(title = "ppe矩阵管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tPpeMatrixService.deleteTPpeMatrixByIds(ids));
    }
}
