package com.ruoyi.project.patrol.emergency.controller;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.ExcelUtils;
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.interceptor.annotation.RepeatSubmit;
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.patrol.dev.domain.TDevCabinet;
import com.ruoyi.project.patrol.dev.service.ITDevCabinetService;
import com.ruoyi.project.patrol.emergency.domain.TEmergencyCabinet;
import com.ruoyi.project.patrol.emergency.service.ITEmergencyCabinetService;
import com.ruoyi.project.patrol.patrol.domain.TPatrolIssues;
import com.ruoyi.project.patrol.patrol.domain.TPatrolPlan;
import com.ruoyi.project.patrol.patrol.service.ITPatrolIssuesService;
import com.ruoyi.project.patrol.patrol.service.ITPatrolPlanService;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * 应急事故柜Controller
 *
 * @author admin
 * @date 2023-11-07
 */
@RestController
@RequestMapping("/emergency/cabinet")
public class TEmergencyCabinetController extends BaseController {
    @Autowired
    private ITEmergencyCabinetService tEmergencyCabinetService;

    @Autowired
    private ITDevCabinetService tDevCabinetService;

    @Autowired
    private ITPatrolPlanService tPatrolPlanService;

    @Autowired
    private ITPatrolIssuesService tPatrolIssuesService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询应急事故柜列表
     */
    @PreAuthorize("@ss.hasPermi('emergency:cabinet:list')")
    @GetMapping("/list")
    public TableDataInfo list(TEmergencyCabinet tEmergencyCabinet) {
        startPage();
        if (StringUtils.isEmpty(tEmergencyCabinet.getIssuesFlag()))
            tEmergencyCabinet.setIssuesFlag("0");
        List<TEmergencyCabinet> list = tEmergencyCabinetService.selectTEmergencyCabinetList(tEmergencyCabinet);
        return getDataTable(list);
    }

    /**
     * 导出应急事故柜列表
     */
    
