package com.ruoyi.project.pssr.controller;

import com.alibaba.fastjson.JSON;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.ruoyi.common.jpush.JiGuangPushService;
import com.ruoyi.common.sendEmail.IMailService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.pssr.domain.*;
import com.ruoyi.project.pssr.service.*;
import com.ruoyi.project.reliability.domain.TSafetyvavle;
import com.ruoyi.project.reliability.service.ITSafetyvavleService;
import com.ruoyi.project.system.domain.SysMessage;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysMessageService;
import com.ruoyi.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * PSSRController
 *
 * @author ssy
 * @date 2024-09-18
 */
@RestController
@RequestMapping("/pssr/aboveall")
public class TPssrAboveallController extends BaseController {
    //region 注入服务层对象
    @Autowired
    private ITPssrAboveallService tPssrAboveallService;

    @Autowired
    private ITPssrSubcontentService tPssrSubcontentService;

    @Autowired
    private ITPssrPatrolService tPssrPatrolService;

    @Autowired
    private ITPssrMaterialService tPssrMaterialService;

    @Autowired
    private ITPssrMaterialRawService tPssrMaterialRawService;

    @Autowired
    private ITPssrMocService tPssrMocService;

    @Autowired
    private ITSafetyvavleService tSafetyvavleService;

    @Autowired
    private ITPssrSafetyValveService tPssrSafetyValveService;

    @Autowired
    private ITPssrPublicService tPssrPublicService;

    @Autowired
    private ITPssrTorchvocService tPssrTorchvocService;

    @Autowired
    private ITPssrLightingService tPssrLightingService;

    @Autowired
    private ITPssrPowerService tPssrPowerService;

    @Autowired
    private ITPssrHygieneService tPssrHygieneService;

    @Autowired
    private ITPssrFrameModelService tPssrFrameModelService;

    @Autowired
    private ITPssrFrameService tPssrFrameService;

    @Autowired
    private ITPssrProtectionService tPssrProtectionService;
    @Autowired
    private TPssrApproveController tPssrApproveController;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ITPssrFranklinismService tPssrFranklinismService;

    @Autowired
    private ITPssrOverhaulExchangerService tPssrOverhaulExchangerService;

    @Autowired
    private ITPssrOverhaulFilterService tPssrOverhaulFilterService;

    @Autowired
    private ITPssrOverhaulTowerService tPssrOverhaulTowerService;

    @Autowired
    private ITPssrOverhaulValveService tPssrOverhaulValveService;

    @Autowired
    private ITPssrBlindService tPssrBlindService;

    @Autowired
    private ITPssrLockService tPssrLockService;

    @Autowired
    private ITPssrPipeService tPssrPipeService;

    @Autowired
    private ITPssrCleaningService tPssrCleaningService;

    @Autowired
    private ITPssrAirtightService tPssrAirtightService;

    @Autowired
    private ITPssrLaboratoryService tPssrLaboratoryService;

    @Autowired
    private ITPssrCircuitService tPssrCircuitService;

    @Autowired
    private ITPssrSafetyBleedService tPssrSafetyBleedService;

    @Autowired
    private ITPssrSafetyBreathService tPssrSafetyBreathService;

    @Autowired
    private ITPssrSafetyBrustService tPssrSafetyBrustService;

    @Autowired
    private ITPssrSafetyFlamearresterService tPssrSafetyFlamearresterService;

    @Autowired
    private ITPssrMotorService tPssrMotorService;

    @Autowired
    private ITPssrInstrumentCalibrationService tPssrInstrumentCalibrationService;

    @Autowired
    private ITPssrNitrogenService tPssrNitrogenService;

    @Autowired
    private ITPssrGasdetectorService tPssrGasdetectorService;

    @Autowired
    private ITPssrAnalyzerService tPssrAnalyzerService;

    @Autowired
    private ITPssrMeasureService tPssrMeasureService;

    @Autowired
    private ITPssrPumpFillService tPssrPumpFillService;

    @Autowired
    private ITPssrPumpCleaningService tPssrPumpCleaningService;

    @Autowired
    private ITPssrPumpOverhaulService tPssrPumpOverhaulService;

    @Autowired
    private ITPssrInstrumentTestService tPssrInstrumentTestService;

    @Autowired
    private ITPssrAlarmService tPssrAlarmService;

    @Autowired
    private ITPssrVesselService tPssrVesselService;

    @Autowired
    private ITPssrProgrammeService tPssrProgrammeService;

    @Autowired
    private JiGuangPushService jiGuangPushService;

    @Autowired
    private IMailService mailService;

    @Autowired
    private ISysMessageService sysMessageService;
    //endregion

    //region 系统方法

    /**
     * 查询PSSR列表
     */
    @PreAuthorize("@ss.hasPermi('pssr:aboveall:list')")
    @GetMapping("/list")
    public TableDataInfo list(TPssrAboveall tPssrAboveall) {
        startPage();
        List<TPssrAboveall> list = tPssrAboveallService.selectTPssrAboveallList(tPssrAboveall);
        return getDataTable(list);
    }

    /**
     * 导出PSSR列表
     */
    @PreAuthorize("@ss.hasPermi('pssr:aboveall:export')")
    @Log(title = "PSSR", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TPssrAboveall tPssrAboveall) {
        List<TPssrAboveall> list = tPssrAboveallService.selectTPssrAboveallList(tPssrAboveall);
        ExcelUtil<TPssrAboveall> util = new ExcelUtil<TPssrAboveall>(TPssrAboveall.class);
        return util.exportExcel(list, "aboveall");
    }

    /**
     * 获取PSSR详细信息
     */
    @PreAuthorize("@ss.hasPermi('pssr:aboveall:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tPssrAboveallService.selectTPssrAboveallById(id));
    }


    /**
     * 修改PSSR
     */
    @PreAuthorize("@ss.hasPermi('pssr:aboveall:edit')")
    @Log(title = "PSSR", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TPssrAboveall tPssrAboveall) {
        return toAjax(tPssrAboveallService.updateTPssrAboveall(tPssrAboveall));
    }

