package com.ruoyi.project.process.controller;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.ExcelUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.project.common.domain.DataEntity;
import com.ruoyi.project.common.domain.TCommonfile;
import com.ruoyi.project.common.service.ITCommonfileService;
import com.ruoyi.project.process.controller.export.*;
import com.ruoyi.project.process.controller.vo.*;
import com.ruoyi.project.process.mapper.TMocMapper;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysDeptService;
import com.ruoyi.project.system.service.ISysDictTypeService;
import com.ruoyi.project.system.service.ISysUserService;
import com.ruoyi.project.training.domain.TTraining;
import com.ruoyi.project.training.service.ITTrainingService;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.process.domain.TMoc;
import com.ruoyi.project.process.service.ITMocService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * MOC管理Controller
 *
 * @author ruoyi
 * @date 2020-12-07
 */
@RestController
@RequestMapping("/process/moc")
public class TMocController extends BaseController
{
    @Autowired
    private ITMocService tMocService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysDictTypeService iSysDictTypeService;

    @Autowired
    private ISysDeptService iSysDeptService;

    @Resource
    private TMocMapper tMocMapper;

    @Autowired
    private ITTrainingService tTrainingService;

    @Autowired
    private ITCommonfileService commonfileService;

    /**
     * 导出临时MOC - 带压消漏清单
     */
    @PreAuthorize("@ss.hasPermi('process:moc:export')")
    @GetMapping("/exportaquifier")
    public AjaxResult exportAquifier(TMoc tMoc)
    {
        // 当前用户归属部门
        Long deptId = userService.selectUserById(getUserId()).getDeptId();
        // 查询条件
        // 1. 数据归属部门=当前用户归属部门
        tMoc.setDeptId(deptId);
        // 2. 时效性=临时
        tMoc.setTimeliness("2");
        // 3. 临时MOC类别
        tMoc.setTempCategory("1");
        // 查询总表数据
        List<TMoc> mocList = tMocService.selectTMocList(tMoc);
        // 带压消漏清单数据
        List<Aquifier> aquifierList = new ArrayList<Aquifier>();
        //字典查询
        List<SysDictData> mocStatusDict = iSysDictTypeService.selectDictDataByType("MOC_STATUS");
        // 遍历总表数据，插入带压消漏清单
        for (TMoc moc : mocList) {
            Aquifier aquifier = new Aquifier();
            aquifier.setMocNo(moc.getMocNo());
            aquifier.setCompanyMocNo(moc.getCompanyMocNo());
            aquifier.setProcessUnit(moc.getProcessUnit());
            aquifier.setDepartment(moc.getDepartment());
            aquifier.setLeakLocation(moc.getLeakLocation());
            aquifier.setTitle(moc.getTitle());
            aquifier.setPressure(moc.getPressure());
            aquifier.setTemperature(moc.getTemperature());
            aquifier.setLeakFluid(moc.getLeakFluid());
            aquifier.setMaterialType(moc.getMaterialType());
            aquifier.setPlanDate(moc.getPlanDate());
            aquifier.setSealDate(moc.getSealDate());
            aquifier.setExtention1(moc.getExtention1());
            aquifier.setExtention2(moc.getExtention2());
            aquifier.setExtention3(moc.getExtention3());
            aquifier.setExtention4(moc.getExtention4());
            aquifier.setExtention5(moc.getExtention5());
            aquifier.setExtention6(moc.getExtention6());
            aquifier.setExtention7(moc.getExtention7());
            aquifier.setExtention8(moc.getExtention8());
            aquifier.setExtention9(moc.getExtention9());
            for (SysDictData sysDictData : mocStatusDict) {
                if (sysDictData.getDictValue().equals(moc.getStatus())) {
                    aquifier.setStatus(sysDictData.getDictLabel());
                }
            }
            aquifier.setRemoveTime(moc.getRemoveTime());
            aquifier.setRemarks(moc.getRemarks());
            aquifierList.add(aquifier);
        }
        ExcelUtil<Aquifier> util = new ExcelUtil<Aquifier>(Aquifier.class);
        return util.exportExcel(aquifierList, "带压消漏清单");
    }

