package com.ruoyi.project.sems.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.ExcelUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
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.config.RuoYiConfig;
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.sems.domain.ParamData;
import com.ruoyi.project.sems.domain.TReportThird;
import com.ruoyi.project.sems.domain.TReportYlrq;
import com.ruoyi.project.sems.mapper.TReportYlrqMapper;
import com.ruoyi.project.sems.service.ITReportThirdService;
import com.ruoyi.project.sems.service.ITReportYlrqService;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.service.ISysDictTypeService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 压力容器年检报告Controller
 *
 * @author ruoyi
 * @date 2021-09-02
 */
@RestController
@RequestMapping("/sems/reportYlrq")
public class TReportYlrqController extends BaseController {

    @Autowired
    private ITReportYlrqService tReportYlrqService;
    @Autowired
    private ITReportThirdService itReportYlrqThirdService;
    @Resource
    private TReportYlrqMapper tReportYlrqMapper;
    @Autowired
    private ISysDictTypeService iSysDictTypeService;

    /**
     * 导出选中设备
     *
     * @param params
     * @return
     * @throws IOException
     */
    @PostMapping("/exportSelected")
    @RepeatSubmit
    public AjaxResult exportbmy(@RequestBody ParamData params) throws IOException {
        logger.info(JSON.toJSONString(params));
        String id = params.getIds();
        String[] ids = id.split(",");
        List<TReportYlrq> list = new ArrayList<>();
        for (String i : ids
        ) {
            TReportYlrq reportYlrqEntity = tReportYlrqService.selectTReportYlrqById(Long.parseLong(i));
            list.add(reportYlrqEntity);
        }
        SXSSFWorkbook wb = new SXSSFWorkbook(1000);
        CellStyle wrapStyle = wb.createCellStyle();
        wrapStyle.setWrapText(true);    //设置自动换行
        //创建sheet页
        Sheet sheet = wb.createSheet("sheet1");
        //设置列的宽度，第一个参数为列的序号，从0开始，第二参数为列宽，单位1/256个字节
        for (int i = 0; i < 42; i++) {
            if (i == 0 || i == 1 || i == 2 || i == 35 || i == 36 || i ==37 || i == 38 || i == 39 || i == 40 || i == 41) {
                sheet.setColumnWidth(i, 20 * 256);
            } else {
                sheet.setColumnWidth(i,  10 * 256);
            }
        }
        //设置开始行和开始列
        Row row0 = sheet.createRow(0);
        CellStyle styleGrey = wb.createCellStyle();
        Font headerFont = wb.createFont();
        headerFont.setBold(true);
        headerFont.setColor(IndexedColors.BLACK.getIndex());
        styleGrey.setFont(headerFont);
        styleGrey.setBorderBottom(BorderStyle.THIN); //下边框
        styleGrey.setBorderLeft(BorderStyle.THIN);//左边框
        styleGrey.setBorderTop(BorderStyle.THIN);//上边框
        styleGrey.setBorderRight(BorderStyle.THIN);//右边框
        styleGrey.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        styleGrey.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        CellStyle styleYellow = wb.createCellStyle();
        styleYellow.setFont(headerFont);
        styleYellow.setBorderBottom(BorderStyle.THIN); //下边框
        styleYellow.setBorderLeft(BorderStyle.THIN);//左边框
        styleYellow.setBorderTop(BorderStyle.THIN);//上边框
        styleYellow.setBorderRight(BorderStyle.THIN);//右边框
        styleYellow.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
        styleYellow.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        Cell cell0 = row0.createCell(0);
        row0.createCell(0).setCellValue("使用证");
        row0.createCell(1).setCellValue("注册编号");
        row0.createCell(2).setCellValue("位号");
        row0.createCell(3).setCellValue("容器管理");
        row0.createCell(4);
        row0.createCell(5);
        row0.createCell(6);
        row0.createCell(7);
        row0.createCell(8);
        row0.createCell(9);
        row0.createCell(10).setCellValue("本体及运行情况");
        row0.createCell(11);
        row0.createCell(12);
        row0.createCell(13);
        row0.createCell(14);
        row0.createCell(15);
        row0.createCell(16);
        row0.createCell(17);
        row0.createCell(18);
        row0.createCell(19);
        row0.createCell(20);
        row0.createCell(21);
        row0.createCell(22);
        row0.createCell(23);
        row0.createCell(24);
        row0.createCell(25);
        row0.createCell(26);
        row0.createCell(27).setCellValue("安全附件及措施");
        row0.createCell(28);
        row0.createCell(29);
        row0.createCell(30);
        row0.createCell(31);
        row0.createCell(32);
        row0.createCell(33);
        row0.createCell(34).setCellValue("其他");
        row0.createCell(35).setCellValue("备注");
        row0.createCell(36).setCellValue("检查日期");
        row0.createCell(37).setCellValue("下次年度检查日期");
        row0.createCell(38).setCellValue("问题及处理");
        row0.createCell(39).setCellValue("检查结论");
        row0.createCell(40).setCellValue("年检报告编号");
        row0.createCell(41).setCellValue("检查结论备注");
        Row row1 = sheet.createRow(1);
        row1.createCell(0);
        row1.createCell(1);
        row1.createCell(2);
        row1.createCell(3).setCellValue("1管理制度、操作规程、运行记录");
        row1.createCell(4).setCellValue("2设计文件、竣工图样、产品合格证、质量证明文件、安装使用说明");
        row1.createCell(5).setCellValue("3监督检验证书以及安装、改造、修理等资料");
        row1.createCell(6).setCellValue("4使用登记证，使用登记表是否与实际相符");
        row1.createCell(7).setCellValue("5日常维护保养、定期安全检查记录");
        row1.createCell(8).setCellValue("6年度检查、定期检验报告齐全，报告中问题解决");
        row1.createCell(9).setCellValue("7作业人员定期培训情况");
        row1.createCell(10).setCellValue("8设备铭牌、漆色、标志、使用证号码");
        row1.createCell(11).setCellValue("9本体裂、过热、变形、泄漏、损伤情况");
        row1.createCell(12).setCellValue("10接口部位、焊接接头等裂纹、泄漏、损伤情况");
        row1.createCell(13).setCellValue("11外表面腐蚀、异常结霜、结露情况");
        row1.createCell(14).setCellValue("12保温层、隔热层、衬里情况");
        row1.createCell(15).setCellValue("13检漏孔、信号孔");
        row1.createCell(16).setCellValue("14容器与相邻管道、构件间异常振动、响声、摩擦");
        row1.createCell(17).setCellValue("15支承、支座、基础、紧固螺栓");
        row1.createCell(18).setCellValue("16遮阳罩、操作台紧固");
        row1.createCell(19).setCellValue("17罐体与底盘等连接");
        row1.createCell(20).setCellValue("18波板、罐内扶梯与罐体连接");
        row1.createCell(21).setCellValue("19罐车拉紧带、鞍座、中间支座");
        row1.createCell(22).setCellValue("20气液相管及其它管路");
        row1.createCell(23).setCellValue("21疏水、排放、排污装置");
        row1.createCell(24).setCellValue("22设备运行稳定情况");
        row1.createCell(25).setCellValue("23接地装置");
        row1.createCell(26).setCellValue("24安全状况等级为4级的压力容器的监控措施");
        row1.createCell(27).setCellValue("25安全阀");
        row1.createCell(28).setCellValue("26压力表");
        row1.createCell(29).setCellValue("27爆破片");
        row1.createCell(30).setCellValue("28测温仪表");
        row1.createCell(31).setCellValue("29液位计");
        row1.createCell(32).setCellValue("30快开门安全联锁装置");
        row1.createCell(33).setCellValue("31紧急切断装置");
        row1.createCell(34).setCellValue("32装卸软管、装卸阀门");
        row1.createCell(35);
        row1.createCell(36);
        row1.createCell(37);
        row1.createCell(38);
        row1.createCell(39);
        row1.createCell(40);
        row1.createCell(41);
        Row row2 = sheet.createRow(2);
        for (int i = 0; i < 42; i++) {
            row2.createCell(i);
            if (i == 41) row2.createCell(i).setCellValue("1：符合要求");
        }
        Row row3 = sheet.createRow(3);
        for (int i = 0; i < 42; i++) {
            row3.createCell(i);
            if (i == 41) row3.createCell(i).setCellValue("2：基本符合要求");
        }
        Row row4 = sheet.createRow(4);
        for (int i = 0; i < 42; i++) {
            row4.createCell(i);
            if (i == 41) row4.createCell(i).setCellValue("3：不符合要求");
        }
        // 合并单元格
        sheet.addMergedRegion(new CellRangeAddress(0,1,0,0));
        sheet.addMergedRegion(new CellRangeAddress(0,1,1,1));
        sheet.addMergedRegion(new CellRangeAddress(0,1,2,2));
        sheet.addMergedRegion(new CellRangeAddress(0,0,3,9));
        sheet.addMergedRegion(new CellRangeAddress(0,0,10,26));
        sheet.addMergedRegion(new CellRangeAddress(0,0,27,33));
        sheet.addMergedRegion(new CellRangeAddress(0,1,35,35));
        sheet.addMergedRegion(new CellRangeAddress(0,1,36,36));
        sheet.addMergedRegion(new CellRangeAddress(0,1,37,37));
        sheet.addMergedRegion(new CellRangeAddress(0,1,38,38));
        sheet.addMergedRegion(new CellRangeAddress(0,1,39,39));
        sheet.addMergedRegion(new CellRangeAddress(0,1,40,40));
        sheet.addMergedRegion(new CellRangeAddress(0,1,41,41));
        // 表头样式
        for (int i = 0; i < 41; i++) {
            if (i < 3) {
                row0.getCell(i).setCellStyle(styleGrey);
                row1.getCell(i).setCellStyle(styleGrey);
            } else {
                row0.getCell(i).setCellStyle(styleYellow);
                row1.getCell(i).setCellStyle(styleYellow);
            }
        }
        //填充数据
        int rowIndex = 2;
        for (TReportYlrq t : list
        ) {
            if (rowIndex == 2 || rowIndex == 3 || rowIndex == 4) {
                sheet.getRow(rowIndex).getCell(0).setCellValue(t.getUseno());
                sheet.getRow(rowIndex).getCell(1).setCellValue(t.getRegno());
                sheet.getRow(rowIndex).getCell(2).setCellValue(t.getDevno());
                sheet.getRow(rowIndex).getCell(3).setCellValue(t.getPj1());
                sheet.getRow(rowIndex).getCell(4).setCellValue(t.getPj2());
                sheet.getRow(rowIndex).getCell(5).setCellValue(t.getPj3());
                sheet.getRow(rowIndex).getCell(6).setCellValue(t.getPj4());
                sheet.getRow(rowIndex).getCell(7).setCellValue(t.getPj5());
                sheet.getRow(rowIndex).getCell(8).setCellValue(t.getPj6());
                sheet.getRow(rowIndex).getCell(9).setCellValue(t.getPj7());
                sheet.getRow(rowIndex).getCell(10).setCellValue(t.getPj8());
                sheet.getRow(rowIndex).getCell(11).setCellValue(t.getPj9());
                sheet.getRow(rowIndex).getCell(12).setCellValue(t.getPj10());
                sheet.getRow(rowIndex).getCell(13).setCellValue(t.getPj11());
                sheet.getRow(rowIndex).getCell(14).setCellValue(t.getPj12());
                sheet.getRow(rowIndex).getCell(15).setCellValue(t.getPj13());
                sheet.getRow(rowIndex).getCell(16).setCellValue(t.getPj14());
                sheet.getRow(rowIndex).getCell(17).setCellValue(t.getPj15());
                sheet.getRow(rowIndex).getCell(18).setCellValue(t.getPj16());
                sheet.getRow(rowIndex).getCell(19).setCellValue(t.getPj17());
                sheet.getRow(rowIndex).getCell(20).setCellValue(t.getPj18());
                sheet.getRow(rowIndex).getCell(21).setCellValue(t.getPj19());
                sheet.getRow(rowIndex).getCell(22).setCellValue(t.getPj20());
                sheet.getRow(rowIndex).getCell(23).setCellValue(t.getPj21());
                sheet.getRow(rowIndex).getCell(24).setCellValue(t.getPj22());
                sheet.getRow(rowIndex).getCell(25).setCellValue(t.getPj23());
                sheet.getRow(rowIndex).getCell(26).setCellValue(t.getPj24());
                sheet.getRow(rowIndex).getCell(27).setCellValue(t.getPj25());
                sheet.getRow(rowIndex).getCell(28).setCellValue(t.getPj26());
                sheet.getRow(rowIndex).getCell(29).setCellValue(t.getPj27());
                sheet.getRow(rowIndex).getCell(30).setCellValue(t.getPj28());
                sheet.getRow(rowIndex).getCell(31).setCellValue(t.getPj29());
                sheet.getRow(rowIndex).getCell(32).setCellValue(t.getPj30());
                sheet.getRow(rowIndex).getCell(33).setCellValue(t.getPj31());
                sheet.getRow(rowIndex).getCell(34).setCellValue(t.getPj32());
                sheet.getRow(rowIndex).getCell(35).setCellValue(t.getRemarks());
                if (t.getCheckDate() != null) {
                    sheet.getRow(rowIndex).getCell(36).setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(t.getCheckDate()));
                }
                if (t.getNextCheckDate() != null) {
                    sheet.getRow(rowIndex).getCell(37).setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(t.getNextCheckDate()));
                }
                sheet.getRow(rowIndex).getCell(38).setCellValue(t.getProblem());
                sheet.getRow(rowIndex).getCell(39).setCellValue(t.getCon());
                sheet.getRow(rowIndex).getCell(40).setCellValue(t.getYearReportNo());
            } else {
                Row row = sheet.createRow(rowIndex);
                row.createCell(0).setCellValue(t.getUseno());
                row.createCell(1).setCellValue(t.getRegno());
                row.createCell(2).setCellValue(t.getDevno());
                row.createCell(3).setCellValue(t.getPj1());
                row.createCell(4).setCellValue(t.getPj2());
                row.createCell(5).setCellValue(t.getPj3());
                row.createCell(6).setCellValue(t.getPj4());
                row.createCell(7).setCellValue(t.getPj5());
                row.createCell(8).setCellValue(t.getPj6());
                row.createCell(9).setCellValue(t.getPj7());
                row.createCell(10).setCellValue(t.getPj8());
                row.createCell(11).setCellValue(t.getPj9());
                row.createCell(12).setCellValue(t.getPj10());
                row.createCell(13).setCellValue(t.getPj11());
                row.createCell(14).setCellValue(t.getPj12());
                row.createCell(15).setCellValue(t.getPj13());
                row.createCell(16).setCellValue(t.getPj14());
                row.createCell(17).setCellValue(t.getPj15());
                row.createCell(18).setCellValue(t.getPj16());
                row.createCell(19).setCellValue(t.getPj17());
                row.createCell(20).setCellValue(t.getPj18());
                row.createCell(21).setCellValue(t.getPj19());
                row.createCell(22).setCellValue(t.getPj20());
                row.createCell(23).setCellValue(t.getPj21());
                row.createCell(24).setCellValue(t.getPj22());
                row.createCell(25).setCellValue(t.getPj23());
                row.createCell(26).setCellValue(t.getPj24());
                row.createCell(27).setCellValue(t.getPj25());
                row.createCell(28).setCellValue(t.getPj26());
                row.createCell(29).setCellValue(t.getPj27());
                row.createCell(30).setCellValue(t.getPj28());
                row.createCell(31).setCellValue(t.getPj29());
                row.createCell(32).setCellValue(t.getPj30());
                row.createCell(33).setCellValue(t.getPj31());
                row.createCell(34).setCellValue(t.getPj32());
                row.createCell(35).setCellValue(t.getRemarks());
                if (t.getCheckDate() != null) {
                    row.createCell(36).setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(t.getCheckDate()));
                }
                if (t.getNextCheckDate() != null) {
                    row.createCell(37).setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(t.getNextCheckDate()));
                }
                row.createCell(38).setCellValue(t.getProblem());
                row.createCell(39).setCellValue(t.getCon());
                row.createCell(40).setCellValue(t.getYearReportNo());
            }
            rowIndex++;
        }

        OutputStream out = null;
        String filename = ExcelUtil.encodingFilename("压力容器年检报告批量更新");
        out = new FileOutputStream(ExcelUtil.getAbsoluteFile(filename));
        wb.write(out);
        wb.close();
        out.close();
        return AjaxResult.success(filename);
    }

    /**
     * 同步数据
     */
    @GetMapping("/sync")
    public AjaxResult syncData() {
        this.tReportYlrqService.deleteTReportYlrqByIds();
        this.tReportYlrqService.insertByTSpecdevYlrq();
        this.tReportYlrqService.updateByTSpecdevYlrq();
        return AjaxResult.success();
    }

    /**
     * 查询压力容器年检报告列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TReportYlrq tReportYlrq) {
        startPage();
        List<TReportYlrq> list = tReportYlrqService.selectTReportYlrqList(tReportYlrq);
        return getDataTable(list);
    }

    /**
     * 导出压力容器年检报告列表
     */
    @PreAuthorize("@ss.hasPermi('sems:reportYlrq:export')")
    @Log(title = "压力容器年检报告", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TReportYlrq tReportYlrq) {
        List<TReportYlrq> list = tReportYlrqService.selectTReportYlrqList(tReportYlrq);
        ExcelUtil<TReportYlrq> util = new ExcelUtil<TReportYlrq>(TReportYlrq.class);
        return util.exportExcel(list, "reportYlrq");
    }

    /**
     * 第三方数据导入
     *
     * @param file 导入文件
     * @return
     * @throws IOException
     */
    @PostMapping("/importForThird")
    public AjaxResult importForThird(@RequestParam("file") MultipartFile file, @RequestParam("plantCode") String plantCode,
                                     @RequestParam("year") String year) throws IOException, ParseException {
        //获取操作人员ID
        Long userId = getUserId();
        //文件保存至服务器下
        String fileName = file.getOriginalFilename();
        //文件存放路径
        String filePath = RuoYiConfig.getProfile() + "/sems/reportYlrqThird";
        String upload = FileUploadUtils.upload(filePath, file);
        // 文件路径保存至数据库
        TReportThird tReportYlrqThird = new TReportThird();
        tReportYlrqThird.setPlantCode(plantCode);
        tReportYlrqThird.setYear(year);
        tReportYlrqThird.setFileName(fileName);
        tReportYlrqThird.setFilePath(upload);
        tReportYlrqThird.setCreateBy(userId + "");
        tReportYlrqThird.setCreateTime(new Date());
        tReportYlrqThird.setType("1");
        int i = this.itReportYlrqThirdService.insertTReportThird(tReportYlrqThird);
        return AjaxResult.success(i);
    }

    /**
     * 批量导入
     */
    @Log(title = "压力容器批量导入更新", businessType = BusinessType.INSERT)
    @PostMapping("/importForUpdate")
    public AjaxResult importData(@RequestParam("file") MultipartFile file) throws IOException {
        //获取操作人员ID
        Long userId = getUserId();
        //报错行数统计
        List<Integer> failRow = new ArrayList<Integer>();
        Workbook workbook = ExcelUtils.getWorkBook(file);
        Sheet sheet = workbook.getSheetAt(0);
        List<TReportYlrq> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        int rowNum = sheet.getPhysicalNumberOfRows();
        int failNumber = 0;
        for (int i = 2; i < rowNum; i++) {
            try {
                logger.info("读取行数：" + i);
                Row row = sheet.getRow(i);
                int cellNum = row.getLastCellNum();
                TReportYlrq entity = new TReportYlrq();
                for (int j = 0; j < cellNum; j++) {
                    Cell cell = row.getCell(j);
                    if (cell == null) {
                        continue;
                    }
                    String cellValue = ExcelUtils.getCellValue(cell);
                    logger.info("cellValue:" + cellValue);
                    if (j == 0) {
                        //使用证
                        entity.setUseno(cellValue);
                    } else if (j == 1) {
                        //注册编号
                        entity.setRegno(cellValue);
                    } else if (j == 2) {
                        //位号
                        entity.setDevno(cellValue);
                    } else if (j == 3) {
                        //容器管理1
                        entity.setPj1(cellValue);
                    } else if (j == 4) {
                        //容器管理2
                        entity.setPj2(cellValue);
                    } else if (j == 5) {
                        //容器管理3
                        entity.setPj3(cellValue);
                    } else if (j == 6) {
                        //容器管理4
                        entity.setPj4(cellValue);
                    } else if (j == 7) {
                        //容器管理5
                        entity.setPj5(cellValue);
                    } else if (j == 8) {
                        //容器管理6
                        entity.setPj6(cellValue);
                    } else if (j == 9) {
                        //容器管理7
                        entity.setPj7(cellValue);
                    } else if (j == 10) {
                        //本体及运行情况8
                        entity.setPj8(cellValue);
                    } else if (j == 11) {
                        //本体及运行情况9
                        entity.setPj9(cellValue);
                    } else if (j == 12) {
                        //本体及运行情况10
                        entity.setPj10(cellValue);
                    } else if (j == 13) {
                        //本体及运行情况11
                        entity.setPj11(cellValue);
                    } else if (j == 14) {
                        //本体及运行情况12
                        entity.setPj12(cellValue);
                    } else if (j == 15) {
                        //本体及运行情况13
                        entity.setPj13(cellValue);
                    } else if (j == 16) {
                        //本体及运行情况14
                        entity.setPj14(cellValue);
                    } else if (j == 17) {
                        //本体及运行情况15
                        entity.setPj15(cellValue);
                    } else if (j == 18) {
                        //本体及运行情况16
                        entity.setPj16(cellValue);
                    } else if (j == 19) {
                        //本体及运行情况17
                        entity.setPj17(cellValue);
                    } else if (j == 20) {
                        //本体及运行情况18
                        entity.setPj18(cellValue);
                    } else if (j == 21) {
                        //本体及运行情况19
                        entity.setPj19(cellValue);
                    } else if (j == 22) {
                        //本体及运行情况20
                        entity.setPj20(cellValue);
                    } else if (j == 23) {
                        //本体及运行情况21
                        entity.setPj21(cellValue);
                    } else if (j == 24) {
                        //本体及运行情况22
                        entity.setPj22(cellValue);
                    } else if (j == 25) {
                        //本体及运行情况23
                        entity.setPj23(cellValue);
                    } else if (j == 26) {
                        //本体及运行情况24
                        entity.setPj24(cellValue);
                    } else if (j == 27) {
                        //安全附件及设施25
                        entity.setPj25(cellValue);
                    } else if (j == 28) {
                        //安全附件及设施26
                        entity.setPj26(cellValue);
                    } else if (j == 29) {
                        //安全附件及设施27
                        entity.setPj27(cellValue);
                    } else if (j == 30) {
                        //安全附件及设施28
                        entity.setPj28(cellValue);
                    } else if (j == 31) {
                        //安全附件及设施29
                        entity.setPj29(cellValue);
                    } else if (j == 32) {
                        //安全附件及设施30
                        entity.setPj30(cellValue);
                    } else if (j == 33) {
                        //安全附件及设施31
                        entity.setPj31(cellValue);
                    } else if (j == 34) {
                        //其他32
                        entity.setPj32(cellValue);
                    } else if (j == 35) {
                        //备注
                        entity.setRemarks(cellValue);
                    } else if (j == 36) {
                        //检查日期
                        entity.setCheckDate(cellValue != null ? new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue) : null);
                    } else if (j == 37) {
                        //下次年度检查日期
                        entity.setNextCheckDate(cellValue != null ? new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue) : null);
                    } else if (j == 38) {
                        //问题及处理
                        entity.setProblem(cellValue);
                    } else if (j == 39) {
                        //检查结论
                        entity.setCon(cellValue);
                    } else if (j == 40) {
                        //年检报告编号
                        entity.setYearReportNo(cellValue);
                    }
                }
                entity.setCreaterCode(userId);
                logger.info("entity:" + entity);
                list.add(entity);
            } catch (Exception e) {
                failNumber++;
                logger.info("e:" + JSON.toJSONString(e));
                failRow.add(i + 1);
            }
        }
        int successNumber = 0;
        int failNum = 0;
        CopyOnWriteArrayList<String> ids = new CopyOnWriteArrayList();
        for (TReportYlrq t : list
        ) {
            failNum++;
            try {
                //根据使用证、注册编号、位号，进行数据更新
                this.tReportYlrqService.updateForImport(t);
                successNumber++;
                ids.add(t.getId().toString());
            } catch (Exception e) {
                failNumber++;
                logger.info("e:" + e);
                failRow.add(failNum + 1);
            }
        }
        new Thread(() -> {
            sysnReport(ids.toArray());
        }, "压力容器年检报告修改同步").start();
        logger.info("list:" + JSON.toJSONString(list));
        logger.info("successNumber:" + String.valueOf(successNumber));
        logger.info("failNumber:" + String.valueOf(failNumber));
        logger.info("failRow:" + String.valueOf(failRow));
        return AjaxResult.success(String.valueOf(successNumber), failRow);
    }

    /**
     * 获取压力容器年检报告详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tReportYlrqService.selectTReportYlrqById(id));
    }

    /**
     * 新增压力容器年检报告
     */
    @PreAuthorize("@ss.hasPermi('sems:reportYlrq:add')")
    @Log(title = "压力容器年检报告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TReportYlrq tReportYlrq) {
        return toAjax(tReportYlrqService.insertTReportYlrq(tReportYlrq));
    }

    /**
     * 修改压力容器年检报告
     */
    @PreAuthorize("@ss.hasPermi('sems:reportYlrq:edit')")
    @Log(title = "压力容器年检报告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TReportYlrq tReportYlrq) {
        tReportYlrqService.updateTReportYlrq(tReportYlrq);
        new Thread(() -> {
            sysnReport(String.valueOf(tReportYlrq.getId()));
        }, "压力容器年检报告修改同步").start();
        return AjaxResult.success();
    }

    /**
     * 同步数据
     *
     * @param ids
     */
    public void sysnReport(Object[] ids) {
        List<SysDictData> con = iSysDictTypeService.selectDictDataByType("REPORT_CON");
        for (Object id : ids
        ) {
            TReportYlrq tReportYlrq = tReportYlrqService.selectTReportYlrqById(Long.parseLong(id.toString()));
            if (StringUtils.isNotEmpty(tReportYlrq.getCon())) {
                for (SysDictData c : con) {
                    if (tReportYlrq.getCon().equals(c.getDictValue())) {
                        tReportYlrq.setCon(c.getDictLabel());
                    }
                }
            }
            tReportYlrqMapper.sysnReport(tReportYlrq);
        }
    }

    /**
     * 同步数据
     *
     * @param id
     */
    public void sysnReport(String id) {
        List<SysDictData> con = iSysDictTypeService.selectDictDataByType("REPORT_CON");
            TReportYlrq tReportYlrq = tReportYlrqService.selectTReportYlrqById(Long.parseLong(id.toString()));
            if (StringUtils.isNotEmpty(tReportYlrq.getCon())) {
                for (SysDictData c : con) {
                    if (tReportYlrq.getCon().equals(c.getDictValue())) {
                        tReportYlrq.setCon(c.getDictLabel());
                    }
                }
            tReportYlrqMapper.sysnReport(tReportYlrq);
        }
    }

    /**
     * 删除压力容器年检报告
     */
    @PreAuthorize("@ss.hasPermi('sems:reportYlrq:remove')")
    @Log(title = "压力容器年检报告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tReportYlrqService.deleteTReportYlrqByIds(ids));
    }
}