    /**
     * 删除PSSR
     */
    @PreAuthorize("@ss.hasPermi('pssr:aboveall:remove')")
    @Log(title = "PSSR", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tPssrAboveallService.deleteTPssrAboveallByIds(ids));
    }

    /**
     * 查询pssr相关用户
     */
    @GetMapping("/listPssrUser")
    public AjaxResult listPssrUser(TPssrSubcontent sub) {
        SysUser user = new SysUser();
//        user.setDeptIds(new Long[]{103L,10042L,10058L});
        return AjaxResult.success(userService.selectPssrUser(user));
    }
    //endregion

    //region 新增

    /**
     * 新增PSSR
     */
    @PreAuthorize("@ss.hasPermi('pssr:aboveall:add')")
    @Log(title = "PSSR", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TPssrAboveall tPssrAboveall) {
        boolean model = false;
        TPssrAboveall aboveall = null;
        if (tPssrAboveall.getId() != null) {
            model = true;
            aboveall = tPssrAboveallService.selectTPssrAboveallById(tPssrAboveall.getId());
            tPssrAboveall.setUnit(aboveall.getUnit());
            tPssrAboveall.setRegion(aboveall.getRegion());
            tPssrAboveall.setId(null);
            tPssrAboveall.setDrivingTime(aboveall.getDrivingTime());
            tPssrAboveall.setIncludePublic(aboveall.getIncludePublic());
        }
        if (tPssrAboveall.getIncludePublic() == 1) {
            tPssrAboveall.setUnit(tPssrAboveall.getUnit() + ",390");
        }
        tPssrAboveallService.insertTPssrAboveall(tPssrAboveall);//新增主表数据

        // 记录所有负责人id
        Set<Long> userIds = new HashSet<>();

        String userId = getUserId().toString();
        //新增子表数据
        for (TPssrSubcontent subcontent : tPssrAboveall.getSubcontents()) {
            if (subcontent.getStatus() == 1) {
                userIds.add(Long.valueOf(subcontent.getConfirm()));
                subcontent.setAboveallId(tPssrAboveall.getId());//主表id
                subcontent.setRegion(tPssrAboveall.getRegion());
                subcontent.setUnit(tPssrAboveall.getUnit());
                subcontent.setUnitDes(tPssrAboveall.getUnitDes());
                subcontent.setApproveStatus(0L);
                subcontent.setCreatedate(new Date());
                subcontent.setCreaterCode(userId);
                tPssrSubcontentService.insertTPssrSubcontent(subcontent);

                new Thread(() -> {
                    if (subcontent.getForShort().equals("xqf")) {

                        List<TPssrPatrol> tPssrPatrols = new ArrayList<>();
//                        手提式/小推车式灭火器/灭火器材箱检查记录,1
                        tPssrPatrols.add(getEntity(1L, subcontent.getId()));
//                        消防水炮检查记录,2
                        tPssrPatrols.add(getEntity(2L, subcontent.getId()));
//                        地上消火栓检查记录,3
                        tPssrPatrols.add(getEntity(3L, subcontent.getId()));
//                        消防水带箱检查记录,4
                        tPssrPatrols.add(getEntity(4L, subcontent.getId()));
//                        消防竖管检查记录,5
                        tPssrPatrols.add(getEntity(5L, subcontent.getId()));
//                        自动喷淋灭火系统检查记录,6
                        tPssrPatrols.add(getEntity(6L, subcontent.getId()));
//                        应急事故柜检查记录,7
                        tPssrPatrols.add(getEntity(7L, subcontent.getId()));
//                        现场可燃气体检测器/火灾报警系统检查记录,8
                        tPssrPatrols.add(getEntity(8L, subcontent.getId()));
//                        CO2气体保护系统检查记录,9
                        tPssrPatrols.add(getEntity(9L, subcontent.getId()));
//                        淋浴洗眼器检查记录,12
                        tPssrPatrols.add(getEntity(12L, subcontent.getId()));
//                        应急沙箱检查记录,13
                        tPssrPatrols.add(getEntity(13L, subcontent.getId()));
//                        常闭式防爆门/防火门检查记录,14
                        tPssrPatrols.add(getEntity(14L, subcontent.getId()));
//                        风向标/风向袋/新风系统正压计检查记录,15
                        tPssrPatrols.add(getEntity(15L, subcontent.getId()));
//                        蒸汽幕检查记录,16
                        tPssrPatrols.add(getEntity(16L, subcontent.getId()));
                        // 急救箱
                        tPssrPatrols.add(getEntity(27L, subcontent.getId()));
                        for (TPssrPatrol pssrPatrol : tPssrPatrols) {
                            tPssrPatrolService.insertTPssrPatrol(pssrPatrol, userId);
                        }
                    } else if (subcontent.getForShort().equals("txjk")) {
                        List<TPssrPatrol> tPssrPatrols = new ArrayList<>();
//                        PAGA系统检查记录,10
                        tPssrPatrols.add(getEntity(10L, subcontent.getId()));
//                        CCTV系统检查记录,11
                        tPssrPatrols.add(getEntity(11L, subcontent.getId()));
                        for (TPssrPatrol pssrPatrol : tPssrPatrols) {
                            tPssrPatrolService.insertTPssrPatrol(pssrPatrol, userId);
                        }
                    } else if (subcontent.getForShort().equals("bxsjcy")) {
                        List<TPssrPatrol> tPssrPatrols = new ArrayList<>();
//                        四合一/苯系物/硫化氢/二氧化碳/射线一氧化碳检测仪检查记录,18
                        tPssrPatrols.add(getEntity(18L, subcontent.getId()));
                        for (TPssrPatrol pssrPatrol : tPssrPatrols) {
                            tPssrPatrolService.insertTPssrPatrol(pssrPatrol, userId);
                        }
                    } else if (subcontent.getForShort().equals("yfl")) {
                        genYfl(subcontent.getId(), userId);
                    } else if (subcontent.getForShort().equals("aqss")) {
                        genAqf(subcontent, userId);
                    } else if (subcontent.getForShort().equals("gygc")) {
                        genGygc(subcontent.getId(), userId);
                    } else if (subcontent.getForShort().equals("hbss")) {
                        genHbss(subcontent.getId(), userId);
                    } else if (subcontent.getForShort().equals("zmdbr")) {
                        genZmdbr(subcontent.getId(), userId);
                    } else if (subcontent.getForShort().equals("lsdy")) {
                        genLsdy(subcontent.getId(), userId);
                    } else if (subcontent.getForShort().equals("xcws")) {
                        genXcws(subcontent.getId(), userId);
                    } else if (subcontent.getForShort().equals("rsfh")) {
                        genRsfh(subcontent.getId(), userId);
                    } else if (subcontent.getForShort().equals("jdjdkj")) {
                        genJdjdkj(subcontent.getId(), userId);
                    } else if (subcontent.getForShort().equals("zdj")) {
                        genZdj(subcontent.getId(), subcontent.getUnit(), userId);
                    }
                }, "新增子表数据").start();
            }
        }

        //同步模板数据
        if (model) {
            genPssrDataByModel(aboveall, tPssrAboveall, userId);
        }

        new Thread(() -> {
            sendEmail(userIds, tPssrAboveall);
        },"新增pssr开始发邮件").start();
        return AjaxResult.success();
    }
    //endregion

    //region 生成固定内容数据

    /**
     * 生成支吊架数据
     */
    private void genZdj(Long subId, String units, String userId) {
        if (StringUtils.isNotEmpty(units)) {
            for (String unit : units.split(",")) {
                TPssrFrameModel model = new TPssrFrameModel();
                model.setUnit(unit);
                tPssrFrameModelService.selectTPssrFrameModelList(model).forEach(item -> {
                    TPssrFrame frame = new TPssrFrame();
                    frame.setSubId(subId);
                    frame.setRegion(item.getRegion());
                    frame.setBracketNumber(item.getBracketNumber());
                    frame.setPipelineNumber(item.getPipelineNumber());
                    frame.setDevNo(item.getDevNo());
                    frame.setSupportType(item.getSupportType());
                    frame.setFramePipe("✔");
                    frame.setFrameRoot("✔");
                    frame.setFunctionalPart("✔");
                    frame.setConnector("✔");
                    frame.setCreaterCode(userId);
                    frame.setCreatedate(new Date());
                    tPssrFrameService.insertTPssrFrame(frame);
                });
            }
        }
    }

    /**
     * 生成人身防护数据
     */
    private void genRsfh(Long subId, String userId) {
        tPssrProtectionService.insertTPssrProtection(new TPssrProtection(subId, "被保温的管道/设备保温防护层外观完好，无缺失。", userId, new Date()));
        tPssrProtectionService.insertTPssrProtection(new TPssrProtection(subId, "保温层连接密封处严密，无管道/设备裸露。", userId, new Date()));
        tPssrProtectionService.insertTPssrProtection(new TPssrProtection(subId, "管道/设备上的保温层不妨碍操作人员进行现场作业。", userId, new Date()));
    }

    /**
     * 生成静电接地跨接数据
     */
    private void genJdjdkj(Long subId, String userId) {
        tPssrFranklinismService.insertTPssrFranklinism(new TPssrFranklinism(1L, subId, "电气设备按要求接地，接线规范。", userId, new Date()));
        tPssrFranklinismService.insertTPssrFranklinism(new TPssrFranklinism(2L, subId, "管线按要求跨接，接线规范。", userId, new Date()));
        tPssrFranklinismService.insertTPssrFranklinism(new TPssrFranklinism(3L, subId, "接线端头牢固，无松动。", userId, new Date()));
        tPssrFranklinismService.insertTPssrFranklinism(new TPssrFranklinism(4L, subId, "接地电缆无破损，状况良好。", userId, new Date()));
        tPssrFranklinismService.insertTPssrFranklinism(new TPssrFranklinism(5L, subId, "接地标记/标识清晰。", userId, new Date()));
    }

    /**
     * 生成现场卫生数据
     */
    private void genXcws(Long subId, String userId) {
        tPssrHygieneService.insertTPssrHygiene(new TPssrHygiene(subId, "现场地面无油渍，积水等。", userId, new Date()));
        tPssrHygieneService.insertTPssrHygiene(new TPssrHygiene(subId, "现场无检修施工后的残留物(碎屑, 密封材料, 螺栓，保温材料等) 。", userId, new Date()));
        tPssrHygieneService.insertTPssrHygiene(new TPssrHygiene(subId, "所需材料安全堆放在指定区域。 ", userId, new Date()));
        tPssrHygieneService.insertTPssrHygiene(new TPssrHygiene(subId, "工器具整齐的放置在指定区域。", userId, new Date()));
        tPssrHygieneService.insertTPssrHygiene(new TPssrHygiene(subId, "地沟、污水管网、污水井、清净下水管网、清净下水管井内无杂物，且畅通无阻。", userId, new Date()));
        tPssrHygieneService.insertTPssrHygiene(new TPssrHygiene(subId, "无多余的脚手架，现存的脚手架不影响操作人员进行现场作业。", userId, new Date()));
    }

    /**
     * 生成临时电源数据
     */
    private void genLsdy(Long subId, String userId) {
        tPssrPowerService.insertTPssrPower(new TPssrPower(subId, "现场电源插座上无接临时用电设备、临时线路。", new Date(), userId));
        tPssrPowerService.insertTPssrPower(new TPssrPower(subId, "现场电源插座外观完好，无破损，插座盖子上紧，无脱落。", new Date(), userId));
    }

    /**
     * 生成照明、电伴热数据
     */
    private void genZmdbr(Long subId, String userId) {
        tPssrLightingService.insertTPssrLighting(new TPssrLighting(1L, subId, "照明灯外观完好，干净，无破损。", "1", userId, new Date()));
        tPssrLightingService.insertTPssrLighting(new TPssrLighting(2L, subId, "照明灯无锈蚀。", "1", userId, new Date()));
        tPssrLightingService.insertTPssrLighting(new TPssrLighting(3L, subId, "照明灯具固定牢固，无松动。", "1", userId, new Date()));
        tPssrLightingService.insertTPssrLighting(new TPssrLighting(4L, subId, "照明导线完好，无破损。", "1", userId, new Date()));
        tPssrLightingService.insertTPssrLighting(new TPssrLighting(5L, subId, "照明开关完好。", "1", userId, new Date()));
        tPssrLightingService.insertTPssrLighting(new TPssrLighting(6L, subId, "照明设备可以正常打开或者关闭。", "1", userId, new Date()));
        tPssrLightingService.insertTPssrLighting(new TPssrLighting(1L, subId, "现场电伴热带无破损断裂，无变形", "2", userId, new Date()));
        tPssrLightingService.insertTPssrLighting(new TPssrLighting(2L, subId, "电伴热带与管道设备紧密贴合，无缝隙", "2", userId, new Date()));
        tPssrLightingService.insertTPssrLighting(new TPssrLighting(3L, subId, "电源接线盒牢固密封，接线端子无松动锈蚀。", "2", userId, new Date()));
    }

    /**
     * 生成环保设施数据
     */
    private void genHbss(Long subId, String userId) {
        tPssrTorchvocService.insertTPssrTorchvoc(new TPssrTorchvoc(1L, subId, "1", "火炬系统检修时所加装的盲板已拆除。", userId, new Date()));
        tPssrTorchvocService.insertTPssrTorchvoc(new TPssrTorchvoc(2L, subId, "1", "火炬系统已完成氮气置换，氧含量<0.2%。", userId, new Date()));
        tPssrTorchvocService.insertTPssrTorchvoc(new TPssrTorchvoc(3L, subId, "1", "公用工程（消防水、仪表风、氮气、电、中、低压蒸汽等）已投用或具备使用条件。", userId, new Date()));
        tPssrTorchvocService.insertTPssrTorchvoc(new TPssrTorchvoc(4L, subId, "1", "火炬系统的所有仪表功能检查正常。", userId, new Date()));
        tPssrTorchvocService.insertTPssrTorchvoc(new TPssrTorchvoc(5L, subId, "1", "火炬点火用燃料气已引到PCV-9994、PC-9991阀前。", userId, new Date()));
        tPssrTorchvocService.insertTPssrTorchvoc(new TPssrTorchvoc(6L, subId, "1", "仪表空气已送至PCV-9995阀前。", userId, new Date()));
        tPssrTorchvocService.insertTPssrTorchvoc(new TPssrTorchvoc(7L, subId, "1", "加热火炬气的甲醇已充进换热器390-E-903，390-E-903的低压蒸汽管线暖管结束。", userId, new Date()));
        tPssrTorchvocService.insertTPssrTorchvoc(new TPssrTorchvoc(8L, subId, "1", "火炬总管分子封用氮已经投用。", userId, new Date()));
        tPssrTorchvocService.insertTPssrTorchvoc(new TPssrTorchvoc(9L, subId, "1", "点火器装置和电视监控器等电器设备已送电，设备完好。", userId, new Date()));
        tPssrTorchvocService.insertTPssrTorchvoc(new TPssrTorchvoc(10L, subId, "1", "火炬消烟蒸汽已引到蒸汽调节阀前，RO-9991～3的前后闸阀全开，管线上所有疏水器均须投用。", userId, new Date()));
        tPssrTorchvocService.insertTPssrTorchvoc(new TPssrTorchvoc(11L, subId, "1", "由LV-9990向水封罐390-V-940注生产工艺水，直到工艺水由从U形管排出已完成。", userId, new Date()));
        tPssrTorchvocService.insertTPssrTorchvoc(new TPssrTorchvoc(12L, subId, "1", "设定390-P-940去急冷水塔的流程，已投用390-P-940自启动及停车控制器UC-9990。", userId, new Date()));
        tPssrTorchvocService.insertTPssrTorchvoc(new TPssrTorchvoc(13L, subId, "1", "已投用各火炬总管端点的吹扫气。", userId, new Date()));
        tPssrTorchvocService.insertTPssrTorchvoc(new TPssrTorchvoc(14L, subId, "1", "火炬长明灯点火完成，已确认正常。", userId, new Date()));
        tPssrTorchvocService.insertTPssrTorchvoc(new TPssrTorchvoc(15L, subId, "1", "火炬系统已可以接收装置物料排放。", userId, new Date()));
        tPssrTorchvocService.insertTPssrTorchvoc(new TPssrTorchvoc(1L, subId, "2", "VOC系统检修时所加装的盲板已拆除。", userId, new Date()));
        tPssrTorchvocService.insertTPssrTorchvoc(new TPssrTorchvoc(2L, subId, "2", "VOC系统已完成氮气置换，氧含量合格。", userId, new Date()));
        tPssrTorchvocService.insertTPssrTorchvoc(new TPssrTorchvoc(3L, subId, "2", "公用工程（消防水、仪表风、氮气、电、中、低压蒸汽等）已投用或具备使用条件。", userId, new Date()));
        tPssrTorchvocService.insertTPssrTorchvoc(new TPssrTorchvoc(4L, subId, "2", "VOC系统的所有仪表功能已检查正常。", userId, new Date()));
        tPssrTorchvocService.insertTPssrTorchvoc(new TPssrTorchvoc(5L, subId, "2", "VOC系统具备使用条件，开车流程打通，已确认正常。", userId, new Date()));
        tPssrTorchvocService.insertTPssrTorchvoc(new TPssrTorchvoc(6L, subId, "2", "按操作程序启动390-K-950，已确认K950运行正常。", userId, new Date()));
        tPssrTorchvocService.insertTPssrTorchvoc(new TPssrTorchvoc(7L, subId, "2", "VOC系统运行正常，已可以接收VOC排放。", userId, new Date()));
    }

    /**
     * 生成巡检数据
     */
    private TPssrPatrol getEntity(Long planType, Long subId) {
        TPssrPatrol tPssrPatrol = new TPssrPatrol();
        tPssrPatrol.setPlanType(planType);
        tPssrPatrol.setSubId(subId);
        return tPssrPatrol;
    }

    /**
     * 生成原辅料数据
     */
    private void genYfl(Long subId, String userId) {
        tPssrMaterialService.insertTPssrMaterial(new TPssrMaterial(subId, "Electr. Power", "MWH", userId, new Date(), 0L));
        tPssrMaterialService.insertTPssrMaterial(new TPssrMaterial(subId, "LP steam ", "Ton", userId, new Date(), 0L));
        tPssrMaterialService.insertTPssrMaterial(new TPssrMaterial(subId, "MP steam", "Ton", userId, new Date(), 0L));
        tPssrMaterialService.insertTPssrMaterial(new TPssrMaterial(subId, "HP steam ", "Ton", userId, new Date(), 0L));
        tPssrMaterialService.insertTPssrMaterial(new TPssrMaterial(subId, "Potable water", "Ton", userId, new Date(), 0L));
        tPssrMaterialService.insertTPssrMaterial(new TPssrMaterial(subId, "production water", "Ton", userId, new Date(), 0L));
        tPssrMaterialService.insertTPssrMaterial(new TPssrMaterial(subId, "LP fire water", "Ton", userId, new Date(), 0L));
        tPssrMaterialService.insertTPssrMaterial(new TPssrMaterial(subId, "HP fire water", "Ton", userId, new Date(), 0L));
        tPssrMaterialService.insertTPssrMaterial(new TPssrMaterial(subId, "Cooling water", "Ton", userId, new Date(), 0L));
        tPssrMaterialService.insertTPssrMaterial(new TPssrMaterial(subId, "Polished Water", "Ton", userId, new Date(), 0L));
        tPssrMaterialService.insertTPssrMaterial(new TPssrMaterial(subId, "Nitrogen", "Nm3", userId, new Date(), 0L));
        tPssrMaterialService.insertTPssrMaterial(new TPssrMaterial(subId, "Plant Air", "Nm3", userId, new Date(), 0L));
        tPssrMaterialService.insertTPssrMaterial(new TPssrMaterial(subId, "Instrument Air", "Nm3", userId, new Date(), 0L));
        tPssrMaterialService.insertTPssrMaterial(new TPssrMaterial(subId, "WW(only quantity)", "Ton", userId, new Date(), 0L));
        tPssrMaterialRawService.insertTPssrMaterialRaw(new TPssrMaterialRaw(subId, "Naphtha", "Ton", userId, new Date(), 0L));
        tPssrMaterialRawService.insertTPssrMaterialRaw(new TPssrMaterialRaw(subId, "Flux Oil", "Ton", userId, new Date(), 0L));
        tPssrMaterialRawService.insertTPssrMaterialRaw(new TPssrMaterialRaw(subId, "Wash Oil", "Ton", userId, new Date(), 0L));
        tPssrMaterialRawService.insertTPssrMaterialRaw(new TPssrMaterialRaw(subId, "C6-C8", "Ton", userId, new Date(), 0L));
        tPssrMaterialRawService.insertTPssrMaterialRaw(new TPssrMaterialRaw(subId, "Natural Gas", "Ton", userId, new Date(), 0L));
        tPssrMaterialRawService.insertTPssrMaterialRaw(new TPssrMaterialRaw(subId, "Ethylene", "Ton", userId, new Date(), 0L));
        tPssrMaterialRawService.insertTPssrMaterialRaw(new TPssrMaterialRaw(subId, "Propylene", "Ton", userId, new Date(), 0L));
        tPssrMaterialRawService.insertTPssrMaterialRaw(new TPssrMaterialRaw(subId, "Toluene", "Ton", userId, new Date(), 0L));
    }

    /**
     * 生成安全阀数据
     */
    private void genAqf(TPssrSubcontent subcontent, String userId) {
        if (StringUtils.isNotEmpty(subcontent.getUnit()))
            for (String unit : subcontent.getUnit().split(",")) {
                TSafetyvavle query = new TSafetyvavle();
                query.setSubarea(unit);
                for (TSafetyvavle vavle : tSafetyvavleService.selectTSafetyvavleList(query)) {
                    TPssrSafetyValve pssr = new TPssrSafetyValve();
                    pssr.setUnit(unit);
                    pssr.setSubId(subcontent.getId());
                    pssr.setDevNo(vavle.getTagno());
                    pssr.setInstallLocation(vavle.getPlacedesc());
                    pssr.setSafetyDev(vavle.getService());
                    pssr.setPidNo(vavle.getPid());
                    pssr.setSetPressure(vavle.getSettingpres());
                    pssr.setApproveStatus(0L);
                    pssr.setCreatedate(new Date());
                    pssr.setCreaterCode(userId);
                    tPssrSafetyValveService.insertTPssrSafetyValve(pssr);
                }
            }
    }

    /**
     * 生成公用工程数据
     */
    private void genGygc(Long subId, String userId) {
        tPssrPublicService.insertTPssrPublic(new TPssrPublic(subId, "公用工程系统N2已投用或具备投用条件。", "界区阀已打开，N2总管压力390PI9114压力达(    )kPa。", "N2总管压力390PI9114压力达(    )kPa。", userId, new Date()));
        tPssrPublicService.insertTPssrPublic(new TPssrPublic(subId, "公用工程系统IA已投用或具备投用条件：", "界区阀已打开，IA管压力PI3901压力达(    )kPa。", "IA管压力PI3901压力达(    )kPa。", userId, new Date()));
        tPssrPublicService.insertTPssrPublic(new TPssrPublic(subId, "公用工程系统PA已投用或具备投用条件。", "界区阀已打开，PA管压力PI3902压力达(    )kPa。", "PA管压力PI3902压力达(    )kPa。", userId, new Date()));
        tPssrPublicService.insertTPssrPublic(new TPssrPublic(subId, "公用工程系统CW已投用或具备投用条件。", "界区阀已打开，CW进出口压差达(    )kPa。", "CW进出口压差达(    )kPa。", userId, new Date()));
        tPssrPublicService.insertTPssrPublic(new TPssrPublic(subId, "公用工程系统高压蒸汽已投用或具备投用条件。", "界区阀已打开，PC9112压力达(    )MPa，TI9061温度达(    )℃。", "PC9112压力达(    )MPa，TI9061温度达(    )℃。", userId, new Date()));
        tPssrPublicService.insertTPssrPublic(new TPssrPublic(subId, "公用工程系统中压蒸汽已投用或具备投用条件。", "界区阀已打开，PC9081压力达(    )MPa，TI9081温度达(    )℃。", "PC9081压力达(    )MPa，TI9081温度达(    )℃。", userId, new Date()));
        tPssrPublicService.insertTPssrPublic(new TPssrPublic(subId, "公用工程系统低压蒸汽已投用或具备投用条件。", "界区阀已打开，PY9109压力达(    )MPa，TI9115温度达(    )℃。", "PY9109压力达(    )MPa，TI9115温度达(    )℃。", userId, new Date()));
        tPssrPublicService.insertTPssrPublic(new TPssrPublic(subId, "公用工程系统精制凝液已投用或具备投用条件。", "界区阀已打开，P3505A/B/C压力达(    )MPa。", "P3505A/B/C压力达(    )MPa。", userId, new Date()));
    }