    /**
     * 批量导入临时MOC - 带压消漏清单
     */
    @PreAuthorize("@ss.hasPermi('process:moc:add')")
    @PostMapping("/aquifier/importData")
    public AjaxResult importAquifierData(@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<TMoc> list = new ArrayList<TMoc>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //字典查询
        List<SysDictData> mocStatusDict = iSysDictTypeService.selectDictDataByType("MOC_STATUS");
        //部门查询
        List<SysDept> dept = iSysDeptService.selectDeptList(new SysDept());
        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();
                TMoc entity = new TMoc();
                entity.setTimeliness("2");
                entity.setTempCategory("1");
                entity.setDeptId(userService.selectUserById(getUserId()).getDeptId());
                for (int j = 0; j < cellNum; j++) {
                    Cell cell = row.getCell(j);
                    //  cell.setCellType(CellType.STRING);
                    String cellValue = ExcelUtils.getCellValue(cell);
                    logger.info("cellValue:" + cellValue);
                    if (j == 0) {
                        entity.setMocNo(cellValue);
                    } else if (j == 1) {
                        entity.setCompanyMocNo(cellValue);
                    } else if (j == 2) {
                        entity.setProcessUnit(cellValue);
                    } else if (j == 3) {
                        entity.setDepartment(cellValue);
                    } else if (j == 4) {
                        entity.setLeakLocation(cellValue);
                    } else if (j == 5) {
                        entity.setTitle(cellValue);
                    } else if (j == 6) {
                        entity.setPressure(cellValue);
                    } else if (j == 7) {
                        entity.setTemperature(cellValue);
                    } else if (j == 8) {
                        entity.setLeakFluid(cellValue);
                    } else if (j == 9) {
                        entity.setMaterialType(cellValue);
                    } else if (j == 10) {
                        if (cellValue.length() > 3) {
                            entity.setPlanDate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 11) {
                        if (cellValue.length() > 3) {
                            entity.setSealDate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 12) {
                        if (cellValue.length() > 3) {
                            entity.setExtention1(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 13) {
                        if (cellValue.length() > 3) {
                            entity.setExtention2(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 14) {
                        if (cellValue.length() > 3) {
                            entity.setExtention3(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 15) {
                        if (cellValue.length() > 3) {
                            entity.setExtention4(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 16) {
                        if (cellValue.length() > 3) {
                            entity.setExtention5(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 17) {
                        if (cellValue.length() > 3) {
                            entity.setExtention6(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 18) {
                        if (cellValue.length() > 3) {
                            entity.setExtention7(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 19) {
                        if (cellValue.length() > 3) {
                            entity.setExtention8(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 20) {
                        if (cellValue.length() > 3) {
                            entity.setExtention9(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    }else if (j == 21) {
                        for (SysDictData sysDictData : mocStatusDict) {
                            if (sysDictData.getDictLabel().equals(cellValue.trim())) {
                                entity.setStatus(sysDictData.getDictValue());
                            }
                        }
                    } else if (j == 22) {
                        if (cellValue.length() > 3) {
                            entity.setRemoveTime(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 23) {
                        entity.setRemarks(cellValue);
                    }
                }
                entity.setCreaterCode(userId.toString());
                logger.info("entity:" + entity);
                list.add(entity);
            }catch (Exception e){
                failNumber++;
                failRow.add(i+1);
            }
        }
        int successNumber = 0;
        int failNum = 0;
        for (TMoc t : list
        ) {
            failNum++;
            try {
                tMocService.insertTMoc(t);
                successNumber++;
            }catch (Exception e){
                failNumber++;
                logger.info("e:" + e);
                failRow.add(failNum+1);
            }
        }
        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);
    }

    /**
     * 导出临时MOC - 临时设施、其它
     */
    @PreAuthorize("@ss.hasPermi('process:moc:export')")
    @GetMapping("/exportfacility")
    public AjaxResult exportFacility(TMoc tMoc)
    {
        // 当前用户归属部门
        Long deptId = userService.selectUserById(getUserId()).getDeptId();
        // 查询条件
        // 1. 数据归属部门=当前用户归属部门
        tMoc.setDeptId(deptId);
        // 2. 时效性=临时
        tMoc.setTimeliness("2");
        // 3. 临时MOC类别
        tMoc.setTempCategory("2");
        // 查询总表数据
        List<TMoc> mocList = tMocService.selectTMocList(tMoc);
        // 临时设施、其它数据
        List<Facility> facilityList = new ArrayList<Facility>();
        //字典查询
        List<SysDictData> tempStateDict = iSysDictTypeService.selectDictDataByType("TEMP_STATE");
        // 遍历总表数据，插入临时设施、其它
        for (TMoc moc : mocList) {
            Facility facility = new Facility();
            facility.setMocNo(moc.getMocNo());
            facility.setCompanyMocNo(moc.getCompanyMocNo());
            facility.setLocation(moc.getLocation());
            facility.setTitle(moc.getTitle());
            facility.setExpTime(moc.getExpTime());
            for (SysDictData sysDictData : tempStateDict) {
                if (sysDictData.getDictValue().equals(moc.getTempState())) {
                    facility.setTempState(sysDictData.getDictLabel());
                }
            }
            facility.setRemoveTime(moc.getRemoveTime());
            facility.setRemarks(moc.getRemarks());
            facilityList.add(facility);
        }
        ExcelUtil<Facility> util = new ExcelUtil<Facility>(Facility.class);
        return util.exportExcel(facilityList, "临时设施及其它");
    }

    /**
     * 批量导入临时MOC - 临时设施、其它
     */
    @PreAuthorize("@ss.hasPermi('process:moc:add')")
    @PostMapping("/facility/importData")
    public AjaxResult importFacilityData(@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<TMoc> list = new ArrayList<TMoc>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //字典查询
        List<SysDictData> tempStateDict = iSysDictTypeService.selectDictDataByType("TEMP_STATE");
        //部门查询
        List<SysDept> dept = iSysDeptService.selectDeptList(new SysDept());
        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();
                TMoc entity = new TMoc();
                entity.setTimeliness("2");
                entity.setTempCategory("2");
                entity.setDeptId(userService.selectUserById(getUserId()).getDeptId());
                for (int j = 0; j < cellNum; j++) {
                    Cell cell = row.getCell(j);
                    //  cell.setCellType(CellType.STRING);
                    String cellValue = ExcelUtils.getCellValue(cell);
                    logger.info("cellValue:" + cellValue);
                    if (j == 0) {
                        entity.setMocNo(cellValue);
                    } else if (j == 1) {
                        entity.setCompanyMocNo(cellValue);
                    } else if (j == 2) {
                        entity.setLocation(cellValue);
                    } else if (j == 3) {
                        entity.setTitle(cellValue);
                    } else if (j == 4) {
                        if (cellValue.length() > 3) {
                            entity.setExpTime(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 5) {
                        for (SysDictData sysDictData : tempStateDict) {
                            if (sysDictData.getDictLabel().equals(cellValue.trim())) {
                                entity.setTempState(sysDictData.getDictValue());
                            }
                        }
                    } else if (j == 6) {
                        if (cellValue.length() > 3) {
                            entity.setRemoveTime(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 7) {
                        entity.setRemarks(cellValue);
                    }
                }
                entity.setCreaterCode(userId.toString());
                logger.info("entity:" + entity);
                list.add(entity);
            }catch (Exception e){
                failNumber++;
                failRow.add(i+1);
            }
        }
        int successNumber = 0;
        int failNum = 0;
        for (TMoc t : list
        ) {
            failNum++;
            try {
                tMocService.insertTMoc(t);
                successNumber++;
            }catch (Exception e){
                failNumber++;
                logger.info("e:" + e);
                failRow.add(failNum+1);
            }
        }
        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);
    }

    /**
     * 导出临时MOC - 仪表联锁旁路
     */
    @PreAuthorize("@ss.hasPermi('process:moc:export')")
    @GetMapping("/exportinterlock")
    public AjaxResult exportInterlock(TMoc tMoc)
    {
        // 当前用户归属部门
        Long deptId = userService.selectUserById(getUserId()).getDeptId();
        // 查询条件
        // 1. 数据归属部门=当前用户归属部门
        tMoc.setDeptId(deptId);
        // 2. 时效性=临时
        tMoc.setTimeliness("2");
        // 3. 临时MOC类别
        tMoc.setTempCategory("3");
        // 查询总表数据
        List<TMoc> mocList = tMocService.selectTMocList(tMoc);
        // 仪表联锁旁路数据
        List<Interlock> interlockList = new ArrayList<Interlock>();
        //字典查询
        List<SysDictData> tempStateDict = iSysDictTypeService.selectDictDataByType("TEMP_STATE");
        // 遍历总表数据，插入仪表联锁旁路
        for (TMoc moc : mocList) {
            Interlock interlock = new Interlock();
            interlock.setMocNo(moc.getMocNo());
            interlock.setCompanyMocNo(moc.getCompanyMocNo());
            interlock.setTitle(moc.getTitle());
            interlock.setFinishDate(moc.getFinishDate());
            interlock.setExpTime(moc.getExpTime());
            for (SysDictData sysDictData : tempStateDict) {
                if (sysDictData.getDictValue().equals(moc.getTempState())) {
                    interlock.setTempState(sysDictData.getDictLabel());
                }
            }
            interlock.setRemoveTime(moc.getRemoveTime());
            interlock.setRemarks(moc.getRemarks());
            interlockList.add(interlock);
        }
        ExcelUtil<Interlock> util = new ExcelUtil<Interlock>(Interlock.class);
        return util.exportExcel(interlockList, "仪表联锁旁路");
    }

    /**
     * 批量导入临时MOC - 仪表联锁旁路
     */
    @PreAuthorize("@ss.hasPermi('process:moc:add')")
    @PostMapping("/interlock/importData")
    public AjaxResult importInterlockData(@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<TMoc> list = new ArrayList<TMoc>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //字典查询
        List<SysDictData> tempStateDict = iSysDictTypeService.selectDictDataByType("TEMP_STATE");
        //部门查询
        List<SysDept> dept = iSysDeptService.selectDeptList(new SysDept());
        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();
                TMoc entity = new TMoc();
                entity.setTimeliness("2");
                entity.setTempCategory("3");
                entity.setDeptId(userService.selectUserById(getUserId()).getDeptId());
                for (int j = 0; j < cellNum; j++) {
                    Cell cell = row.getCell(j);
                    //  cell.setCellType(CellType.STRING);
                    String cellValue = ExcelUtils.getCellValue(cell);
                    logger.info("cellValue:" + cellValue);
                    if (j == 0) {
                        entity.setMocNo(cellValue);
                    } else if (j == 1) {
                        entity.setCompanyMocNo(cellValue);
                    } else if (j == 2) {
                        entity.setTitle(cellValue);
                    } else if (j == 3) {
                        if (cellValue.length() > 3) {
                            entity.setFinishDate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 4) {
                        if (cellValue.length() > 3) {
                            entity.setExpTime(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 5) {
                        for (SysDictData sysDictData : tempStateDict) {
                            if (sysDictData.getDictLabel().equals(cellValue.trim())) {
                                entity.setTempState(sysDictData.getDictValue());
                            }
                        }
                    } else if (j == 6) {
                        if (cellValue.length() > 3) {
                            entity.setRemoveTime(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 7) {
                        entity.setRemarks(cellValue);
                    }
                }
                entity.setCreaterCode(userId.toString());
                logger.info("entity:" + entity);
                list.add(entity);
            }catch (Exception e){
                failNumber++;
                failRow.add(i+1);
            }
        }
        int successNumber = 0;
        int failNum = 0;
        for (TMoc t : list
        ) {
            failNum++;
            try {
                tMocService.insertTMoc(t);
                successNumber++;
            }catch (Exception e){
                failNumber++;
                logger.info("e:" + e);
                failRow.add(failNum+1);
            }
        }
        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);
    }

    /**
     * 导出MOC
     */
    @PreAuthorize("@ss.hasPermi('process:moc:export')")
    @GetMapping("/export")
    public AjaxResult export(TMoc tMoc)
    {
        // 当前用户归属部门
        Long deptId = userService.selectUserById(getUserId()).getDeptId();
        // 查询条件
        // 1. 数据归属部门=当前用户归属部门
        tMoc.setDeptId(deptId);
        // 查询总表数据
        List<TMoc> mocList = tMocService.selectTMocList(tMoc);
        // MOC数据
        List<Moc> mocSummaryList = new ArrayList<Moc>();
        //字典查询
        List<SysDictData> mocAreaDict = iSysDictTypeService.selectDictDataByType("MOC_AREA");
        List<SysDictData> mocTypeDict = iSysDictTypeService.selectDictDataByType("MOC_TYPE");
        List<SysDictData> mcDetailDict = iSysDictTypeService.selectDictDataByType("MC_DETAIL");
        List<SysDictData> mocRiskLevelDict = iSysDictTypeService.selectDictDataByType("MOC_RISKLEVEL");
        // 遍历总表数据，插入MOC表
        for (TMoc moc : mocList) {
            Moc mocSummary = new Moc();
            mocSummary.setMocNo(moc.getMocNo());
            mocSummary.setCompanyMocNo(moc.getCompanyMocNo());
            mocSummary.setTimeliness("1".equals(moc.getTimeliness()) ? "永久" : "临时");
            mocSummary.setProjectNo(moc.getProjectNo());
            for (SysDictData sysDictData : mocAreaDict) {
                if (sysDictData.getDictValue().equals(moc.getArea())) {
                    mocSummary.setArea(sysDictData.getDictLabel());
                }
            }
            mocSummary.setTitle(moc.getTitle());
            for (SysDictData sysDictData : mocTypeDict) {
                if (sysDictData.getDictValue().equals(moc.getMocType())) {
                    mocSummary.setMocType(sysDictData.getDictLabel());
                }
            }
            mocSummary.setOwner(moc.getOwner());
            mocSummary.setApproveTime(moc.getApproveTime());
            mocSummary.setMcTime(moc.getMcTime());
            for (SysDictData sysDictData : mcDetailDict) {
                if (sysDictData.getDictValue().equals(moc.getMcDetail())) {
                    mocSummary.setMcDetail(sysDictData.getDictLabel());
                }
            }
            mocSummary.setRemarks(moc.getRemarks());
            for (SysDictData sysDictData : mocRiskLevelDict) {
                if (sysDictData.getDictValue().equals(moc.getRiskLevel())) {
                    mocSummary.setRiskLevel(sysDictData.getDictLabel());
                }
            }
            mocSummary.setEhsCheck(moc.getEhsCheck());
            mocSummary.setTraining(moc.getTraining());
            mocSummary.setPssr(moc.getPssr());
            mocSummary.setPssrNo(moc.getPssrNo());
            mocSummaryList.add(mocSummary);
        }
        ExcelUtil<Moc> util = new ExcelUtil<Moc>(Moc.class);
        return util.exportExcel(mocSummaryList, "MOC汇总");
    }

    /**
     * 导出临时MOC
     */
    @PreAuthorize("@ss.hasPermi('process:moc:export')")
    @GetMapping("/exporttemporary")
    public AjaxResult exportTemporaryMoc(TMoc tMoc)
    {
        // 当前用户归属部门
        Long deptId = userService.selectUserById(getUserId()).getDeptId();
        // 查询条件
        // 1. 数据归属部门=当前用户归属部门
        tMoc.setDeptId(deptId);
        // 2. 时效性=临时
        tMoc.setTimeliness("2");
        // 查询总表数据
        List<TMoc> mocList = tMocService.selectTMocList(tMoc);
        // 临时MOC数据
        List<TemporaryMoc> temporaryMocList = new ArrayList<TemporaryMoc>();
        //字典查询
        List<SysDictData> mocAreaDict = iSysDictTypeService.selectDictDataByType("MOC_AREA");
        List<SysDictData> mocTypeDict = iSysDictTypeService.selectDictDataByType("MOC_TYPE");
        List<SysDictData> mcDetailDict = iSysDictTypeService.selectDictDataByType("MC_DETAIL");
        List<SysDictData> mocRiskLevelDict = iSysDictTypeService.selectDictDataByType("MOC_RISKLEVEL");
        List<SysDictData> tempStateDict = iSysDictTypeService.selectDictDataByType("TEMP_STATE");
        List<SysDictData> mocStatusDict = iSysDictTypeService.selectDictDataByType("MOC_STATUS");
        // 遍历总表数据，插入临时MOC表
        for (TMoc moc : mocList) {
            TemporaryMoc temporaryMoc = new TemporaryMoc();
            temporaryMoc.setMocNo(moc.getMocNo());
            temporaryMoc.setCompanyMocNo(moc.getCompanyMocNo());
            temporaryMoc.setProjectNo(moc.getProjectNo());
            for (SysDictData sysDictData : mocAreaDict) {
                if (sysDictData.getDictValue().equals(moc.getArea())) {
                    temporaryMoc.setArea(sysDictData.getDictLabel());
                }
            }
            temporaryMoc.setTitle(moc.getTitle());
            for (SysDictData sysDictData : mocTypeDict) {
                if (sysDictData.getDictValue().equals(moc.getMocType())) {
                    temporaryMoc.setMocType(sysDictData.getDictLabel());
                }
            }
            temporaryMoc.setOwner(moc.getOwner());
            temporaryMoc.setApproveTime(moc.getApproveTime());
            temporaryMoc.setMcTime(moc.getMcTime());
            for (SysDictData sysDictData : mcDetailDict) {
                if (sysDictData.getDictValue().equals(moc.getMcDetail())) {
                    temporaryMoc.setMcDetail(sysDictData.getDictLabel());
                }
            }
            temporaryMoc.setExpTime(moc.getExpTime());
            for (SysDictData sysDictData : tempStateDict) {
                if (sysDictData.getDictValue().equals(moc.getTempState())) {
                    temporaryMoc.setTempState(sysDictData.getDictLabel());
                }
            }
            for (SysDictData sysDictData : mocStatusDict) {
                if (sysDictData.getDictValue().equals(moc.getStatus())) {
                    temporaryMoc.setStatus(sysDictData.getDictLabel());
                }
            }
            temporaryMoc.setRemoveTime(moc.getRemoveTime());
            temporaryMoc.setRemarks(moc.getRemarks());
            for (SysDictData sysDictData : mocRiskLevelDict) {
                if (sysDictData.getDictValue().equals(moc.getRiskLevel())) {
                    temporaryMoc.setRiskLevel(sysDictData.getDictLabel());
                }
            }
            temporaryMoc.setEhsCheck(moc.getEhsCheck());
            temporaryMoc.setTraining(moc.getTraining());
            temporaryMoc.setPssr(moc.getPssr());
            temporaryMoc.setPssrNo(moc.getPssrNo());
            temporaryMocList.add(temporaryMoc);
        }
        ExcelUtil<TemporaryMoc> util = new ExcelUtil<TemporaryMoc>(TemporaryMoc.class);
        return util.exportExcel(temporaryMocList, "临时MOC");
    }

    /**
     * 批量导入临时MOC
     */
    @PreAuthorize("@ss.hasPermi('process:moc:add')")
    @PostMapping("/temporary/importData")
    public AjaxResult importTemporaryData(@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<TMoc> list = new ArrayList<TMoc>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //字典查询
        List<SysDictData> mocAreaDict = iSysDictTypeService.selectDictDataByType("MOC_AREA");
        List<SysDictData> mocTypeDict = iSysDictTypeService.selectDictDataByType("MOC_TYPE");
        List<SysDictData> mcDetailDict = iSysDictTypeService.selectDictDataByType("MC_DETAIL");
        List<SysDictData> mocRiskLevelDict = iSysDictTypeService.selectDictDataByType("MOC_RISKLEVEL");
        List<SysDictData> tempStateDict = iSysDictTypeService.selectDictDataByType("TEMP_STATE");
        List<SysDictData> mocStatusDict = iSysDictTypeService.selectDictDataByType("MOC_STATUS");
        //部门查询
        List<SysDept> dept = iSysDeptService.selectDeptList(new SysDept());
        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();
                TMoc entity = new TMoc();
                entity.setTimeliness("2");
                entity.setDeptId(userService.selectUserById(getUserId()).getDeptId());
                for (int j = 0; j < cellNum; j++) {
                    Cell cell = row.getCell(j);
                    //  cell.setCellType(CellType.STRING);
                    String cellValue = ExcelUtils.getCellValue(cell);
                    logger.info("cellValue:" + cellValue);
                    if (j == 0) {
                        entity.setMocNo(cellValue);
                    } else if (j == 1) {
                        entity.setCompanyMocNo(cellValue);
                    } else if (j == 2) {
                        entity.setProjectNo(cellValue);
                    } else if (j == 3) {
                        for (SysDictData sysDictData : mocAreaDict) {
                            if (sysDictData.getDictLabel().equals(cellValue.trim())) {
                                entity.setArea(sysDictData.getDictValue());
                            }
                        }
                    } else if (j == 4) {
                        entity.setTitle(cellValue);
                    } else if (j == 5) {
                        for (SysDictData sysDictData : mocTypeDict) {
                            if (sysDictData.getDictLabel().equals(cellValue.trim())) {
                                entity.setMocType(sysDictData.getDictValue());
                            }
                        }
                    } else if (j == 6) {
                        entity.setOwner(cellValue);
                    } else if (j == 7) {
                        if (cellValue.length() > 3) {
                            entity.setApproveTime(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 8) {
                        if (cellValue.length() > 3) {
                            entity.setMcTime(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 9) {
                        for (SysDictData sysDictData : mcDetailDict) {
                            if (sysDictData.getDictLabel().equals(cellValue.trim())) {
                                entity.setMcDetail(sysDictData.getDictValue());
                            }
                        }
                    } else if (j == 10) {
                        if (cellValue.length() > 3) {
                            entity.setExpTime(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 11) {
                        for (SysDictData sysDictData : tempStateDict) {
                            if (sysDictData.getDictLabel().equals(cellValue.trim())) {
                                entity.setTempState(sysDictData.getDictValue());
                            }
                        }
                    } else if (j == 12) {
                        for (SysDictData sysDictData : mocStatusDict) {
                            if (sysDictData.getDictLabel().equals(cellValue.trim())) {
                                entity.setStatus(sysDictData.getDictValue());
                            }
                        }
                    } else if (j == 13) {
                        if (cellValue.length() > 3) {
                            entity.setRemoveTime(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 14) {
                        entity.setRemarks(cellValue);
                    } else if (j == 15) {
                        for (SysDictData sysDictData : mocRiskLevelDict) {
                            if (sysDictData.getDictLabel().equals(cellValue.trim())) {
                                entity.setRiskLevel(sysDictData.getDictValue());
                            }
                        }
                    } else if (j == 16) {
                        if (cellValue.length() > 3) {
                            entity.setEhsCheck(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 17) {
                        if (cellValue.length() > 3) {
                            entity.setTraining(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 18) {
                        if (cellValue.length() > 3) {
                            entity.setPssr(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 19) {
                        entity.setPssrNo(cellValue);
                    }
                }
                entity.setCreaterCode(userId.toString());
                logger.info("entity:" + entity);
                list.add(entity);
            }catch (Exception e){
                failNumber++;
                failRow.add(i+1);
            }
        }
        int successNumber = 0;
        int failNum = 0;
        for (TMoc t : list
        ) {
            failNum++;
            try {
                tMocService.insertTMoc(t);
                successNumber++;
            }catch (Exception e){
                failNumber++;
                logger.info("e:" + e);
                failRow.add(failNum+1);
            }
        }
        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);
    }

    /**
     * 导出永久MOC
     */
    @PreAuthorize("@ss.hasPermi('process:moc:export')")
    @GetMapping("/exportpermanent")
    public AjaxResult exportPermanentMoc(TMoc tMoc)
    {
        // 当前用户归属部门
        Long deptId = userService.selectUserById(getUserId()).getDeptId();
        // 查询条件
        // 1. 数据归属部门=当前用户归属部门
        tMoc.setDeptId(deptId);
        // 2. 时效性=永久
        tMoc.setTimeliness("1");
        // 查询总表数据
        List<TMoc> mocList = tMocService.selectTMocList(tMoc);
        // 永久MOC数据
        List<PermanentMoc> permanentMocList = new ArrayList<PermanentMoc>();
        //字典查询
        List<SysDictData> mocAreaDict = iSysDictTypeService.selectDictDataByType("MOC_AREA");
        List<SysDictData> mocTypeDict = iSysDictTypeService.selectDictDataByType("MOC_TYPE");
        List<SysDictData> mcDetailDict = iSysDictTypeService.selectDictDataByType("MC_DETAIL");
        List<SysDictData> mocRiskLevelDict = iSysDictTypeService.selectDictDataByType("MOC_RISKLEVEL");
        List<SysDictData> yesNoEnDict = iSysDictTypeService.selectDictDataByType("MOC_UPDATE");
        // 遍历总表数据，插入永久MOC表
        for (TMoc moc : mocList) {
            PermanentMoc permanentMoc = new PermanentMoc();
            permanentMoc.setMocNo(moc.getMocNo());
            permanentMoc.setCompanyMocNo(moc.getCompanyMocNo());
            permanentMoc.setProjectNo(moc.getProjectNo());
            for (SysDictData sysDictData : mocAreaDict) {
                if (sysDictData.getDictValue().equals(moc.getArea())) {
                    permanentMoc.setArea(sysDictData.getDictLabel());
                }
            }
            permanentMoc.setTitle(moc.getTitle());
            for (SysDictData sysDictData : mocTypeDict) {
                if (sysDictData.getDictValue().equals(moc.getMocType())) {
                    permanentMoc.setMocType(sysDictData.getDictLabel());
                }
            }
            permanentMoc.setOwner(moc.getOwner());
            permanentMoc.setApproveTime(moc.getApproveTime());
            permanentMoc.setMcTime(moc.getMcTime());
            for (SysDictData sysDictData : mcDetailDict) {
                if (sysDictData.getDictValue().equals(moc.getMcDetail())) {
                    permanentMoc.setMcDetail(sysDictData.getDictLabel());
                }
            }
            permanentMoc.setRemarks(moc.getRemarks());
            for (SysDictData sysDictData : mocRiskLevelDict) {
                if (sysDictData.getDictValue().equals(moc.getRiskLevel())) {
                    permanentMoc.setRiskLevel(sysDictData.getDictLabel());
                }
            }
            permanentMoc.setEhsCheck(moc.getEhsCheck());
            permanentMoc.setTraining(moc.getTraining());
            for (SysDictData sysDictData : yesNoEnDict) {
                if (sysDictData.getDictValue().equals(moc.getSopUpdate())) {
                    permanentMoc.setSopUpdate(sysDictData.getDictLabel());
                }
            }
            for (SysDictData sysDictData : yesNoEnDict) {
                if (sysDictData.getDictValue().equals(moc.getPidMaster())) {
                    permanentMoc.setPidMaster(sysDictData.getDictLabel());
                }
            }
            for (SysDictData sysDictData : yesNoEnDict) {
                if (sysDictData.getDictValue().equals(moc.getDocUpdate())) {
                    permanentMoc.setDocUpdate(sysDictData.getDictLabel());
                }
            }
            permanentMoc.setPssr(moc.getPssr());
            permanentMoc.setPssrNo(moc.getPssrNo());
            permanentMocList.add(permanentMoc);
        }
        ExcelUtil<PermanentMoc> util = new ExcelUtil<PermanentMoc>(PermanentMoc.class);
        return util.exportExcel(permanentMocList, "永久MOC");
    }

    /**
     * 批量导入永久MOC
     */
    @PreAuthorize("@ss.hasPermi('process:moc:add')")
    @PostMapping("/permanent/importData")
    public AjaxResult importPermanentData(@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<TMoc> list = new ArrayList<TMoc>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //字典查询
        List<SysDictData> mocAreaDict = iSysDictTypeService.selectDictDataByType("MOC_AREA");
        List<SysDictData> mocTypeDict = iSysDictTypeService.selectDictDataByType("MOC_TYPE");
        List<SysDictData> mcDetailDict = iSysDictTypeService.selectDictDataByType("MC_DETAIL");
        List<SysDictData> mocRiskLevelDict = iSysDictTypeService.selectDictDataByType("MOC_RISKLEVEL");
        List<SysDictData> yesNoEnDict = iSysDictTypeService.selectDictDataByType("MOC_UPDATE");
        //部门查询
        List<SysDept> dept = iSysDeptService.selectDeptList(new SysDept());
        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();
                TMoc entity = new TMoc();
                entity.setTimeliness("1");
                entity.setDeptId(userService.selectUserById(getUserId()).getDeptId());
                for (int j = 0; j < cellNum; j++) {
                    Cell cell = row.getCell(j);
                    //  cell.setCellType(CellType.STRING);
                    String cellValue = ExcelUtils.getCellValue(cell);
                    logger.info("cellValue:" + cellValue);
                    if (j == 0) {
                        entity.setMocNo(cellValue);
                    } else if (j == 1) {
                        entity.setCompanyMocNo(cellValue);
                    } else if (j == 2) {
                        entity.setProjectNo(cellValue);
                    } else if (j == 3) {
                        for (SysDictData sysDictData : mocAreaDict) {
                            if (sysDictData.getDictLabel().equals(cellValue.trim())) {
                                entity.setArea(sysDictData.getDictValue());
                            }
                        }
                    } else if (j == 4) {
                        entity.setTitle(cellValue);
                    } else if (j == 5) {
                        for (SysDictData sysDictData : mocTypeDict) {
                            if (sysDictData.getDictLabel().equals(cellValue.trim())) {
                                entity.setMocType(sysDictData.getDictValue());
                            }
                        }
                    } else if (j == 6) {
                        entity.setOwner(cellValue);
                    } else if (j == 7) {
                        if (cellValue.length() > 3) {
                            entity.setApproveTime(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 8) {
                        if (cellValue.length() > 3) {
                            entity.setMcTime(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 9) {
                        for (SysDictData sysDictData : mcDetailDict) {
                            if (sysDictData.getDictLabel().equals(cellValue.trim())) {
                                entity.setMcDetail(sysDictData.getDictValue());
                            }
                        }
                    } else if (j == 10) {
                        entity.setRemarks(cellValue);
                    } else if (j == 11) {
                        for (SysDictData sysDictData : mocRiskLevelDict) {
                            if (sysDictData.getDictLabel().equals(cellValue.trim())) {
                                entity.setRiskLevel(sysDictData.getDictValue());
                            }
                        }
                    } else if (j == 12) {
                        if (cellValue.length() > 3) {
                            entity.setEhsCheck(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 13) {
                        if (cellValue.length() > 3) {
                            entity.setTraining(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 14) {
                        for (SysDictData sysDictData : yesNoEnDict) {
                            if (sysDictData.getDictLabel().equals(cellValue.trim())) {
                                entity.setSopUpdate(sysDictData.getDictValue());
                            }
                        }
                    } else if (j == 15) {
                        for (SysDictData sysDictData : yesNoEnDict) {
                            if (sysDictData.getDictLabel().equals(cellValue.trim())) {
                                entity.setPidMaster(sysDictData.getDictValue());
                            }
                        }
                    } else if (j == 16) {
                        for (SysDictData sysDictData : yesNoEnDict) {
                            if (sysDictData.getDictLabel().equals(cellValue.trim())) {
                                entity.setDocUpdate(sysDictData.getDictValue());
                            }
                        }
                    } else if (j == 17) {
                        if (cellValue.length() > 3) {
                            entity.setPssr(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                        }
                    } else if (j == 18) {
                        entity.setPssrNo(cellValue);
                    }
                }
                entity.setCreaterCode(userId.toString());
                logger.info("entity:" + entity);
                list.add(entity);
            }catch (Exception e){
                failNumber++;
                failRow.add(i+1);
            }
        }
        int successNumber = 0;
        int failNum = 0;
        for (TMoc t : list
        ) {
            failNum++;
            try {
                tMocService.insertTMoc(t);
                successNumber++;
            }catch (Exception e){
                failNumber++;
                logger.info("e:" + e);
                failRow.add(failNum+1);
            }
        }
        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("/sealDateData")
    public List<DataEntity> sealDateData(SealDateVO sealDateVO)
    {
        return tMocMapper.selectSealDateData(sealDateVO);
    }

    // 部门统计
    @GetMapping("/departmentData")
    public List<DataEntity> departmentData(DepartmentVO departmentVO)
    {
        return tMocMapper.selectDepartmentData(departmentVO);
    }
    // 泄露位置统计
    @GetMapping("/leakLocationData")
    public List<DataEntity> leakLocationData(LeakLocationVO leakLocationVO)
    {
        return tMocMapper.selectLeakLocationData(leakLocationVO);
    }

    // 介质类型统计
    @GetMapping("/materialTypeData")
    public List<DataEntity> materialTypeData(MaterialTypeVO materialTypeVO)
    {
        return tMocMapper.selectMaterialTypeData(materialTypeVO);
    }

    // 卡具状态统计
    @GetMapping("/statusData")
    public List<DataEntity> statusData(StatusVO statusVO)
    {
        List<DataEntity> statuslList = tMocMapper.selectStatusData(statusVO);
        List<SysDictData> dictList = iSysDictTypeService.selectDictDataByType("MOC_STATUS");
        for (DataEntity d: statuslList
        ) {
            for (SysDictData s: dictList
            ) {
                if (StringUtils.isBlank(d.getDataName())){
                    d.setDataName("未知");
                    break;
                }
                if (s.getDictValue().equals(d.getDataName())){
                    d.setDataName(s.getDictLabel());
                    break;
                }
            }
        }
        return statuslList;
    }

    // 临时MOC统计
    @GetMapping("/tempStateData")
    public List<DataEntity> tempStateData(TempStateVO tempStateVO)
    {
        List<DataEntity> riskLevelList = tMocMapper.selectTempStateData(tempStateVO);
        List<SysDictData> dictList = iSysDictTypeService.selectDictDataByType("TEMP_STATE");
        for (DataEntity d: riskLevelList
        ) {
            for (SysDictData s: dictList
            ) {
                if (StringUtils.isBlank(d.getDataName())){
                    d.setDataName("未知");
                    break;
                }
                if (s.getDictValue().equals(d.getDataName())){
                    d.setDataName(s.getDictLabel());
                    break;
                }
            }
        }
        return riskLevelList;
    }

    // 风险等级统计
    @GetMapping("/riskLevelData")
    public List<DataEntity> riskLevelData(RiskLevelVO riskLevelVO)
    {
        List<DataEntity> riskLevelList = tMocMapper.selectRiskLevelData(riskLevelVO);
        List<SysDictData> dictList = iSysDictTypeService.selectDictDataByType("MOC_RISKLEVEL");
        for (DataEntity d: riskLevelList
        ) {
            for (SysDictData s: dictList
            ) {
                if (StringUtils.isBlank(d.getDataName())){
                    d.setDataName("未知");
                    break;
                }
                if (s.getDictValue().equals(d.getDataName())){
                    d.setDataName(s.getDictLabel());
                    break;
                }
            }
        }
        return riskLevelList;
    }

    // MC情况统计
    @GetMapping("/mcDetailData")
    public List<DataEntity> mcDetailData(McDetailVO mcDetailVO)
    {
        List<DataEntity> mocDetailList = tMocMapper.selectMcDetailData(mcDetailVO);
        List<SysDictData> dictList = iSysDictTypeService.selectDictDataByType("MC_DETAIL");
        for (DataEntity d: mocDetailList
        ) {
            for (SysDictData s: dictList
            ) {
                if (StringUtils.isBlank(d.getDataName())){
                    d.setDataName("未知");
                    break;
                }
                if (s.getDictValue().equals(d.getDataName())){
                    d.setDataName(s.getDictLabel());
                    break;
                }
            }
        }
        return mocDetailList;
    }

    // 区域统计
    @GetMapping("/areaData")
    public List<DataEntity> areaData(AreaVO areaVO)
    {
        List<DataEntity> areaList = tMocMapper.selectAreaData(areaVO);
        List<SysDictData> dictList = iSysDictTypeService.selectDictDataByType("MOC_AREA");
        for (DataEntity d: areaList
        ) {
            for (SysDictData s: dictList
            ) {
                if (StringUtils.isBlank(d.getDataName())){
                    d.setDataName("未知");
                    break;
                }
                if (s.getDictValue().equals(d.getDataName())){
                    d.setDataName(s.getDictLabel());
                    break;
                }
            }
        }
        return areaList;
    }

    // MOC编号统计
    @GetMapping("/mocNoData")
    public List<DataEntity> mocNoData(MocNoVO mocNoVO)
    {
        return tMocMapper.selectMocData(mocNoVO);
    }

    /**
     * 遍历MOC filelist
     * 设置isEhsCheck、isTrainingset、ispssr
     *
     * @param list MOC list
     */
    private void getFiles(List<TMoc> list) {
        for (TMoc m : list) {
            TCommonfile commonfile = new TCommonfile();
            commonfile.setpId(m.getId());
            if (m.getEhsCheck() != null) {
                commonfile.setpType("moc-ehsCheck");
                List<TCommonfile> commonfilesEhsCheck = commonfileService.selectAllFileList(commonfile);
                if (commonfilesEhsCheck.size() == 0) {
                    m.setIsEhsCheck("1");
                }
            }
            if (m.getTraining() != null) {
                commonfile.setpType("moc-training");
                List<TCommonfile> commonfilesTraining = commonfileService.selectAllFileList(commonfile);
                if (commonfilesTraining.size() == 0) {
                    m.setIsTraining("1");
                }
            }
            if (m.getPssr() != null) {
                commonfile.setpType("moc-pssr");
                List<TCommonfile> commonfilesPssr = commonfileService.selectAllFileList(commonfile);
                if (commonfilesPssr.size() == 0) {
                    m.setIsPssr("1");
                }
            }
        }
    }

    /**
     * 查询临时MOC管理列表 - 仪表联锁旁路
     */
    @PreAuthorize("@ss.hasPermi('process:moc:list')")
    @GetMapping("/listinterlock")
    public TableDataInfo listInterlock(TMoc tMoc)
    {
        tMoc.setTimeliness("2");
        tMoc.setTempCategory("3");
        startPage();
        List<TMoc> list = tMocService.selectTMocList(tMoc);
        this.getFiles(list);
        return getDataTable(list);
    }

    /**
     * 查询临时MOC管理列表 - 临时设施、其它
     */
    @PreAuthorize("@ss.hasPermi('process:moc:list')")
    @GetMapping("/listfacility")
    public TableDataInfo listFacility(TMoc tMoc)
    {
        tMoc.setTimeliness("2");
        tMoc.setTempCategory("2");
        startPage();
        List<TMoc> list = tMocService.selectTMocList(tMoc);
        this.getFiles(list);
        return getDataTable(list);
    }

    /**
     * 查询临时MOC管理列表 - 带压消漏清单
     */
    @PreAuthorize("@ss.hasPermi('process:moc:list')")
    @GetMapping("/listaquifier")
    public TableDataInfo listAquifier(TMoc tMoc)
    {
        tMoc.setTimeliness("2");
        tMoc.setTempCategory("1");
        startPage();
        List<TMoc> list = tMocService.selectTMocList(tMoc);
        this.getFiles(list);
        return getDataTable(list);
    }

    /**
     * 查询临时MOC管理列表
     */
    @PreAuthorize("@ss.hasPermi('process:moc:list')")
    @GetMapping("/listtemporary")
    public TableDataInfo listTemporary(TMoc tMoc)
    {
        tMoc.setTimeliness("2");
        startPage();
        List<TMoc> list = tMocService.selectTMocList(tMoc);
        this.getFiles(list);
        return getDataTable(list);
    }

    /**
     * 查询永久MOC管理列表
     */
    @PreAuthorize("@ss.hasPermi('process:moc:list')")
    @GetMapping("/listpermanent")
    public TableDataInfo listPermanent(TMoc tMoc)
    {
        tMoc.setTimeliness("1");
        startPage();
        List<TMoc> list = tMocService.selectTMocList(tMoc);
        this.getFiles(list);
        return getDataTable(list);
    }

    /**
     * 查询MOC管理列表
     */
    @PreAuthorize("@ss.hasPermi('process:moc:list')")
    @GetMapping("/list")
    public TableDataInfo list(TMoc tMoc)
    {
        startPage();
        List<TMoc> list = tMocService.selectTMocList(tMoc);
        this.getFiles(list);
        return getDataTable(list);
    }

    //申请年份统计
    @GetMapping("/yearData")
    public List<DataEntity> engData(Map param)
    {
        param.put("params" , new HashMap<>());
        List<DataEntity> list = tMocMapper.selectYearData(param);
        return list;
    }

    //情况统计
    @GetMapping("/trueStateData")
    public List<DataEntity> trueStateData(Map param)
    {
        param.put("params" , new HashMap<>());
        List<DataEntity> list = tMocMapper.selectTrueStateData(param);
        List<SysDictData> dictList = iSysDictTypeService.selectDictDataByType("MOC_STATE");
        for (DataEntity d: list
        ) {
            for (SysDictData s: dictList
            ) {
                if (StringUtils.isBlank(d.getDataName())){
                    d.setDataName("未知");
                    break;
                }
                if (s.getDictValue().equals(d.getDataName())){
                    d.setDataName(s.getDictLabel());
                    break;
                }
            }
        }
        return list;
    }

    //变更统计
    @GetMapping("/changeData")
    public List<DataEntity> changeData(Map param)
    {
        param.put("params" , new HashMap<>());
        List<DataEntity> list = tMocMapper.selectChangeData(param);
        List<SysDictData> dictList = iSysDictTypeService.selectDictDataByType("MOC_CHANGE");
        for (DataEntity d: list
        ) {
            for (SysDictData s: dictList
            ) {
                if (StringUtils.isBlank(d.getDataName())){
                    d.setDataName("未知");
                    break;
                }
                if (s.getDictValue().equals(d.getDataName())){
                    d.setDataName(s.getDictLabel());
                    break;
                }
            }
        }
        return list;
    }

    //变更统计
    @GetMapping("/categoryData")
    public List<DataEntity> categoryData(Map param)
    {
        param.put("params" , new HashMap<>());
        List<DataEntity> list = tMocMapper.selectCategoryData(param);
        List<SysDictData> dictList = iSysDictTypeService.selectDictDataByType("MOC_CATEGORY");
        for (DataEntity d: list
        ) {
            for (SysDictData s: dictList
            ) {
                if (StringUtils.isBlank(d.getDataName())){
                    d.setDataName("未知");
                    break;
                }
                if (s.getDictValue().equals(d.getDataName())){
                    d.setDataName(s.getDictLabel());
                    break;
                }
            }
        }
        return list;
    }

    //风险统计
    @GetMapping("/riskData")
    public List<DataEntity> riskData(Map param)
    {
        param.put("params" , new HashMap<>());
        List<DataEntity> list = tMocMapper.selectRiskData(param);
        List<SysDictData> dictList = iSysDictTypeService.selectDictDataByType("MOC_RISKLEVEL");
        for (DataEntity d: list
        ) {
            for (SysDictData s: dictList
            ) {
                if (StringUtils.isBlank(d.getDataName())){
                    d.setDataName("未知");
                    break;
                }
                if (s.getDictValue().equals(d.getDataName())){
                    d.setDataName(s.getDictLabel());
                    break;
                }
            }
        }
        return list;
    }


    /**
     * 获取MOC管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('process:moc:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tMocService.selectTMocById(id));
    }

    /**
     * 新增MOC管理
     */
    @PreAuthorize("@ss.hasPermi('process:moc:add')")
    @Log(title = "MOC管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMoc tMoc)
    {
        tMoc.setCreaterCode(getUserId().toString());
        TTraining tTraining = new TTraining();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        tTraining.setPlantCode(tMoc.getPlantCode());
        tTraining.setCourseid(tMoc.getMocNo());
        tTraining.setYear(sdf.format(new Date()));;
        tTraining.setCourse("MOC培训");
        tTraining.setTrainingType("12");
        tTraining.setContent(tMoc.getTitle());
        tTraining.setIsfinish(Long.parseLong("0"));
        tTraining.setDeptId(tMoc.getDeptId());
        tTrainingService.insertTTraining(tTraining);
        return toAjax(tMocService.insertTMoc(tMoc));
    }

    /**
     * 修改MOC管理
     */
    @PreAuthorize("@ss.hasPermi('process:moc:edit')")
    @Log(title = "MOC管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMoc tMoc)
    {
        tMoc.setUpdaterCode(getUserId().toString());
        tMoc.setUpdatedate(new Date());
//        if (tMoc.getChangeNature().equals("12")) {
//            tMoc.setTemporaryState("14");
//        }
        return toAjax(tMocService.updateTMoc(tMoc));
    }

    /**
     * 延期操作
     */
    @PreAuthorize("@ss.hasPermi('process:moc:edit')")
    @Log(title = "MOC管理", businessType = BusinessType.UPDATE)
    @PutMapping("/delay")
    public AjaxResult delay(@RequestBody DelayVO delayVo) {

        return toAjax(tMocService.delay(delayVo));
    }

    /**
     * 删除MOC管理
     */
    @PreAuthorize("@ss.hasPermi('process:moc:remove')")
    @Log(title = "MOC管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tMocService.deleteTMocByIds(ids));
    }
}
