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

import com.alibaba.fastjson.JSON;
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.TDevAnnihilator;
import com.ruoyi.project.patrol.dev.service.ITDevAnnihilatorService;
import com.ruoyi.project.patrol.fire.domain.TFireAnnihilator;
import com.ruoyi.project.patrol.fire.service.ITFireAnnihilatorService;
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 org.springframework.web.multipart.MultipartFile;

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.*;

/**
 * 手提式/小推车式灭火器/灭火器材箱Controller
 *
 * @author ruoyi
 * @date 2023-08-22
 */
@RestController
@RequestMapping("/fire/annihilator")
public class TFireAnnihilatorController extends BaseController {
    @Autowired
    private ITFireAnnihilatorService tFireAnnihilatorService;

    @Autowired
    private ITDevAnnihilatorService tDevAnnihilatorService;

    @Autowired
    private ITPatrolPlanService tPatrolPlanService;

    @Autowired
    private ITPatrolIssuesService tPatrolIssuesService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询手提式/小推车式灭火器/灭火器材箱列表
     */
    @PreAuthorize("@ss.hasPermi('fire:annihilator:list')")
    @Log(title = "手提式/小推车式灭火器/灭火器材箱", businessType = BusinessType.OTHER)
    @GetMapping("/list")
    public TableDataInfo list(TFireAnnihilator tFireAnnihilator) {
        startPage();
        if (StringUtils.isEmpty(tFireAnnihilator.getIssuesFlag()))
            tFireAnnihilator.setIssuesFlag("0");
        List<TFireAnnihilator> list = tFireAnnihilatorService.selectTFireAnnihilatorList(tFireAnnihilator);
        return getDataTable(list);
    }

    /**
     * 导出手提式/小推车式灭火器/灭火器材箱列表
     */
    