//endregion

    //region 根据模板旧数据生成数据

    /**
     * 根据模板旧数据生成数据
     */
    private void genPssrDataByModel(TPssrAboveall model, TPssrAboveall newData, String userId) {
//        new Thread(() -> {//同步模板数据
        TPssrSubcontent subcontent = new TPssrSubcontent();
        subcontent.setAboveallId(model.getId());
        //查询旧数据对应的子表信息，然后根据模板生成新的子表数据
        for (TPssrSubcontent oldSub : tPssrSubcontentService.selectTPssrSubcontentList(subcontent)) {
            //查询模板中是否有该子表数据，如果有则生成新的子表数据
            TPssrSubcontent sub = new TPssrSubcontent();
            sub.setAboveallId(newData.getId());
            sub.setForShort(oldSub.getForShort());
            TPssrSubcontent newSub = tPssrSubcontentService.selectTPssrSubcontentList(sub).get(0);
            switch (oldSub.getForShort()) {
                case "jxxm":
                    genJxxm(oldSub, newSub, userId);
                    break;
                case "kgfa":
                    genKgfa(oldSub, newSub, userId);
                    break;
                case "sjbg":
                    genSjgb(oldSub, newSub, userId);
                    break;
                case "mb":
                    genMb(oldSub, newSub, userId);
                    break;
                case "sbqjd":
                    genSbqjd(oldSub, newSub, userId);
                    break;
                case "qm":
                    genQm(oldSub, newSub, userId);
                    break;
                case "sksgfmzt":
                    genSksgfmzt(oldSub, newSub, userId);
                    break;
                case "sys":
                    genSys(oldSub, newSub, userId);
                    break;
                case "dj":
                    genDj(oldSub, newSub, userId);
                    break;
                case "dqzh":
                    genDqzh(oldSub, newSub, userId);
                    break;
                case "aqss":
                    genAqss(oldSub, newSub, userId);
                    break;
                case "jb":
                    genJb(oldSub, newSub, userId);
                    break;
                case "ybjy":
                    genYbjy(oldSub, newSub, userId);
                    break;
                case "yblscs":
                    genYblscs(oldSub, newSub, userId);
                    break;
                case "bjlszqr":
                    genBjlszqr(oldSub, newSub, userId);
                    break;
                case "djsb":
                    genDjsb(oldSub, newSub, userId);
                    break;
                case "qtjcy":
                    genQtjcy(oldSub, newSub, userId);
                    break;
                case "zxfxy":
                    genZxfxy(oldSub, newSub, userId);
                    break;
            }
        }
//        }, "新增模板子表数据").start();
    }

    /**
     * 生成短接数据
     */
    private void genDj(TPssrSubcontent oldSub, TPssrSubcontent newSub, String userId) {
        TPssrCircuit circuit = new TPssrCircuit();
        circuit.setSubId(oldSub.getId());
        for (TPssrCircuit oldItem : tPssrCircuitService.selectTPssrCircuitList(circuit)) {
            TPssrCircuit newItem = oldItem;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setConfirmationDate(null);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrCircuitService.insertTPssrCircuit(newItem);
        }
    }

    /**
     * 生成实验室数据
     */
    private void genSys(TPssrSubcontent oldSub, TPssrSubcontent newSub, String userId) {
        TPssrLaboratory laboratory = new TPssrLaboratory();
        laboratory.setSubId(oldSub.getId());
        for (TPssrLaboratory oldItem : tPssrLaboratoryService.selectTPssrLaboratoryList(laboratory)) {
            TPssrLaboratory newItem = oldItem;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setConfirmationDate(null);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrLaboratoryService.insertTPssrLaboratory(newItem);
        }
    }

    /**
     * 生成锁开锁关数据
     */
    private void genSksgfmzt(TPssrSubcontent oldSub, TPssrSubcontent newSub, String userId) {
        TPssrLock lock = new TPssrLock();
        lock.setSubId(oldSub.getId());
        for (TPssrLock oldItem : tPssrLockService.selectTPssrLockList(lock)) {
            TPssrLock newItem = oldItem;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setConfirmationDate(null);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrLockService.insertTPssrLock(newItem);
        }
    }

    /**
     * 生成气密数据
     */
    private void genQm(TPssrSubcontent oldSub, TPssrSubcontent newSub, String userId) {
        TPssrAirtight airtight = new TPssrAirtight();
        airtight.setSubId(oldSub.getId());
        for (TPssrAirtight oldItem : tPssrAirtightService.selectTPssrAirtightList(airtight)) {
            TPssrAirtight newItem = oldItem;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setConfirmationDate(null);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrAirtightService.insertTPssrAirtight(newItem);
        }
    }

    /**
     * 生成设备清洁度数据
     */
    private void genSbqjd(TPssrSubcontent oldSub, TPssrSubcontent newSub, String userId) {
        TPssrCleaning cleaning = new TPssrCleaning();
        cleaning.setSubId(oldSub.getId());
        for (TPssrCleaning oldItem : tPssrCleaningService.selectTPssrCleaningList(cleaning)) {
            TPssrCleaning newItem = oldItem;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setConfirmationDate(null);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrCleaningService.insertTPssrCleaning(newItem);
        }
    }

    /**
     * 生成盲板数据
     */
    private void genMb(TPssrSubcontent oldSub, TPssrSubcontent newSub, String userId) {
        TPssrBlind blind = new TPssrBlind();
        blind.setSubId(oldSub.getId());
        for (TPssrBlind oldItem : tPssrBlindService.selectTPssrBlindList(blind)) {
            TPssrBlind newItem = oldItem;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setInstallDate(null);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrBlindService.insertTPssrBlind(newItem);
        }
    }

    /**
     * 生成氮气置换数据
     */
    private void genDqzh(TPssrSubcontent oldSub, TPssrSubcontent newSub, String userId) {
        TPssrNitrogen nitrogen = new TPssrNitrogen();
        nitrogen.setSubId(oldSub.getId());
        for (TPssrNitrogen oldItem : tPssrNitrogenService.selectTPssrNitrogenList(nitrogen)) {
            TPssrNitrogen newItem = oldItem;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setConfirmationDate(null);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrNitrogenService.insertTPssrNitrogen(newItem);
        }
    }

    /**
     * 生成安全设施数据
     */
    private void genAqss(TPssrSubcontent oldSub, TPssrSubcontent newSub, String userId) {
        //泄放阀
        TPssrSafetyBleed safetyBleed = new TPssrSafetyBleed();
        safetyBleed.setSubId(oldSub.getId());
        for (TPssrSafetyBleed oldItem : tPssrSafetyBleedService.selectTPssrSafetyBleedList(safetyBleed)) {
            TPssrSafetyBleed newItem = oldItem;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrSafetyBleedService.insertTPssrSafetyBleed(newItem);
        }
        //呼吸阀
        TPssrSafetyBreath safetyBreath = new TPssrSafetyBreath();
        safetyBreath.setSubId(oldSub.getId());
        for (TPssrSafetyBreath oldItem : tPssrSafetyBreathService.selectTPssrSafetyBreathList(safetyBreath)) {
            TPssrSafetyBreath newItem = oldItem;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrSafetyBreathService.insertTPssrSafetyBreath(newItem);
        }
        //爆破片
        TPssrSafetyBrust safetyBrust = new TPssrSafetyBrust();
        safetyBrust.setSubId(oldSub.getId());
        for (TPssrSafetyBrust oldItem : tPssrSafetyBrustService.selectTPssrSafetyBrustList(safetyBrust)) {
            TPssrSafetyBrust newItem = oldItem;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrSafetyBrustService.insertTPssrSafetyBrust(newItem);
        }
        //阻火器
        TPssrSafetyFlamearrester safetyFlamearrester = new TPssrSafetyFlamearrester();
        safetyFlamearrester.setSubId(oldSub.getId());
        for (TPssrSafetyFlamearrester oldItem : tPssrSafetyFlamearresterService.selectTPssrSafetyFlamearresterList(safetyFlamearrester)) {
            TPssrSafetyFlamearrester newItem = oldItem;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrSafetyFlamearresterService.insertTPssrSafetyFlamearrester(newItem);
        }
    }

    /**
     * 生成机泵数据
     */
    private void genJb(TPssrSubcontent oldSub, TPssrSubcontent newSub, String userId) {
        TPssrPumpCleaning pumpCleaning = new TPssrPumpCleaning();
        pumpCleaning.setSubId(oldSub.getId());
        for (TPssrPumpCleaning oldItem : tPssrPumpCleaningService.selectTPssrPumpCleaningList(pumpCleaning)) {
            TPssrPumpCleaning newItem = oldItem;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setConfirmationDate(null);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrPumpCleaningService.insertTPssrPumpCleaning(newItem);
        }
        TPssrPumpFill pumpFill = new TPssrPumpFill();
        pumpFill.setSubId(oldSub.getId());
        for (TPssrPumpFill oldItem : tPssrPumpFillService.selectTPssrPumpFillList(pumpFill)) {
            TPssrPumpFill newItem = oldItem;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setConfirmationDate(null);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrPumpFillService.insertTPssrPumpFill(newItem);
        }
        TPssrPumpOverhaul pumpOverhaul = new TPssrPumpOverhaul();
        pumpOverhaul.setSubId(oldSub.getId());
        for (TPssrPumpOverhaul oldItem : tPssrPumpOverhaulService.selectTPssrPumpOverhaulList(pumpOverhaul)) {
            TPssrPumpOverhaul newItem = oldItem;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setConfirmationDate(null);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrPumpOverhaulService.insertTPssrPumpOverhaul(newItem);
        }
    }

    /**
     * 生成仪表校验数据
     */
    private void genYbjy(TPssrSubcontent oldSub, TPssrSubcontent newSub, String userId) {
        TPssrInstrumentCalibration instrumentCalibration = new TPssrInstrumentCalibration();
        instrumentCalibration.setSubId(oldSub.getId());
        for (TPssrInstrumentCalibration oldItem : tPssrInstrumentCalibrationService.selectTPssrInstrumentCalibrationList(instrumentCalibration)) {
            TPssrInstrumentCalibration newItem = oldItem;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setConfirmationDate(null);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrInstrumentCalibrationService.insertTPssrInstrumentCalibration(newItem);
        }
    }

    /**
     * 生成仪表连锁测试数据
     */
    private void genYblscs(TPssrSubcontent oldSub, TPssrSubcontent newSub, String userId) {
        TPssrInstrumentTest instrumentTest = new TPssrInstrumentTest();
        instrumentTest.setSubId(oldSub.getId());
        for (TPssrInstrumentTest oldItem : tPssrInstrumentTestService.selectTPssrInstrumentTestList(instrumentTest)) {
            TPssrInstrumentTest newItem = oldItem;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrInstrumentTestService.insertTPssrInstrumentTest(newItem);
        }
    }

    /**
     * 生成报警连锁值确认数据
     */
    private void genBjlszqr(TPssrSubcontent oldSub, TPssrSubcontent newSub, String userId) {
        TPssrAlarm alarm = new TPssrAlarm();
        alarm.setSubId(oldSub.getId());
        for (TPssrAlarm oldItem : tPssrAlarmService.selectTPssrAlarmList(alarm)) {
            TPssrAlarm newItem = oldItem;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrAlarmService.insertTPssrAlarm(newItem);
        }
    }

    /**
     * 生成电机设备数据
     */
    private void genDjsb(TPssrSubcontent oldSub, TPssrSubcontent newSub, String userId) {
        TPssrMotor motor = new TPssrMotor();
        motor.setSubId(oldSub.getId());
        for (TPssrMotor oldItem : tPssrMotorService.selectTPssrMotorList(motor)) {
            TPssrMotor newItem = oldItem;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setConfirmationDate(null);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrMotorService.insertTPssrMotor(newItem);
        }
    }

    /**
     * 生成气体检测仪数据
     */
    private void genQtjcy(TPssrSubcontent oldSub, TPssrSubcontent newSub, String userId) {
        TPssrGasdetector gasdetector = new TPssrGasdetector();
        gasdetector.setSubId(oldSub.getId());
        for (TPssrGasdetector oldItem : tPssrGasdetectorService.selectTPssrGasdetectorList(gasdetector)) {
            TPssrGasdetector newItem = oldItem;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setConfirmationDate(null);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrGasdetectorService.insertTPssrGasdetector(newItem);
        }
    }

    /**
     * 生成在线分析仪数据
     */
    private void genZxfxy(TPssrSubcontent oldSub, TPssrSubcontent newSub, String userId) {
        TPssrAnalyzer analyzer = new TPssrAnalyzer();
        analyzer.setSubId(oldSub.getId());
        for (TPssrAnalyzer oldItem : tPssrAnalyzerService.selectTPssrAnalyzerList(analyzer)) {
            TPssrAnalyzer newItem = oldItem;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setConfirmationDate(null);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrAnalyzerService.insertTPssrAnalyzer(newItem);
        }
    }

    /**
     * 生成设计变更数据
     */
    private void genSjgb(TPssrSubcontent oldSub, TPssrSubcontent newSub, String userId) {
        TPssrMoc moc = new TPssrMoc();
        moc.setSubId(oldSub.getId());
        for (TPssrMoc oldItem : tPssrMocService.selectTPssrMocList(moc)) {
            TPssrMoc newItem = oldItem;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setConfirmationDate(null);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrMocService.insertTPssrMoc(newItem);
        }
    }

    /**
     * 生成检修项目数据
     */
    private void genJxxm(TPssrSubcontent oldSub, TPssrSubcontent newSub, String userId) {
        //换热器
        TPssrOverhaulExchanger overhaulExchanger = new TPssrOverhaulExchanger();
        overhaulExchanger.setSubId(oldSub.getId());
        for (TPssrOverhaulExchanger exchanger : tPssrOverhaulExchangerService.selectTPssrOverhaulExchangerList(overhaulExchanger)) {
            TPssrOverhaulExchanger newItem = exchanger;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setCompletionDate(null);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrOverhaulExchangerService.insertTPssrOverhaulExchanger(newItem);
        }
        //过滤器
        TPssrOverhaulFilter overhaulFilter = new TPssrOverhaulFilter();
        overhaulFilter.setSubId(oldSub.getId());
        for (TPssrOverhaulFilter filter : tPssrOverhaulFilterService.selectTPssrOverhaulFilterList(overhaulFilter)) {
            TPssrOverhaulFilter newItem = filter;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setCompletionDate(null);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrOverhaulFilterService.insertTPssrOverhaulFilter(newItem);
        }
        //塔罐
        TPssrOverhaulTower overhaulTower = new TPssrOverhaulTower();
        overhaulTower.setSubId(oldSub.getId());
        for (TPssrOverhaulTower tower : tPssrOverhaulTowerService.selectTPssrOverhaulTowerList(overhaulTower)) {
            TPssrOverhaulTower newItem = tower;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setCompletionDate(null);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrOverhaulTowerService.insertTPssrOverhaulTower(newItem);
        }
        //阀门
        TPssrOverhaulValve overhaulValve = new TPssrOverhaulValve();
        overhaulValve.setSubId(oldSub.getId());
        for (TPssrOverhaulValve valve : tPssrOverhaulValveService.selectTPssrOverhaulValveList(overhaulValve)) {
            TPssrOverhaulValve newItem = valve;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setDoneDate(null);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrOverhaulValveService.insertTPssrOverhaulValve(newItem);
        }
    }

    /**
     * 生成开工方案数据
     */
    private void genKgfa(TPssrSubcontent oldSub, TPssrSubcontent newSub, String userId) {
        TPssrProgramme programme = new TPssrProgramme();
        programme.setSubId(oldSub.getId());
        for (TPssrProgramme oldItem : tPssrProgrammeService.selectTPssrProgrammeList(programme)) {
            TPssrProgramme newItem = oldItem;
            newItem.setSubId(newSub.getId());
            newItem.setId(null);
            newItem.setCreatedate(new Date());
            newItem.setCreaterCode(userId);
            newItem.setApproveStatus(0L);
            newItem.setUpdaterCode(null);
            newItem.setUpdatedate(null);
            tPssrProgrammeService.insertTPssrProgramme(newItem);
        }
    }
    //endregion

    // region 发邮件
    private void sendEmail(Set<Long> userIds, TPssrAboveall tPssrAboveall) {
        for (Long userId : userIds) {
            try {
                SysUser sysUser = userService.selectUserById(userId);
                String email = sysUser.getEmail();
                String username = sysUser.getNickName();
                String loginName = sysUser.getUserName();
                String usernameEN = PinyinHelper.convertToPinyinString(username, " ", PinyinFormat.WITHOUT_TONE);
                logger.info("email:" + email);
                logger.info("username:" + username);
                //写html开始内容
                String start = "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title></title></head><body><div style=\"background-color:#ECECEC; padding: 35px;\">" +
                        "<table cellpadding=\"0\" align=\"center\"" +
                        "style=\"width: 600px; margin: 0px auto; text-align: left; position: relative; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; font-size: 14px; font-family:微软雅黑, 黑体; line-height: 1.5; box-shadow: rgb(153, 153, 153) 0px 0px 5px; border-collapse: collapse; background-position: initial initial; background-repeat: initial initial;background:#fff;\">" +
                        "<tbody><tr><th valign=\"middle\" style=\"height: 25px; line-height: 25px; padding: 15px 35px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #42a3d3; background-color: #49bcff; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px;\">" +
                        "<font face=\"微软雅黑\" size=\"5\" style=\"color: rgb(255, 255, 255); \">PSSR检查确认 </font><font face=\"微软雅黑\" size=\"3\" style=\"color: rgb(255, 255, 255); \">PSSR</font></th></tr>";
                //表html中间内容
                String prime = "";
                String center = "<tr><td><div style=\"padding:25px 35px 40px; background-color:#fff;\"><h2 style=\"margin: 5px 0px; \">" +
                        "<font color=\"#333333\" style=\"line-height: 20px; \"><font style=\"line-height: 22px; \" size=\"4\">" +
                        "亲爱的 username(loginName)</font><br><font style=\"line-height: 22px; \" size=\"4\">" +
                        "Dear usernameEN(loginName)</font></font></h2>" +
                        "<p>region区域unit的PSSR检查确认已经开始，预计开车时间为startDate：<br>" +
                        "You Have A New PSSR:<br>" +
                        "请登录<a href=\"https://cpms.basf-ypc.net.cn/cpms/index.html#/pssr/subcontent/aboveallId\">PSSR清单中</a>查看。<br>" +
                        "Please log in the <a href=\"https://cpms.basf-ypc.net.cn/cpms/index.html#/pssr/aboveall\">PSSR</a> to handle it.</p>" +
                        "<p align=\"right\">date</p>" +
                        "<div style=\"width:700px;margin:0 auto;\">" +
                        "<div style=\"padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;\">" +
                        "<p>此为系统邮件，请勿回复<br>This e-Mail is an automatic reminder sent by CPMS, please do not reply</p>" +
                        "</div></div></div></td></tr>";
                String one = center.replaceFirst("username", username);
                String two = one.replaceFirst("usernameEN", usernameEN);
                String three = two.replace("loginName", loginName);
                String four = three.replace("region", tPssrAboveall.getRegion());
                String five = four.replace("unit", "，" + tPssrAboveall.getUnit() + "单元");
                String six = five.replace("aboveallId", tPssrAboveall.getId().toString() );
                String seven = six.replaceFirst("startDate", DateUtils.dateTime(tPssrAboveall.getDrivingTime()));
                String result = seven.replaceFirst("date", DateUtils.dateTime());
                prime = prime + result;
                //写html结尾内容
                String end = "</tbody></table></div></body></html>";
                //拼接html
                String html = start + prime + end;
                logger.info("html:" + html);
                mailService.sendHtmlMail(email, "PSSR：您有新的PSSR检查待处理", html);

            } catch (Exception e) {
                logger.error("邮件发送失败" + JSON.toJSONString(e));
            }
            try {
                //极光推送
                jiGuangPushService.send("PSSR审批", "您有一个新的待办任务，请及时处理。", String.valueOf(userId));

                //系统消息推送
                SysMessage sysMessage = new SysMessage();
                sysMessage.setMsgTitle("PSSR审批");
                sysMessage.setMsgContent("您有一个新的待办任务，请及时处理。");
                sysMessage.setUserId(userId);
                sysMessageService.insertSysMessage(sysMessage);
            } catch (Exception e) {
                logger.error("推送失败：{}", JSON.toJSONString(e));
            }
        }
    }
    // endregion
}