    @Log(title = "应急事故柜", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TEmergencyCabinet tEmergencyCabinet) {
        try {
            tEmergencyCabinet.setIssuesFlag("0");
            XSSFWorkbook workbook = new XSSFWorkbook();
            List<TEmergencyCabinet> list1 = generateSheet1(workbook, tEmergencyCabinet);
            List<TEmergencyCabinet> list2 = generateSheet(workbook, tEmergencyCabinet, "2");
            List<TEmergencyCabinet> list3 = generateSheet(workbook, tEmergencyCabinet, "3");
            if (list1.size() == 0 || list2.size() == 0 || list3.size() == 0) {
                return AjaxResult.error("暂无可导出数据");
            }
            // 设置单元格样式
            // 文字居中，自动换行，四周边框
            CellStyle style = workbook.createCellStyle();
            style.setAlignment(HorizontalAlignment.CENTER);
            style.setVerticalAlignment(VerticalAlignment.CENTER);
            style.setWrapText(true);
            style.setBorderTop(BorderStyle.THIN);
            style.setBorderBottom(BorderStyle.THIN);
            style.setBorderLeft(BorderStyle.THIN);
            style.setBorderRight(BorderStyle.THIN);

            // 设置垂直居中，四周边框
            CellStyle style2 = workbook.createCellStyle();
            style2.setVerticalAlignment(VerticalAlignment.CENTER);
            style2.setBorderTop(BorderStyle.THIN);
            style2.setBorderBottom(BorderStyle.THIN);
            style2.setBorderLeft(BorderStyle.THIN);
            style2.setBorderRight(BorderStyle.THIN);
            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
                XSSFSheet sheet = workbook.getSheetAt(i);
                for (Row row : sheet) {
                    row.setHeightInPoints(30);
                    if (!sheet.getSheetName().contains("事故柜1")) {
                        if (row.getRowNum() == 4) {
                            row.setHeightInPoints(80);
                        }
                    }
                    if (row.getRowNum() == 0) {
                        continue;
                    }
                    if (row.getRowNum() == 2) {
                        for (Cell cell : row) {
                            if (cell.getColumnIndex() == 0 || cell.getColumnIndex() == 1 || cell.getColumnIndex() == 2)
                                cell.setCellStyle(style2);
                            else
                                cell.setCellStyle(style);
                        }
                    } else {
                        for (Cell cell : row) {
                            cell.setCellStyle(style);
                        }
                    }
                }
                int nextRowNum = sheet.getLastRowNum();
                nextRowNum++;
                XSSFRow row30 = sheet.createRow(nextRowNum);
                row30.createCell(0).setCellValue("说明：");
                ExcelUtils.createVoidCell(1, 8, row30);
                sheet.addMergedRegion(new CellRangeAddress(nextRowNum, nextRowNum, 0, 8));

                nextRowNum++;
                XSSFRow row31 = sheet.createRow(nextRowNum);
                row31.createCell(0).setCellValue("1.检查正常打“√”。");
                ExcelUtils.createVoidCell(1, 8, row31);
                sheet.addMergedRegion(new CellRangeAddress(nextRowNum, nextRowNum, 0, 8));

                nextRowNum++;
                XSSFRow row32 = sheet.createRow(nextRowNum);
                row32.createCell(0).setCellValue("2.检查有不正常情况打“X”，并记录存在问题，处理结果及完成时间。");
                ExcelUtils.createVoidCell(1, 8, row32);
                sheet.addMergedRegion(new CellRangeAddress(nextRowNum, nextRowNum, 0, 8));

                nextRowNum++;
                XSSFRow row33 = sheet.createRow(nextRowNum);
                row33.createCell(0).setCellValue("3. 检查频率为每一个月检查维护一次。");
                ExcelUtils.createVoidCell(1, 8, row33);
                sheet.addMergedRegion(new CellRangeAddress(nextRowNum, nextRowNum, 0, 8));
            }

            //返回
            String fileName = "PSSR_13_消气防设施_应急事故柜检查记录.xlsx";
            OutputStream os = Files.newOutputStream(Paths.get(ExcelUtil.getAbsoluteFile(fileName)));
            workbook.write(os);
            workbook.close();
            return AjaxResult.success(fileName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private List<TEmergencyCabinet> generateSheet1(XSSFWorkbook workbook, TEmergencyCabinet tEmergencyCabinet) {
        tEmergencyCabinet.setDevType("1");
        List<TEmergencyCabinet> list = tEmergencyCabinetService.selectTEmergencyCabinetList(tEmergencyCabinet);
        XSSFSheet sheet = workbook.createSheet("事故柜1检查记录");// 设置列宽
        sheet.setColumnWidth(0, 1200);
        sheet.setColumnWidth(1, 4500);
        sheet.setColumnWidth(2, 3500);
        sheet.setColumnWidth(3, 3500);
        sheet.setColumnWidth(4, 3500);
        sheet.setColumnWidth(5, 4500);
        sheet.setColumnWidth(6, 4500);
        sheet.setColumnWidth(7, 3500);
        //第一行
        XSSFRow row1 = sheet.createRow(0);
        row1.createCell(0).setCellValue("CBP-6.3.0-SCFSH-020.07  Rev:04  2021/10/01");
        ExcelUtils.createVoidCell(1, 7, row1);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));//合并
        //第二行
        XSSFRow row2 = sheet.createRow(1);
        row2.createCell(0).setCellValue("事故柜1检查记录");
        ExcelUtils.createVoidCell(1, 7, row2);
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 7));
        //第三行
        XSSFRow row3 = sheet.createRow(2);
        row3.createCell(0).setCellValue("年度：" + tEmergencyCabinet.getYear());
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 2));
        ExcelUtils.createVoidCell(1, 7, row3);
        row3.createCell(3).setCellValue("检查频率：1次/月");
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 3, 7));
        //第四行
        XSSFRow row4 = sheet.createRow(3);
        row4.createCell(0).setCellValue("序号");
        row4.createCell(1).setCellValue("器材名称");
        row4.createCell(2).setCellValue("器材标配数量");
        row4.createCell(3).setCellValue("检查内容");
        ExcelUtils.createVoidCell(4, 7, row4);
        sheet.addMergedRegion(new CellRangeAddress(3, 3, 3, 7));
        //第五行
        XSSFRow row5 = sheet.createRow(4);
        sheet.addMergedRegion(new CellRangeAddress(3, 4, 0, 0));
        sheet.addMergedRegion(new CellRangeAddress(3, 4, 1, 1));
        sheet.addMergedRegion(new CellRangeAddress(3, 4, 2, 2));
        ExcelUtils.createVoidCell(0, 2, row5);
        row5.createCell(3).setCellValue("器材数量");
        row5.createCell(4).setCellValue("器材完好度");
        row5.createCell(5).setCellValue("器材整洁度");
        row5.createCell(6).setCellValue("器材标签有效期");
        row5.createCell(7).setCellValue("检查人");
        Long planId = null;
        //遍历数组赋值
        for (int i = 0; i < list.size(); i++) {
            TEmergencyCabinet cabinet = list.get(i);
            planId = cabinet.getPlanId();
            TDevCabinet tDevCabinet = new TDevCabinet();
            tDevCabinet.setDevType(cabinet.getDevType());
            tDevCabinet.setDevName(cabinet.getDevName());
            tDevCabinet.setDevNum(cabinet.getDevNum());
            TDevCabinet item = tDevCabinetService.selectTDevCabinetList(tDevCabinet).get(0);
            XSSFRow row = sheet.createRow(5 + i);
            row.createCell(0).setCellValue(i + 1);
            row.createCell(1).setCellValue(cabinet.getDevName());
            row.createCell(2).setCellValue(cabinet.getDevNum());
            if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("quantityEquipment")) {
                row.createCell(3).setCellValue("/");
            } else
                row.createCell(3).setCellValue("1".equals(cabinet.getQuantityEquipment()) ? "√" : cabinet.getQuantityEquipment());
            if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("equipmentIntegrity")) {
                row.createCell(4).setCellValue("/");
            } else
                row.createCell(4).setCellValue("1".equals(cabinet.getEquipmentIntegrity()) ? "√" : cabinet.getEquipmentIntegrity());
            if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("equipmentCleanliness")) {
                row.createCell(5).setCellValue("/");
            } else
                row.createCell(5).setCellValue("1".equals(cabinet.getEquipmentCleanliness()) ? "√" : cabinet.getEquipmentCleanliness());
            if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("labelValidity")) {
                row.createCell(6).setCellValue("/");
            } else
                row.createCell(6).setCellValue("1".equals(cabinet.getLabelValidity()) ? "√" : cabinet.getLabelValidity());
            XSSFCell cell10 = row.createCell(7);
            if (StringUtils.isNotEmpty(cabinet.getUpdaterCode())) {
                SysUser sysUser = sysUserService.selectUserById(Long.valueOf(cabinet.getUpdaterCode()));
                cell10.setCellValue(sysUser == null ? "" : sysUser.getNickName());
            }
        }
        String confirmed = "";
        Date confirmedDate = null;
        TPatrolPlan tPatrolPlan = tPatrolPlanService.selectTPatrolPlanById(planId);
        if (Objects.nonNull(tPatrolPlan) && Objects.nonNull(tPatrolPlan.getConfirmed())) {
            SysUser sysUser = sysUserService.selectUserById(Long.valueOf(tPatrolPlan.getConfirmed()));
            if (sysUser!=null) {
                confirmed = sysUser.getSignUrl();
                confirmedDate = tPatrolPlan.getConfirmDate();
            }
        }
        // 循环后下一行
        int nextRowNum = 5 + list.size();
        XSSFRow row28 = sheet.createRow(nextRowNum);
        row28.setHeightInPoints(30);
        XSSFCell row28Cell1 = row28.createCell(0);
        row28Cell1.setCellValue("签名");
        ExcelUtils.createVoidCell(1, 2, row28);
        sheet.addMergedRegion(new CellRangeAddress(nextRowNum, nextRowNum, 0, 2));
        if (StringUtils.isNotEmpty(confirmed))
            ExcelUtils.insertPicture(workbook, sheet, confirmed, nextRowNum, 5, 1, 1);
        ExcelUtils.createVoidCell(3, 7, row28);
        sheet.addMergedRegion(new CellRangeAddress(nextRowNum, nextRowNum, 3, 7));
        // 下一行
        nextRowNum++;
        XSSFRow row29 = sheet.createRow(nextRowNum);
        row29.setHeightInPoints(30);
        XSSFCell row29Cell1 = row29.createCell(0);
        row29Cell1.setCellValue("日期");
        ExcelUtils.createVoidCell(1, 2, row29);
        sheet.addMergedRegion(new CellRangeAddress(nextRowNum, nextRowNum, 0, 2));
        row29.createCell(3).setCellValue(confirmedDate != null ? new SimpleDateFormat("yyyy-MM-dd").format(confirmedDate) : "");
        ExcelUtils.createVoidCell(4, 7, row29);
        sheet.addMergedRegion(new CellRangeAddress(nextRowNum, nextRowNum, 3, 7));
        return list;
    }

    private List<TEmergencyCabinet> generateSheet(XSSFWorkbook workbook, TEmergencyCabinet tEmergencyCabinet, String devType) {
        tEmergencyCabinet.setDevType(devType);
        List<TEmergencyCabinet> list = tEmergencyCabinetService.selectTEmergencyCabinetList(tEmergencyCabinet);
        XSSFSheet sheet = workbook.createSheet("事故柜" + devType + "检查记录");// 设置列宽
        sheet.setColumnWidth(0, 1200);
        sheet.setColumnWidth(1, 4500);
        sheet.setColumnWidth(2, 3500);
        sheet.setColumnWidth(3, 3500);
        sheet.setColumnWidth(4, 3500);
        sheet.setColumnWidth(5, 4500);
        sheet.setColumnWidth(6, 4500);
        sheet.setColumnWidth(7, 3500);
        //第一行
        XSSFRow row1 = sheet.createRow(0);
        row1.createCell(0).setCellValue("CBP-6.3.0-SCFSH-020.08  Rev:04  2021/10/01");
        ExcelUtils.createVoidCell(1, 7, row1);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));//合并
        //第二行
        XSSFRow row2 = sheet.createRow(1);
        row2.createCell(0).setCellValue("事故柜1检查记录");
        ExcelUtils.createVoidCell(1, 7, row2);
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 7));
        //第三行
        XSSFRow row3 = sheet.createRow(2);
        row3.createCell(0).setCellValue("年度：" + tEmergencyCabinet.getYear());
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 2));
        ExcelUtils.createVoidCell(1, 7, row3);
        row3.createCell(3).setCellValue("检查频率：1次/月");
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 3, 7));
        //第四行
        XSSFRow row4 = sheet.createRow(3);
        row4.createCell(0).setCellValue("序号");
        row4.createCell(1).setCellValue("器材名称");
        row4.createCell(2).setCellValue("器材标配数量");
        row4.createCell(3).setCellValue("检查内容");
        ExcelUtils.createVoidCell(4, 7, row4);
        sheet.addMergedRegion(new CellRangeAddress(3, 3, 3, 7));
        //第五行
        XSSFRow row5 = sheet.createRow(4);
        sheet.addMergedRegion(new CellRangeAddress(3, 4, 0, 0));
        sheet.addMergedRegion(new CellRangeAddress(3, 4, 1, 1));
        sheet.addMergedRegion(new CellRangeAddress(3, 4, 2, 2));
        ExcelUtils.createVoidCell(0, 2, row5);
        row5.createCell(3).setCellValue("器材数量");
        row5.createCell(4).setCellValue("器材完好度");
        row5.createCell(5).setCellValue("自给空气呼吸器气压值（>27MPa）");
        row5.createCell(6).setCellValue("自给空气呼吸器有效日期 （钢瓶有效期15年，三年检查一次并贴标）");
        row5.createCell(7).setCellValue("检查人");
        Long planId = null;
        //遍历数组赋值
        for (int i = 0; i < list.size(); i++) {
            TEmergencyCabinet cabinet = list.get(i);
            planId = cabinet.getPlanId();
            XSSFRow row = sheet.createRow(5 + i);
            row.createCell(0).setCellValue(i + 1);
            row.createCell(1).setCellValue(cabinet.getDevName());
            row.createCell(2).setCellValue(cabinet.getDevNum());
            TDevCabinet tDevCabinet = new TDevCabinet();
            tDevCabinet.setDevType(cabinet.getDevType());
            tDevCabinet.setDevName(cabinet.getDevName());
            tDevCabinet.setDevNum(cabinet.getDevNum());
            TDevCabinet item = tDevCabinetService.selectTDevCabinetList(tDevCabinet).get(0);
            if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("quantityEquipment")) {
                row.createCell(3).setCellValue("/");
            } else
                row.createCell(3).setCellValue("1".equals(cabinet.getQuantityEquipment()) ? "√" : cabinet.getQuantityEquipment());
            if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("quantityEquipment")) {
                row.createCell(4).setCellValue("/");
            } else
                row.createCell(4).setCellValue("1".equals(cabinet.getEquipmentIntegrity()) ? "√" : cabinet.getEquipmentIntegrity());
            if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("barometricValue")) {
                row.createCell(5).setCellValue("/");
            } else
                row.createCell(5).setCellValue("1".equals(cabinet.getBarometricValue()) ? "√" : cabinet.getBarometricValue());
            if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("respiratorExpiration")) {
                row.createCell(6).setCellValue("/");
            } else
                row.createCell(6).setCellValue("1".equals(cabinet.getRespiratorExpiration()) ? "√" : cabinet.getRespiratorExpiration());
            XSSFCell cell10 = row.createCell(7);
            if (StringUtils.isNotEmpty(cabinet.getUpdaterCode())) {
                SysUser sysUser = sysUserService.selectUserById(Long.valueOf(cabinet.getUpdaterCode()));
                cell10.setCellValue(sysUser == null ? "" : sysUser.getNickName());
            }
        }
        String confirmed = "";
        Date confirmedDate = null;
        TPatrolPlan tPatrolPlan = tPatrolPlanService.selectTPatrolPlanById(planId);
        if (Objects.nonNull(tPatrolPlan) && Objects.nonNull(tPatrolPlan.getConfirmed())) {
            SysUser sysUser = sysUserService.selectUserById(Long.valueOf(tPatrolPlan.getConfirmed()));            if (sysUser!=null) {
                confirmed = sysUser.getSignUrl();
                confirmedDate = tPatrolPlan.getConfirmDate();
            }
        }
        // 循环后下一行
        int nextRowNum = 5 + list.size();
        XSSFRow row28 = sheet.createRow(nextRowNum);
        row28.setHeightInPoints(30);
        XSSFCell row28Cell1 = row28.createCell(0);
        row28Cell1.setCellValue("签名");
        ExcelUtils.createVoidCell(1, 2, row28);
        sheet.addMergedRegion(new CellRangeAddress(nextRowNum, nextRowNum, 0, 2));
        if (StringUtils.isNotEmpty(confirmed))
            ExcelUtils.insertPicture(workbook, sheet, confirmed, nextRowNum, 5, 1, 1);
        ExcelUtils.createVoidCell(3, 7, row28);
        sheet.addMergedRegion(new CellRangeAddress(nextRowNum, nextRowNum, 3, 7));
        // 下一行
        nextRowNum++;
        XSSFRow row29 = sheet.createRow(nextRowNum);
        row29.setHeightInPoints(30);
        XSSFCell row29Cell1 = row29.createCell(0);
        row29Cell1.setCellValue("日期");
        ExcelUtils.createVoidCell(1, 2, row29);
        sheet.addMergedRegion(new CellRangeAddress(nextRowNum, nextRowNum, 0, 2));
        row29.createCell(4).setCellValue(confirmedDate != null ? new SimpleDateFormat("yyyy-MM-dd").format(confirmedDate) : "");
        ExcelUtils.createVoidCell(3, 7, row29);
        sheet.addMergedRegion(new CellRangeAddress(nextRowNum, nextRowNum, 3, 7));
        return list;
    }

    /**
     * 获取应急事故柜详细信息
     */
    @PreAuthorize("@ss.hasPermi('emergency:cabinet:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        TEmergencyCabinet cabinet = tEmergencyCabinetService.selectTEmergencyCabinetById(id);
        if (cabinet!=null) {
            TDevCabinet tDevCabinet = new TDevCabinet();
            tDevCabinet.setPrecision(cabinet.getDevName());
            tDevCabinet.setDevType(cabinet.getDevType());
            cabinet.setIgnoreColumn(tDevCabinetService.selectTDevCabinetList(tDevCabinet).get(0).getIgnoreColumn());
        }
        return AjaxResult.success(cabinet);
    }

    /**
     * 获取应急事故柜详细信息
     */
    @PreAuthorize("@ss.hasPermi('emergency:cabinet:query')")
    @GetMapping(value = "/info")
    public AjaxResult getInfo(TEmergencyCabinet tEmergencyCabinet) {
        if (StringUtils.isNull(tEmergencyCabinet.getId()) || StringUtils.isNull(tEmergencyCabinet.getPlanId())) {
            return AjaxResult.error("ID或计划id不能为空！");
        }
        TDevCabinet tDevCabinet = tDevCabinetService.selectTDevCabinetById(tEmergencyCabinet.getId());
        TEmergencyCabinet item = new TEmergencyCabinet();
        item.setPlanId(tEmergencyCabinet.getPlanId());
        item.setDevName(tDevCabinet.getDevName());
        item.setDevType(tDevCabinet.getDevType());
        item.setIssuesFlag("0");
        List<TEmergencyCabinet> list = tEmergencyCabinetService.selectTEmergencyCabinetList(item);
        if (CollectionUtils.isNotEmpty(list)) {
            TEmergencyCabinet cabinet = list.get(0);
            cabinet.setIgnoreColumn(tDevCabinet.getIgnoreColumn());
            return AjaxResult.success(cabinet);
        }
        return AjaxResult.error("此设备不在当前巡检计划中!");
    }

    /**
     * 新增应急事故柜
     */
    @PreAuthorize("@ss.hasPermi('emergency:cabinet:add')")
    @Log(title = "应急事故柜", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TEmergencyCabinet tEmergencyCabinet) {
        return toAjax(tEmergencyCabinetService.insertTEmergencyCabinet(tEmergencyCabinet));
    }

    /**
     * 修改应急事故柜
     */
    @PreAuthorize("@ss.hasPermi('emergency:cabinet:edit')")
    @Log(title = "应急事故柜", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@RequestBody TEmergencyCabinet tEmergencyCabinet) {
        tEmergencyCabinet.setUpdatedate(new Date());
        tEmergencyCabinet.setUpdaterCode(getUserId().toString());
        tEmergencyCabinet.setCheckStatus("1");
        tEmergencyCabinet.setCheckDate(new Date());
        TEmergencyCabinet cabinet = tEmergencyCabinetService.selectTEmergencyCabinetById(tEmergencyCabinet.getId());
        TPatrolPlan tPatrolPlan = tPatrolPlanService.selectTPatrolPlanById(tEmergencyCabinet.getPlanId());
        if (tPatrolPlan.getStatus() == 1) {
            return AjaxResult.error("当前选择的计划已确认！");
        }
        if (!"1".equals(cabinet.getCheckStatus())) {
            tPatrolPlan.setUncheckedNum(tPatrolPlan.getUncheckedNum() - 1);
            tPatrolPlan.setCheckedNum(tPatrolPlan.getCheckedNum() + 1);
            tPatrolPlanService.updateTPatrolPlan(tPatrolPlan);
        }
        TDevCabinet tDevCabinet = new TDevCabinet();
        tDevCabinet.setDevType(cabinet.getDevType());
        tDevCabinet.setDevName(cabinet.getDevName());
        tDevCabinet.setDevNum(cabinet.getDevNum());
        TDevCabinet item = tDevCabinetService.selectTDevCabinetList(tDevCabinet).get(0);
        if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("quantityEquipment")) {
            tEmergencyCabinet.setQuantityEquipment("/");
        }
        if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("barometricValue")) {
            tEmergencyCabinet.setBarometricValue("/");
        }
        if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("equipmentCleanliness")) {
            tEmergencyCabinet.setEquipmentCleanliness("/");
        }
        if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("labelValidity")) {
            tEmergencyCabinet.setLabelValidity("/");
        }
        if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("equipmentIntegrity")) {
            tEmergencyCabinet.setEquipmentIntegrity("/");
        }
        if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("respiratorExpiration")) {
            tEmergencyCabinet.setRespiratorExpiration("/");
        }
        if (StringUtils.isNotEquals(tEmergencyCabinet.getBarometricValue(), "1", "/") ||
                StringUtils.isNotEquals(tEmergencyCabinet.getEquipmentCleanliness(), "1", "/") ||
                StringUtils.isNotEquals(tEmergencyCabinet.getEquipmentIntegrity(), "1", "/") ||
                StringUtils.isNotEquals(tEmergencyCabinet.getLabelValidity(), "1", "/") ||
                StringUtils.isNotEquals(tEmergencyCabinet.getQuantityEquipment(), "1", "/") ||
                StringUtils.isNotEquals(tEmergencyCabinet.getRespiratorExpiration(), "1", "/")) {
            tEmergencyCabinet.setIssuesStatus("1");
            tEmergencyCabinet.setCheckStatus("0");
            tEmergencyCabinetService.updateTEmergencyCabinet(tEmergencyCabinet);
            tEmergencyCabinet.setIssuesFlag("1");
            tEmergencyCabinet.setCheckStatus("1");
            tEmergencyCabinet.setCreatedate(new Date());
            tEmergencyCabinet.setCreaterCode(getUserId().toString());
            tEmergencyCabinet.setDevName(cabinet.getDevName());
            tEmergencyCabinet.setDevType(cabinet.getDevType());
            tEmergencyCabinet.setDevNum(cabinet.getDevNum());
            tEmergencyCabinetService.insertTEmergencyCabinet(tEmergencyCabinet);
            TPatrolIssues tPatrolIssues = new TPatrolIssues();
            tPatrolIssues.setPatrolType("7");
            tPatrolIssues.setDevDescribe("事故柜" + cabinet.getDevType() + "_" + cabinet.getDevName());
            tPatrolIssues.setLinkId(tEmergencyCabinet.getId());
            tPatrolIssues.setStatus(0L);
            tPatrolIssuesService.insertTPatrolIssues(tPatrolIssues);
        } else {
            if ("1".equals(cabinet.getIssuesStatus())) {
                TPatrolIssues tPatrolIssues = new TPatrolIssues();
                tPatrolIssues.setPatrolType("7");
                tPatrolIssues.setDevDescribe("事故柜" + cabinet.getDevType() + "_" + cabinet.getDevName());
                tPatrolIssues.setCreatedate(new Date());
                for (TPatrolIssues patrolIssues : tPatrolIssuesService.selectTPatrolIssuesList(tPatrolIssues)) {
                    patrolIssues.setStatus(1L);
                    tPatrolIssuesService.updateTPatrolIssues(patrolIssues);
                }
            }
            tEmergencyCabinet.setIssuesStatus("0");
            tEmergencyCabinetService.updateTEmergencyCabinet(tEmergencyCabinet);
        }
        return AjaxResult.success();
    }

    /**
     * 删除应急事故柜
     */
    @PreAuthorize("@ss.hasPermi('emergency:cabinet:remove')")
    @Log(title = "应急事故柜", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tEmergencyCabinetService.deleteTEmergencyCabinetByIds(ids));
    }
}
