package com.testor.module.hazard.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.testor.biz.file.controller.SysFileController;
import com.testor.common.util.DateUtil;
import com.testor.module.hazard.dao.THazardConditionConfirmationDao;
import com.testor.module.hazard.model.domain.*;
import com.testor.module.hazard.model.dto.THazardConditionConfirmationDTO;
import com.testor.module.hazard.model.dto.THazardWorkPlanParam;
import com.testor.module.hazard.model.enums.WorkPlanStatusEnum;
import com.testor.module.hazard.service.*;
import com.tongtech.tfw.backend.common.models.exceptions.ServiceException;
import com.tongtech.tfw.backend.core.helper.IdHelper;
import com.tongtech.tfw.backend.core.helper.bean.BeanHelper;
import com.tongtech.tfw.workflow.service.TfwProcessInsService;
import com.tongtech.tfw.workflow.service.dto.ProcessInsStartParams;
import com.tongtech.tfw.workflow.service.dto.ProcessInsStartResult;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.tongtech.tfw.backend.common.models.supers.SuperServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 安全条件确认Service业务层处理
 *
 * @author testor-framework
 * @date 2024-12-24 21:53:21
 */
@Service
public class THazardConditionConfirmationServiceImpl extends SuperServiceImpl<THazardConditionConfirmationDao, THazardConditionConfirmation> implements THazardConditionConfirmationService
{
    @Autowired
    private THazardConditionConfirmationDetailsService tHazardConditionConfirmationDetailsService;
    @Autowired
    private THazardWorkPlanService tHazardWorkPlanService;
    @Autowired
    private TfwProcessInsService tfwProcessInsService;
    @Autowired
    private THazardConditionConfirmationRecordService tHazardConditionConfirmationRecordService;

    private static final Logger log = LoggerFactory.getLogger(THazardConditionConfirmationServiceImpl.class);


