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.*;
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.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 员工PPE检查记录Controller
 *
 * @author ssy
 * @date 2023-11-30
 */
@RestController
@RequestMapping("/production/main")
public class TPpeMainController extends BaseController {
    @Autowired
    private ITPpeMainService tPpeMainService;
    @Autowired
    private ITPpeStaffLockerService tPpeStaffLockerService;
    @Autowired
    private ITPpeCmldefLockerService tPpeCmldefLockerService;
    @Autowired
    private ITPpeWardrobeService tPpeWardrobeService;
    @Autowired
    private ITPpePersonService tPpePersonService;

    /**
     * 查询员工PPE检查记录列表
     */

    @GetMapping("/list")
    public TableDataInfo list(TPpeMain tPpeMain) {
        startPage();
        List<TPpeMain> list = tPpeMainService.selectTPpeMainList(tPpeMain);
        return getDataTable(list);
    }


    /**
     * 查询员工PPE检查记录列表
     */

    @GetMapping("/lastCheckData")
    public AjaxResult lastCheckData(TPpeMain tPpeMain) {
        List<TPpeMain> list = tPpeMainService.selectTPpeMainList(tPpeMain);
        TPpeMain main = new TPpeMain();
        if (CollectionUtils.isNotEmpty(list)) {
            main = list.get(0);
            List<TPpeCmldefLocker> tPpeCmldefLockers = tPpeCmldefLockerService.selectTPpeCmldefLockerListByMainId(main.getId());
            main.setCmldefLockers(tPpeCmldefLockers);
            if (CollectionUtils.isNotEmpty(tPpeCmldefLockers))
                main.setCmldefRemark(tPpeCmldefLockers.get(0).getRemarks());
            List<TPpeStaffLocker> tPpeStaffLockers = tPpeStaffLockerService.selectTPpeStaffLockerListByMainId(main.getId());
            main.setStaffLockers(tPpeStaffLockers);
            if (CollectionUtils.isNotEmpty(tPpeStaffLockers)) main.setStaffRemark(tPpeStaffLockers.get(0).getRemarks());
            List<TPpeWardrobe> tPpeWardrobes = tPpeWardrobeService.selectTPpeWardrobeListByMainId(main.getId());
            main.setWardrobes(tPpeWardrobes);
            if (CollectionUtils.isNotEmpty(tPpeWardrobes)) main.setWardrobeRemark(tPpeWardrobes.get(0).getRemarks());
        }
        return AjaxResult.success(main);
    }

    /**
     * 导出员工PPE检查记录列表
     */

    @Log(title = "员工PPE检查记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TPpeMain tPpeMain) {
        try {
            TPpeMain main = tPpeMainService.selectTPpeMainById(tPpeMain.getId());
            TPpePerson person = tPpePersonService.selectTPpePersonByEmployeeid(main.getStaffId());
            XSSFWorkbook workbook = new XSSFWorkbook();
            //region 创建工作簿
            XSSFSheet sheet = workbook.createSheet();
            sheet.setColumnWidth(0, 6850);
            sheet.setColumnWidth(1, 10000);
            sheet.setColumnWidth(2, 3000);
            sheet.setColumnWidth(3, 3000);
            sheet.setColumnWidth(4, 3000);
            sheet.setColumnWidth(5, 3000);
            sheet.setColumnWidth(6, 3000);
            sheet.setColumnWidth(7, 3000);
            sheet.setColumnWidth(8, 3000);
            sheet.setColumnWidth(9, 3000);
            sheet.setColumnWidth(10, 3000);
            sheet.setColumnWidth(11, 3000);
            sheet.setColumnWidth(12, 3000);
            sheet.setColumnWidth(13, 3000);
            sheet.setColumnWidth(14, 3000);
            sheet.setColumnWidth(15, 3000);
            sheet.setColumnWidth(16, 3000);
            sheet.setColumnWidth(17, 3000);
            sheet.setColumnWidth(18, 3000);
            //endregion
            //region 表头
            //第一行
            XSSFCellStyle style1 = workbook.createCellStyle();
            XSSFRow row1 = sheet.createRow(0);
            XSSFFont font1 = workbook.createFont();
            font1.setBold(true);
            font1.setFontName("微软雅黑");
            font1.setFontHeight(26);
            row1.setHeightInPoints(45);
            XSSFCell row1Cell = row1.createCell(0);
            row1Cell.setCellValue("蒸汽裂解装置PPE检查记录表");
            createVoidCell(1, 18, row1);
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 18));//合并
            style1.setAlignment(HorizontalAlignment.CENTER);
            style1.setVerticalAlignment(VerticalAlignment.CENTER);
            style1.setFont(font1);
            row1Cell.setCellStyle(style1);
            // 第二行
            sheet.createRow(1).setHeightInPoints(27);
            // 第三行
            XSSFRow row3 = sheet.createRow(2);
            createVoidCell(0, 18, row3);//创建空格子