    @Log(title = "手提式/小推车式灭火器/灭火器材箱", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TFireAnnihilator tFireAnnihilator) throws IOException {
        tFireAnnihilator.setIssuesFlag("0");
        List<TFireAnnihilator> list = tFireAnnihilatorService.selectTFireAnnihilatorList(tFireAnnihilator);
        if (list.size() == 0) {
            return AjaxResult.error("暂无可导出数据");
        }
        XSSFWorkbook workbook = new XSSFWorkbook();
        // 创建工作簿
        XSSFSheet sheet = workbook.createSheet();
        // 设置列宽
        sheet.setColumnWidth(0, 1200);
        sheet.setColumnWidth(1, 2800);
        sheet.setColumnWidth(2, 5500);
        sheet.setColumnWidth(3, 3500);
        sheet.setColumnWidth(4, 2600);
        sheet.setColumnWidth(5, 4500);
        sheet.setColumnWidth(6, 3500);
        sheet.setColumnWidth(7, 3500);
        sheet.setColumnWidth(8, 3500);
        sheet.setColumnWidth(9, 3500);
        sheet.setColumnWidth(10, 3500);
        //第一行
        XSSFRow row1 = sheet.createRow(0);
        row1.createCell(0).setCellValue("CBP-6.3.0-SCFSH-020.01  Rev:04  2021/10/01");
        ExcelUtils.createVoidCell(1, 10, row1);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 10));//合并
        //第二行
        XSSFRow row2 = sheet.createRow(1);
        row2.createCell(0).setCellValue("手提式/小推车式灭火器/灭火器材箱检查记录");
        ExcelUtils.createVoidCell(1, 10, row2);
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 10));
        //第三行
        XSSFRow row3 = sheet.createRow(2);
        row3.createCell(0).setCellValue("年度：" + tFireAnnihilator.getYear());
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 2));
        ExcelUtils.createVoidCell(1, 10, row3);
        row3.createCell(5).setCellValue("检查频率：1次/半月");
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 5, 10));
        //第四行
        XSSFRow row4 = sheet.createRow(3);
        row4.createCell(0).setCellValue("序号");
        row4.createCell(1).setCellValue("位号");
        row4.createCell(2).setCellValue("位置");
        row4.createCell(3).setCellValue("灭火器型号&类型");
        row4.createCell(4).setCellValue("数量");
        row4.createCell(5).setCellValue("检查内容");
        ExcelUtils.createVoidCell(6, 10, row4);
        sheet.addMergedRegion(new CellRangeAddress(3, 3, 5, 10));
        //第五行
        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));
        sheet.addMergedRegion(new CellRangeAddress(3, 4, 3, 3));
        sheet.addMergedRegion(new CellRangeAddress(3, 4, 4, 4));
        ExcelUtils.createVoidCell(0, 4, row5);
        row5.createCell(5).setCellValue("灭火器材箱外观及箱体内外整洁");
        row5.createCell(6).setCellValue("灭火器外观/胶管/喷嘴/连接管");
        row5.createCell(7).setCellValue("灭火器压力");
        row5.createCell(8).setCellValue("灭火器规格及检测时间");
        row5.createCell(9).setCellValue("防雨防晒护罩");
        row5.createCell(10).setCellValue("检查人");
        Long planId = null;
        //遍历数组赋值
        for (int i = 0; i < list.size(); i++) {
            TFireAnnihilator annihilator = list.get(i);
            TDevAnnihilator tDevAnnihilator = new TDevAnnihilator();
            tDevAnnihilator.setDevNo(annihilator.getDevNo());
            tDevAnnihilator.setPosition(annihilator.getPosition());
            List<TDevAnnihilator> tDevAnnihilators = tDevAnnihilatorService.selectTDevAnnihilatorList(tDevAnnihilator);
            TDevAnnihilator item = tDevAnnihilators.get(0);
            planId = annihilator.getPlanId();
            XSSFRow row = sheet.createRow(5 + i);
            row.createCell(0).setCellValue(i + 1);
            row.createCell(1).setCellValue(annihilator.getDevNo());
            row.createCell(2).setCellValue(annihilator.getPosition());
            row.createCell(3).setCellValue(annihilator.getAnniType());
            row.createCell(4).setCellValue(annihilator.getAnniNum());
            if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("anniClean"))
                row.createCell(5).setCellValue("/");
            else
                row.createCell(5).setCellValue("1".equals(annihilator.getAnniClean()) ? "√" : annihilator.getAnniClean());
            if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("anniBody"))
                row.createCell(6).setCellValue("/");
            else
                row.createCell(6).setCellValue("1".equals(annihilator.getAnniBody()) ? "√" : annihilator.getAnniBody());
            if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("anniPressure"))
                row.createCell(7).setCellValue("/");
            else
                row.createCell(7).setCellValue("1".equals(annihilator.getAnniPressure()) ? "√" : annihilator.getAnniPressure());
            if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("anniSpecs"))
                row.createCell(8).setCellValue("/");
            else
                row.createCell(8).setCellValue("1".equals(annihilator.getAnniSpecs()) ? "√" : annihilator.getAnniSpecs());
            if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("anniSafe"))
                row.createCell(9).setCellValue("/");
            else
                row.createCell(9).setCellValue("1".equals(annihilator.getAnniSafe()) ? "√" : annihilator.getAnniSafe());
            XSSFCell cell10 = row.createCell(10);
            if (StringUtils.isNotEmpty(annihilator.getUpdaterCode())) {
                SysUser sysUser = sysUserService.selectUserById(Long.valueOf(annihilator.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, 6, row28);
        sheet.addMergedRegion(new CellRangeAddress(nextRowNum, nextRowNum, 0, 4));
        if (StringUtils.isNotEmpty(confirmed))
            ExcelUtils.insertPicture(workbook, sheet, confirmed, nextRowNum, 7, 1, 1);
        ExcelUtils.createVoidCell(3, 10, row28);
        sheet.addMergedRegion(new CellRangeAddress(nextRowNum, nextRowNum, 5, 10));
        // 下一行
        nextRowNum++;
        XSSFRow row29 = sheet.createRow(nextRowNum);
        row29.setHeightInPoints(30);
        XSSFCell row29Cell1 = row29.createCell(0);
        row29Cell1.setCellValue("日期");
        ExcelUtils.createVoidCell(1, 4, row29);
        sheet.addMergedRegion(new CellRangeAddress(nextRowNum, nextRowNum, 0, 4));
        row29.createCell(5).setCellValue(confirmedDate != null ? new SimpleDateFormat("yyyy-MM-dd").format(confirmedDate) : "");
        ExcelUtils.createVoidCell(6, 10, row29);
        sheet.addMergedRegion(new CellRangeAddress(nextRowNum, nextRowNum, 5, 10));
        // 设置单元格样式
        // 文字居中，自动换行，四周边框
        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 (Row row : sheet) {
            row.setHeightInPoints(30);
            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);
                }
            }
        }
        nextRowNum++;
        XSSFRow row30 = sheet.createRow(nextRowNum);
        row30.createCell(0).setCellValue("说明：");
        ExcelUtils.createVoidCell(1, 10, row30);
        sheet.addMergedRegion(new CellRangeAddress(nextRowNum, nextRowNum, 0, 10));

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

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

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

        //返回
        String fileName = "PSSR_13_消气防设施_手提式、小推车式灭火器、灭火器材箱检查记录.xlsx";
        OutputStream os = Files.newOutputStream(Paths.get(ExcelUtil.getAbsoluteFile(fileName)));
        workbook.write(os);
        workbook.close();
        return AjaxResult.success(fileName);
    }

    /**
     * 获取手提式/小推车式灭火器/灭火器材箱详细信息
     */
    @PreAuthorize("@ss.hasPermi('fire:annihilator:query')")
    @Log(title = "手提式/小推车式灭火器/灭火器材箱", businessType = BusinessType.OTHER)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        TFireAnnihilator fireAnnihilator = tFireAnnihilatorService.selectTFireAnnihilatorById(id);
        if (fireAnnihilator!=null) {
            TDevAnnihilator tDevAnnihilator = new TDevAnnihilator();
            tDevAnnihilator.setPosition(fireAnnihilator.getPosition());
            tDevAnnihilator.setPrecision(fireAnnihilator.getDevNo());
            fireAnnihilator.setIgnoreColumn(tDevAnnihilatorService.selectTDevAnnihilatorList(tDevAnnihilator).get(0).getIgnoreColumn());
        }
        return AjaxResult.success(fireAnnihilator);
    }

    /**
     * 获取手提式/小推车式灭火器/灭火器材箱详细信息
     */
    @PreAuthorize("@ss.hasPermi('fire:annihilator:query')")
    @Log(title = "手提式/小推车式灭火器/灭火器材箱", businessType = BusinessType.OTHER)
    @GetMapping(value = "/info")
    public AjaxResult getInfo(TFireAnnihilator tFireAnnihilator) {
        if (StringUtils.isEmpty(tFireAnnihilator.getDevNo()) || StringUtils.isNull(tFireAnnihilator.getPlanId())) {
            return AjaxResult.error("位号或计划id不能为空！");
        }
        TFireAnnihilator annihilator = new TFireAnnihilator();
        annihilator.setPlanId(tFireAnnihilator.getPlanId());
        annihilator.setPrecision(tFireAnnihilator.getDevNo());
        annihilator.setIssuesFlag("0");
        List<TFireAnnihilator> tFireAnnihilators = tFireAnnihilatorService.selectTFireAnnihilatorList(annihilator);
        if (CollectionUtils.isNotEmpty(tFireAnnihilators)) {
            TFireAnnihilator fireAnnihilator = tFireAnnihilators.get(0);
            TDevAnnihilator tDevAnnihilator = new TDevAnnihilator();
            tDevAnnihilator.setPosition(fireAnnihilator.getPosition());
            tDevAnnihilator.setPrecision(fireAnnihilator.getDevNo());
            fireAnnihilator.setIgnoreColumn(tDevAnnihilatorService.selectTDevAnnihilatorList(tDevAnnihilator).get(0).getIgnoreColumn());
            return AjaxResult.success(fireAnnihilator);
        }
        return AjaxResult.error("此设备不在当前巡检计划中!");
    }

    /**
     * 新增手提式/小推车式灭火器/灭火器材箱
     */
    @PreAuthorize("@ss.hasPermi('fire:annihilator:add')")
    @Log(title = "手提式/小推车式灭火器/灭火器材箱", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TFireAnnihilator tFireAnnihilator) {
        return toAjax(tFireAnnihilatorService.insertTFireAnnihilator(tFireAnnihilator));
    }

    /**
     * 修改手提式/小推车式灭火器/灭火器材箱
     */
    @PreAuthorize("@ss.hasPermi('fire:annihilator:edit')")
    @Log(title = "手提式/小推车式灭火器/灭火器材箱", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@RequestBody TFireAnnihilator tFireAnnihilator) {
        tFireAnnihilator.setUpdatedate(new Date());
        tFireAnnihilator.setUpdaterCode(getUserId().toString());
        tFireAnnihilator.setCheckStatus("1");
        tFireAnnihilator.setCheckDate(new Date());
        TFireAnnihilator annihilator = tFireAnnihilatorService.selectTFireAnnihilatorById(tFireAnnihilator.getId());
        TPatrolPlan tPatrolPlan = tPatrolPlanService.selectTPatrolPlanById(tFireAnnihilator.getPlanId());
        if (tPatrolPlan.getStatus() == 1) {
            return AjaxResult.error("当前选择的计划已确认！");
        }
        if (!"1".equals(annihilator.getCheckStatus())) {
            tPatrolPlan.setUncheckedNum(tPatrolPlan.getUncheckedNum() - 1);
            tPatrolPlan.setCheckedNum(tPatrolPlan.getCheckedNum() + 1);
            tPatrolPlanService.updateTPatrolPlan(tPatrolPlan);
        }
        TDevAnnihilator tDevAnnihilator = new TDevAnnihilator();
        tDevAnnihilator.setDevNo(annihilator.getDevNo());
        tDevAnnihilator.setPosition(annihilator.getPosition());
        List<TDevAnnihilator> tDevAnnihilators = tDevAnnihilatorService.selectTDevAnnihilatorList(tDevAnnihilator);
        TDevAnnihilator item = tDevAnnihilators.get(0);
        if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("anniClean")) {
            tFireAnnihilator.setAnniClean("/");
        }
        if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("anniBody")) {
            tFireAnnihilator.setAnniBody("/");
        }
        if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("anniPressure")) {
            tFireAnnihilator.setAnniPressure("/");
        }
        if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("anniSpecs")) {
            tFireAnnihilator.setAnniSpecs("/");
        }
        if (StringUtils.isNotEmpty(item.getIgnoreColumn()) && item.getIgnoreColumn().contains("anniSafe")) {
            tFireAnnihilator.setAnniSafe("/");
        }
        if (StringUtils.isNotEquals(tFireAnnihilator.getAnniClean(), "1", "/") || StringUtils.isNotEquals(tFireAnnihilator.getAnniBody(), "1", "/") ||
                StringUtils.isNotEquals(tFireAnnihilator.getAnniPressure(), "1", "/") || StringUtils.isNotEquals(tFireAnnihilator.getAnniSpecs(), "1", "/") ||
                StringUtils.isNotEquals(tFireAnnihilator.getAnniSafe(), "1", "/")) {
            tFireAnnihilator.setIssuesStatus("1");
            tFireAnnihilator.setCheckStatus("0");
            tFireAnnihilatorService.updateTFireAnnihilator(tFireAnnihilator);
            tFireAnnihilator.setIssuesFlag("1");
            tFireAnnihilator.setCheckStatus("1");
            tFireAnnihilator.setCreatedate(new Date());
            tFireAnnihilator.setCreaterCode(getUserId().toString());
            tFireAnnihilator.setDevNo(annihilator.getDevNo());
            tFireAnnihilator.setPosition(annihilator.getPosition());
            tFireAnnihilator.setAnniType(annihilator.getAnniType());
            tFireAnnihilator.setAnniNum(annihilator.getAnniNum());
            tFireAnnihilator.setRate(annihilator.getRate());
            tFireAnnihilatorService.insertTFireAnnihilator(tFireAnnihilator);
            TPatrolIssues tPatrolIssues = new TPatrolIssues();
            tPatrolIssues.setPatrolType("1");
            tPatrolIssues.setDevDescribe(annihilator.getDevNo() + " " + annihilator.getPosition());
            tPatrolIssues.setLinkId(tFireAnnihilator.getId());
            tPatrolIssues.setStatus(0L);
            tPatrolIssuesService.insertTPatrolIssues(tPatrolIssues);
        } else {
            if ("1".equals(annihilator.getIssuesStatus())) {
                TPatrolIssues tPatrolIssues = new TPatrolIssues();
                tPatrolIssues.setPatrolType("1");
                tPatrolIssues.setDevDescribe(annihilator.getDevNo() + " " + annihilator.getPosition());
                tPatrolIssues.setCreatedate(new Date());
                for (TPatrolIssues patrolIssues : tPatrolIssuesService.selectTPatrolIssuesList(tPatrolIssues)) {
                    patrolIssues.setStatus(1L);
                    tPatrolIssuesService.updateTPatrolIssues(patrolIssues);
                }
            }
            tFireAnnihilator.setIssuesStatus("0");
            tFireAnnihilatorService.updateTFireAnnihilator(tFireAnnihilator);
        }
        return AjaxResult.success();
    }

    /**
     * 删除手提式/小推车式灭火器/灭火器材箱
     */
    @PreAuthorize("@ss.hasPermi('fire:annihilator:remove')")
    @Log(title = "手提式/小推车式灭火器/灭火器材箱", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tFireAnnihilatorService.deleteTFireAnnihilatorByIds(ids));
    }

    @Log(title = "设备总表格批量导入更新", businessType = BusinessType.INSERT)
    @PostMapping("/importData")
    public AjaxResult importData(@RequestParam("file") MultipartFile file) throws IOException {
        //获取操作人员ID
        Long userId = getUserId();
        //报错行数统计
        List<Integer> failRow = new ArrayList<>();
        Workbook workbook = ExcelUtils.getWorkBook(file);
        Sheet sheet = workbook.getSheetAt(0);
        List<TFireAnnihilator> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int rowNum = sheet.getPhysicalNumberOfRows();
        int failNumber = 0;
        for (int i = 1; i < rowNum; i++) {
            try {
                logger.info("读取行数：" + i);
                Row row = sheet.getRow(i);
                int cellNum = row.getLastCellNum();
                TFireAnnihilator entity = new TFireAnnihilator();
                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.setDevNo(cellValue);
                    } else if (j == 1) {
                        // 位置
                        entity.setPosition(cellValue);
                    } else if (j == 2) {
                        // 灭火器型号&类型
                        entity.setAnniType(cellValue);
                    } else if (j == 3) {
                        // 数量
                        entity.setAnniNum(cellValue);
                    } else if (j == 4) {
                        // 灭火器材箱外观及箱体内外整洁
                        entity.setAnniClean(cellValue);
                    } else if (j == 5) {
                        // 灭火器外观/胶管/喷嘴/连接管
                        entity.setAnniBody(cellValue);
                    } else if (j == 6) {
                        // 灭火器压力
                        entity.setAnniPressure(cellValue);
                    } else if (j == 7) {
                        // 灭火器规格
                        entity.setAnniSpecs(cellValue);
                    } else if (j == 8) {
                        // 检测时间
                        if (StringUtils.isNotEmpty(cellValue)) {
                            Date date = sdf.parse(cellValue);
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTime(date);
                            int day = calendar.get(Calendar.DATE);
                            if (day <= 15)
                                entity.setRate("1");
                            else
                                entity.setRate("2");
                            entity.setAnniCheckDate(date);
                            entity.setYear((long) calendar.get(Calendar.YEAR));
                            entity.setMonth((long) calendar.get(Calendar.MONTH) + 1);
                        }
                    } else if (j == 9) {
                        // 防雨防晒护罩
                        entity.setAnniSafe(cellValue);
                    }
                }
                entity.setCreaterCode(String.valueOf(userId));
                entity.setCheckStatus("1");
                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;
        for (TFireAnnihilator t : list
        ) {
            failNum++;
            try {
                //
                TFireAnnihilator annihilator = new TFireAnnihilator();
                annihilator.setPosition(t.getPosition());
                annihilator.setDevNo(t.getDevNo());
                annihilator.setYear(t.getYear());
                annihilator.setMonth(t.getMonth());
                annihilator.setRate(t.getRate());
                List<TFireAnnihilator> tFireAnnihilators = tFireAnnihilatorService.selectTFireAnnihilatorList(annihilator);
                if (CollectionUtils.isNotEmpty(tFireAnnihilators)) {
                    t.setId(tFireAnnihilators.get(0).getId());
                    edit(t);
                    successNumber++;
                } else {
                    failNumber++;
                }
            } catch (Exception e) {
                failNumber++;
                logger.info("e:" + e);
                failRow.add(failNum + 1);
            }
        }
        logger.info("list:" + JSON.toJSONString(list));
        logger.info("successNumber:" + successNumber);
        logger.info("failNumber:" + failNumber);
        logger.info("failRow:" + failRow);
        return AjaxResult.success(String.valueOf(successNumber), failRow);
    }


}