    @Override
    @Transactional(rollbackFor = Exception.class, timeout = 30)
    public THazardConditionConfirmation saveOrUpdateEntity(THazardConditionConfirmationDTO tHazardConditionConfirmationDTO) {
        log.info("========== 开始保存或更新安全条件确认 ==========");
        log.info("【入参】planId: {}, workTypeId: {}, id: {}, 详情数量: {}",
                tHazardConditionConfirmationDTO.getPlanId(),
                tHazardConditionConfirmationDTO.getWorkTypeId(),
                tHazardConditionConfirmationDTO.getId(),
                tHazardConditionConfirmationDTO.getTHazardConditionConfirmationDetails() != null ?
                        tHazardConditionConfirmationDTO.getTHazardConditionConfirmationDetails().size() : 0);

        try {
            THazardConditionConfirmation tHazardConditionConfirmation = BeanHelper.beanToBean(tHazardConditionConfirmationDTO, THazardConditionConfirmation.class);
            String id = tHazardConditionConfirmationDTO.getId();

            // 如果是新增操作
            if (StringUtils.isBlank(id)) {
                log.info("【操作类型】新增操作 - 开始查询最大批次记录");

                // 查询最大批次的记录
                THazardConditionConfirmation maxRecord = this.lambdaQuery()
                        .eq(THazardConditionConfirmation::getPlanId, tHazardConditionConfirmationDTO.getPlanId())
                        .eq(THazardConditionConfirmation::getWorkTypeId, tHazardConditionConfirmationDTO.getWorkTypeId())
                        .orderByDesc(THazardConditionConfirmation::getBatch)
                        .last("LIMIT 1")
                        .one();

                if (maxRecord != null) {
                    log.info("【查询结果】找到历史记录 - maxRecordId: {}, maxBatch: {}, createDate: {}",
                            maxRecord.getId(), maxRecord.getBatch(), maxRecord.getCreateDate());

                    // 有历史记录,判断是否跨天
                    Date createDate = maxRecord.getCreateDate();

                    // ✅ 空值检查
                    if (createDate == null) {
                        log.error("【数据异常】历史记录创建时间为空 - recordId: {}, 使用当前时间", maxRecord.getId());
                        createDate = new Date();
                    }

                    LocalDate maxDate = createDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                    LocalDate today = LocalDate.now();

                    log.info("【时间判断】最大批次日期: {}, 当前日期: {}, 是否同一天: {}", maxDate, today, maxDate.isEqual(today));

                    if (!today.isEqual(maxDate)) {
                        // 跨天,新批次 = 最大批次 + 1
                        Integer maxBatch = maxRecord.getBatch();

                        // ✅ 空值检查
                        if (maxBatch == null) {
                            log.error("【数据异常】历史记录批次号为空 - recordId: {}, 设置为0", maxRecord.getId());
                            maxBatch = 0;
                        }

                        Integer newBatch = maxBatch + 1;
                        tHazardConditionConfirmation.setBatch(newBatch);
                        log.info("【批次设置】跨天新增 - 原批次: {}, 新批次: {}", maxBatch, newBatch);
                    } else {
                        // 同一天,不允许新增
                        log.error("【批次设置失败】同一天重复新增 - planId: {}, workTypeId: {}, 已存在批次: {}",
                                tHazardConditionConfirmationDTO.getPlanId(),
                                tHazardConditionConfirmationDTO.getWorkTypeId(),
                                maxRecord.getBatch());
                        throw new ServiceException("同一天只能新增一次同作业类型的安全条件确认");
                    }
                } else {
                    // ✅ 关键修复:没有历史记录时,设置初始批次为1
                    tHazardConditionConfirmation.setBatch(1);
                    log.info("【批次设置】首次新增 - 设置初始批次为: 1");
                }
            } else {
                log.info("【操作类型】更新操作 - id: {}", id);

                // 更新操作逻辑保持不变
                THazardConditionConfirmation existingEntity = this.getById(id);
                log.info("【查询结果】现有记录: {}", existingEntity);

                if (existingEntity != null) {
                    Integer batch = existingEntity.getBatch();
                    log.info("【现有批次】: {}", batch);

                    // 检查是否有同一批次但不同ID的记录
                    long count = this.lambdaQuery()
                            .eq(THazardConditionConfirmation::getPlanId, tHazardConditionConfirmationDTO.getPlanId())
                            .eq(THazardConditionConfirmation::getWorkTypeId, tHazardConditionConfirmationDTO.getWorkTypeId())
                            .eq(THazardConditionConfirmation::getBatch, batch)
                            .ne(THazardConditionConfirmation::getId, id)
                            .count();

                    log.info("【重复检查】同一批次其他记录数: {}", count);

                    if (count > 0) {
                        log.error("【更新失败】存在重复记录 - planId: {}, workTypeId: {}, batch: {}",
                                tHazardConditionConfirmationDTO.getPlanId(),
                                tHazardConditionConfirmationDTO.getWorkTypeId(),
                                batch);
                        throw new ServiceException("当前计划已存在该作业类型的安全条件确认");
                    }
                } else {
                    log.warn("【警告】未找到ID对应的记录 - id: {}", id);
                }
            }

            log.info("【保存前】准备保存 - id: {}, planId: {}, workTypeId: {}, batch: {}",
                    tHazardConditionConfirmation.getId(),
                    tHazardConditionConfirmation.getPlanId(),
                    tHazardConditionConfirmation.getWorkTypeId(),
                    tHazardConditionConfirmation.getBatch());

            // ✅ 保存主表 - 添加异常捕获和结果检查
            boolean saveResult = false;
            try {
                saveResult = this.saveOrUpdate(tHazardConditionConfirmation);
                log.info("【主表保存结果】: {}, 生成ID: {}", saveResult, tHazardConditionConfirmation.getId());

                if (!saveResult) {
                    log.error("【主表保存失败】planId: {}, workTypeId: {}",
                            tHazardConditionConfirmation.getPlanId(),
                            tHazardConditionConfirmation.getWorkTypeId());
                    throw new ServiceException("条件确认主表保存失败");
                }
            } catch (Exception e) {
                log.error("【主表保存异常】planId: {}, workTypeId: {}, 异常信息: {}",
                        tHazardConditionConfirmation.getPlanId(),
                        tHazardConditionConfirmation.getWorkTypeId(),
                        e.getMessage(), e);
                throw e;
            }

            // 删除之前的关联详情数据
            try {
                tHazardConditionConfirmationDetailsService.remove(new QueryWrapper<THazardConditionConfirmationDetails>()
                        .eq("condition_confirmation_id", tHazardConditionConfirmation.getId()));
                log.info("【删除旧详情】删除完成 - confirmationId: {}", tHazardConditionConfirmation.getId());
            } catch (Exception e) {
                log.error("【删除旧详情异常】confirmationId: {}, 异常: {}",
                        tHazardConditionConfirmation.getId(), e.getMessage(), e);
                throw e;
            }

            // ✅ 保存新的关联详情数据 - 添加空值检查
            List<THazardConditionConfirmationDetails> tHazardConditionConfirmationDetails =
                    tHazardConditionConfirmationDTO.getTHazardConditionConfirmationDetails();

            log.info("【保存新详情】详情数量: {}", tHazardConditionConfirmationDetails != null ? tHazardConditionConfirmationDetails.size() : 0);

            if (tHazardConditionConfirmationDetails != null && !tHazardConditionConfirmationDetails.isEmpty()) {
                try {
                    for (THazardConditionConfirmationDetails detail : tHazardConditionConfirmationDetails) {
                        detail.setConditionConfirmationId(tHazardConditionConfirmation.getId());
                        detail.setPlanId(tHazardConditionConfirmation.getPlanId());
                    }

                    boolean batchSaveResult = tHazardConditionConfirmationDetailsService.saveBatch(tHazardConditionConfirmationDetails);
                    log.info("【详情批量保存结果】: {}, 数量: {}", batchSaveResult, tHazardConditionConfirmationDetails.size());

                    if (!batchSaveResult) {
                        log.error("【详情保存失败】主表已保存,但详情保存失败 - confirmationId: {}, 详情数量: {}",
                                tHazardConditionConfirmation.getId(),
                                tHazardConditionConfirmationDetails.size());
                        throw new ServiceException("条件确认详情保存失败");
                    }
                } catch (Exception e) {
                    log.error("【详情保存异常】confirmationId: {}, 异常: {}",
                            tHazardConditionConfirmation.getId(), e.getMessage(), e);
                    throw e;
                }
            } else {
                log.warn("【详情数据为空】confirmationId: {}, 可能导致数据不完整", tHazardConditionConfirmation.getId());
            }

            log.info("【完成】安全条件确认保存成功 - 最终ID: {}, 批次: {}",
                    tHazardConditionConfirmation.getId(),
                    tHazardConditionConfirmation.getBatch());
            log.info("========== 结束保存或更新安全条件确认 ==========");

            return tHazardConditionConfirmation;

        } catch (ServiceException e) {
            log.error("【业务异常】保存失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("【系统异常】保存失败: {}", e.getMessage(), e);
            throw new ServiceException("保存条件确认失败: " + e.getMessage());
        }
    }




    /*@Override
    @Transactional(rollbackFor = Exception.class)
    public THazardConditionConfirmation saveOrUpdateEntity(THazardConditionConfirmationDTO tHazardConditionConfirmationDTO) {
        THazardConditionConfirmation tHazardConditionConfirmation = BeanHelper.beanToBean(tHazardConditionConfirmationDTO, THazardConditionConfirmation.class);
        log.info("保存或更新安全条件确认参数数据：{}", tHazardConditionConfirmation);
        String id = tHazardConditionConfirmationDTO.getId();

        // 查询条件包装器
        LambdaQueryWrapper<THazardConditionConfirmation> tHazardConditionConfirmationLambdaQueryWrapper = new LambdaQueryWrapper<THazardConditionConfirmation>()
                .eq(THazardConditionConfirmation::getPlanId, tHazardConditionConfirmationDTO.getPlanId())
                .eq(THazardConditionConfirmation::getWorkTypeId, tHazardConditionConfirmationDTO.getWorkTypeId())
                .orderByDesc(THazardConditionConfirmation::getBatch);
        log.info("tHazardConditionConfirmationLambdaQueryWrapper参数：{}", tHazardConditionConfirmationLambdaQueryWrapper);

        log.info("saveOrUpdateEntity之id值：{}", id);

        // 如果是新增操作
        if (StringUtils.isBlank(id)) {
            // 查询最大批次的记录
            List<THazardConditionConfirmation> hazardConditionConfirmations = this.list(tHazardConditionConfirmationLambdaQueryWrapper);
            log.info("saveOrUpdateEntity之最大批次的记录：{}", hazardConditionConfirmations);
            if (!CollectionUtils.isEmpty(hazardConditionConfirmations)) {
                THazardConditionConfirmation hazardConditionConfirmation = hazardConditionConfirmations.get(0);
                Date createDate = hazardConditionConfirmation.getCreateDate();
                LocalDateTime createDatelocalDateTime = DateUtil.dateToLocalDateTime(createDate);

                log.info("saveOrUpdateEntity之最大批次的记录创建时间：{}", createDatelocalDateTime);
                log.info("saveOrUpdateEntity之当前时间：{}", LocalDateTime.now());
                // 如果当前时间在最大批次的创建时间之后，增加批次
                if (DateUtil.notSameDayAndAfter(LocalDateTime.now(), createDatelocalDateTime)) {
                    tHazardConditionConfirmation.setBatch(hazardConditionConfirmation.getBatch() + 1);
                }else {
                    throw new ServiceException("同一天只能新增一次同作业类型的安全条件确认");
                }
            }
        }else {
            // 如果是更新操作，检查同一批次是否已有其他记录
            THazardConditionConfirmation existingEntity = this.getById(id);
            log.info("saveOrUpdateEntity之更新操作参数数据：{}", existingEntity);
            if (existingEntity != null) {
                Integer batch = existingEntity.getBatch();
                log.info("saveOrUpdateEntity之批次：{}", batch);
                // 检查是否有同一批次但不同ID的记录
                tHazardConditionConfirmationLambdaQueryWrapper.ne(THazardConditionConfirmation::getId, tHazardConditionConfirmationDTO.getId())
                        .eq(THazardConditionConfirmation::getBatch, batch);
                if (this.count(tHazardConditionConfirmationLambdaQueryWrapper) > 0) {
                    throw new RuntimeException("当前计划已存在该作业类型的安全条件确认");
                }
            }
        }

        log.info("保存或更新tHazardConditionConfirmation：{}", tHazardConditionConfirmation);
        // 保存或更新
        this.saveOrUpdate(tHazardConditionConfirmation);

        // 删除之前的关联详情数据
        tHazardConditionConfirmationDetailsService.remove(new QueryWrapper<THazardConditionConfirmationDetails>().eq("condition_confirmation_id", tHazardConditionConfirmation.getId()));

        // 保存新的关联详情数据
        List<THazardConditionConfirmationDetails> tHazardConditionConfirmationDetails = tHazardConditionConfirmationDTO.getTHazardConditionConfirmationDetails();
        for (THazardConditionConfirmationDetails detail : tHazardConditionConfirmationDetails) {
            detail.setConditionConfirmationId(tHazardConditionConfirmation.getId());
            detail.setPlanId(tHazardConditionConfirmation.getPlanId());
        }
        tHazardConditionConfirmationDetailsService.saveBatch(tHazardConditionConfirmationDetails);  // 使用批量保存方法提高性能

        return tHazardConditionConfirmation;
    }*/

    /*@Override
    @Transactional(rollbackFor = Exception.class)
    public THazardConditionConfirmation saveOrUpdateEntity(THazardConditionConfirmationDTO tHazardConditionConfirmationDTO) {
        THazardConditionConfirmation tHazardConditionConfirmation = BeanHelper.beanToBean(tHazardConditionConfirmationDTO, THazardConditionConfirmation.class);
        log.info("保存或更新安全条件确认参数数据：{}", tHazardConditionConfirmation);
        String id = tHazardConditionConfirmationDTO.getId();

        // 查询条件包装器
        *//*LambdaQueryWrapper<THazardConditionConfirmation> tHazardConditionConfirmationLambdaQueryWrapper = new LambdaQueryWrapper<THazardConditionConfirmation>()
                .eq(THazardConditionConfirmation::getPlanId, tHazardConditionConfirmationDTO.getPlanId())
                .eq(THazardConditionConfirmation::getWorkTypeId, tHazardConditionConfirmationDTO.getWorkTypeId())
                .orderByDesc(THazardConditionConfirmation::getBatch);
        log.info("tHazardConditionConfirmationLambdaQueryWrapper参数：{}", tHazardConditionConfirmationLambdaQueryWrapper);

        log.info("saveOrUpdateEntity之id值：{}", id);

        // 如果是新增操作
        if (StringUtils.isBlank(id)) {
            // 查询最大批次的记录
            List<THazardConditionConfirmation> hazardConditionConfirmations = this.list(tHazardConditionConfirmationLambdaQueryWrapper);
            log.info("saveOrUpdateEntity之最大批次的记录：{}", hazardConditionConfirmations);
            if (!CollectionUtils.isEmpty(hazardConditionConfirmations)) {
                THazardConditionConfirmation hazardConditionConfirmation = hazardConditionConfirmations.get(0);
                Date createDate = hazardConditionConfirmation.getCreateDate();
                LocalDateTime createDatelocalDateTime = DateUtil.dateToLocalDateTime(createDate);

                log.info("saveOrUpdateEntity之最大批次的记录创建时间：{}", createDatelocalDateTime);
                log.info("saveOrUpdateEntity之当前时间：{}", LocalDateTime.now());
                // 如果当前时间在最大批次的创建时间之后，增加批次
                if (DateUtil.notSameDayAndAfter(LocalDateTime.now(), createDatelocalDateTime)) {
                    tHazardConditionConfirmation.setBatch(hazardConditionConfirmation.getBatch() + 1);
                }else {
                    throw new ServiceException("同一天只能新增一次同作业类型的安全条件确认");
                }
            }
        }else {
            // 如果是更新操作，检查同一批次是否已有其他记录
            THazardConditionConfirmation existingEntity = this.getById(id);
            log.info("saveOrUpdateEntity之更新操作参数数据：{}", existingEntity);
            if (existingEntity != null) {
                Integer batch = existingEntity.getBatch();
                log.info("saveOrUpdateEntity之批次：{}", batch);
                // 检查是否有同一批次但不同ID的记录
                tHazardConditionConfirmationLambdaQueryWrapper.ne(THazardConditionConfirmation::getId, tHazardConditionConfirmationDTO.getId())
                        .eq(THazardConditionConfirmation::getBatch, batch);
                if (this.count(tHazardConditionConfirmationLambdaQueryWrapper) > 0) {
                    throw new RuntimeException("当前计划已存在该作业类型的安全条件确认");
                }
            }
        }*//*

        // 查询同一计划、作业类型的最大批次
        Integer maxBatch = this.lambdaQuery()
                .eq(THazardConditionConfirmation::getPlanId, tHazardConditionConfirmationDTO.getPlanId())
                .eq(THazardConditionConfirmation::getWorkTypeId, tHazardConditionConfirmationDTO.getWorkTypeId())
                .select(THazardConditionConfirmation::getBatch, THazardConditionConfirmation::getCreateDate)
                .orderByDesc(THazardConditionConfirmation::getBatch)
                .last("LIMIT 1")
                .oneOpt()
                .map(THazardConditionConfirmation::getBatch)
                .orElse(0);

        // 新增操作
        if (StringUtils.isBlank(id)) {
            if (maxBatch > 0) {
                // 查询最大批次的创建日期
                THazardConditionConfirmation maxRecord = this.lambdaQuery()
                        .eq(THazardConditionConfirmation::getPlanId, tHazardConditionConfirmationDTO.getPlanId())
                        .eq(THazardConditionConfirmation::getWorkTypeId, tHazardConditionConfirmationDTO.getWorkTypeId())
                        .eq(THazardConditionConfirmation::getBatch, maxBatch)
                        .orderByDesc(THazardConditionConfirmation::getCreateDate)
                        .last("LIMIT 1")
                        .one();

                LocalDate maxDate = maxRecord.getCreateDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                LocalDate today = LocalDate.now();

                if (!today.isEqual(maxDate)) {
                    // 跨天，新批次 = 最大批次 + 1
                    tHazardConditionConfirmation.setBatch(maxBatch + 1);
                } else {
                    throw new ServiceException("同一天只能新增一次同作业类型的安全条件确认");
                }
            } else {
                tHazardConditionConfirmation.setBatch(1);
            }
        } else {
            // 更新操作逻辑
            THazardConditionConfirmation existingEntity = this.getById(id);
            if (existingEntity != null) {
                Integer batch = existingEntity.getBatch();
                // 检查同一批次是否有重复
                long count = this.lambdaQuery()
                        .eq(THazardConditionConfirmation::getPlanId, tHazardConditionConfirmationDTO.getPlanId())
                        .eq(THazardConditionConfirmation::getWorkTypeId, tHazardConditionConfirmationDTO.getWorkTypeId())
                        .eq(THazardConditionConfirmation::getBatch, batch)
                        .ne(THazardConditionConfirmation::getId, id)
                        .count();
                if (count > 0) {
                    throw new ServiceException("当前计划已存在该作业类型的安全条件确认");
                }
            }
        }

        log.info("保存或更新tHazardConditionConfirmation：{}", tHazardConditionConfirmation);
        // 保存或更新
        this.saveOrUpdate(tHazardConditionConfirmation);

        // 删除之前的关联详情数据
        tHazardConditionConfirmationDetailsService.remove(new QueryWrapper<THazardConditionConfirmationDetails>().eq("condition_confirmation_id", tHazardConditionConfirmation.getId()));

        // 保存新的关联详情数据
        List<THazardConditionConfirmationDetails> tHazardConditionConfirmationDetails = tHazardConditionConfirmationDTO.getTHazardConditionConfirmationDetails();
        for (THazardConditionConfirmationDetails detail : tHazardConditionConfirmationDetails) {
            detail.setConditionConfirmationId(tHazardConditionConfirmation.getId());
            detail.setPlanId(tHazardConditionConfirmation.getPlanId());
        }
        tHazardConditionConfirmationDetailsService.saveBatch(tHazardConditionConfirmationDetails);  // 使用批量保存方法提高性能

        return tHazardConditionConfirmation;
    }*/

    @Override
    public THazardConditionConfirmationDTO getDetailsById(String planId, String workTypeId, String batch) {
        THazardConditionConfirmation tHazardConditionConfirmation = this.getOne(new LambdaQueryWrapper<THazardConditionConfirmation>()
                .eq(THazardConditionConfirmation::getPlanId, planId)
                .eq(THazardConditionConfirmation::getWorkTypeId, workTypeId)
                .eq(THazardConditionConfirmation::getBatch, batch)
        );
        if (tHazardConditionConfirmation == null) {
            return null;
        }
        THazardWorkPlan tHazardWorkPlan = tHazardWorkPlanService.getById(tHazardConditionConfirmation.getPlanId());
        THazardConditionConfirmationDTO tHazardConditionConfirmationDTO = BeanHelper.beanToBean(tHazardConditionConfirmation, THazardConditionConfirmationDTO.class);
        tHazardConditionConfirmationDTO.setTHazardConditionConfirmationDetails(tHazardConditionConfirmationDetailsService.list(new QueryWrapper<THazardConditionConfirmationDetails>().eq("condition_confirmation_id", tHazardConditionConfirmation.getId())));
        //作业单位
        tHazardConditionConfirmationDTO.setWorkUnitName(tHazardWorkPlanService.getOrgNamesByOrgId(tHazardWorkPlan.getWorkUnit()));
        //作业现场负责人
        tHazardConditionConfirmationDTO.setWorkSiteResponsibleName(tHazardWorkPlanService.getUserNamesByUserIds(tHazardWorkPlan.getWorkSiteResponsible()));
        //作业地点
        tHazardConditionConfirmationDTO.setWorkLocation(tHazardWorkPlan.getWorkLocation());
        //监护人
        tHazardConditionConfirmationDTO.setGuardianName(tHazardWorkPlanService.getUserNamesByUserIds(tHazardWorkPlan.getGuardian()));
        //作业级别
        tHazardConditionConfirmationDTO.setWorkLevelName(tHazardWorkPlan.getWorkLevel());
        //安全管理部门人员名称
        tHazardConditionConfirmationDTO.setDepartmentName(tHazardWorkPlanService.getUserNamesByUserIds(tHazardConditionConfirmation.getDepartment()));
        return tHazardConditionConfirmationDTO;
    }

    @XxlJob("hazardousWorkDailyCheck")
    @Override
    public void hazardousWorkDailyCheck(String todayStr){
        LocalDate today = LocalDate.now();
        if (StringUtils.isBlank(todayStr)) {
            todayStr = today.format(DateTimeFormatter.ISO_DATE);
        }
        //查询所有未完成的工作计划,且状态不为关闭或取消或已超时(关闭时判断结束时间在当前时间之前设置的状态)
        /*THazardWorkPlanParam tHazardWorkPlanParam = new THazardWorkPlanParam();
        tHazardWorkPlanParam.setActualStartTime(new Date());*/
        QueryWrapper<THazardWorkPlan> query = new QueryWrapper<>();
        query.le(THazardWorkPlan.ACTUAL_START_TIME, new Date());
        query.isNotNull(THazardWorkPlan.ACTUAL_START_TIME);
        query.ne(THazardWorkPlan.WORK_STATUS, WorkPlanStatusEnum.CLOSED.getValue());
        query.ne(THazardWorkPlan.WORK_STATUS, WorkPlanStatusEnum.CANCELED.getValue());
        query.ne(THazardWorkPlan.WORK_STATUS, WorkPlanStatusEnum.OVERDUE.getValue());
        query.ne(THazardWorkPlan.WORK_STATUS, WorkPlanStatusEnum.DRAFT.getValue());
        List<THazardWorkPlan> tHazardWorkPlans = tHazardWorkPlanService.list(query);
        for (THazardWorkPlan tHazardWorkPlan : tHazardWorkPlans) {
            List<THazardConditionConfirmationRecord> tHazardConditionConfirmations = tHazardConditionConfirmationRecordService.list(new QueryWrapper<THazardConditionConfirmationRecord>()
                    .eq(THazardConditionConfirmationRecord.PLAN_ID, tHazardWorkPlan.getId())
                    .eq(THazardConditionConfirmationRecord.STATUS, "0")
                    .likeRight("create_date", todayStr)
                    .orderByDesc("create_date")
            );
            if(tHazardConditionConfirmations.isEmpty()){
                ProcessInsStartParams processInsStartParams = new ProcessInsStartParams();
                processInsStartParams.setBizKey(tHazardWorkPlan.getId());
                processInsStartParams.setProcessDefinitionKey("hazardousWorkDailyCheck");
                processInsStartParams.setOrgId(tHazardWorkPlan.getOrgId());
                processInsStartParams.setUserId(tHazardWorkPlan.getCreateBy());
                Map<String, Object> varMap = new HashMap<>();
                varMap.put("workSiteResponsible",getSubstringAfterDash(tHazardWorkPlan.getWorkSiteResponsible()));
                processInsStartParams.setVariables(varMap);
                ProcessInsStartResult processInsStartResult = tfwProcessInsService.startProcessInsByKey(processInsStartParams);
                String id = IdHelper.getId32bit();
                THazardConditionConfirmationRecord tHazardConditionConfirmationRecord = new THazardConditionConfirmationRecord();
                tHazardConditionConfirmationRecord.setId(id);
                tHazardConditionConfirmationRecord.setPlanId(tHazardWorkPlan.getId());
                tHazardConditionConfirmationRecord.setProcessId(processInsStartResult.getCode());
                tHazardConditionConfirmationRecordService.save(tHazardConditionConfirmationRecord);
            }
        }
    }

    /**
     * 提取 0-id,1-id ，格式中的id字段 返回如 id1,id2
     * @param input 需要转化的id
     * @return
     */
    public String getSubstringAfterDash(String input) {
        String[] split = input.split(",");
        List<String> ids = new ArrayList<>();
        // 查找"-"的位置
        for (String id : split) {
            int index = id.indexOf("-");
            // 如果找到"-"，则截取"-"后的部分
            if (index != -1) {
                ids.add(id.substring(index + 1));  // 截取"-"后的部分
            }
        }

        // 如果没有"-"，返回原字符串
        return StringUtils.join(ids,",");
    }
}