            XSSFCellStyle style2 = workbook.createCellStyle();
            XSSFFont font2 = workbook.createFont();
            font2.setFontName("华文楷体");
            font2.setFontHeight(22);
            style2.setAlignment(HorizontalAlignment.LEFT);
            style2.setVerticalAlignment(VerticalAlignment.CENTER);
            style2.setFont(font2);
            XSSFCell row3Cell1 = row3.createCell(0);
            row3Cell1.setCellValue("员工姓名：" + person.getName());
            row3Cell1.setCellStyle(style2);
            sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 1));
            XSSFCell row3Cell2 = row3.createCell(3);
            row3Cell2.setCellValue("性别：" + (StringUtils.isNotEmpty(person.getSex()) ? (person.getSex().equals("0") ? "男" : "女") : ""));
            row3Cell2.setCellStyle(style2);
            sheet.addMergedRegion(new CellRangeAddress(2, 2, 3, 7));
            String text = "岗位：";
            List<Integer> r = new ArrayList<>();
            if ("1".equals(person.getBoiler())) {
                text += "裂解R  ";
                r.add(text.indexOf("裂解R") + 2);
            } else {
                text += "裂解□ ";
            }
            if ("1".equals(person.getCracking())) {
                text += "热区R ";
                r.add(text.indexOf("热区R") + 2);
            } else {
                text += "热区□ ";
            }
            if ("1".equals(person.getHotarea())) {
                text += "冷区R ";
                r.add(text.indexOf("冷区R") + 2);
            } else {
                text += "冷区□ ";
            }
            if ("1".equals(person.getColdarea())) {
                text += "PGU&AEUR ";
                r.add(text.indexOf("PGU&AEUR") + 7);
            } else {
                text += "PGU&AEU□ ";
            }
            if ("1".equals(person.getOperator())) {
                text += "白班操作员R ";
                r.add(text.indexOf("白班操作员R") + 5);
            } else {
                text += "白班操作员□  ";
            }
            XSSFCell row3Cell3 = row3.createCell(8);
            row3Cell3.setCellValue(text);
            XSSFRichTextString rts = new XSSFRichTextString(text);
            XSSFFont postFont = workbook.createFont();
            postFont.setFontName("Wingdings 2");
            postFont.setFontHeight(22);
            rts.applyFont(font2);
            for (Integer i : r) {
                rts.applyFont(i, i + 1, postFont);
            }
            row3Cell3.setCellValue(rts);
            sheet.addMergedRegion(new CellRangeAddress(2, 2, 8, 18));//合并
            row3.setHeightInPoints(80);
            // endregion
            int lastRowNum = 6; //记录最后一行
            int startRow = 8;
            int endRow = 13;
            int startCol = 14;
            int endCol = 17;
            //region 第一张表格
            String remarks = null;
            List<TPpeStaffLocker> tPpeStaffLockers = tPpeStaffLockerService.selectTPpeStaffLockerListByMainId(main.getId());
            if (CollectionUtils.isNotEmpty(tPpeStaffLockers)) {
                startRow += tPpeStaffLockers.size() + 2;
                endRow += tPpeStaffLockers.size() + 2;
                //第四行
                XSSFRow row4 = sheet.createRow(3);
                row4.setHeightInPoints(44.5F);
                createVoidCell(0, 18, row4);
                XSSFCell row4Cell = row4.createCell(0);
                row4Cell.setCellValue("自行车棚员工PPE柜");
                for (Cell cell : row4) {
                    cell.setCellStyle(getStyle(workbook, "Arial", 22, true));
                }

                //第五行
                XSSFRow row5 = sheet.createRow(4);
                row5.setHeightInPoints(37.5F);
                createVoidCell(0, 18, row5);
                XSSFCell row5Cell1 = row5.createCell(0);
                row5Cell1.setCellValue("PPE种类");
                row5Cell1.setCellStyle(getStyle(workbook, "Arial", 18, true));
                XSSFCell row5Cell2 = row5.createCell(1);
                row5Cell2.setCellValue("检查内容");
                for (Cell cell : row5) {
                    if (cell.getColumnIndex() != 0) {
                        cell.setCellStyle(getStyle(workbook, "等线", 18, true));
                    }
                }


                //第六行
                XSSFRow row6 = sheet.createRow(5);
                row6.setHeightInPoints(53.5F);
                createVoidCell(0, 18, row6);
                XSSFCell row6Cell2 = row6.createCell(1);
                row6Cell2.setCellValue("应配备岗位");
                XSSFCell row6Cell3 = row6.createCell(2);
                row6Cell3.setCellValue("是否配备①");
                XSSFCell row6Cell6 = row6.createCell(5);
                row6Cell6.setCellValue("是否无破损、未被污染");
                XSSFCell row6Cell8 = row6.createCell(9);
                row6Cell8.setCellValue("摆放符合要求⑥");
                XSSFCell row6Cell10 = row6.createCell(11);
                row6Cell10.setCellValue("是否按要求贴“使用时间卡”②");
                XSSFCell row6Cell12 = row6.createCell(13);
                row6Cell12.setCellValue("是否在使用有效期内③");
                XSSFCell row6Cell14 = row6.createCell(15);
                row6Cell14.setCellValue("检验合格的标识在有效期内④");
                XSSFCell row6Cell16 = row6.createCell(17);
                row6Cell16.setCellValue("是否配有专用湿巾");
                for (Cell cell : row6) {
                    if (cell.getColumnIndex() != 0) {
                        cell.setCellStyle(getStyle(workbook, "等线", 18, true));
                    }
                }

                //第七行
                XSSFRow row7 = sheet.createRow(6);
                row7.setHeightInPoints(40);
                createVoidCell(0, 18, row7);
                row7.createCell(2).setCellValue("是");
                row7.createCell(3).setCellValue("N.A");
                row7.createCell(4).setCellValue("否");
                row7.createCell(5).setCellValue("是");
                row7.createCell(7).setCellValue("否");
                row7.createCell(9).setCellValue("是");
                row7.createCell(10).setCellValue("否");
                row7.createCell(11).setCellValue("是");
                row7.createCell(12).setCellValue("否");
                row7.createCell(13).setCellValue("是");
                row7.createCell(14).setCellValue("否");
                row7.createCell(15).setCellValue("是");
                row7.createCell(16).setCellValue("否");
                row7.createCell(17).setCellValue("是");
                row7.createCell(18).setCellValue("否");
                for (Cell cell : row7) {
                    if (cell.getColumnIndex() != 0 && cell.getColumnIndex() != 1) {
                        cell.setCellStyle(getStyle(workbook, "等线", 18, false));
                    } else {
                        cell.setCellStyle(getStyle(workbook, "Arial", 18, true));
                    }
                }
                //合并
                sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 18));
                sheet.addMergedRegion(new CellRangeAddress(4, 4, 1, 18));
                sheet.addMergedRegion(new CellRangeAddress(4, 6, 0, 0));
                sheet.addMergedRegion(new CellRangeAddress(5, 6, 1, 1));
                sheet.addMergedRegion(new CellRangeAddress(5, 5, 2, 4));
                sheet.addMergedRegion(new CellRangeAddress(5, 5, 5, 8));
                sheet.addMergedRegion(new CellRangeAddress(5, 5, 9, 10));
                sheet.addMergedRegion(new CellRangeAddress(5, 5, 11, 12));
                sheet.addMergedRegion(new CellRangeAddress(5, 5, 13, 14));
                sheet.addMergedRegion(new CellRangeAddress(5, 5, 15, 16));
                sheet.addMergedRegion(new CellRangeAddress(5, 5, 17, 18));
                sheet.addMergedRegion(new CellRangeAddress(6, 6, 5, 6));
                sheet.addMergedRegion(new CellRangeAddress(6, 6, 7, 8));
                remarks = "";
                for (int i = 0; i < tPpeStaffLockers.size(); i++) {
                    TPpeStaffLocker tPpeStaffLocker = tPpeStaffLockers.get(i);
                    lastRowNum++;
                    XSSFRow row = sheet.createRow(lastRowNum);
                    row.setHeightInPoints(70);
                    createVoidCell(0, 18, row);
                    row.createCell(0).setCellValue(tPpeStaffLocker.getPpeType());
                    if (tPpeStaffLocker.getUsePost().contains(",")) {
                        tPpeStaffLocker.setUsePost(tPpeStaffLocker.getUsePost().replace(",", "、"));
                    }
                    row.createCell(1).setCellValue(tPpeStaffLocker.getUsePost());
                    row.createCell(2).setCellValue(getValue("是", tPpeStaffLocker.getIsEquip()));
                    row.createCell(3).setCellValue(getValue("N.A", tPpeStaffLocker.getIsEquip(), tPpeStaffLocker.getUsePost()));
                    row.createCell(4).setCellValue(getValue("否", tPpeStaffLocker.getIsEquip()));
                    row.createCell(5).setCellValue(getValue("是", tPpeStaffLocker.getIsPollution()));
                    row.createCell(7).setCellValue(getValue("否", tPpeStaffLocker.getIsPollution()));
                    row.createCell(9).setCellValue(getValue("是", tPpeStaffLocker.getPutAsRequired()));
                    row.createCell(10).setCellValue(getValue("否", tPpeStaffLocker.getPutAsRequired()));
                    row.createCell(11).setCellValue(getValue("是", tPpeStaffLocker.getIsRequire()));
                    row.createCell(12).setCellValue(getValue("否", tPpeStaffLocker.getIsRequire()));
                    row.createCell(13).setCellValue(getValue("是", tPpeStaffLocker.getIsValidity()));
                    row.createCell(14).setCellValue(getValue("否", tPpeStaffLocker.getIsValidity()));
                    row.createCell(15).setCellValue(getValue("是", tPpeStaffLocker.getIsIdentification()));
                    row.createCell(16).setCellValue(getValue("否", tPpeStaffLocker.getIsIdentification()));
                    row.createCell(17).setCellValue(getValue("是", tPpeStaffLocker.getIsWetTissue()));
                    row.createCell(18).setCellValue(getValue("否", tPpeStaffLocker.getIsWetTissue()));
                    for (Cell cell : row) {
                        if (cell.getColumnIndex() == 0 || cell.getColumnIndex() == 1) {
                            if (i % 2 == 0 && cell.getColumnIndex() != 18 && cell.getColumnIndex() != 17)
                                cell.setCellStyle(getStyle(workbook, "宋体", 18, false, true));
                            else cell.setCellStyle(getStyle(workbook, "宋体", 18, false));
                        } else if (StringUtils.isNotEmpty(cell.getStringCellValue()) && ("/".equals(cell.getStringCellValue()) || "N.A".equals(cell.getStringCellValue()))) {
                            if (i % 2 == 0 && cell.getColumnIndex() != 18 && cell.getColumnIndex() != 17) {
                                cell.setCellStyle(getStyle(workbook, "Arial", 18, false, true));
                            } else {
                                cell.setCellStyle(getStyle(workbook, "Arial", 18, false));
                            }
                        } else {
                            if (i % 2 == 0 && cell.getColumnIndex() != 18 && cell.getColumnIndex() != 17) {
                                cell.setCellStyle(getStyle(workbook, "Wingdings 2", 18, false, true));
                            } else {
                                cell.setCellStyle(getStyle(workbook, "Wingdings 2", 18, false));
                            }
                        }
                    }
                    sheet.addMergedRegion(new CellRangeAddress(row.getRowNum(), row.getRowNum(), 5, 6));
                    sheet.addMergedRegion(new CellRangeAddress(row.getRowNum(), row.getRowNum(), 7, 8));
                    remarks = tPpeStaffLocker.getRemarks();
                }
                sheet.addMergedRegion(new CellRangeAddress(7, lastRowNum, 17, 17));
                sheet.addMergedRegion(new CellRangeAddress(7, lastRowNum, 18, 18));
                XSSFRow row21 = sheet.createRow(++lastRowNum);
                row21.setHeightInPoints(100);
                createVoidCell(0, 18, row21);
                row21.createCell(0).setCellValue("备注");
                row21.createCell(1).setCellValue(remarks);
                for (Cell cell : row21) {
                    if (cell.getColumnIndex() == 0) cell.setCellStyle(getStyle(workbook, "等线", 18, true));
                    else cell.setCellStyle(getStyle(workbook, "等线", 18, false));
                }
                sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 1, 18));
                sheet.createRow(++lastRowNum).setHeightInPoints(40);
            }
            // endregion

            //region 第二张表格
            remarks = "";
            List<TPpeCmldefLocker> tPpeCmldefLockers = tPpeCmldefLockerService.selectTPpeCmldefLockerListByMainId(main.getId());
            if (CollectionUtils.isNotEmpty(tPpeCmldefLockers)) {
                startRow += tPpeCmldefLockers.size() + 6;
                endRow += tPpeCmldefLockers.size() + 6;
                XSSFRow row23 = sheet.createRow(++lastRowNum);
                row23.setHeightInPoints(40);
                createVoidCell(0, 10, row23);
                sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 0, 10));
                row23.createCell(0).setCellValue("自行车棚防化靴/防化服柜");

                XSSFRow row24 = sheet.createRow(++lastRowNum);
                row24.setHeightInPoints(37.5F);
                createVoidCell(0, 10, row24);
                XSSFCell row24Cell1 = row24.createCell(0);
                row24Cell1.setCellValue("PPE种类");
                row24Cell1.setCellStyle(getStyle(workbook, "Arial", 18, true));
                XSSFCell row24Cell2 = row24.createCell(1);
                row24Cell2.setCellValue("检查内容");
                for (Cell cell : row24) {
                    if (cell.getColumnIndex() != 0) {
                        cell.setCellStyle(getStyle(workbook, "等线", 18, true));
                    }
                }
                sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum + 2, 0, 0));
                sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 1, 10));

                XSSFRow row25 = sheet.createRow(++lastRowNum);
                row25.setHeightInPoints(53.5F);
                createVoidCell(0, 10, row25);
                XSSFCell row25Cell2 = row25.createCell(1);
                row25Cell2.setCellValue("应配备岗位");
                XSSFCell row25Cell3 = row25.createCell(2);
                row25Cell3.setCellValue("是否配备①");
                XSSFCell row25Cell6 = row25.createCell(5);
                row25Cell6.setCellValue("是否无破损、未被污染");
                XSSFCell row25Cell8 = row25.createCell(7);
                row25Cell8.setCellValue("摆放符合要求⑥");
                XSSFCell row25Cell14 = row25.createCell(9);
                row25Cell14.setCellValue("检验合格的标识在有效期内④");
                for (Cell cell : row25) {
                    if (cell.getColumnIndex() != 0) {
                        cell.setCellStyle(getStyle(workbook, "等线", 18, true));
                    }
                }
                sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum + 1, 1, 1));
                sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 2, 4));
                sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 5, 6));
                sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 7, 8));
                sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 9, 10));

                XSSFRow row26 = sheet.createRow(++lastRowNum);
                row26.setHeightInPoints(40);
                createVoidCell(0, 10, row26);
                row26.createCell(2).setCellValue("是");
                row26.createCell(3).setCellValue("N.A");
                row26.createCell(4).setCellValue("否");
                row26.createCell(5).setCellValue("是");
                row26.createCell(6).setCellValue("否");
                row26.createCell(7).setCellValue("是");
                row26.createCell(8).setCellValue("否");
                row26.createCell(9).setCellValue("是");
                row26.createCell(10).setCellValue("否");
                for (Cell cell : row26) {
                    if (cell.getColumnIndex() != 0 && cell.getColumnIndex() != 1) {
                        cell.setCellStyle(getStyle(workbook, "等线", 18, false));
                    } else {
                        cell.setCellStyle(getStyle(workbook, "Arial", 18, true));
                    }
                }
                for (Cell cell : row23) {
                    cell.setCellStyle(getStyle(workbook, "Arial", 22, true));
                }
                for (int i = 0; i < tPpeCmldefLockers.size(); i++) {
                    TPpeCmldefLocker tPpeCmldefLocker = tPpeCmldefLockers.get(i);
                    lastRowNum++;
                    XSSFRow row = sheet.createRow(lastRowNum);
                    row.setHeightInPoints(70);
                    createVoidCell(0, 10, row);
                    row.createCell(0).setCellValue(tPpeCmldefLocker.getPpeType());
                    if (tPpeCmldefLocker.getUsePost().contains(",")) {
                        tPpeCmldefLocker.setUsePost(tPpeCmldefLocker.getUsePost().replace(",", "、"));
                    }
                    row.createCell(1).setCellValue(tPpeCmldefLocker.getUsePost());
                    row.createCell(2).setCellValue(getValue("是", tPpeCmldefLocker.getIsEquip()));
                    row.createCell(3).setCellValue(getValue("N.A", tPpeCmldefLocker.getIsEquip(), tPpeCmldefLocker.getUsePost()));
                    row.createCell(4).setCellValue(getValue("否", tPpeCmldefLocker.getIsEquip()));
                    row.createCell(5).setCellValue(getValue("是", tPpeCmldefLocker.getIsPollution()));
                    row.createCell(6).setCellValue(getValue("否", tPpeCmldefLocker.getIsPollution()));
                    row.createCell(7).setCellValue(getValue("是", tPpeCmldefLocker.getPutAsRequired()));
                    row.createCell(8).setCellValue(getValue("否", tPpeCmldefLocker.getPutAsRequired()));
                    row.createCell(9).setCellValue(getValue("是", tPpeCmldefLocker.getIsIdentification()));
                    row.createCell(10).setCellValue(getValue("否", tPpeCmldefLocker.getIsIdentification()));
                    for (Cell cell : row) {
                        if (cell.getColumnIndex() == 0 || cell.getColumnIndex() == 1) {
                            if (i % 2 == 0) cell.setCellStyle(getStyle(workbook, "宋体", 18, false, true));
                            else cell.setCellStyle(getStyle(workbook, "宋体", 18, false));
                        } else if (StringUtils.isNotEmpty(cell.getStringCellValue()) && "N.A".equals(cell.getStringCellValue())) {
                            if (i % 2 == 0) {
                                cell.setCellStyle(getStyle(workbook, "Arial", 18, false, true));
                            } else {
                                cell.setCellStyle(getStyle(workbook, "Arial", 18, false));
                            }
                        } else {
                            if (i % 2 == 0) {
                                cell.setCellStyle(getStyle(workbook, "Wingdings 2", 18, false, true));
                            } else {
                                cell.setCellStyle(getStyle(workbook, "Wingdings 2", 18, false));
                            }
                        }
                    }
                    remarks = tPpeCmldefLocker.getRemarks();
                }
                XSSFRow row27 = sheet.createRow(++lastRowNum);
                row27.setHeightInPoints(100);
                createVoidCell(0, 10, row27);
                row27.createCell(0).setCellValue("备注");
                row27.createCell(1).setCellValue(remarks);
                for (Cell cell : row27) {
                    if (cell.getColumnIndex() == 0) cell.setCellStyle(getStyle(workbook, "等线", 18, true));
                    else cell.setCellStyle(getStyle(workbook, "等线", 18, false));
                }
                sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 1, 10));
                sheet.createRow(++lastRowNum).setHeightInPoints(40);

                // 图片路径
                String templatePath1 = "static/ppeImg/ppe1.png";
                ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
                BufferedImage bufferImg1 = ImageIO.read(getClass().getClassLoader().getResourceAsStream(templatePath1));
                ImageIO.write(bufferImg1, "png", byteArrayOut1);
                XSSFClientAnchor anchor1 = new XSSFClientAnchor(0, 0, 0, 0, 12, 23, 15, 28);
                XSSFDrawing patriarch1 = sheet.createDrawingPatriarch();
                patriarch1.createPicture(anchor1, workbook.addPicture(byteArrayOut1.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG));
                byteArrayOut1.close();

                String templatePath2 = "static/ppeImg/ppe2.png";
                ByteArrayOutputStream byteArrayOut2 = new ByteArrayOutputStream();
                BufferedImage bufferImg2 = ImageIO.read(getClass().getClassLoader().getResourceAsStream(templatePath2));
                ImageIO.write(bufferImg2, "png", byteArrayOut2);
                XSSFClientAnchor anchor2 = new XSSFClientAnchor(0, 0, 0, 0, 16, 23, 19, 29);
                XSSFDrawing patriarch2 = sheet.createDrawingPatriarch();
                patriarch2.createPicture(anchor2, workbook.addPicture(byteArrayOut2.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG));
                byteArrayOut2.close();
            }
            //endregion

            //region 第三张表格
            XSSFRow row29 = sheet.createRow(++lastRowNum);
            row29.setHeightInPoints(40);
            createVoidCell(0, 12, row29);
            row29.createCell(0).setCellValue("更衣柜");
            sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 0, 12));
            for (Cell cell : row29) {
                cell.setCellStyle(getStyle(workbook, "等线", 22, true));
            }

            XSSFRow row30 = sheet.createRow(++lastRowNum);
            createVoidCell(0, 12, row30);
            row30.createCell(0).setCellValue("检查内容");
            row30.setHeightInPoints(40);
            sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 0, 12));
            for (Cell cell : row30) {
                cell.setCellStyle(getStyle(workbook, "等线", 18, true));
            }

            XSSFRow row31 = sheet.createRow(++lastRowNum);
            createVoidCell(0, 12, row31);
            row31.setHeightInPoints(76);
            row31.createCell(0).setCellValue("没有存放污染工作服/工作鞋");
            row31.createCell(5).setCellValue("摆放符合要求⑥");
            row31.createCell(9).setCellValue("未存放食物/饮料");
            sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 0, 4));
            sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 5, 8));
            sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 9, 12));
            for (Cell cell : row31) {
                cell.setCellStyle(getStyle(workbook, "等线", 18, true));
            }

            XSSFRow row32 = sheet.createRow(++lastRowNum);
            createVoidCell(0, 12, row32);
            row32.setHeightInPoints(40);
            row32.createCell(0).setCellValue("是");
            row32.createCell(1).setCellValue("否");
            row32.createCell(5).setCellValue("是");
            row32.createCell(7).setCellValue("否");
            row32.createCell(9).setCellValue("是");
            row32.createCell(11).setCellValue("否");
            for (Cell cell : row32) {
                cell.setCellStyle(getStyle(workbook, "等线", 18, false));
            }
            sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 1, 4));
            sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 5, 6));
            sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 7, 8));
            sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 9, 10));
            sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 11, 12));
            List<TPpeWardrobe> tPpeWardrobes = tPpeWardrobeService.selectTPpeWardrobeListByMainId(main.getId());
            for (TPpeWardrobe tPpeWardrobe : tPpeWardrobes) {
                XSSFRow row = sheet.createRow(++lastRowNum);
                createVoidCell(0, 12, row);
                row.setHeightInPoints(40);
                row.createCell(0).setCellValue(getValue("是", tPpeWardrobe.getNoPollution()));
                row.createCell(1).setCellValue(getValue("否", tPpeWardrobe.getNoPollution()));
                row.createCell(5).setCellValue(getValue("是", tPpeWardrobe.getPutAsRequired()));
                row.createCell(7).setCellValue(getValue("否", tPpeWardrobe.getPutAsRequired()));
                row.createCell(9).setCellValue(getValue("是", tPpeWardrobe.getNoFood()));
                row.createCell(11).setCellValue(getValue("否", tPpeWardrobe.getNoFood()));
                for (Cell cell : row) {
                    cell.setCellStyle(getStyle(workbook, "Wingdings 2", 18, false));
                }
                sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 1, 4));
                sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 5, 6));
                sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 7, 8));
                sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 9, 10));
                sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 11, 12));
                remarks = tPpeWardrobe.getRemarks();
            }
            XSSFRow row33 = sheet.createRow(++lastRowNum);
            row33.setHeightInPoints(100);
            createVoidCell(0, 12, row33);
            row33.createCell(0).setCellValue("备注");
            row33.createCell(1).setCellValue(remarks);
            for (Cell cell : row33) {
                if (cell.getColumnIndex() == 0) cell.setCellStyle(getStyle(workbook, "等线", 18, true));
                else cell.setCellStyle(getStyle(workbook, "等线", 18, false));
            }
            sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 1, 12));
            sheet.createRow(++lastRowNum).setHeightInPoints(40);
            //endregion

            //region 注
            XSSFCellStyle style3 = workbook.createCellStyle();
            XSSFFont font = workbook.createFont();
            font.setFontName("宋体");
            font.setFontHeight(24);
            style3.setVerticalAlignment(VerticalAlignment.CENTER);
            style3.setFont(font);

            XSSFRow row38 = sheet.createRow(++lastRowNum);
            row38.setHeightInPoints(50);
            XSSFCell row38cell = row38.createCell(0);
            row38cell.setCellValue("注：");
            row38cell.setCellStyle(style3);

            XSSFRow row39 = sheet.createRow(++lastRowNum);
            createVoidCell(0, 18, row39);
            row39.setHeightInPoints(121.5F);
            XSSFCell row39cell = row39.createCell(0);
            row39cell.setCellValue(workbook.getCreationHelper().createRichTextString("①、“是否配备”如果实际配备选择“是”；\n" + "               如果不需要配备而没有配备选择“N.A”；\n" + "               如果是应配备而没有配备选择“否”。"));
            row39cell.setCellStyle(style3);
            sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 0, 18));

            XSSFRow row40 = sheet.createRow(++lastRowNum);
            row40.setHeightInPoints(40);
            XSSFCell row40cell = row40.createCell(0);
            row40cell.setCellValue("②、滤罐93ABEK2Hg/St型及滤罐90AX型拆封后必须按要求贴上“使用时间卡”并填写使用信息。");
            row40cell.setCellStyle(style3);
            sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 0, 18));

            XSSFRow row41 = sheet.createRow(++lastRowNum);
            createVoidCell(0, 18, row41);
            row41.setHeightInPoints(100);
            XSSFCell row41cell = row41.createCell(0);
            String str3 = "③、安全帽有效期30个月；\n   未污染无破损的Ansell38-628合成橡胶手套、Ansell37-675.676丁腈橡胶手套有效期为三年；\n    滤罐93ABEK2Hg/St型及滤罐90AX型有效期指 拆封日期不超过一个月且“使用时间卡”未已用完。";
            XSSFRichTextString rts3 = workbook.getCreationHelper().createRichTextString(str3);
            row41cell.setCellStyle(style3);
            rts3.applyFont(font);
            row41cell.setCellValue(rts3);
            sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 0, 18));

            XSSFRow row42 = sheet.createRow(++lastRowNum);
            row42.setHeightInPoints(40);
            XSSFCell row42cell = row42.createCell(0);
            row42cell.setCellValue("④、3S宽视野全面罩及连体防化服MC5000必须贴有在效期内检验合格的标识。");
            row42cell.setCellStyle(style3);
            sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 0, 18));

            XSSFRow row43 = sheet.createRow(++lastRowNum);
            row43.setHeightInPoints(40);
            XSSFCell row43cell = row43.createCell(0);
            row43cell.setCellValue("⑤、女员工只用检查更衣柜。");
            row43cell.setCellStyle(style3);
            sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 0, 18));

            XSSFRow row44 = sheet.createRow(++lastRowNum);
            row44.setHeightInPoints(40);
            XSSFCell row44cell = row44.createCell(0);
            row44cell.setCellValue("⑥、摆放要求参见图例。");
            row44cell.setCellStyle(style3);
            sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 0, 18));

            XSSFRow row45 = sheet.createRow(++lastRowNum);
            row45.setHeightInPoints(40);
            XSSFCell row45cell = row45.createCell(0);
            row45cell.setCellValue("检查发现其他异常，填写在“备注”栏，检查结果出现“否”，检查人应要求被检查人对不符合项目立即整改。");
            row45cell.setCellStyle(style3);
            sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 0, 18));


            XSSFCellStyle style4 = workbook.createCellStyle();
            XSSFFont font3 = workbook.createFont();
            font3.setFontName("Arial");
            font3.setFontHeight(26);
            style4.setVerticalAlignment(VerticalAlignment.CENTER);
            style4.setFont(font3);
            XSSFRow row46 = sheet.createRow(++lastRowNum);
            row46.setHeightInPoints(60);
            XSSFCell row46cell8 = row46.createCell(8);
            row46cell8.setCellValue("检查人：" + main.getChecker());
            XSSFCell row46cell13 = row46.createCell(13);
            row46cell13.setCellValue("检查时间：" + new SimpleDateFormat("yyyy-MM-dd").format(main.getCheckDate()));
            row46cell13.setCellStyle(style4);
            row46cell8.setCellStyle(style4);
            sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 13, 17));
            sheet.addMergedRegion(new CellRangeAddress(lastRowNum, lastRowNum, 8, 12));

            //endregion

            //region 插入图片
            String templatePath3 = "static/ppeImg/ppe3.png";
            ByteArrayOutputStream byteArrayOut3 = new ByteArrayOutputStream();
            BufferedImage bufferImg3 = ImageIO.read(getClass().getClassLoader().getResourceAsStream(templatePath3));
            ImageIO.write(bufferImg3, "png", byteArrayOut3);
            XSSFClientAnchor anchor3 = new XSSFClientAnchor(0, 0, 0, 0, startCol, startRow, endCol, endRow);
            XSSFDrawing patriarch3 = sheet.createDrawingPatriarch();
            patriarch3.createPicture(anchor3, workbook.addPicture(byteArrayOut3.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG));
            byteArrayOut3.close();
            //endregion

            String fileName = ExcelUtil.encodingFilename("main");
            OutputStream os = Files.newOutputStream(Paths.get(ExcelUtil.getAbsoluteFile(fileName)));
            workbook.write(os);
            return AjaxResult.success(fileName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    private String getValue(String current, String value, String... post) {
        if (StringUtils.isEmpty(value)) {
            return "*";
        }
        if (current.equals("N.A")) {
            if (post[0].equals("所有岗位")) return "/";
            else if (current.equals(value)) {
                return "R";
            } else {
                return "*";
            }
        } else if (value.equals("N.A")) {
            return "N.A";
        } else if (current.equals(value)) {
            return "R";
        } else {
            return "*";
        }
    }

    private XSSFCellStyle getStyle(XSSFWorkbook workbook, String fontName, double fontHeight, boolean bold, boolean... isColor) {
        // 边框，水平、垂直居中、自动换行
        XSSFCellStyle style = workbook.createCellStyle();
        XSSFFont font = workbook.createFont();
        font.setFontName(fontName);
        font.setFontHeight(fontHeight);
        font.setBold(bold);
        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);
        style.setFont(font);
        if (isColor.length > 0 && isColor[0]) {
            style.setFillForegroundColor(new XSSFColor(new Color(0XE2EFDA), null));
            style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        }
        return style;
    }

    /**
     * 创建空格子
     */
    public void createVoidCell(int startCell, int endCell, XSSFRow row) {
        for (int i = startCell; i <= endCell; i++) {
            row.createCell(i);
        }
    }

    /**
     * 获取员工PPE检查记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        TPpeMain main = tPpeMainService.selectTPpeMainById(id);
        if (main != null) {
            List<TPpeCmldefLocker> tPpeCmldefLockers = tPpeCmldefLockerService.selectTPpeCmldefLockerListByMainId(main.getId());
            main.setCmldefLockers(tPpeCmldefLockers);
            if (CollectionUtils.isNotEmpty(tPpeCmldefLockers))
                main.setCmldefRemark(tPpeCmldefLockers.get(0).getRemarks());
            List<TPpeStaffLocker> tPpeStaffLockers = tPpeStaffLockerService.selectTPpeStaffLockerListByMainId(main.getId());
            main.setStaffLockers(tPpeStaffLockers);
            if (CollectionUtils.isNotEmpty(tPpeStaffLockers)) main.setStaffRemark(tPpeStaffLockers.get(0).getRemarks());
            List<TPpeWardrobe> tPpeWardrobes = tPpeWardrobeService.selectTPpeWardrobeListByMainId(main.getId());
            main.setWardrobes(tPpeWardrobes);
            if (CollectionUtils.isNotEmpty(tPpeWardrobes)) main.setWardrobeRemark(tPpeWardrobes.get(0).getRemarks());
        }
        return AjaxResult.success(main);
    }

    /**
     * 新增员工PPE检查记录
     */
    @Log(title = "员工PPE检查记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TPpeMain tPpeMain) {
        tPpeMain.setChecker(getNickName());
        tPpeMain.setCreaterCode(getUserId().toString());
        tPpeMain.setCheckDate(new Date());
        tPpeMain.setCreatedate(new Date());
        if (tPpeMainService.insertTPpeMain(tPpeMain) > 0) {
            if (CollectionUtils.isNotEmpty(tPpeMain.getCmldefLockers()))
                for (TPpeCmldefLocker cmldefLocker : tPpeMain.getCmldefLockers()) {
                    cmldefLocker.setMainId(tPpeMain.getId());
                    cmldefLocker.setDeptId(tPpeMain.getDeptId());
                    cmldefLocker.setRemarks(tPpeMain.getCmldefRemark());
                    cmldefLocker.setCreatedate(new Date());
                    cmldefLocker.setCreaterCode(getUserId().toString());
                    tPpeCmldefLockerService.insertTPpeCmldefLocker(cmldefLocker);
                }
            if (CollectionUtils.isNotEmpty(tPpeMain.getStaffLockers()))
                for (TPpeStaffLocker staffLocker : tPpeMain.getStaffLockers()) {
                    staffLocker.setMainId(tPpeMain.getId());
                    staffLocker.setDeptId(tPpeMain.getDeptId());
                    staffLocker.setRemarks(tPpeMain.getStaffRemark());
                    staffLocker.setCreatedate(new Date());
                    staffLocker.setCreaterCode(getUserId().toString());
                    tPpeStaffLockerService.insertTPpeStaffLocker(staffLocker);
                }
            for (TPpeWardrobe wardrobe : tPpeMain.getWardrobes()) {
                wardrobe.setMainId(tPpeMain.getId());
                wardrobe.setDeptId(tPpeMain.getDeptId());
                wardrobe.setRemarks(tPpeMain.getWardrobeRemark());
                wardrobe.setCreatedate(new Date());
                wardrobe.setCreaterCode(getUserId().toString());
                tPpeWardrobeService.insertTPpeWardrobe(wardrobe);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 修改员工PPE检查记录
     */

    @Log(title = "员工PPE检查记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TPpeMain tPpeMain) {
        return toAjax(tPpeMainService.updateTPpeMain(tPpeMain));
    }

    /**
     * 删除员工PPE检查记录
     */
    @Log(title = "员工PPE检查记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tPpeMainService.deleteTPpeMainByIds(ids));
    }
}
