package com.yeejoin.amos.boot.module.jg.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.olym.shaded.org.apache.commons.collections4.MapUtils;
import com.yeejoin.amos.boot.biz.common.annotation.ResultFieldMapping;
import com.yeejoin.amos.boot.biz.common.bo.CompanyBo;
import com.yeejoin.amos.boot.biz.common.bo.ReginParams;
import com.yeejoin.amos.boot.biz.common.entity.BaseEntity;
import com.yeejoin.amos.boot.biz.common.utils.DateUtils;
import com.yeejoin.amos.boot.biz.common.utils.RedisKey;
import com.yeejoin.amos.boot.biz.common.utils.RedisUtils;
import com.yeejoin.amos.boot.biz.common.utils.SnowflakeIdUtil;
import com.yeejoin.amos.boot.module.common.api.enums.CylinderTypeEnum;
import com.yeejoin.amos.boot.module.common.biz.refresh.DataRefreshEvent;
import com.yeejoin.amos.boot.module.jg.api.dto.*;
import com.yeejoin.amos.boot.module.jg.api.entity.*;
import com.yeejoin.amos.boot.module.jg.api.enums.*;
import com.yeejoin.amos.boot.module.jg.api.mapper.*;
import com.yeejoin.amos.boot.module.jg.api.service.IJgChangeRegistrationUnitService;
import com.yeejoin.amos.boot.module.jg.api.vo.SortVo;
import com.yeejoin.amos.boot.module.jg.biz.config.LocalBadRequest;
import com.yeejoin.amos.boot.module.jg.biz.context.EquipUsedCheckStrategyContext;
import com.yeejoin.amos.boot.module.jg.biz.context.FlowingEquipRedisContext;
import com.yeejoin.amos.boot.module.jg.biz.edit.permission.FillingEditPermForCurrentUser;
import com.yeejoin.amos.boot.module.jg.biz.event.publisher.EventPublisher;
import com.yeejoin.amos.boot.module.jg.biz.feign.TzsServiceFeignClient;
import com.yeejoin.amos.boot.module.jg.biz.service.ICommonService;
import com.yeejoin.amos.boot.module.jg.biz.service.ICompensateFlowDataOfRedis;
import com.yeejoin.amos.boot.module.ymt.api.entity.*;
import com.yeejoin.amos.boot.module.ymt.api.enums.ApplicationFormTypeEnum;
import com.yeejoin.amos.boot.module.ymt.api.enums.EquimentEnum;
import com.yeejoin.amos.boot.module.ymt.api.enums.FlowStatusEnum;
import com.yeejoin.amos.boot.module.ymt.api.mapper.*;
import com.yeejoin.amos.component.feign.model.FeignClientResult;
import com.yeejoin.amos.feign.privilege.Privilege;
import com.yeejoin.amos.feign.privilege.model.AgencyUserModel;
import com.yeejoin.amos.feign.privilege.model.CompanyModel;
import com.yeejoin.amos.feign.systemctl.model.TaskV2Model;
import com.yeejoin.amos.feign.workflow.Workflow;
import com.yeejoin.amos.feign.workflow.model.*;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang3.ObjectUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.typroject.tyboot.core.foundation.context.RequestContext;
import org.typroject.tyboot.core.foundation.utils.ValidationUtil;
import org.typroject.tyboot.core.rdbms.service.BaseService;
import org.typroject.tyboot.core.restful.exception.instance.BadRequest;
import org.typroject.tyboot.core.restful.utils.ResponseModel;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.yeejoin.amos.boot.module.jg.biz.service.impl.JgUseRegistrationServiceImpl.getAuditPassedDate;

/**
 * 单位变更登记服务实现类
 *
 * @author system_generator
 * @date 2023-12-22
 */
@Service
public class JgChangeRegistrationUnitServiceImpl extends BaseService<JgChangeRegistrationUnitDto, JgChangeRegistrationUnit, JgChangeRegistrationUnitMapper> implements IJgChangeRegistrationUnitService, ICompensateFlowDataOfRedis<JgChangeRegistrationUnit> {
    private static final String SUBMIT_TYPE_FLOW = "1";
    private static final String PROCESS_DEFINITION_KEY = "unitChange";
    private static final String TABLE_PAGE_ID = "1734141426742095873";
    private static final String RECORD = "record";
    private static final String SEQUENCE_NBR = "sequenceNbr";
    private static final String EQU_CODE_CC = "5000";
    private final List<String> NOT_FLOWING_STATE = Arrays.asList("使用单位待提交", "一级受理已驳回", "使用单位已撤回", "已作废", "已完成");


    @Autowired
    EquipmentCategoryMapper equipmentCategoryMapper;
    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    JgChangeRegistrationUnitEqMapper JgChangeRegistrationUnitEqMapper;

    @Autowired
    JgChangeRegistrationUnitEqServiceImpl registrationUnitEqService;

    @Autowired
    CmWorkflowServiceImpl cmWorkflowService;
    @Autowired
    private JgChangeRegistrationUnitMapper JgChangeRegistrationUnitMapper;
    @Autowired
    private IdxBizJgRegisterInfoMapper idxBizJgRegisterInfoMapper;
    @Autowired
    RegistrationInfoMapper tzsJgRegistrationInfoMapper;
    @Autowired
    OtherInfoMapper tzsJgOtherInfoMapper;
    @Autowired
    IdxBizJgUseInfoMapper useInfoMapper;
    @Autowired
    IdxBizJgRegisterInfoMapper registerInfoMapper;
    @Autowired
    TzsServiceFeignClient tzsServiceFeignClient;

    @Autowired
    CommonServiceImpl commonServiceImpl;


    @Autowired
    private JgRegistrationHistoryMapper jgRegistrationHistoryMapper;

    @Autowired
    private JgScrapCancelMapper jgScrapCancelMapper;

    @Autowired
    private JgScrapCancelEqMapper jgScrapCancelEqMapper;

    @Autowired
    private SuperviseInfoMapper superviseInfoMapper;

    @Autowired
    private IdxBizJgUseInfoMapper idxBizJgUseInfoMapper;

    @Autowired
    private JgUseRegistrationManageMapper jgUseRegistrationManageMapper;

    @Autowired
    private JgCertificateChangeRecordEqServiceImpl jgCertificateChangeRecordEqServiceImpl;

    //改造登记关系表mapper
    @Autowired
    private JgChangeRegistrationReformEqMapper jgChangeRegistrationReformEqMapper;
    //移装登记关系表
    @Autowired
    private JgChangeRegistrationTransferEqMapper jgChangeRegistrationTransferEqMapper;
    //使用登记关系表mapper
    @Autowired
    private JgUseRegistrationEqMapper jgUseRegistrationEqMapper;
    //单位变更登记关系表mapper
    @Autowired
    private JgChangeRegistrationUnitEqMapper jgChangeRegistrationUnitEqMapper;
    //更名变更登记关系表mapper
    @Autowired
    private JgChangeRegistrationNameEqMapper jgChangeRegistrationNameEqMapper;

    @Autowired
    private IdxBizJgUseInfoServiceImpl idxBizJgUseInfoService;
    @Autowired
    private IdxBizJgRegisterInfoServiceImpl idxBizJgRegisterInfoService;
    @Autowired
    private IdxBizJgFactoryInfoServiceImpl idxBizJgFactoryInfoService;

    @Autowired
    ICommonService commonService;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    JgUseRegistrationManageServiceImpl registrationManageService;
    @Autowired
    JgRegistrationHistoryServiceImpl registrationHistoryService;

    @Autowired
    private SnowflakeIdUtil sequence;

    @Autowired
    JgCertificateChangeRecordServiceImpl certificateChangeRecordService;

    @Autowired
    JgCertificateChangeRecordEqServiceImpl certificateChangeRecordEqService;

    @Autowired
    JgUseRegistrationManageServiceImpl useRegistrationManageService;

    @Autowired
    private TzBaseEnterpriseInfoMapper tzBaseEnterpriseInfoMapper;
    @Autowired
    private IdxBizJgProjectContraptionServiceImplService projectContraptionService;

    @Autowired
    private EventPublisher eventPublisher;

    @Autowired
    private JgResumeInfoServiceImpl jgResumeInfoService;
    @Autowired
    private IdxBizJgSupervisionInfoMapper idxBizJgSupervisionInfoMapper;

    /**
     * 根据sequenceNbr查询：1、查询单位变更信息，2、查询使用登记证列表
     *
     * @param sequenceNbr     主键
     * @param selectedOrgInfo
     * @return 单位变更
     */
    @Override
    @ResultFieldMapping({@ResultFieldMapping.ResultFieldMap(sourceField = "changeRegisInfo.useUnitCreditCode", targetField = "changeRegisInfo.useUnitSeq", serviceClass = CommonServiceImpl.class, queryMethod = "queryUnitInfoByCreditCode")})
    public Map<String, Map<String, Object>> queryBySequenceNbr(Long sequenceNbr, ReginParams selectedOrgInfo) {
        // 单位变更信息
        JgChangeRegistrationUnit notice = JgChangeRegistrationUnitMapper.selectById(sequenceNbr);
        if (Objects.isNull(notice)) {
            return null;
        }
        this.doCompensate(notice);
        Map<String, Object> changeInfo = BeanUtil.beanToMap(notice, false, true);
        //回填新单位地址，台套办理的使用登记证打印时 需要，用来校验必输
        if (!ObjectUtils.isEmpty(notice.getNewUseUnitCreditCode())) {
            TzBaseEnterpriseInfo useCodeResult = tzBaseEnterpriseInfoMapper.selectOne(new LambdaQueryWrapper<TzBaseEnterpriseInfo>().eq(TzBaseEnterpriseInfo::getUseUnitCode, notice.getNewUseUnitCreditCode()));
            if (useCodeResult != null) {
                changeInfo.put("address", useCodeResult.getAddress());
            }
        }
        //组装下拉数据
        if (!ValidationUtil.isEmpty(notice.getReceiveOrgCode()) && !ValidationUtil.isEmpty(notice.getReceiveOrgName())) {
            changeInfo.put("receiveOrgCode", notice.getReceiveOrgCode() + "_" + notice.getReceiveOrgName());
        }
        if (!ValidationUtil.isEmpty(notice.getReceiveOrgCode()) && !ValidationUtil.isEmpty(notice.getReceiveOrgName())) {
            changeInfo.put("orgBranchCode", notice.getOrgBranchCode() + "_" + notice.getOrgBranchName());
        }
        if (!ValidationUtil.isEmpty(notice.getUseUnitCreditCode()) && !ValidationUtil.isEmpty(notice.getUseUnitName())) {
            changeInfo.put("useUnitCreditCode", notice.getUseUnitCreditCode() + "_" + notice.getUseUnitName());
        }
        if (!ValidationUtil.isEmpty(notice.getNewUseUnitCreditCode()) && !ValidationUtil.isEmpty(notice.getNewUseUnitName())) {
            changeInfo.put("newUseUnitCreditCode", notice.getNewUseUnitCreditCode() + "_" + notice.getNewUseUnitName());
        }
        //附件格式处理
        changeInfo.put("changeCertificateList", JSON.parse(notice.getChangeCertificate()));
        changeInfo.put("otherAccessoriesList", JSON.parse(notice.getOtherAccessories()));
        //查询使用登记证列表
        JgRegistrationHistory historyServiceOne = registrationHistoryService.getOne(
                new LambdaQueryWrapper<JgRegistrationHistory>()
                        .eq(JgRegistrationHistory::getCurrentDocumentId, notice.getApplyNo())
                        .eq(JgRegistrationHistory::getIsDelete, false));
        if (PipelineEnum.PRESSURE_PIPELINE.getCode().equals(notice.getEquListCode())) {
            //添加申请编号
            JSONArray jsonArray = JSON.parseArray(historyServiceOne.getChangeData());
            if (!CollectionUtils.isEmpty(jsonArray)) {
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i); // 直接获取 JSONObject
                    jsonObject.put("applyNo", notice.getApplyNo());
                }
                changeInfo.put("projectContraptionList", jsonArray);
            }
        }
        if (CylinderTypeEnum.CYLINDER.getCode().equals(notice.getEquCategoryCode())) {
            changeInfo.put("deviceList", JSON.parse(historyServiceOne.getChangeData()));
            if (!ValidationUtil.isEmpty(notice.getSafetyManager())) {
                changeInfo.put("safetyManagerName", notice.getSafetyManager().split("_")[1]);
            }
        } else {
            changeInfo.put("registrationList", JSON.parse(historyServiceOne.getChangeData()));
        }
        changeInfo.put("havingPermission", notice.getNextExecuteUserIds() != null && notice.getNextExecuteUserIds().contains(RequestContext.getExeUserId()));
        String companyType = selectedOrgInfo.getCompany().getCompanyType();
        // todo 1 公共权限赋值查询
        new FillingEditPermForCurrentUser(new JSONObject(changeInfo), selectedOrgInfo.getCompany(), BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION).getData();
        // todo 注意顺序 通用的FillingEditPermForCurrentUser里有companyType了 但是和此业务不一样，故还是使用原来的，否则页面需要调整较多
        if ("个人主体".equals(companyType)) {
            changeInfo.put("companyType", "person");
        } else {
            changeInfo.put("companyType", "company");
        }
        // todo 本来FillingEditPermForCurrentUser已赋值但是此业务也使用的key companyType故新赋值key
        changeInfo.put("companyType2", CommonServiceImpl.getCompanyType(selectedOrgInfo.getCompany()));
        return new HashMap<String, Map<String, Object>>() {{
//            this.put("changeRegisInfo", fillEquipInfoWithRuntimeOrHistoryData(notice, changeInfo));
            this.put("changeRegisInfo", changeInfo);
        }};
    }

    private Map<String, Object> fillEquipInfoWithRuntimeOrHistoryData(JgChangeRegistrationUnit notice, Map<String, Object> changeInfo) {
        // 已完成时显示历史数据
        if (notice.getStatus().equals(FlowStatusEnum.TO_BE_FINISHED.getName())) {
            JSONObject his = commonService.queryHistoryData(notice.getSequenceNbr());
            // 兼容老数据
            if (his == null) {
                return getEquipInfo(notice.getSequenceNbr(), changeInfo);
            }
            CommonServiceImpl.formatTime2StrDateForEquip(his);
            his.putAll(changeInfo);
            return his;
        }
        // 非完成状态返回实时使用登记证数据
        return getEquipInfo(notice.getSequenceNbr(), changeInfo);
    }


    private Map<String, Object> getEquipInfo(Long sequenceNbr, Map<String, Object> changeInfo) {
        String[] fields = {"productPhoto", "designDoc", "designStandard", "factoryStandard",
                "productQualityYieldProve", "insUseMaintainExplain", "inspectReport", "otherAccessoriesList",
                "proxyStatementAttachment", "installContractAttachment", "changeCertificateList"};

        // 设备信息
        List<Map<String, Object>> equipmentInfos = JgChangeRegistrationUnitMapper.queryEquipInformation(sequenceNbr);
        String equipId = JgChangeRegistrationUnitMapper.queryEquipIdBySeq(sequenceNbr);
        Map<String, Object> detailFieldCamelCaseByRecord = idxBizJgRegisterInfoService.getDetailFieldCamelCaseByRecord(equipId);
        detailFieldCamelCaseByRecord.remove("useUnitCreditCode");


        for (String s : fields) {
            if (changeInfo.containsKey(s) && !ObjectUtils.isEmpty(changeInfo.get(s))) {
                changeInfo.put(s, JSON.parseArray(changeInfo.get(s).toString()));
            }
            if (equipmentInfos.get(0).containsKey(s) && !ObjectUtils.isEmpty(equipmentInfos.get(0).get(s))) {
                equipmentInfos.get(0).put(s, JSON.parseArray(equipmentInfos.get(0).get(s).toString()));
            }
        }

        Map<String, Object> newMap = new HashMap<>();
        newMap.putAll(detailFieldCamelCaseByRecord);
        newMap.putAll(changeInfo);
        newMap.putAll(equipmentInfos.get(0));
        return newMap;
    }

    /**
     * 更新单位变更
     *
     * @param noticeDto 单位变更
     */
    @GlobalTransactional(rollbackFor = Exception.class)
    public JgChangeRegistrationUnitDto updateInstallationNotice(String submitType, JgChangeRegistrationUnitDto noticeDto, String op, ReginParams reginParams) {
        try {
            if (Objects.isNull(noticeDto) || StringUtils.isEmpty(submitType)) {
                throw new IllegalArgumentException("参数不能为空");
            }
            if (PipelineEnum.PRESSURE_PIPELINE.getCode().equals(noticeDto.getEquListCode())) {
                dealProjectContraptionToUpdate(submitType, noticeDto, op, reginParams);
            } else if (CylinderTypeEnum.CYLINDER.getCode().equals(noticeDto.getEquCategoryCode())) {
                dealCylinderToUpdate1(submitType, noticeDto, op, reginParams);
            } else {
                // 字段转换
                this.convertField(noticeDto);
                // 获取单位变更使用登记证列表
                List<Map<String, Object>> registrationList = noticeDto.getRegistrationList();
                if (CollectionUtils.isEmpty(registrationList)) {
                    throw new BadRequest("使用登记证不能为空");
                }
                //根据使用登记证查询设备
                List<Long> registrationIdList = registrationList.stream().filter(v -> v.containsKey(SEQUENCE_NBR) &&
                                ObjectUtils.isNotEmpty(v.get(SEQUENCE_NBR)) && canConvertToLong(v.get(SEQUENCE_NBR).toString()))
                        .map(v -> Long.parseLong(v.get(SEQUENCE_NBR).toString())).collect(Collectors.toList());
                // 获取单位变更设备列表
                if (CollectionUtils.isEmpty(registrationIdList)) {
                    throw new BadRequest("使用登记证不能为空");
                }
                List<JSONObject> deviceList = registrationManageService.queryEquByCertificateSeqList(registrationIdList);
                // 获取单位变更设备列表
                if (CollectionUtils.isEmpty(deviceList)) {
                    throw new BadRequest("使用登记证下无设备");
                }
                //场车区分区外还是区内
                if (EQU_CODE_CC.equals(noticeDto.getEquListCode())) {
                    String city = Objects.toString(noticeDto.getCity(), "");
                    String userCity = Optional.ofNullable(city).map(c -> c.split("_")[0]).orElseThrow(() -> new BadRequest("请选择地市后暂存！"));
                    this.validateDeviceCity(deviceList, userCity, "0".equals(noticeDto.getChangeType()));
                    this.setEquAddress(noticeDto);
                }
                ArrayList<String> roleListAll = new ArrayList<>();
                ArrayList<String> roleListNext = new ArrayList<>();
                if (SUBMIT_TYPE_FLOW.equals(submitType)) {
                    // 发起流程
                    if (!StringUtils.hasText(noticeDto.getInstanceId())) {
                        List<ProcessTaskDTO> processTasks = new ArrayList<>();
                        ActWorkflowBatchDTO actWorkflowBatchDTO = new ActWorkflowBatchDTO();
                        List<ActWorkflowStartDTO> list = new ArrayList<>();
                        ActWorkflowStartDTO dto = new ActWorkflowStartDTO();
                        dto.setProcessDefinitionKey(PROCESS_DEFINITION_KEY);
                        dto.setBusinessKey(String.valueOf(new Date().getTime()));
                        dto.setCompleteFirstTask(true);
                        list.add(dto);
                        actWorkflowBatchDTO.setProcess(list);
                        dto.setNextExecuteUserCompanyCode(noticeDto.getReceiveCompanyCode());
                        processTasks = cmWorkflowService.startBatch(actWorkflowBatchDTO);
                        String instanceId = processTasks.get(0).getProcessInstance().getId();
                        String nextTaskId = processTasks.get(0).getNextTask().get(0).getId();
                        String taskname = processTasks.get(0).getNextTask().get(0).getName();
                        this.buildRoleList(processTasks, roleListNext, roleListAll);
                        List<WorkflowResultDto> workflowResultDtos = commonServiceImpl.buildWorkFlowInfo(processTasks);
                        String nextUserIds = workflowResultDtos.get(0).getNextExecutorUserIds();

                        JgChangeRegistrationUnit notice = new JgChangeRegistrationUnit();
                        BeanUtils.copyProperties(noticeDto, notice);
                        notice.setChangeCertificate(noticeDto.getChangeCertificate());
                        notice.setRemark(noticeDto.getRemark());
                        if (!ObjectUtils.isEmpty(notice.getInstanceStatus())) {
                            notice.setInstanceStatus(notice.getInstanceStatus() + "," + roleListAll);
                        } else {
                            notice.setInstanceStatus(String.join(",", roleListAll));
                        }
                        notice.setInstanceId(instanceId);
                        notice.setNextTaskId(nextTaskId);
                        notice.setSupervisoryCode(noticeDto.getSupervisoryCode());
                        notice.setPromoter(RequestContext.getExeUserId());
                        notice.setNextExecutorIds(String.join(",", roleListNext));
                        notice.setStatus(WorkFlowStatusEnum.UNITCHANGE_RECEIVE.getPass());
                        notice.setNextExecuteUserIds(nextUserIds);
                        setNewUnitInfo(reginParams, notice);
                        JgChangeRegistrationUnitMapper.updateById(notice);
                        commonServiceImpl.deleteTasksByRelationId(notice.getSequenceNbr() + "");
                        createTaskModel(notice, taskname, "1", nextUserIds);
                        commonServiceImpl.saveExecuteFlowData2Redis(notice.getInstanceId(), buildInstanceRuntimeData(notice));
                    } else {
                        ArrayList<String> roleList = new ArrayList<>();
                        String taskId = noticeDto.getNextTaskId();
                        //组装信息
                        TaskResultDTO task = new TaskResultDTO();
                        task.setResultCode("approvalStatus");
                        task.setTaskId(taskId);
                        task.setComment("");
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("approvalStatus", op);
                        if (!ObjectUtils.isEmpty(noticeDto.getInstanceStatus()) &&
                                (noticeDto.getStatus().equals(WorkFlowStatusEnum.UNIT_RENAME_SUBMIT.getReject()) ||
                                        noticeDto.getStatus().equals(WorkFlowStatusEnum.UNIT_RENAME_SUBMIT.getRollBack()))) {
                            map.put("approvalStatus", "提交");
                        }
                        task.setVariable(map);
                        //执行流程
                        task.setNextExecuteUserCompanyCode(noticeDto.getReceiveCompanyCode());
                        ProcessTaskDTO processTaskDTO = cmWorkflowService.completeOrReject(taskId, task, op);
                        JgChangeRegistrationUnit bean = new JgChangeRegistrationUnit();
                        BeanUtils.copyProperties(noticeDto, bean);
                        String taskCode = FlowStatusEnum.TO_BE_FINISHED.getName();
                        String taskName1 = "";
                        String nextTaskId = "";
                        this.buildRoleList(Collections.singletonList(processTaskDTO), roleList, roleListAll);
                        List<WorkflowResultDto> workflowResultDtos = commonServiceImpl.buildWorkFlowInfo(Collections.singletonList(processTaskDTO));
                        String nextUserIds = workflowResultDtos.get(0).getNextExecutorUserIds();
                        if (processTaskDTO != null && processTaskDTO.getNextTask() != null && processTaskDTO.getNextTask().size() > 0) {
                            taskCode = processTaskDTO.getNextTask().get(0).getKey();
                            taskName1 = processTaskDTO.getNextTask().get(0).getName();
                            nextTaskId = processTaskDTO.getNextTask().get(0).getId();
                        }
                        bean.setNextExecutorIds(String.join(",", roleList));
                        if (!ObjectUtils.isEmpty(bean.getInstanceStatus())) {
                            bean.setInstanceStatus(bean.getInstanceStatus() + "," + String.join(",", roleList));
                        } else {
                            bean.setInstanceStatus(String.join(",", roleList));
                        }
                        bean.setNextTaskId(nextTaskId);
                        bean.setPromoter(RequestContext.getExeUserId());
                        bean.setStatus(WorkFlowStatusEnum.getMessage(taskCode).getPass());
                        executeOneStep(bean, taskName1, nextUserIds, op);
                        bean.setNextExecuteUserIds(nextUserIds);
                        setNewUnitInfo(reginParams, bean);
                        JgChangeRegistrationUnitMapper.updateById(bean);
                        commonServiceImpl.saveExecuteFlowData2Redis(bean.getInstanceId(), buildInstanceRuntimeData(bean));
                    }
                } else {
                    JgChangeRegistrationUnit bean = new JgChangeRegistrationUnit();
                    BeanUtils.copyProperties(noticeDto, bean);
                    setNewUnitInfo(reginParams, bean);
                    JgChangeRegistrationUnitMapper.updateById(bean);
                }
                List<JgChangeRegistrationUnitEq> jgRelationEquipList = new ArrayList<>();
                deviceList.forEach(device -> {
                    JgChangeRegistrationUnitEq unitEq = new JgChangeRegistrationUnitEq();
                    if (device.containsKey("USE_ORG_CODE")) {
                        unitEq.setRegistrationCertificate(device.getString("USE_ORG_CODE"));//使用登记证主键
                    }
                    if (device.containsKey("SEQUENCE_NBR")) {
                        unitEq.setEquId(device.getString("SEQUENCE_NBR"));//设备主键
                    }
                    unitEq.setUnitChangeRegistrationId(noticeDto.getSequenceNbr().toString());//单位变更业务主键
                    unitEq.setDeviceInfo(JSONObject.toJSONString(device));//设备信息json
                    jgRelationEquipList.add(unitEq);
                });
                JgRegistrationHistory jgRegistrationHistory = new JgRegistrationHistory();
                //登记类别
                jgRegistrationHistory.setRegistrationClass("单位变更登记");
                jgRegistrationHistory.setCurrentDocumentId(noticeDto.getApplyNo());
                //使用登记证历史数据
                jgRegistrationHistory.setChangeData(JSONObject.toJSONString(registrationList));
                //先删除之前设备关系数据，在插入最新设备关系数据
                registrationUnitEqService.getBaseMapper().delete(new LambdaQueryWrapper<JgChangeRegistrationUnitEq>()
                        .eq(JgChangeRegistrationUnitEq::getUnitChangeRegistrationId, noticeDto.getSequenceNbr()));
                registrationUnitEqService.saveBatch(jgRelationEquipList);
                //先删除之前未提交的历史使用登记证信息，在插入最新的使用登记证信息
                registrationHistoryService.getBaseMapper().delete(new LambdaQueryWrapper<JgRegistrationHistory>()
                        .eq(JgRegistrationHistory::getCurrentDocumentId, noticeDto.getApplyNo()));
                registrationHistoryService.save(jgRegistrationHistory);

                if (SUBMIT_TYPE_FLOW.equals(submitType)) {
                    //如果是提交，用于校验设备是否已经在流程中，如果不在标记设备已经在流程中
                    CompanyBo company = reginParams.getCompany();
                    deviceList.forEach(jsonObject -> EquipUsedCheckStrategyContext.getUsedStrategy(PROCESS_DEFINITION_KEY)
                            .equipRepeatUsedCheck(String.valueOf(jsonObject.get("SEQUENCE_NBR")),
                                    CompanyTypeEnum.INDIVIDUAL.getName().equals(company.getCompanyType()) ?
                                            company.getCompanyCode().split("_")[1] :
                                            company.getCompanyCode()));
                }
            }
            return noticeDto;
        } catch (BadRequest | LocalBadRequest e) {
            log.error(e.getMessage(), e);
            this.rollBackForDelRedisData();
            throw e;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            this.rollBackForDelRedisData();
            throw new BadRequest(e.getMessage());
        } finally {
            FlowingEquipRedisContext.clean();
        }
    }

    private void dealCylinderToUpdate1(String submitType, JgChangeRegistrationUnitDto changeRegistrationUnitDto, String op, ReginParams reginParams) {
        // 字段转换
        this.convertField(changeRegistrationUnitDto);
        // 获取单位变更设备列表
        List<Map<String, Object>> deviceList = changeRegistrationUnitDto.getDeviceList();
        if (CollectionUtils.isEmpty(deviceList)) {
            throw new BadRequest("请选择设备后提交");
        }
        String applyNo = changeRegistrationUnitDto.getApplyNo();
        List<String> applyNoList = ValidationUtil.isEmpty(applyNo)
                ? Optional.ofNullable(tzsServiceFeignClient.applicationFormCode(ApplicationFormTypeEnum.DWBG.getCode(), 1))
                .filter(r -> r.getStatus() == HttpStatus.OK.value() && !CollectionUtils.isEmpty(r.getResult()))
                .map(ResponseModel::getResult)
                .orElseThrow(() -> new BadRequest("生成申请变更单位编码失败！"))
                : Collections.singletonList(applyNo);

        if (SUBMIT_TYPE_FLOW.equals(submitType)) { //放在最后面防止前面有异常
            //如果是提交，用于校验设备是否已经在流程中，如果不在标记设备已经在流程中
            CompanyBo company = reginParams.getCompany();
            deviceList.forEach(jsonObject -> EquipUsedCheckStrategyContext.getUsedStrategy(PROCESS_DEFINITION_KEY)
                    .equipRepeatUsedCheck(String.valueOf(jsonObject.get(RECORD)),
                            CompanyTypeEnum.INDIVIDUAL.getName().equals(company.getCompanyType()) ? company.getCompanyCode().split("_")[1] : company.getCompanyCode()));
        }
        ArrayList<String> roleListAll = new ArrayList<>();
        ArrayList<String> roleListNext = new ArrayList<>();
        if (SUBMIT_TYPE_FLOW.equals(submitType)) {
            // 发起流程
            if (!StringUtils.hasText(changeRegistrationUnitDto.getInstanceId())) {
                List<ProcessTaskDTO> processTasks = new ArrayList<>();
                ActWorkflowBatchDTO actWorkflowBatchDTO = new ActWorkflowBatchDTO();
                List<ActWorkflowStartDTO> list = new ArrayList<>();
                ActWorkflowStartDTO dto = new ActWorkflowStartDTO();
                dto.setProcessDefinitionKey(PROCESS_DEFINITION_KEY);
                dto.setBusinessKey(String.valueOf(new Date().getTime()));
                dto.setCompleteFirstTask(true);
                list.add(dto);
                actWorkflowBatchDTO.setProcess(list);
                dto.setNextExecuteUserCompanyCode(changeRegistrationUnitDto.getReceiveCompanyCode());
                processTasks = cmWorkflowService.startBatch(actWorkflowBatchDTO);
                String instanceId = processTasks.get(0).getProcessInstance().getId();
                String nextTaskId = processTasks.get(0).getNextTask().get(0).getId();
                String taskname = processTasks.get(0).getNextTask().get(0).getName();
                this.buildRoleList(processTasks, roleListNext, roleListAll);
                List<WorkflowResultDto> workflowResultDtos = commonServiceImpl.buildWorkFlowInfo(processTasks);
                String nextUserIds = workflowResultDtos.get(0).getNextExecutorUserIds();

                JgChangeRegistrationUnit notice = new JgChangeRegistrationUnit();
                BeanUtils.copyProperties(changeRegistrationUnitDto, notice);
                notice.setChangeCertificate(changeRegistrationUnitDto.getChangeCertificate());
                notice.setRemark(changeRegistrationUnitDto.getRemark());
                if (!ObjectUtils.isEmpty(notice.getInstanceStatus())) {
                    notice.setInstanceStatus(notice.getInstanceStatus() + "," + roleListAll);
                } else {
                    notice.setInstanceStatus(String.join(",", roleListAll));
                }
                notice.setInstanceId(instanceId);
                notice.setNextTaskId(nextTaskId);
                notice.setPromoter(RequestContext.getExeUserId());
                notice.setNextExecutorIds(String.join(",", roleListNext));
                notice.setStatus(WorkFlowStatusEnum.UNITCHANGE_RECEIVE.getPass());
                notice.setNextExecuteUserIds(nextUserIds);
                setNewUnitInfo(reginParams, notice);
                JgChangeRegistrationUnitMapper.updateById(notice);
                commonServiceImpl.deleteTasksByRelationId(notice.getSequenceNbr() + "");
                createTaskModel(notice, taskname, "1", nextUserIds);
                commonServiceImpl.saveExecuteFlowData2Redis(notice.getInstanceId(), buildInstanceRuntimeData(notice));
            } else {
                ArrayList<String> roleList = new ArrayList<>();
                String taskId = changeRegistrationUnitDto.getNextTaskId();
                //组装信息
                TaskResultDTO task = new TaskResultDTO();
                task.setResultCode("approvalStatus");
                task.setTaskId(taskId);
                task.setComment("");
                HashMap<String, Object> map = new HashMap<>();
                map.put("approvalStatus", op);
                if (!ObjectUtils.isEmpty(changeRegistrationUnitDto.getInstanceStatus()) &&
                        (changeRegistrationUnitDto.getStatus().equals(WorkFlowStatusEnum.UNIT_RENAME_SUBMIT.getReject()) ||
                                changeRegistrationUnitDto.getStatus().equals(WorkFlowStatusEnum.UNIT_RENAME_SUBMIT.getRollBack()))) {
                    map.put("approvalStatus", "提交");
                }
                task.setVariable(map);
                //执行流程
                task.setNextExecuteUserCompanyCode(changeRegistrationUnitDto.getReceiveCompanyCode());
                ProcessTaskDTO processTaskDTO = cmWorkflowService.completeOrReject(taskId, task, op);
                JgChangeRegistrationUnit bean = new JgChangeRegistrationUnit();
                BeanUtils.copyProperties(changeRegistrationUnitDto, bean);
                String taskCode = FlowStatusEnum.TO_BE_FINISHED.getName();
                String taskName1 = "";
                String nextTaskId = "";
                this.buildRoleList(Collections.singletonList(processTaskDTO), roleList, roleListAll);
                List<WorkflowResultDto> workflowResultDtos = commonServiceImpl.buildWorkFlowInfo(Collections.singletonList(processTaskDTO));
                String nextUserIds = workflowResultDtos.get(0).getNextExecutorUserIds();
                if (processTaskDTO != null && processTaskDTO.getNextTask() != null && !processTaskDTO.getNextTask().isEmpty()) {
                    taskCode = processTaskDTO.getNextTask().get(0).getKey();
                    taskName1 = processTaskDTO.getNextTask().get(0).getName();
                    nextTaskId = processTaskDTO.getNextTask().get(0).getId();
                }
                bean.setNextExecutorIds(String.join(",", roleList));
                if (!ObjectUtils.isEmpty(bean.getInstanceStatus())) {
                    bean.setInstanceStatus(bean.getInstanceStatus() + "," + String.join(",", roleList));
                } else {
                    bean.setInstanceStatus(String.join(",", roleList));
                }
                bean.setNextTaskId(nextTaskId);
                bean.setPromoter(RequestContext.getExeUserId());
                bean.setStatus(WorkFlowStatusEnum.getMessage(taskCode).getPass());
                executeOneStep(bean, taskName1, nextUserIds, op);
                bean.setNextExecuteUserIds(nextUserIds);
                setNewUnitInfo(reginParams, bean);
                JgChangeRegistrationUnitMapper.updateById(bean);
                commonServiceImpl.saveExecuteFlowData2Redis(bean.getInstanceId(), buildInstanceRuntimeData(bean));
            }
        } else {
            JgChangeRegistrationUnit bean = new JgChangeRegistrationUnit();
            BeanUtils.copyProperties(changeRegistrationUnitDto, bean);
            setNewUnitInfo(reginParams, bean);
            JgChangeRegistrationUnitMapper.updateById(bean);
        }
        List<JgChangeRegistrationUnitEq> jgRelationEquipList = new ArrayList<>();
        deviceList.forEach(device -> {
            JgChangeRegistrationUnitEq unitEq = new JgChangeRegistrationUnitEq();
            unitEq.setRegistrationCertificate(changeRegistrationUnitDto.getUseRegistrationCertificate());
            unitEq.setEquId(Objects.toString(device.get(RECORD)));
            unitEq.setUnitChangeRegistrationId(changeRegistrationUnitDto.getSequenceNbr().toString());
            unitEq.setDeviceInfo(JSONObject.toJSONString(device));//设备信息json
            jgRelationEquipList.add(unitEq);
        });
        JgRegistrationHistory jgRegistrationHistory = new JgRegistrationHistory();
        //登记类别
        jgRegistrationHistory.setRegistrationClass("单位变更登记");
        jgRegistrationHistory.setCurrentDocumentId(changeRegistrationUnitDto.getApplyNo());
        //使用登记证历史数据
        jgRegistrationHistory.setChangeData(JSON.toJSONString(deviceList));
        //先删除之前设备关系数据，在插入最新设备关系数据
        registrationUnitEqService.getBaseMapper().delete(new LambdaQueryWrapper<JgChangeRegistrationUnitEq>()
                .eq(JgChangeRegistrationUnitEq::getUnitChangeRegistrationId, changeRegistrationUnitDto.getSequenceNbr()));
        registrationUnitEqService.saveBatch(jgRelationEquipList);
        //先删除之前未提交的历史使用登记证信息，在插入最新的使用登记证信息
        registrationHistoryService.getBaseMapper().delete(new LambdaQueryWrapper<JgRegistrationHistory>()
                .eq(JgRegistrationHistory::getCurrentDocumentId, changeRegistrationUnitDto.getApplyNo()));
        registrationHistoryService.save(jgRegistrationHistory);
    }

    /**
     * 气瓶单位变更业务
     * @param submitType 提交类型
     * @param dto 提交参数
     * @param op
     * @param reginParams
     */
    private void saveOrUpdateCylinder(String submitType, JgChangeRegistrationUnitDto dto, String op, ReginParams reginParams) {
        // 1️⃣ 字段转换与前置校验
        convertField(dto);
        validateDeviceList(dto.getDeviceList());

        // 2️⃣ 获取申请编号
        List<String> applyNoList = resolveApplyNo(dto);
        dto.setApplyNo(applyNoList.get(0));

        // 3️⃣ 校验设备是否正在流程中
        if (SUBMIT_TYPE_FLOW.equals(submitType)) {
            validateEquipUsed(dto.getDeviceList(), reginParams);
        }

        // 4️⃣ 流程逻辑统一处理
        if (SUBMIT_TYPE_FLOW.equals(submitType)) {
            handleFlow(dto, op, reginParams);
        } else {
            handleSaveOnly(dto, reginParams);
        }
        // 5️⃣ 保存设备关系与历史
        saveUnitEquipAndHistory(dto);
    }

    private void validateDeviceList(List<Map<String, Object>> deviceList) {
        if (CollectionUtils.isEmpty(deviceList)) {
            throw new BadRequest("请选择设备后提交");
        }
    }

    private List<String> resolveApplyNo(JgChangeRegistrationUnitDto dto) {
        if (ValidationUtil.isEmpty(dto.getApplyNo())) {
            return Optional.ofNullable(
                            tzsServiceFeignClient.applicationFormCode(ApplicationFormTypeEnum.DWBG.getCode(), 1)
                    ).filter(r -> r.getStatus() == HttpStatus.OK.value() && CollUtil.isNotEmpty(r.getResult()))
                    .map(ResponseModel::getResult)
                    .orElseThrow(() -> new BadRequest("生成申请变更单位编码失败！"));
        }
        return Collections.singletonList(dto.getApplyNo());
    }

    private void validateEquipUsed(List<Map<String, Object>> deviceList, ReginParams reginParams) {
        CompanyBo company = reginParams.getCompany();
        String companyCode = CompanyTypeEnum.INDIVIDUAL.getName().equals(company.getCompanyType())
                ? company.getCompanyCode().split("_")[1]
                : company.getCompanyCode();

        deviceList.forEach(json -> EquipUsedCheckStrategyContext
                .getUsedStrategy(PROCESS_DEFINITION_KEY)
                .equipRepeatUsedCheck(String.valueOf(json.get(RECORD)), companyCode));
    }

    private void handleFlow(JgChangeRegistrationUnitDto dto, String op, ReginParams reginParams) {
        if (!StringUtils.hasText(dto.getInstanceId())) {
            startNewFlow(dto, reginParams);
        } else {
            updateExistingFlow(dto, op, reginParams);
        }
    }

    private void startNewFlow(JgChangeRegistrationUnitDto dto, ReginParams reginParams) {
        ActWorkflowStartDTO startDTO = new ActWorkflowStartDTO();
        startDTO.setProcessDefinitionKey(PROCESS_DEFINITION_KEY);
        startDTO.setBusinessKey(String.valueOf(System.currentTimeMillis()));
        startDTO.setCompleteFirstTask(true);
        startDTO.setNextExecuteUserCompanyCode(dto.getReceiveCompanyCode());

        ActWorkflowBatchDTO batch = new ActWorkflowBatchDTO();
        batch.setProcess(Collections.singletonList(startDTO));

        List<ProcessTaskDTO> processTasks = cmWorkflowService.startBatch(batch);
        ProcessTaskDTO first = processTasks.get(0);

        String instanceId = first.getProcessInstance().getId();
        String nextTaskId = first.getNextTask().get(0).getId();
        String taskName = first.getNextTask().get(0).getName();

        ArrayList<String> roleListAll = new ArrayList<>();
        ArrayList<String> roleListNext = new ArrayList<>();
        buildRoleList(processTasks, roleListNext, roleListAll);

        List<WorkflowResultDto> workflowInfos = commonServiceImpl.buildWorkFlowInfo(processTasks);
        String nextUserIds = workflowInfos.get(0).getNextExecutorUserIds();

        JgChangeRegistrationUnit entity = new JgChangeRegistrationUnit();
        BeanUtils.copyProperties(dto, entity);
        entity.setInstanceId(instanceId);
        entity.setNextTaskId(nextTaskId);
        entity.setPromoter(RequestContext.getExeUserId());
        entity.setNextExecutorIds(String.join(",", roleListNext));
        entity.setNextExecuteUserIds(nextUserIds);
        entity.setStatus(WorkFlowStatusEnum.UNITCHANGE_RECEIVE.getPass());
        entity.setInstanceStatus(String.join(",", roleListAll));

        setNewUnitInfo(reginParams, entity);
        JgChangeRegistrationUnitMapper.updateById(entity);

        commonServiceImpl.deleteTasksByRelationId(entity.getSequenceNbr() + "");
        createTaskModel(entity, taskName, "1", nextUserIds);
        commonServiceImpl.saveExecuteFlowData2Redis(entity.getInstanceId(), buildInstanceRuntimeData(entity));
    }

    private void handleProcessUpdate(JgChangeRegistrationUnitDto dto, ProcessTaskDTO processTask, String op, ReginParams reginParams) {
        ArrayList<String> roleList = new ArrayList<>();
        ArrayList<String> roleListAll = new ArrayList<>();

        buildRoleList(Collections.singletonList(processTask), roleList, roleListAll);
        List<WorkflowResultDto> workflowInfos = commonServiceImpl.buildWorkFlowInfo(Collections.singletonList(processTask));
        String nextUserIds = workflowInfos.get(0).getNextExecutorUserIds();

        String taskCode = FlowStatusEnum.TO_BE_FINISHED.getName();
        String taskName = "";
        String nextTaskId = "";

        if (processTask.getNextTask() != null && !processTask.getNextTask().isEmpty()) {
            taskCode = processTask.getNextTask().get(0).getKey();
            taskName = processTask.getNextTask().get(0).getName();
            nextTaskId = processTask.getNextTask().get(0).getId();
        }

        JgChangeRegistrationUnit entity = new JgChangeRegistrationUnit();
        BeanUtils.copyProperties(dto, entity);
        entity.setNextTaskId(nextTaskId);
        entity.setPromoter(RequestContext.getExeUserId());
        entity.setNextExecutorIds(String.join(",", roleList));
        entity.setNextExecuteUserIds(nextUserIds);
        entity.setInstanceStatus(String.join(",", roleListAll));
        entity.setStatus(WorkFlowStatusEnum.getMessage(taskCode).getPass());

        executeOneStep(entity, taskName, nextUserIds, op);
        setNewUnitInfo(reginParams, entity);
        JgChangeRegistrationUnitMapper.updateById(entity);
        commonServiceImpl.saveExecuteFlowData2Redis(entity.getInstanceId(), buildInstanceRuntimeData(entity));
    }

    private void updateExistingFlow(JgChangeRegistrationUnitDto dto, String op, ReginParams reginParams) {
        String taskId = dto.getNextTaskId();

        TaskResultDTO task = new TaskResultDTO();
        task.setResultCode("approvalStatus");
        task.setTaskId(taskId);
        task.setComment("");

        HashMap<String, Object> variable = new HashMap<>();
        variable.put("approvalStatus", op);
        if (!ObjectUtils.isEmpty(dto.getInstanceStatus())
                && (WorkFlowStatusEnum.UNIT_RENAME_SUBMIT.getReject().equals(dto.getStatus())
                || WorkFlowStatusEnum.UNIT_RENAME_SUBMIT.getRollBack().equals(dto.getStatus()))) {
            variable.put("approvalStatus", "提交");
        }
        task.setVariable(variable);
        task.setNextExecuteUserCompanyCode(dto.getReceiveCompanyCode());

        ProcessTaskDTO processTask = cmWorkflowService.completeOrReject(taskId, task, op);
        handleProcessUpdate(dto, processTask, op, reginParams);
    }

    private void handleSaveOnly(JgChangeRegistrationUnitDto dto, ReginParams reginParams) {
        JgChangeRegistrationUnit entity = new JgChangeRegistrationUnit();
        BeanUtils.copyProperties(dto, entity);
        setNewUnitInfo(reginParams, entity);
        JgChangeRegistrationUnitMapper.updateById(entity);
    }

    private void saveUnitEquipAndHistory(JgChangeRegistrationUnitDto dto) {
        List<Map<String, Object>> deviceList = dto.getDeviceList();

        List<JgChangeRegistrationUnitEq> unitEqs = deviceList.stream().map(device -> {
            JgChangeRegistrationUnitEq eq = new JgChangeRegistrationUnitEq();
            eq.setRegistrationCertificate(dto.getUseRegistrationCertificate());
            eq.setEquId(String.valueOf(device.get(RECORD)));
            eq.setUnitChangeRegistrationId(dto.getSequenceNbr().toString());
            eq.setDeviceInfo(JSONObject.toJSONString(device));
            return eq;
        }).collect(Collectors.toList());

        registrationUnitEqService.remove(new LambdaQueryWrapper<JgChangeRegistrationUnitEq>()
                .eq(JgChangeRegistrationUnitEq::getUnitChangeRegistrationId, dto.getSequenceNbr()));
        registrationUnitEqService.saveBatch(unitEqs);

        JgRegistrationHistory history = new JgRegistrationHistory();
        history.setRegistrationClass("单位变更登记");
        history.setCurrentDocumentId(dto.getApplyNo());
        history.setChangeData(JSON.toJSONString(deviceList));

        registrationHistoryService.remove(new LambdaQueryWrapper<JgRegistrationHistory>()
                .eq(JgRegistrationHistory::getCurrentDocumentId, dto.getApplyNo()));
        registrationHistoryService.save(history);
    }

    /**
     * 气瓶单位变更业务
     * @param submitType
     * @param changeRegistrationUnitDto
     * @param op
     * @param reginParams
     */
    private JgChangeRegistrationUnitDto dealCylinderToSave(String submitType, JgChangeRegistrationUnitDto changeRegistrationUnitDto, String op, ReginParams reginParams) {
        String[] taskName = new String[]{"流程结束"};
        // 字段转换
        this.convertField(changeRegistrationUnitDto);
        // 获取单位变更设备列表
        List<Map<String, Object>> deviceList = changeRegistrationUnitDto.getDeviceList();
        if (CollectionUtils.isEmpty(deviceList)) {
            throw new BadRequest("请选择设备后提交");
        }
        String applyNo = changeRegistrationUnitDto.getApplyNo();
        List<String> applyNoList = ValidationUtil.isEmpty(applyNo)
                ? Optional.ofNullable(tzsServiceFeignClient.applicationFormCode(ApplicationFormTypeEnum.DWBG.getCode(), 1))
                .filter(r -> r.getStatus() == HttpStatus.OK.value() && !CollectionUtils.isEmpty(r.getResult()))
                .map(ResponseModel::getResult)
                .orElseThrow(() -> new BadRequest("生成申请变更单位编码失败！"))
                : Collections.singletonList(applyNo);

        if (SUBMIT_TYPE_FLOW.equals(submitType)) {//放在最后面防止前面有异常
            //如果是提交，用于校验设备是否已经在流程中，如果不在标记设备已经在流程中
            CompanyBo company = reginParams.getCompany();
            deviceList.forEach(jsonObject -> EquipUsedCheckStrategyContext.getUsedStrategy(PROCESS_DEFINITION_KEY)
                    .equipRepeatUsedCheck(String.valueOf(jsonObject.get(RECORD)),
                            CompanyTypeEnum.INDIVIDUAL.getName().equals(company.getCompanyType()) ?
                                    company.getCompanyCode().split("_")[1] :
                                    company.getCompanyCode()));
        }
        ArrayList<String> roleListFirst = new ArrayList<>();
        List<String> instanceIdList = new ArrayList<>();
        List<String> nextExecutorUserIds = new ArrayList<>();
        List<String> taskIds = new ArrayList<>();
        List<WorkflowResultDto> workflowResultDtos = new ArrayList<>();
        String nextExecutorRoleIds = "";
        // 判断当前是否为提交
        if (SUBMIT_TYPE_FLOW.equals(submitType)) {
            //发起流程
            ActWorkflowBatchDTO actWorkflowBatchDTO = new ActWorkflowBatchDTO();
            List<ActWorkflowStartDTO> list = new ArrayList<>();
            ActWorkflowStartDTO dto = new ActWorkflowStartDTO();
            dto.setProcessDefinitionKey(PROCESS_DEFINITION_KEY);
            dto.setBusinessKey(String.valueOf(new Date().getTime()));
            dto.setNextExecuteUserCompanyCode(changeRegistrationUnitDto.getReceiveCompanyCode());
            dto.setCompleteFirstTask(true);
            list.add(dto);
            actWorkflowBatchDTO.setProcess(list);
            List<ProcessTaskDTO> processTaskDTOS = cmWorkflowService.startBatch(actWorkflowBatchDTO);
            workflowResultDtos = commonServiceImpl.buildWorkFlowInfo(processTaskDTOS);
            nextExecutorRoleIds = workflowResultDtos.get(0).getNextExecutorRoleIds();
            nextExecutorUserIds.add(workflowResultDtos.get(0).getNextExecutorUserIds());
            for (WorkflowResultDto processTaskDTO : workflowResultDtos) {
                instanceIdList.add(processTaskDTO.getInstanceId());
                taskIds.add(processTaskDTO.getNextTaskId());
            }
        }

        String finalNextExecutorRoleIds = nextExecutorRoleIds;
        CompanyBo companyBo = commonServiceImpl.getOneCompany(changeRegistrationUnitDto.getReceiveCompanyCode());
        changeRegistrationUnitDto.setApplyNo(applyNoList.get(0));
        changeRegistrationUnitDto.setReceiveCompanyOrgCode(companyBo.getOrgCode());
        changeRegistrationUnitDto.setReceiveOrgCode(companyBo.getCompanyCode());
        changeRegistrationUnitDto.setCreateUserId(RequestContext.getExeUserId());
        changeRegistrationUnitDto.setApplyDate(new Date());
        if (SUBMIT_TYPE_FLOW.equals(submitType)) {
            changeRegistrationUnitDto.setNextExecutorIds(finalNextExecutorRoleIds);
            changeRegistrationUnitDto.setInstanceStatus(String.join(",", roleListFirst));
            changeRegistrationUnitDto.setPromoter(reginParams.getUserModel().getUserId());
            changeRegistrationUnitDto.setStatus(taskName[0]);
        }
        if (!CollectionUtils.isEmpty(nextExecutorUserIds)) {
            changeRegistrationUnitDto.setNextExecuteUserIds(nextExecutorUserIds.get(0));
        }
        if (!CollectionUtils.isEmpty(instanceIdList)) {
            changeRegistrationUnitDto.setInstanceId(instanceIdList.get(0));
            changeRegistrationUnitDto.setStatus(WorkFlowStatusEnum.UNITCHANGE_RECEIVE.getPass());
        } else {
            changeRegistrationUnitDto.setStatus(WorkFlowStatusEnum.UNITCHANGE_SUBMIT.getPass());
        }
        if (!CollectionUtils.isEmpty(taskIds)) {
            changeRegistrationUnitDto.setNextTaskId(taskIds.get(0));
        }
        changeRegistrationUnitDto.setSequenceNbr(sequence.nextId());
        changeRegistrationUnitDto.setCreateDate(new Date());
        changeRegistrationUnitDto.setCreateUserName(reginParams.getUserModel().getRealName());
        changeRegistrationUnitDto.setUseRegistCode(changeRegistrationUnitDto.getUseRegistCode());
        Optional.of(deviceList)
                .filter(list -> !list.isEmpty())
                .map(list -> list.get(0).get("equListName"))
                .map(Object::toString)
                .ifPresent(changeRegistrationUnitDto::setEquList);
        JgChangeRegistrationUnit registrationUnit = new JgChangeRegistrationUnit();
        BeanUtils.copyProperties(changeRegistrationUnitDto, registrationUnit);
        setNewUnitInfo(reginParams, registrationUnit);
        JgChangeRegistrationUnitMapper.insert(registrationUnit);

        List<TaskModelDto> modelDtos = new ArrayList<>();
        if (SUBMIT_TYPE_FLOW.equals(submitType)) {
            TaskModelDto taskModelDto = new TaskModelDto();
            TaskMessageDto ta = new TaskMessageDto();
            ta.setInstanceId(registrationUnit.getInstanceId());
            ta.setStatus(registrationUnit.getStatus());
            ta.setPromoter(registrationUnit.getPromoter());
            ta.setNextExecuteUserIds(registrationUnit.getNextExecuteUserIds());
            ta.setSequenceNbr(registrationUnit.getSequenceNbr());
            ta.setNextTaskId(registrationUnit.getNextTaskId());
            taskModelDto.setModel(ta);
            taskModelDto.setFlowCreateDate(new Date());
            taskModelDto.setTaskName(workflowResultDtos.get(0).getNextTaskName());
            taskModelDto.setFlowCode(registrationUnit.getNextTaskId());
            taskModelDto.setTaskContent(this.buildTaskContent(registrationUnit));
            taskModelDto.setTaskCode(registrationUnit.getApplyNo());
            taskModelDto.setTaskType(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getCode());
            taskModelDto.setTaskTypeLabel(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getName());
            taskModelDto.setRelationId(registrationUnit.getInstanceId());
            taskModelDto.setExecuteUserIds(workflowResultDtos.get(0).getNextExecutorUserIds());
            taskModelDto.setTaskStatusLabel("未处理");
            taskModelDto.setFlowStatus(commonServiceImpl.getDictionaryCodeByName(WorkFlowStatusEnum.UNITCHANGE_RECEIVE.getPass()));
            taskModelDto.setFlowStatusLabel(WorkFlowStatusEnum.UNITCHANGE_RECEIVE.getPass());
            taskModelDto.setStartUserId(RequestContext.getExeUserId());
            taskModelDto.setStartUser(workflowResultDtos.get(0).getStartUserName());
            taskModelDto.setStartUserCompanyName(reginParams.getCompany().getCompanyName());
            taskModelDto.setStartDate(new Date());
            taskModelDto.setNextExecuteUser(workflowResultDtos.get(0).getNextExecutorRoleIds());
            modelDtos.add(taskModelDto);
        } else {
            TaskModelDto taskModelDto = new TaskModelDto();
            //行数据
            taskModelDto.setModel(registrationUnit);
            //摘要 按原有规则组装
            taskModelDto.setTaskContent(this.buildTaskContent(registrationUnit));
            //申请单号
            taskModelDto.setTaskCode(registrationUnit.getApplyNo());
            //业务类型枚举code值
            taskModelDto.setTaskType(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getCode());
            ////业务主键
            taskModelDto.setRelationId(registrationUnit.getSequenceNbr() + "");
            modelDtos.add(taskModelDto);
//            }
        }
        commonServiceImpl.saveExecuteFlowData2Redis(registrationUnit.getInstanceId(), buildInstanceRuntimeData(registrationUnit));
        commonServiceImpl.buildTaskModel(modelDtos);

        List<JgChangeRegistrationUnitEq> jgRelationEquipList = new ArrayList<>();
        deviceList.forEach(device -> {
            JgChangeRegistrationUnitEq unitEq = new JgChangeRegistrationUnitEq();
            unitEq.setRegistrationCertificate(changeRegistrationUnitDto.getUseRegistrationCertificate());
            unitEq.setEquId(Objects.toString(device.get(RECORD)));
            unitEq.setUnitChangeRegistrationId(registrationUnit.getSequenceNbr().toString());//单位变更业务主键
            unitEq.setDeviceInfo(JSONObject.toJSONString(device));//设备信息json
            jgRelationEquipList.add(unitEq);
        });
        JgRegistrationHistory jgRegistrationHistory = new JgRegistrationHistory();
        //登记类别
        jgRegistrationHistory.setRegistrationClass("单位变更登记");
        jgRegistrationHistory.setCurrentDocumentId(registrationUnit.getApplyNo());
        //使用登记证历史数据
        jgRegistrationHistory.setChangeData(JSONObject.toJSONString(deviceList));
        //先删除之前设备关系数据，在插入最新设备关系数据
        registrationUnitEqService.getBaseMapper().delete(new LambdaQueryWrapper<JgChangeRegistrationUnitEq>()
                .eq(JgChangeRegistrationUnitEq::getUnitChangeRegistrationId, registrationUnit.getSequenceNbr()));
        registrationUnitEqService.saveBatch(jgRelationEquipList);
        //先删除之前未提交的历史使用登记证信息，在插入最新的使用登记证信息
        registrationHistoryService.getBaseMapper().delete(new LambdaQueryWrapper<JgRegistrationHistory>()
                .eq(JgRegistrationHistory::getCurrentDocumentId, registrationUnit.getApplyNo()));
        registrationHistoryService.save(jgRegistrationHistory);
        return changeRegistrationUnitDto;
    }

    /**
     * 更新装置的单位变更
     *
     * @param submitType
     * @param noticeDto
     * @param op
     * @param reginParams
     */
    private void dealProjectContraptionToUpdate(String submitType, JgChangeRegistrationUnitDto noticeDto, String op, ReginParams reginParams) {
        // 字段转换
        this.convertField(noticeDto);
        // 获取单位变更装置列表
        List<Map<String, Object>> projectContraptionList = noticeDto.getProjectContraptionList();
        if (CollectionUtils.isEmpty(projectContraptionList)) {
            throw new BadRequest("工程/装置列表不能为空");
        }
        //根据使用登记证查询设备
        List<String> registrationNoList = new ArrayList<>(); //使用登记证编号
        Map<String, String> useRegistrationCodeMap = new HashMap<>();
        for (Map<String, Object> objectMap : projectContraptionList) {
            if (objectMap.containsKey("useRegistrationCode") && ObjectUtils.isNotEmpty(objectMap.get("useRegistrationCode"))) {
                registrationNoList.add(objectMap.get("useRegistrationCode").toString());
                useRegistrationCodeMap.put(objectMap.get(SEQUENCE_NBR).toString(), objectMap.get("useRegistrationCode").toString());
            }
        }
        //查询装置下的管道信息
        List<Map<String, Object>> deviceList = this.queryPipelineList(projectContraptionList);
        if (CollUtil.isEmpty(deviceList)) {
            throw new BadRequest("工程/装置下没有管道信息");
        }
        if (SUBMIT_TYPE_FLOW.equals(submitType)) { //放在最后面防止前面有异常
            //如果是提交，用于校验设备是否已经在流程中，如果不在标记设备已经在流程中
            CompanyBo company = reginParams.getCompany();
            deviceList.forEach(jsonObject -> EquipUsedCheckStrategyContext.getUsedStrategy(PROCESS_DEFINITION_KEY)
                    .equipRepeatUsedCheck(String.valueOf(jsonObject.get("record")),
                            CompanyTypeEnum.INDIVIDUAL.getName().equals(company.getCompanyType()) ? company.getCompanyCode().split("_")[1] : company.getCompanyCode()));
        }
        ArrayList<String> roleListAll = new ArrayList<>();
        ArrayList<String> roleListNext = new ArrayList<>();
        if (SUBMIT_TYPE_FLOW.equals(submitType)) {
            // 发起流程
            if (!StringUtils.hasText(noticeDto.getInstanceId())) {
                List<ProcessTaskDTO> processTasks = new ArrayList<>();
                ActWorkflowBatchDTO actWorkflowBatchDTO = new ActWorkflowBatchDTO();
                List<ActWorkflowStartDTO> list = new ArrayList<>();
                ActWorkflowStartDTO dto = new ActWorkflowStartDTO();
                dto.setProcessDefinitionKey(PROCESS_DEFINITION_KEY);
                dto.setBusinessKey(String.valueOf(new Date().getTime()));
                dto.setCompleteFirstTask(true);
                list.add(dto);
                actWorkflowBatchDTO.setProcess(list);
                dto.setNextExecuteUserCompanyCode(noticeDto.getReceiveCompanyCode());
                processTasks = cmWorkflowService.startBatch(actWorkflowBatchDTO);
                String instanceId = processTasks.get(0).getProcessInstance().getId();
                String nextTaskId = processTasks.get(0).getNextTask().get(0).getId();
                String taskname = processTasks.get(0).getNextTask().get(0).getName();
                this.buildRoleList(processTasks, roleListNext, roleListAll);
                List<WorkflowResultDto> workflowResultDtos = commonServiceImpl.buildWorkFlowInfo(processTasks);
                String nextUserIds = workflowResultDtos.get(0).getNextExecutorUserIds();

                JgChangeRegistrationUnit notice = new JgChangeRegistrationUnit();
                BeanUtils.copyProperties(noticeDto, notice);
                notice.setChangeCertificate(noticeDto.getChangeCertificate());
                notice.setRemark(noticeDto.getRemark());
                if (!ObjectUtils.isEmpty(notice.getInstanceStatus())) {
                    notice.setInstanceStatus(notice.getInstanceStatus() + "," + roleListAll);
                } else {
                    notice.setInstanceStatus(String.join(",", roleListAll));
                }
                notice.setInstanceId(instanceId);
                notice.setNextTaskId(nextTaskId);
                notice.setSupervisoryCode(noticeDto.getSupervisoryCode());
                notice.setPromoter(RequestContext.getExeUserId());
                notice.setNextExecutorIds(String.join(",", roleListNext));
                notice.setStatus(WorkFlowStatusEnum.UNITCHANGE_RECEIVE.getPass());
                notice.setNextExecuteUserIds(nextUserIds);
                setNewUnitInfo(reginParams, notice);
                JgChangeRegistrationUnitMapper.updateById(notice);
                commonServiceImpl.deleteTasksByRelationId(notice.getSequenceNbr() + "");
                createTaskModel(notice, taskname, "1", nextUserIds);
                commonServiceImpl.saveExecuteFlowData2Redis(notice.getInstanceId(), buildInstanceRuntimeData(notice));
            } else {
                ArrayList<String> roleList = new ArrayList<>();
                String taskId = noticeDto.getNextTaskId();
                //组装信息
                TaskResultDTO task = new TaskResultDTO();
                task.setResultCode("approvalStatus");
                task.setTaskId(taskId);
                task.setComment("");
                HashMap<String, Object> map = new HashMap<>();
                map.put("approvalStatus", op);
                if (!ObjectUtils.isEmpty(noticeDto.getInstanceStatus()) &&
                        (noticeDto.getStatus().equals(WorkFlowStatusEnum.UNIT_RENAME_SUBMIT.getReject()) ||
                                noticeDto.getStatus().equals(WorkFlowStatusEnum.UNIT_RENAME_SUBMIT.getRollBack()))) {
                    map.put("approvalStatus", "提交");
                }
                task.setVariable(map);
                //执行流程
                task.setNextExecuteUserCompanyCode(noticeDto.getReceiveCompanyCode());
                ProcessTaskDTO processTaskDTO = cmWorkflowService.completeOrReject(taskId, task, op);
                JgChangeRegistrationUnit bean = new JgChangeRegistrationUnit();
                BeanUtils.copyProperties(noticeDto, bean);
                String taskCode = FlowStatusEnum.TO_BE_FINISHED.getName();
                String taskName1 = "";
                String nextTaskId = "";
                this.buildRoleList(Collections.singletonList(processTaskDTO), roleList, roleListAll);
                List<WorkflowResultDto> workflowResultDtos = commonServiceImpl.buildWorkFlowInfo(Collections.singletonList(processTaskDTO));
                String nextUserIds = workflowResultDtos.get(0).getNextExecutorUserIds();
                if (processTaskDTO != null && processTaskDTO.getNextTask() != null && processTaskDTO.getNextTask().size() > 0) {
                    taskCode = processTaskDTO.getNextTask().get(0).getKey();
                    taskName1 = processTaskDTO.getNextTask().get(0).getName();
                    nextTaskId = processTaskDTO.getNextTask().get(0).getId();
                }
                bean.setNextExecutorIds(String.join(",", roleList));
                if (!ObjectUtils.isEmpty(bean.getInstanceStatus())) {
                    bean.setInstanceStatus(bean.getInstanceStatus() + "," + String.join(",", roleList));
                } else {
                    bean.setInstanceStatus(String.join(",", roleList));
                }
                bean.setNextTaskId(nextTaskId);
                bean.setPromoter(RequestContext.getExeUserId());
                bean.setStatus(WorkFlowStatusEnum.getMessage(taskCode).getPass());
                executeOneStep(bean, taskName1, nextUserIds, op);
                bean.setNextExecuteUserIds(nextUserIds);
                setNewUnitInfo(reginParams, bean);
                JgChangeRegistrationUnitMapper.updateById(bean);
                commonServiceImpl.saveExecuteFlowData2Redis(bean.getInstanceId(), buildInstanceRuntimeData(bean));
            }
        } else {
            JgChangeRegistrationUnit bean = new JgChangeRegistrationUnit();
            BeanUtils.copyProperties(noticeDto, bean);
            setNewUnitInfo(reginParams, bean);
            JgChangeRegistrationUnitMapper.updateById(bean);
        }
        List<JgChangeRegistrationUnitEq> jgRelationEquipList = new ArrayList<>();
        deviceList.forEach(device -> {
            JgChangeRegistrationUnitEq unitEq = new JgChangeRegistrationUnitEq();
            if (device.containsKey("projectContraptionId")) {
                String projectContraptionId = (String) device.get("projectContraptionId");
                unitEq.setRegistrationCertificate(useRegistrationCodeMap.get(projectContraptionId));
                unitEq.setProjectContraptionId(projectContraptionId);
            }
            if (device.containsKey("record")) {
                unitEq.setEquId((String) device.get("record")); //设备主键
            }
            unitEq.setUnitChangeRegistrationId(noticeDto.getSequenceNbr().toString());//单位变更业务主键
            unitEq.setDeviceInfo(JSON.toJSONString(device));//设备信息json
            jgRelationEquipList.add(unitEq);
        });
        JgRegistrationHistory jgRegistrationHistory = new JgRegistrationHistory();
        //登记类别
        jgRegistrationHistory.setRegistrationClass("单位变更登记");
        jgRegistrationHistory.setCurrentDocumentId(noticeDto.getApplyNo());
        //使用登记证历史数据
        jgRegistrationHistory.setChangeData(JSON.toJSONString(projectContraptionList));
        //先删除之前设备关系数据，在插入最新设备关系数据
        registrationUnitEqService.getBaseMapper().delete(new LambdaQueryWrapper<JgChangeRegistrationUnitEq>()
                .eq(JgChangeRegistrationUnitEq::getUnitChangeRegistrationId, noticeDto.getSequenceNbr()));
        registrationUnitEqService.saveBatch(jgRelationEquipList);
        //先删除之前未提交的历史使用登记证信息，在插入最新的使用登记证信息
        registrationHistoryService.getBaseMapper().delete(new LambdaQueryWrapper<JgRegistrationHistory>()
                .eq(JgRegistrationHistory::getCurrentDocumentId, noticeDto.getApplyNo()));
        registrationHistoryService.save(jgRegistrationHistory);
    }

    private void setNewUnitInfo(ReginParams reginParams, JgChangeRegistrationUnit notice) {
        notice.setNewUseUnitName(CompanyTypeEnum.INDIVIDUAL.getName().equals(reginParams.getCompany().getCompanyType()) ?
                reginParams.getCompany().getCompanyName().split("_")[1] : reginParams.getCompany().getCompanyName());
        notice.setNewUseUnitCreditCode(CompanyTypeEnum.INDIVIDUAL.getName().equals(reginParams.getCompany().getCompanyType()) ?
                reginParams.getCompany().getCompanyCode().split("_")[1] :
                reginParams.getCompany().getCompanyCode());
    }


    /**
     * 分页查询
     *
     * @param page  分页对象
     * @param model 查询参数
     * @param type  类型：enterprise-企业端、supervision-监管端
     * @return 单位变更列表
     */
    @Override
    public Page<JgChangeRegistrationUnitDto> queryForJgChangeRegistrationUnitPage(Page<JgChangeRegistrationUnit> page,
                                                                                  String sort,
                                                                                  JgChangeRegistrationUnitDto model,
                                                                                  String type,
                                                                                  ReginParams reginParams,
                                                                                  String orgCode) {
        String companyCode = reginParams.getCompany().getCompanyCode();
        if (CompanyTypeEnum.INDIVIDUAL.getName().equals(reginParams.getCompany().getCompanyType())) {
            companyCode = reginParams.getCompany().getCompanyCode().split("_")[1];
        }
        String companyType = reginParams.getCompany().getCompanyType();
        String currentUserId = reginParams.getUserModel().getUserId();
        SortVo sortMap = commonServiceImpl.sortFieldConversion(sort);
        Page<JgChangeRegistrationUnit> noticePage = JgChangeRegistrationUnitMapper.queryForPage(page, sortMap, model, type, companyCode, model.getRoleIds(), currentUserId, orgCode);

        Page<JgChangeRegistrationUnitDto> noticeDtoPage = new Page<>();
        BeanUtils.copyProperties(noticePage, noticeDtoPage, "records");
        List<JgChangeRegistrationUnitDto> records = noticePage.getRecords().stream().map(notice -> {
            JgChangeRegistrationUnitDto noticeDto = new JgChangeRegistrationUnitDto();
            BeanUtils.copyProperties(notice, noticeDto);
            noticeDto.setCompanyType(companyType);
            return noticeDto;
        }).collect(Collectors.toList());
        noticeDtoPage.setRecords(records);

        return noticeDtoPage;
    }

    /**
     * 批量删除
     *
     * @param sequenceNbrs 主键
     * @return 是否删除成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteForBatch(Long[] sequenceNbrs) {
        if (Objects.isNull(sequenceNbrs) || sequenceNbrs.length == 0) {
            return false;
        }
        for (Long sequenceNbr : sequenceNbrs) {
            JgChangeRegistrationUnit changeRegistrationUnit = this.baseMapper.selectById(sequenceNbr);
            // 删除代办 + 中止流程
            commonServiceImpl.deleteTaskModel(String.valueOf(sequenceNbr), changeRegistrationUnit.getInstanceId());
            // 删除单子
            this.baseMapper.deleteById(sequenceNbr);
            // 删除单子对应eq
            jgChangeRegistrationUnitEqMapper.delete(new LambdaQueryWrapper<JgChangeRegistrationUnitEq>()
                    .eq(JgChangeRegistrationUnitEq::getUnitChangeRegistrationId, sequenceNbr));
            // 删除单子对应历史表数据
            jgRegistrationHistoryMapper.delete(new LambdaQueryWrapper<JgRegistrationHistory>()
                    .eq(JgRegistrationHistory::getCurrentDocumentId, changeRegistrationUnit.getApplyNo()));
        }
        return Boolean.TRUE;
    }

    public void saveRecord(JgChangeRegistrationUnit registrationUnit) {
        Long sequenceNbr = registrationUnit.getSequenceNbr();
        List<JgChangeRegistrationUnitEq> eqList = registrationUnitEqService.lambdaQuery().eq(JgChangeRegistrationUnitEq::getUnitChangeRegistrationId, sequenceNbr).list();
        List<JgRegistrationHistory> historyList = registrationHistoryService.lambdaQuery().eq(JgRegistrationHistory::getCurrentDocumentId, registrationUnit.getApplyNo()).list();
        if (historyList.size() != 1) {
            return;
        }
        List<JgCertificateChangeRecord> changeRecordList = new ArrayList<>();
        List<JgCertificateChangeRecordEq> changeRecordEqList = new ArrayList<>();
        JSONArray historyJson = JSON.parseArray(historyList.get(0).getChangeData());
        for (Object object : historyJson) {
            JgCertificateChangeRecord changeRecord = new JgCertificateChangeRecord();
            long nextId = sequence.nextId();
            JSONObject jsonObject = JSON.parseObject(object.toString());
            String registrationCode = jsonObject.getString("useRegistrationCode");
            String certificateNo = jsonObject.getString("certificateNo");
            String equCategoryCode = jsonObject.getString("equCategoryCode");
            changeRecord.setApplyNo(registrationUnit.getApplyNo());//申请编号
            changeRecord.setReceiveOrgName(registrationUnit.getReceiveOrgName());//接收机构/登记机关
            changeRecord.setAuditPassDate(registrationUnit.getApplyDate());//办理日期
            changeRecord.setRegType("单位变更登记");//登记类别
            changeRecord.setRegDate(registrationUnit.getCreateDate());//申请日期
            changeRecord.setChangeContent(this.buildRecordContent(registrationUnit));//变更内容
            changeRecord.setUseRegistrationCode(registrationCode);//使用登记编号
            changeRecord.setReceiveCompanyCode(registrationUnit.getReceiveCompanyCode());//接收机构公司代码
            changeRecord.setCertificateNo(certificateNo);//登记证书唯一码
            changeRecord.setUseUnitCreditCode(registrationUnit.getNewUseUnitCreditCode());//使用单位统一信用代码
            changeRecord.setUseUnitName(registrationUnit.getNewUseUnitName());//使用单位名称
            changeRecord.setEquCategory(equCategoryCode);//设备类别编码
            changeRecord.setCreateDate(new Date());
            changeRecord.setSequenceNbr(nextId);
            changeRecordList.add(changeRecord);
            for (JgChangeRegistrationUnitEq unitEq : eqList) {
                if (!StringUtils.isEmpty(unitEq.getRegistrationCertificate()) && !StringUtils.isEmpty(registrationCode) &&
                        unitEq.getRegistrationCertificate().equals(registrationCode)) {
                    JSONObject deviceJson = JSON.parseObject(unitEq.getDeviceInfo());
                    JgCertificateChangeRecordEq changeRecordEq = new JgCertificateChangeRecordEq();
                    changeRecordEq.setChangeRecordId(String.valueOf(nextId));//登记证记录主键
                    changeRecordEq.setEquId(deviceJson.getString("SEQUENCE_NBR"));//设备主键
                    changeRecordEqList.add(changeRecordEq);
                }
            }
        }
        if (!changeRecordList.isEmpty()) {
            certificateChangeRecordService.saveBatch(changeRecordList);
        }
        if (!changeRecordEqList.isEmpty()) {
            certificateChangeRecordEqService.saveBatch(changeRecordEqList);
        }
    }

    private void checkForRepeatUsedEquip(JSONObject map, CompanyBo company) {
        if (!ObjectUtils.isEmpty(map.get("submit"))) {
            // 流程中或已完成
            EquipUsedCheckStrategyContext.getUsedStrategy(PROCESS_DEFINITION_KEY).equipRepeatUsedCheck(map.get("equipId").toString(), company.getCompanyCode());
        }
    }

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public List<JgChangeRegistrationUnitDto> saveNotice(String submitType, JSONObject map, ReginParams reginParams) {
        try {
            JgChangeRegistrationUnitDto model = BeanUtil.toBeanIgnoreError(map.get("changeRegisInfo"), JgChangeRegistrationUnitDto.class);
            Map<String, Object> tableData = (Map<String, Object>) map.get("changeRegisInfo");
            String[] taskName = new String[]{"流程结束"};
            // 字段转换
            convertField(model);
            if (CylinderTypeEnum.CYLINDER.getCode().equals(model.getEquCategoryCode())) {
                this.dealCylinderToSave(submitType, model, "", reginParams);
            } else{
                // 获取单位变更使用登记证列表
                List<Map<String, Object>> registrationList = model.getRegistrationList();
                if (CollectionUtils.isEmpty(registrationList)) {
                    throw new BadRequest("使用登记证不能为空");
                }
                //根据使用登记证查询设备
                List<Long> registrationIdList = new ArrayList<>();//使用登记证id
                List<String> registrationNoList = new ArrayList<>();//使用登记证编号
                for (Map<String, Object> objectMap : registrationList) {
                    if (objectMap.containsKey(SEQUENCE_NBR) && ObjectUtils.isNotEmpty(objectMap.get(SEQUENCE_NBR))
                            && canConvertToLong(objectMap.get(SEQUENCE_NBR).toString())) {
                        registrationIdList.add(Long.parseLong(objectMap.get(SEQUENCE_NBR).toString()));
                    }
                    if (objectMap.containsKey("useRegistrationCode") && ObjectUtils.isNotEmpty(objectMap.get("useRegistrationCode"))) {
                        registrationNoList.add(objectMap.get("useRegistrationCode").toString());
                    }
                }
                if (CollectionUtils.isEmpty(registrationIdList)) {
                    throw new BadRequest("使用登记证不能为空");
                }
                List<JSONObject> deviceList = registrationManageService.queryEquByCertificateSeqList(registrationIdList);
                // 获取单位变更设备列表
                if (CollectionUtils.isEmpty(deviceList)) {
                    throw new BadRequest("使用登记证下无设备");
                }
                //场车区分区外还是区内
                if (EQU_CODE_CC.equals(model.getEquListCode())) {
                    String city = Objects.toString(tableData.get("city"), "");
                    String userCity = Optional.ofNullable(city).map(c -> c.split("_")[0]).orElseThrow(() -> new BadRequest("请选择地市后暂存！"));
                    this.validateDeviceCity(deviceList, userCity, "0".equals(model.getChangeType()));
                    this.setEquAddress(model);
                }

                // 获取单位变更单号
                ResponseModel<List<String>> listResponseModel = tzsServiceFeignClient.applicationFormCode(ApplicationFormTypeEnum.DWBG.getCode(), 1);
                List<String> applyNoList = listResponseModel.getResult();
                if (CollectionUtils.isEmpty(applyNoList)) {
                    log.error("生成申请变更单位编码失败");
                    return new ArrayList<>();
                }
                if (SUBMIT_TYPE_FLOW.equals(submitType)) {//放在最后面防止前面有异常
                    //如果是提交，用于校验设备是否已经在流程中，如果不在标记设备已经在流程中
                    CompanyBo company = reginParams.getCompany();
                    deviceList.forEach(jsonObject -> EquipUsedCheckStrategyContext.getUsedStrategy(PROCESS_DEFINITION_KEY)
                            .equipRepeatUsedCheck(String.valueOf(jsonObject.get("SEQUENCE_NBR")),
                                    CompanyTypeEnum.INDIVIDUAL.getName().equals(company.getCompanyType()) ?
                                            company.getCompanyCode().split("_")[1] :
                                            company.getCompanyCode()));
                }
                ArrayList<String> roleListFirst = new ArrayList<>();
                List<String> instanceIdList = new ArrayList<>();
                List<String> nextExecutorUserIds = new ArrayList<>();
                List<String> taskIds = new ArrayList<>();
                List<WorkflowResultDto> workflowResultDtos = new ArrayList<>();
                String nextExecutorRoleIds = "";
                // 判断当前是否为提交
                if (SUBMIT_TYPE_FLOW.equals(submitType)) {
                    //发起流程
                    ActWorkflowBatchDTO actWorkflowBatchDTO = new ActWorkflowBatchDTO();
                    List<ActWorkflowStartDTO> list = new ArrayList<>();
                    ActWorkflowStartDTO dto = new ActWorkflowStartDTO();
                    dto.setProcessDefinitionKey(PROCESS_DEFINITION_KEY);
                    dto.setBusinessKey(String.valueOf(new Date().getTime()));
                    dto.setNextExecuteUserCompanyCode(model.getReceiveCompanyCode());
                    dto.setCompleteFirstTask(true);
                    list.add(dto);
                    actWorkflowBatchDTO.setProcess(list);
                    List<ProcessTaskDTO> processTaskDTOS = cmWorkflowService.startBatch(actWorkflowBatchDTO);
                    workflowResultDtos = commonServiceImpl.buildWorkFlowInfo(processTaskDTOS);
                    nextExecutorRoleIds = workflowResultDtos.get(0).getNextExecutorRoleIds();
                    nextExecutorUserIds.add(workflowResultDtos.get(0).getNextExecutorUserIds());
                    for (WorkflowResultDto processTaskDTO : workflowResultDtos) {
                        instanceIdList.add(processTaskDTO.getInstanceId());
                        taskIds.add(processTaskDTO.getNextTaskId());
                    }
                }

                String finalNextExecutorRoleIds = nextExecutorRoleIds;
                CompanyBo companyBo = commonServiceImpl.getOneCompany(model.getReceiveCompanyCode());
                model.setApplyNo(applyNoList.get(0));
                model.setReceiveCompanyOrgCode(companyBo.getOrgCode());
                model.setCreateUserId(RequestContext.getExeUserId());
                model.setApplyDate(new Date());
                if (SUBMIT_TYPE_FLOW.equals(submitType)) {
                    model.setNextExecutorIds(finalNextExecutorRoleIds);
                    model.setInstanceStatus(String.join(",", roleListFirst));
                    model.setPromoter(reginParams.getUserModel().getUserId());
                    model.setStatus(taskName[0]);
                }
                if (!CollectionUtils.isEmpty(nextExecutorUserIds)) {
                    model.setNextExecuteUserIds(nextExecutorUserIds.get(0));
                }
                if (!CollectionUtils.isEmpty(instanceIdList)) {
                    model.setInstanceId(instanceIdList.get(0));
                    model.setStatus(WorkFlowStatusEnum.UNITCHANGE_RECEIVE.getPass());
                } else {
                    model.setStatus(WorkFlowStatusEnum.UNITCHANGE_SUBMIT.getPass());
                }
                if (!CollectionUtils.isEmpty(taskIds)) {
                    model.setNextTaskId(taskIds.get(0));
                }
                model.setSequenceNbr(sequence.nextId());
                model.setCreateDate(new Date());
                model.setCreateUserName(reginParams.getUserModel().getRealName());
                model.setUseRegistCode(String.join(",", registrationNoList));
                model.setEquList(registrationList.get(0).get("equList").toString());
                JgChangeRegistrationUnit registrationUnit = new JgChangeRegistrationUnit();
                BeanUtils.copyProperties(model, registrationUnit);
                setNewUnitInfo(reginParams, registrationUnit);
                JgChangeRegistrationUnitMapper.insert(registrationUnit);

                List<TaskModelDto> modelDtos = new ArrayList<>();
                if (SUBMIT_TYPE_FLOW.equals(submitType)) {
                    TaskModelDto taskModelDto = new TaskModelDto();
                    TaskMessageDto ta = new TaskMessageDto();
                    ta.setInstanceId(registrationUnit.getInstanceId());
                    ta.setStatus(registrationUnit.getStatus());
                    ta.setPromoter(registrationUnit.getPromoter());
                    ta.setNextExecuteUserIds(registrationUnit.getNextExecuteUserIds());
                    ta.setSequenceNbr(registrationUnit.getSequenceNbr());
                    ta.setNextTaskId(registrationUnit.getNextTaskId());
                    taskModelDto.setModel(ta);
                    taskModelDto.setFlowCreateDate(new Date());
                    taskModelDto.setTaskName(workflowResultDtos.get(0).getNextTaskName());
                    taskModelDto.setFlowCode(registrationUnit.getNextTaskId());
                    taskModelDto.setTaskContent(this.buildTaskContent(registrationUnit));
                    taskModelDto.setTaskCode(registrationUnit.getApplyNo());
                    taskModelDto.setTaskType(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getCode());
                    taskModelDto.setTaskTypeLabel(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getName());
                    taskModelDto.setRelationId(registrationUnit.getInstanceId());
                    taskModelDto.setExecuteUserIds(workflowResultDtos.get(0).getNextExecutorUserIds());
                    taskModelDto.setTaskStatusLabel("未处理");
                    taskModelDto.setFlowStatus(commonServiceImpl.getDictionaryCodeByName(WorkFlowStatusEnum.UNITCHANGE_RECEIVE.getPass()));
                    taskModelDto.setFlowStatusLabel(WorkFlowStatusEnum.UNITCHANGE_RECEIVE.getPass());
                    taskModelDto.setStartUserId(RequestContext.getExeUserId());
                    taskModelDto.setStartUser(workflowResultDtos.get(0).getStartUserName());
                    taskModelDto.setStartUserCompanyName(reginParams.getCompany().getCompanyName());
                    taskModelDto.setStartDate(new Date());
                    taskModelDto.setNextExecuteUser(workflowResultDtos.get(0).getNextExecutorRoleIds());
                    modelDtos.add(taskModelDto);
                } else {
                    TaskModelDto taskModelDto = new TaskModelDto();
                    //行数据
                    taskModelDto.setModel(registrationUnit);
                    //摘要 按原有规则组装
                    taskModelDto.setTaskContent(this.buildTaskContent(registrationUnit));
                    //申请单号
                    taskModelDto.setTaskCode(registrationUnit.getApplyNo());
                    //业务类型枚举code值
                    taskModelDto.setTaskType(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getCode());
                    ////业务主键
                    taskModelDto.setRelationId(registrationUnit.getSequenceNbr() + "");
                    modelDtos.add(taskModelDto);
//            }
                }
                commonServiceImpl.saveExecuteFlowData2Redis(registrationUnit.getInstanceId(), buildInstanceRuntimeData(registrationUnit));
                commonServiceImpl.buildTaskModel(modelDtos);

                List<JgChangeRegistrationUnitEq> jgRelationEquipList = new ArrayList<>();
                deviceList.forEach(device -> {
                    JgChangeRegistrationUnitEq unitEq = new JgChangeRegistrationUnitEq();
                    if (device.containsKey("USE_ORG_CODE")) {
                        unitEq.setRegistrationCertificate(device.getString("USE_ORG_CODE"));//使用登记证主键
                    }
                    if (device.containsKey("SEQUENCE_NBR")) {
                        unitEq.setEquId(device.getString("SEQUENCE_NBR"));//设备主键
                    }
                    unitEq.setUnitChangeRegistrationId(registrationUnit.getSequenceNbr().toString());//单位变更业务主键
                    unitEq.setDeviceInfo(JSONObject.toJSONString(device));//设备信息json
                    jgRelationEquipList.add(unitEq);
                });
                JgRegistrationHistory jgRegistrationHistory = new JgRegistrationHistory();
                //登记类别
                jgRegistrationHistory.setRegistrationClass("单位变更登记");
                jgRegistrationHistory.setCurrentDocumentId(registrationUnit.getApplyNo());
                //使用登记证历史数据
                jgRegistrationHistory.setChangeData(JSONObject.toJSONString(deviceList));
                //先删除之前设备关系数据，在插入最新设备关系数据
                registrationUnitEqService.getBaseMapper().delete(new LambdaQueryWrapper<JgChangeRegistrationUnitEq>()
                        .eq(JgChangeRegistrationUnitEq::getUnitChangeRegistrationId, registrationUnit.getSequenceNbr()));
                registrationUnitEqService.saveBatch(jgRelationEquipList);
                //先删除之前未提交的历史使用登记证信息，在插入最新的使用登记证信息
                registrationHistoryService.getBaseMapper().delete(new LambdaQueryWrapper<JgRegistrationHistory>()
                        .eq(JgRegistrationHistory::getCurrentDocumentId, registrationUnit.getApplyNo()));
                registrationHistoryService.save(jgRegistrationHistory);
                return Collections.singletonList(model);
            }
            return Collections.singletonList(model);
        } catch (BadRequest | LocalBadRequest e) {
            log.error(e.getMessage(), e);
            this.rollBackForDelRedisData();
            throw e;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            this.rollBackForDelRedisData();
            throw new BadRequest(e.getMessage());
        } finally {
            FlowingEquipRedisContext.clean();
        }
    }

    private void rollBackForDelRedisData() {
        FlowingEquipRedisContext.getContext().forEach(e -> {
            EquipUsedCheckStrategyContext.getUsedStrategy(PROCESS_DEFINITION_KEY).delDataForCheckWithKey(e.getData(), e.getRedisKey());
        });
    }

    public void setEquAddress(JgChangeRegistrationUnitDto model) {
        String address = Stream.of(getRegionNameByCode("province", model.getProvince()),
                        getRegionNameByCode("city", model.getCity()),
                        getRegionNameByCode("region", model.getCounty()),
                        getRegionNameByCode("street", model.getStreet()))
                .filter(s -> s != null && !s.isEmpty())
                .collect(Collectors.joining())
                + Objects.toString(model.getFullAddress(), "");
        model.setEquAddress(address);
        model.setFullAddress(Objects.toString(model.getFullAddress(), ""));
    }

    /**
     * 根据code获取市区县名字
     *
     * @param type type
     * @return name
     */
    private String getRegionNameByCode(String type, String code) {
        List<LinkedHashMap<String, Object>> regions = (List<LinkedHashMap<String, Object>>) redisUtils.get(type.toUpperCase());
        return regions.stream()
                .filter(item -> String.valueOf(item.get("regionCode")).equals(code))
                .map(item -> (String) item.get("regionName"))
                .findFirst()
                .orElse("");
    }

    // 获取设备的地市编码
    private String getDeviceCity(Map<String, Object> device) {
        Object usePlaceCodeObj = device.get("USE_PLACE_CODE");
        if (usePlaceCodeObj == null) {
            throw new BadRequest("没有查询到地市信息");
        }
        String[] parts = ((String) usePlaceCodeObj).split("#");
        if (parts.length < 2 || parts[1] == null || parts[1].isEmpty()) {
            throw new BadRequest("没有查询到地市信息");
        }
        return parts[1];
    }

    // 校验设备地市
    private void validateDeviceCity(List<JSONObject> deviceList, String userCity, boolean isIntraDistrictChange) {
        for (Map<String, Object> device : deviceList) {
            String deviceCity = getDeviceCity(device);

            if (isIntraDistrictChange && !userCity.equals(deviceCity)) {
                throw new BadRequest("区内变更只能选择与设备地址中相同地市！");
            }

            if (!isIntraDistrictChange && userCity.equals(deviceCity)) {
                throw new BadRequest("区外变更只能选择与设备地址中不同地市！");
            }
        }
    }

    public boolean canConvertToLong(String str) {
        try {
            Long.parseLong(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    private void updateHistoryAsync(List<JgChangeRegistrationUnitEq> jgRelationEquipList) {
        jgRelationEquipList.forEach(j -> {
            commonService.saveOrUpdateHistory(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getName(),
                    getEquipInfoJson(j.getUnitChangeRegistrationId()), j.getEquId(), j.getUnitChangeRegistrationId());
        });
    }

    private JSONObject getEquipInfoJson(String id) {
        return new JSONObject(getEquipInfo(Long.parseLong(id), new HashMap<>()));
    }


    private void convertField(JgChangeRegistrationUnitDto model) {
        // 处理图片
        if (!ValidationUtil.isEmpty(model.getChangeCertificateList())) {
            model.setChangeCertificate(JSON.toJSONString(model.getChangeCertificateList()));
        }
        // 处理其他附件
        if (!ValidationUtil.isEmpty(model.getOtherAccessoriesList())) {
            model.setOtherAccessories(JSON.toJSONString(model.getOtherAccessoriesList()));
        }
        // 分割单位
        String useUnitId = model.getUseUnitCreditCode();
        if (!ObjectUtils.isEmpty(useUnitId)) {
            String[] useUnitList = useUnitId.split("_");
            if (useUnitList.length > 1) {
                model.setUseUnitCreditCode(useUnitList[0]);
                model.setUseUnitName(useUnitList[1]);
            }
        }

        // 分割单位
        String orgBranchCode = model.getOrgBranchCode();
        if (!ObjectUtils.isEmpty(orgBranchCode)) {
            String[] orgBranchCodeList = orgBranchCode.split("_");
            if (orgBranchCodeList.length > 1) {
                model.setOrgBranchCode(orgBranchCodeList[0]);
                model.setOrgBranchName(orgBranchCodeList[1]);
            }
        }

        String receiveOrgCode = model.getReceiveOrgCode();
        if (!ObjectUtils.isEmpty(receiveOrgCode)) {
            String[] receiveOrgCodeList = receiveOrgCode.split("_");
            if (receiveOrgCodeList.length > 1) {
                model.setReceiveOrgCode(receiveOrgCodeList[0]);
                model.setReceiveCompanyCode(receiveOrgCodeList[0]);
                model.setReceiveOrgName(receiveOrgCodeList[1]);
            }
        }
        this.setEquAddress(model);
    }


    @GlobalTransactional(rollbackFor = Exception.class)
    public boolean submit(JgChangeRegistrationUnit notice, String op) {

        AjaxResult ajaxResult = Workflow.taskClient.getTask(notice.getInstanceId());
        JSONObject dataObject = JSON.parseObject(JSON.toJSONString(ajaxResult.get("data")));
        String taskId = dataObject.getString("id");
        //组装信息
        TaskResultDTO dto = new TaskResultDTO();
        dto.setResultCode("approvalStatus");
        dto.setTaskId(taskId);
        if (!ValidationUtil.isEmpty(notice.getProcessAdvice())) {
            dto.setComment(notice.getProcessAdvice());
        }
        HashMap<String, Object> map = new HashMap<>();
        if (notice.getStatus().equals(WorkFlowStatusEnum.UNIT_RENAME_SUBMIT.getReject()) || notice.getStatus().equals(WorkFlowStatusEnum.UNIT_RENAME_SUBMIT.getRollBack())) {
            map.put("approvalStatus", "提交");
        } else {
            map.put("approvalStatus", op);
        }
        dto.setVariable(map);
        //执行流程
        AjaxResult ajaxResult1;
        try {
            ajaxResult1 = Workflow.taskClient.completeByTask(taskId, dto);
            if (ajaxResult1.get("code").equals(200)) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            log.error("提交失败：{}", e);
            return false;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void cancel(JgChangeRegistrationUnitDto noticeDto) {
        String lockKey = CommonServiceImpl.buildJgExecuteLockKey(noticeDto.getInstanceId());
        RLock lock = redissonClient.getLock(lockKey);
        try {
            boolean isLocked = lock.tryLock(0, 180, TimeUnit.SECONDS);
            // 解决并发问题：多个人同时操作一个流程（并发执行通过、驳回、撤回）
            if (!isLocked) {
                throw new BadRequest("当前流程已经被执行！请重新打开页面查看并执行！");
            }
            // 撤回校验
            commonServiceImpl.checkForRevocationFlow(noticeDto.getNextTaskId(), noticeDto.getInstanceId());

            String taskCode;
            List<String> roleListNext = new ArrayList<>();
            List<String> roleListAll = new ArrayList<>();
            ProcessTaskDTO processTaskDTO = cmWorkflowService.rollBack(noticeDto.getInstanceId());
            String nextTaskId = processTaskDTO.getNextTask().get(0).getId();
            this.buildRoleList(Collections.singletonList(processTaskDTO), roleListNext, roleListAll);
            List<WorkflowResultDto> workflowResultDtos = commonServiceImpl.buildWorkFlowInfo(Collections.singletonList(processTaskDTO));

            // 待调整
            taskCode = processTaskDTO.getNextTask().get(0).getKey();
            JgChangeRegistrationUnit changeUnit = this.baseMapper.selectById(noticeDto.getSequenceNbr());
            changeUnit.setStatus(WorkFlowStatusEnum.getMessage(taskCode).getRollBack());
            changeUnit.setPromoter("");
            changeUnit.setNextTaskId(nextTaskId);
            changeUnit.setNextExecuteUserIds(workflowResultDtos.get(0).getNextExecutorUserIds());
            changeUnit.setNextExecutorIds(String.join(",", roleListNext));
            JgChangeRegistrationUnitMapper.updateById(changeUnit);
            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(changeUnit));
            jsonObject.put("nextTaskId", changeUnit.getNextTaskId());
            jsonObject.put("nextExecuteUser", changeUnit.getNextExecutorIds());
            jsonObject.put("taskType", BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getCode());
            jsonObject.put("flowStatus", this.getTaskCodeByName(changeUnit.getStatus()));
            jsonObject.put("flowStatusLabel", changeUnit.getStatus());

            commonServiceImpl.rollbackTask(noticeDto.getInstanceId(), jsonObject);

            // redis流程实时数据更新
            commonServiceImpl.saveExecuteFlowData2Redis(noticeDto.getInstanceId(), this.buildInstanceRuntimeData(changeUnit));
            this.delRepeatUseEquipData(changeUnit.getApplyNo(), changeUnit.getStatus(), changeUnit.getNewUseUnitCreditCode());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
    }

    public InstanceRuntimeData buildInstanceRuntimeData(JgChangeRegistrationUnit jgChangeRegistrationUnit) {
        return InstanceRuntimeData.builder()
                .nextExecuteUserIds(jgChangeRegistrationUnit.getNextExecuteUserIds())
                .promoter(jgChangeRegistrationUnit.getPromoter())
                .nextTaskId(jgChangeRegistrationUnit.getNextTaskId())
                .build();
    }

    /**
     * @param dto
     * @param op          void
     * @param reginParams
     * @description 执行流程
     * @author wangguo
     * @date 2024/7/18 11:00
     **/
    @GlobalTransactional(rollbackFor = Exception.class, timeoutMills = 6000000)
    @Transactional(rollbackFor = Exception.class)
    public void accept(JgChangeRegistrationUnitDto dto, String op, ReginParams reginParams) {
        String lockKey = CommonServiceImpl.buildJgExecuteLockKey(dto.getInstanceId());
        RLock lock = redissonClient.getLock(lockKey);
        try {
            boolean isLocked = lock.tryLock(0, 180, TimeUnit.SECONDS);
            // 解决并发问题：多个人同时操作一个流程（并发执行通过、驳回、撤回）
            if (!isLocked) {
                throw new BadRequest("当前流程已经被执行！请重新打开页面查看并执行！");
            }
            // 流程执行时，状态及权限校验
            commonServiceImpl.checkForExecuteFlow(dto.getNextTaskId(), dto.getInstanceId());

            String[] taskName = new String[]{"已完成"};
            String userId = RequestContext.getExeUserId();
            String taskId = dto.getNextTaskId();
            JgChangeRegistrationUnit registration = this.JgChangeRegistrationUnitMapper.selectById(dto.getSequenceNbr());
            registration.setProcessAdvice(dto.getProcessAdvice());
            List<String> roleListAll = new ArrayList<>();
            ArrayList<String> roleList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(dto.getRegistrationList()) && EQU_CODE_CC.equals(dto.getEquListCode())) {
                dto.getRegistrationList().forEach(v -> {
                    String newCarNumber = String.valueOf(v.get("newCarNumber"));
                    String useOrgCode = String.valueOf(v.get("useRegistrationCode"));
                    if (!ObjectUtils.isEmpty(newCarNumber) && !"null".equals(newCarNumber) && "0".equals(op)) {
                        // 更新安全追溯-场内车辆中的“车牌号字段” 保证唯一性
                        Integer count = idxBizJgRegisterInfoMapper.checkCarNumberUniqueness(newCarNumber, useOrgCode);
                        if (count > 0) {
                            throw new BadRequest("车牌号已存在，请重新输入！");
                        }
                    }
                });
            }

            //组装信息
            TaskResultDTO task = new TaskResultDTO();
            task.setResultCode("approvalStatus");
            task.setTaskId(taskId);
            task.setComment(StringUtils.isEmpty(dto.getProcessAdvice()) ? "" : dto.getProcessAdvice());
            task.setNextExecuteUserCompanyCode(this.getNextUserOrgCode(op, registration));

            HashMap<String, Object> map = new HashMap<>();
            map.put("approvalStatus", op);
            if (!ObjectUtils.isEmpty(registration.getInstanceStatus()) &&
                    (registration.getStatus().equals(WorkFlowStatusEnum.UNIT_RENAME_SUBMIT.getReject()) ||
                            registration.getStatus().equals(WorkFlowStatusEnum.UNIT_RENAME_SUBMIT.getRollBack()))) {
                map.put("approvalStatus", "提交");
            }
            task.setVariable(map);
            //执行流程
            ProcessTaskDTO processTaskDTO = cmWorkflowService.completeOrReject(taskId, task, op);
            String taskCode = FlowStatusEnum.TO_BE_FINISHED.getName();
            String taskName1 = "";
            String nextTaskId = "";
            this.buildRoleList(Collections.singletonList(processTaskDTO), roleList, roleListAll);
            List<WorkflowResultDto> workflowResultDtos = commonServiceImpl.buildWorkFlowInfo(Collections.singletonList(processTaskDTO));
            String nextUserIds = workflowResultDtos.get(0).getNextExecutorUserIds();
            if (processTaskDTO != null && processTaskDTO.getNextTask() != null && processTaskDTO.getNextTask().size() > 0) {
                taskCode = processTaskDTO.getNextTask().get(0).getKey();
                taskName1 = processTaskDTO.getNextTask().get(0).getName();
                nextTaskId = processTaskDTO.getNextTask().get(0).getId();
            }
            registration.setStatus(taskName[0]);
            JgRegistrationHistory history = registrationHistoryService.lambdaQuery()
                    .eq(JgRegistrationHistory::getCurrentDocumentId, registration.getApplyNo())
                    .eq(JgRegistrationHistory::getIsDelete, false).one();
            if ("0".equals(op)) {
                if (roleList.isEmpty()) {
                    registration.setStatus(taskName[0]);
                    registration.setAuditPassDate(new Date());
                    //如果是工业管道 如果工程装置对应一个证就直接变更给新单位；如果工程装置对应证下还有其他装置就变更后直接给新单位再重新生成一个登记证号；如果两个工程装置对应同一个使用登记证变更后生成两个新的登记证
                    if (PipelineEnum.PRESSURE_PIPELINE.getCode().equals(dto.getEquListCode())) {
                        this.dealProjectContraption(dto, nextTaskId, registration, op);
                    }
                    //气瓶单位变更，如果新单位没有证，则生成新的使用登记证，如果新单位有证且是系统生成的证，则选择增补到已有的证下面同时，更新设备其他信息
                    else if(CylinderTypeEnum.CYLINDER.getCode().equals(dto.getEquCategoryCode())) {
                        this.dealCylinderAuditPassData(dto, nextTaskId, registration, op);
                    } else {
                        // 生成新的使用登记编号，使用登记证  修改设备的使用登记信息表，使用单位名称，使用单位统一信用代码，使用登记证编号，更新ES
                        LambdaQueryWrapper<JgChangeRegistrationUnitEq> queryWrapper = new LambdaQueryWrapper<>();
                        queryWrapper.eq(JgChangeRegistrationUnitEq::getUnitChangeRegistrationId, dto.getSequenceNbr());
                        List<JgChangeRegistrationUnitEq> jgChangeRegistrationUnitEqs = JgChangeRegistrationUnitEqMapper.selectList(queryWrapper);
                        //修改ES中使用单位信息和使用单位代码
                        Map<String, Map<String, Object>> resultMap = new HashMap<>();

                        //更新产权信息
                        dto.getRegistrationList().forEach(v -> {
                            String useCode = String.valueOf(v.get("useRegistrationCode"));
                            String newCarNumber = String.valueOf(v.get("newCarNumber"));
                            String estateUnitSeq = String.valueOf(v.get("estateUnitName"));
                            TzBaseEnterpriseInfo enterpriseInfo = (estateUnitSeq != null && !estateUnitSeq.isEmpty())
                                    ? tzBaseEnterpriseInfoMapper.selectById(estateUnitSeq)
                                    : null;
                            List<String> records = idxBizJgRegisterInfoService.getBaseMapper()
                                    .selectList(new LambdaQueryWrapper<IdxBizJgRegisterInfo>()
                                            .eq(IdxBizJgRegisterInfo::getUseOrgCode, useCode))
                                    .stream()
                                    .map(IdxBizJgRegisterInfo::getRecord)
                                    .collect(Collectors.toList());
                            if (!records.isEmpty() && enterpriseInfo != null) {
                                idxBizJgUseInfoService.update(
                                        new IdxBizJgUseInfo()
                                                .setEstateUnitCreditCode(enterpriseInfo.getUseUnitCode())
                                                .setEquState(EquimentEnum.ZAIYONG.getCode().toString())
                                                .setEstateUnitName(enterpriseInfo.getUseUnit()),
                                        new LambdaQueryWrapper<IdxBizJgUseInfo>()
                                                .in(IdxBizJgUseInfo::getRecord, records)
                                );
                            }
                            //场车更新车牌号
                            if (EQU_CODE_CC.equals(dto.getEquListCode()) && newCarNumber != null && !"null".equals(newCarNumber) && useCode != null) {
                                idxBizJgRegisterInfoService.update(new IdxBizJgRegisterInfo().setCarNumber(newCarNumber),
                                        new LambdaQueryWrapper<IdxBizJgRegisterInfo>().in(IdxBizJgRegisterInfo::getUseOrgCode, useCode)
                                );
                            }
                        });
                        Map<String, String> seqToEquId = new HashMap<>();
                        for (JgChangeRegistrationUnitEq jgChangeRegistrationUnitEq : jgChangeRegistrationUnitEqs) {
                            String equipId = jgChangeRegistrationUnitEq.getEquId();
                            LambdaQueryWrapper<IdxBizJgUseInfo> useInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
                            useInfoLambdaQueryWrapper.eq(IdxBizJgUseInfo::getRecord, equipId);
                            IdxBizJgUseInfo useInfo = useInfoMapper.selectOne(useInfoLambdaQueryWrapper);
                            if (ObjectUtils.isNotEmpty(useInfo)) {
                                useInfo.setUseUnitName(registration.getNewUseUnitName());
                                useInfo.setUseUnitCreditCode(registration.getNewUseUnitCreditCode());
                                // 只有变更类型为场车时，更新地址相关字段
                                if (EQU_CODE_CC.equals(registration.getEquListCode())) {
                                    this.updateUseInfoAddress(useInfo, registration);
                                    IdxBizJgSupervisionInfo supervisionInfo = new IdxBizJgSupervisionInfo();
                                    supervisionInfo.setOrgBranchCode(registration.getOrgBranchCode());
                                    supervisionInfo.setOrgBranchName(registration.getOrgBranchName());
                                    idxBizJgSupervisionInfoMapper.update(supervisionInfo, Wrappers.<IdxBizJgSupervisionInfo>lambdaQuery()
                                            .eq(IdxBizJgSupervisionInfo::getRecord, equipId));
                                }
                                useInfoMapper.updateById(useInfo);
                            }
                            LambdaQueryWrapper<IdxBizJgRegisterInfo> registerInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
                            registerInfoLambdaQueryWrapper.eq(IdxBizJgRegisterInfo::getRecord, equipId);
                            IdxBizJgRegisterInfo registerInfo = registerInfoMapper.selectOne(registerInfoLambdaQueryWrapper);
                            Map<String, Object> esUpdateParams = new HashMap<>();
                            String[] companyCode = registration.getNewUseUnitCreditCode().split("_");
                            esUpdateParams.put("USE_UNIT_CREDIT_CODE", companyCode.length > 1 ? companyCode[1] : registration.getNewUseUnitCreditCode());
                            esUpdateParams.put("USE_UNIT_NAME", registration.getNewUseUnitName());
                            esUpdateParams.put("REC_DATE", new Date());// 更新时间，设备筛选时排在前面 bug-21476
                            esUpdateParams.put("EQU_STATE", "1");
                            if (EQU_CODE_CC.equals(registration.getEquListCode())) {
                                esUpdateParams.put("USE_PLACE", joinWithSeparator("/", useInfo.getProvinceName(), useInfo.getCityName(), useInfo.getCountyName(), useInfo.getStreetName()));
                                esUpdateParams.put("USE_PLACE_CODE", joinWithSeparator("#", useInfo.getProvince(), useInfo.getCity(), useInfo.getCounty(), useInfo.getFactoryUseSiteStreet()));
                                esUpdateParams.put("ORG_BRANCH_CODE", registration.getOrgBranchCode());
                                esUpdateParams.put("ORG_BRANCH_NAME", registration.getOrgBranchName());
                            }
                            // 如果是变更类型=1，还需要更新证号到 ES
                            if ("1".equals(registration.getChangeType()) && EQU_CODE_CC.equals(dto.getEquListCode())) {
                                // 新证号生成逻辑
                                String newCode = commonServiceImpl.generateRegistrationCode(equipId, registration.getReceiveCompanyCode(), false);
                                esUpdateParams.put("USE_ORG_CODE", newCode);
                                seqToEquId.put(registerInfo.getUseOrgCode(), newCode);
                                registerInfo.setUseOrgCode(newCode);
                                registerInfoMapper.updateById(registerInfo);
                            }
                            if (ObjectUtils.isNotEmpty(registerInfo) && ObjectUtils.isNotEmpty(registerInfo.getCarNumber())) {
                                esUpdateParams.put("CAR_NUMBER", registerInfo.getCarNumber());
                            }
                            resultMap.put(equipId, esUpdateParams);
                            //修改各类告知列表，置为废弃
                            this.updataInvalidStatusByHistory(equipId, registration.getApplyNo());
                            tzsServiceFeignClient.commonUpdateEsDataByIds(resultMap);
                        }

                        registration.setNextTaskId(nextTaskId);
                        TaskV2Model taskV2Model = updateTaskModel(registration, op);

                        JSONArray historyJson = JSON.parseArray(history.getChangeData());
                        // 更新历史表中一二级审批车牌号
                        history.setChangeData(JSONObject.toJSONString(dto.getRegistrationList()));
                        registrationHistoryService.updateById(history);
                        List<String> ids = historyJson.stream().map(v -> JSON.parseObject(v.toString()).getString(SEQUENCE_NBR)).collect(Collectors.toList());
                        List<JgUseRegistrationManage> list = useRegistrationManageService.lambdaQuery().in(BaseEntity::getSequenceNbr, ids).list();
                        // 用来存放新生成的登记证
                        List<JgUseRegistrationManage> newList = new ArrayList<>();
                        for (JgUseRegistrationManage registrationManage : list) {
                            // 修改使用登记证管理使用单位信息
                            registrationManage.setUseUnitName(registration.getNewUseUnitName());
                            registrationManage.setUseUnitCreditCode(registration.getNewUseUnitCreditCode());
                            registrationManage.setReceiveOrgName(registration.getReceiveOrgName());
                            registrationManage.setReceiveCompanyCode(registration.getReceiveOrgCode());
                            registrationManage.setAuditPassDate(registration.getAuditPassDate());
                            registrationManage.setVersion(registrationManage.getVersion() + 1);
                            if ("1".equals(registration.getChangeType()) && EQU_CODE_CC.equals(dto.getEquListCode())) {
                                // -------------------- 旧证处理 --------------------
                                registrationManage.setCertificateStatus("已注销");
                                registrationManage.setChangeReason(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getName());

                                // -------------------- 新证处理 --------------------
                                JgUseRegistrationManage newManage = new JgUseRegistrationManage();
                                BeanUtils.copyProperties(registrationManage, newManage);

                                String newCode = seqToEquId.get(registrationManage.getUseRegistrationCode());
                                newManage.setUseRegistrationCode(newCode);
                                registration.setUseRegistCode(newCode);
                                // 新证属性重置
                                newManage.setCertificateStatus("已登记");
                                newManage.setVersion(1);
                                newManage.setEquUseAddress(registration.getEquAddress());
                                // 如果 equListCode = 5000，则设置车牌号
                                if (EQU_CODE_CC.equals(registrationManage.getEquListCode())) {
                                    String targetCode = registrationManage.getUseRegistrationCode();
                                    dto.getRegistrationList().stream()
                                            .filter(item -> targetCode.equals(item.get("useRegistrationCode")))
                                            .map(item -> String.valueOf(item.get("newCarNumber")))
                                            .filter(carNumber -> carNumber != null && !"null".equalsIgnoreCase(carNumber.trim()))
                                            .findFirst()
                                            .ifPresent(newManage::setCarNumber);
                                }

                                newManage.setChangeReason(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getName());

                                // 回填新单位地址
                                if (!ObjectUtils.isEmpty(registration.getNewUseUnitCreditCode())) {
                                    TzBaseEnterpriseInfo useCodeResult = tzBaseEnterpriseInfoMapper.selectOne(
                                            new QueryWrapper<TzBaseEnterpriseInfo>()
                                                    .eq("use_unit_code", registration.getNewUseUnitCreditCode())
                                    );
                                    if (useCodeResult != null) {
                                        newManage.setUseUnitAddress(useCodeResult.getAddress());
                                    }
                                }
                                newManage.setSequenceNbr(null);
                                newList.add(newManage);
                            } else {
                                registrationManage.setCertificateStatus("已登记");
                                registrationManage.setEquUseAddress(registration.getEquAddress());
                                if (EQU_CODE_CC.equals(registrationManage.getEquListCode())) {
                                    String targetCode = registrationManage.getUseRegistrationCode();
                                    dto.getRegistrationList().stream()
                                            .filter(item -> targetCode.equals(item.get("useRegistrationCode")))
                                            .map(item -> String.valueOf(item.get("newCarNumber")))
                                            .filter(carNumber -> carNumber != null && !"null".equalsIgnoreCase(carNumber.trim()))
                                            .findFirst()
                                            .ifPresent(registrationManage::setCarNumber);
                                }
                                registrationManage.setChangeReason(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getName());
                                if (!ObjectUtils.isEmpty(registration.getNewUseUnitCreditCode())) {
                                    TzBaseEnterpriseInfo useCodeResult = tzBaseEnterpriseInfoMapper.selectOne(
                                            new QueryWrapper<TzBaseEnterpriseInfo>()
                                                    .eq("use_unit_code", registration.getNewUseUnitCreditCode())
                                    );
                                    if (useCodeResult != null) {
                                        registrationManage.setUseUnitAddress(useCodeResult.getAddress());
                                    }
                                }
                            }
                        }

                        if (CollUtil.isNotEmpty(newList)) {
                            list.addAll(newList);
                        }
                        useRegistrationManageService.saveOrUpdateBatch(list);
                        this.createResume(registration,jgChangeRegistrationUnitEqs, taskV2Model.getRoutePath());
                    }
                    //新增使用登记证和设备变更记录
                    this.saveRecord(registration);
                    // 发送数据变更消息
                    this.sendDataRefreshMsg(registration);
                } else {
                    registration.setNextExecutorIds(String.join(",", roleList));
                    if (!ObjectUtils.isEmpty(registration.getInstanceStatus())) {
                        registration.setInstanceStatus(registration.getInstanceStatus() + "," + String.join(",", roleList));
                    } else {
                        registration.setInstanceStatus(String.join(",", roleList));
                    }
                    registration.setPromoter(userId);
                    registration.setNextExecuteUserIds(nextUserIds);
                    registration.setNextTaskId(nextTaskId);
                    registration.setStatus(WorkFlowStatusEnum.getMessage(taskCode).getPass());
                    executeOneStep(registration, taskName1, nextUserIds, op);
                    if (EQU_CODE_CC.equals(dto.getEquListCode())) {
                        history.setChangeData(JSONObject.toJSONString(dto.getRegistrationList()));
                        registrationHistoryService.updateById(history);
                    }
                }
            } else {
                registration.setPromoter("");
                registration.setNextExecutorIds(String.join(",", roleList));
                registration.setNextTaskId(nextTaskId);
                registration.setNextExecuteUserIds(nextUserIds);
                registration.setStatus(WorkFlowStatusEnum.getMessage(taskCode).getReject());
                executeOneStep(registration, taskName1, nextUserIds, op);
            }
            JgChangeRegistrationUnitMapper.updateById(registration);
            // redis流程实时数据更新
            commonServiceImpl.saveExecuteFlowData2Redis(dto.getInstanceId(), this.buildInstanceRuntimeData(registration));
            this.delRepeatUseEquipData(registration.getApplyNo(), registration.getStatus(), registration.getNewUseUnitCreditCode());
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new BadRequest("数据异常，请联系管理员");
        } finally {
            if (lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
    }

    /**
     * 处理气瓶单位变更三级审核完成后数据
     * @param dto 变更数据
     * @param nextTaskId taskId
     * @param registration 单位变更主表数据
     * @param op 通过
     */
    private void dealCylinderAuditPassData1(JgChangeRegistrationUnitDto dto, String nextTaskId, JgChangeRegistrationUnit registration, String op) {
        List<Map<String, Object>> deviceList = dto.getDeviceList();
        if (CollUtil.isEmpty(deviceList)) {
            throw new BadRequest("设备列表不能为空！");
        }
        registration.setNextTaskId(nextTaskId);
        TaskV2Model taskV2Model = updateTaskModel(registration, op);
        AtomicReference<Boolean> flag = new AtomicReference<>(Boolean.FALSE);
        String useRegistrationCode;
        JgUseRegistrationManageDto manageDto = new JgUseRegistrationManageDto();
        manageDto.setCertificateStatus(CertificateStatusEnum.YIDENGJI.getName());
        manageDto.setUseUnitCreditCode(registration.getNewUseUnitCreditCode());
        manageDto.setRegType("0");
        useRegistrationCode = Optional.ofNullable(jgUseRegistrationManageMapper.queryByUseUnitCreditCode(manageDto))
                .filter(list -> !list.isEmpty())
                .flatMap(list -> list.stream()
                        .map(JgUseRegistrationManageDto::getUseRegistrationCode)
                        .filter(code -> code.startsWith("瓶30"))
                        .findFirst())
                .map(code -> {
                    flag.set(Boolean.TRUE);
                    return code;
                }).orElse(commonServiceImpl.generateRegistrationCode(Objects.toString(deviceList.get(0).get(RECORD)), dto.getReceiveCompanyCode(), true));

        List<String> records = new ArrayList<>();
        Map<String, Map<String, Object>> resultMap = new HashMap<>();

        TzBaseEnterpriseInfo enterpriseInfo = Optional.ofNullable(dto.getEstateUnitSeq())
                .filter(ObjectUtils::isNotEmpty)
                .map(tzBaseEnterpriseInfoMapper::selectById)
                .orElse(null);

        for (Map<String, Object> map : deviceList) {
            String record = MapUtils.getString(map, RECORD);
            records.add(record);
            LambdaQueryWrapper<IdxBizJgUseInfo> useInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            useInfoLambdaQueryWrapper.eq(IdxBizJgUseInfo::getRecord, record);
            IdxBizJgUseInfo useInfo = useInfoMapper.selectOne(useInfoLambdaQueryWrapper);

            //更新产权信息
            if (ObjectUtils.isNotEmpty(useInfo)) {
                if (enterpriseInfo != null) {
                    useInfo.setEstateUnitCreditCode(enterpriseInfo.getUseUnitCode()).setEstateUnitName(enterpriseInfo.getUseUnit());
                }
                useInfoMapper.updateById(useInfo
                        .setPhone(dto.getPhone())
                        .setUseUnitCreditCode(registration.getNewUseUnitCreditCode())
                        .setUseUnitName(registration.getNewUseUnitName())
                        .setSafetyManager(Optional.ofNullable(dto.getSafetyManager())
                                .map(s -> s.split(","))
                                .filter(arr -> arr.length > 1)
                                .map(arr -> arr[1])
                                .orElse(""))
                        .setProvinceName("陕西省")
                        .setProvince(dto.getProvince())
                        .setCity(dto.getCity())
                        .setCityName(getRegionNameByCode("city", dto.getCity()))
                        .setCounty(dto.getCounty())
                        .setCountyName(getRegionNameByCode("region", dto.getCounty()))
                        .setFactoryUseSiteStreet(dto.getStreet())
                        .setStreetName(getRegionNameByCode("street", dto.getStreet()))
                        .setAddress(dto.getFullAddress())
                        .setCity(dto.getCity())
                        .setCounty(dto.getCounty())
                        .setFactoryUseSiteStreet(dto.getStreet())
                        .setUseDate(dto.getUseDate()));
            }
            //更新属地信息
            IdxBizJgSupervisionInfo idxBizJgSupervisionInfo = new IdxBizJgSupervisionInfo();
            if (!ObjectUtils.isEmpty(dto.getOrgBranchCode())) {
                HashMap<String, Object> parentMessage = (HashMap<String, Object>) Privilege.companyClient.queryByOrgcode(registration.getOrgBranchCode()).getResult();
                CompanyModel parentModel = JSON.parseObject(JSON.toJSONString(parentMessage.get("compnay")), CompanyModel.class);
                idxBizJgSupervisionInfo.setOrgBranchCode(registration.getOrgBranchCode());
                idxBizJgSupervisionInfo.setOrgBranchName(registration.getOrgBranchName());
                idxBizJgSupervisionInfo.setCompanyOrgBranchCode(parentModel.getCompanyCode());
                LambdaQueryWrapper<IdxBizJgSupervisionInfo> eq = new QueryWrapper<IdxBizJgSupervisionInfo>().lambda()
                        .eq(IdxBizJgSupervisionInfo::getRecord, record);
                idxBizJgSupervisionInfoMapper.update(idxBizJgSupervisionInfo, eq);
            }
            //更新注册信息
            LambdaUpdateWrapper<IdxBizJgRegisterInfo> lambda = new UpdateWrapper<IdxBizJgRegisterInfo>().lambda();
            lambda.eq(IdxBizJgRegisterInfo::getRecord, record);
            lambda.set(true, IdxBizJgRegisterInfo::getUseOrgCode, useRegistrationCode);
            idxBizJgRegisterInfoService.update(lambda);

            Map<String, Object> esUpdateParams = new HashMap<>();
            esUpdateParams.put("USE_UNIT_CREDIT_CODE", registration.getNewUseUnitCreditCode());
            esUpdateParams.put("USE_UNIT_NAME", registration.getNewUseUnitName());
            esUpdateParams.put("REC_DATE", new Date());
            esUpdateParams.put("USE_PLACE", joinWithSeparator("/", useInfo.getProvinceName(), useInfo.getCityName(), useInfo.getCountyName(), useInfo.getStreetName()));
            esUpdateParams.put("USE_PLACE_CODE", joinWithSeparator("#", useInfo.getProvince(), useInfo.getCity(), useInfo.getCounty(), useInfo.getFactoryUseSiteStreet()));
            esUpdateParams.put("ORG_BRANCH_CODE", registration.getOrgBranchCode());
            esUpdateParams.put("ORG_BRANCH_NAME", registration.getOrgBranchName());
            esUpdateParams.put("USE_ORG_CODE", useRegistrationCode);
            resultMap.put(record, esUpdateParams);
            tzsServiceFeignClient.commonUpdateEsDataByIds(resultMap);
        }
        JgUseRegistrationManage registrationManage;
        if (flag.get()){
            //获取使用登记证
            LambdaQueryWrapper<JgUseRegistrationManage> useRegistrationManageWrapper = new LambdaQueryWrapper<>();
            useRegistrationManageWrapper.eq(JgUseRegistrationManage::getUseRegistrationCode, useRegistrationCode)
                    .isNotNull(JgUseRegistrationManage::getUseRegistrationCode)
                    .eq(JgUseRegistrationManage::getCertificateStatus, "已登记")
                    .eq(BaseEntity::getIsDelete, false);
            registrationManage = useRegistrationManageService.getBaseMapper().selectOne(useRegistrationManageWrapper);
            //修改使用登记证管理使用单位地址、使用单位代码、接收机构、接收机构公司代码、办理日期、使用单位地址
            registrationManage.setUseUnitName(registration.getNewUseUnitName());
            registrationManage.setUseUnitCreditCode(registration.getNewUseUnitCreditCode());
            registrationManage.setReceiveOrgName(registration.getReceiveOrgName());
            registrationManage.setReceiveCompanyCode(registration.getReceiveOrgCode());
            registrationManage.setAuditPassDate(registration.getAuditPassDate());
            registrationManage.setVersion(registrationManage.getVersion() + 1);
            registrationManage.setChangeReason(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getName());
            if (!ObjectUtils.isEmpty(registration.getNewUseUnitCreditCode())) {//回填新单位地址
                TzBaseEnterpriseInfo useCodeResult = tzBaseEnterpriseInfoMapper.selectOne(new QueryWrapper<TzBaseEnterpriseInfo>().eq("use_unit_code", registration.getNewUseUnitCreditCode()));
                if (useCodeResult != null) {
                    registrationManage.setUseUnitAddress(useCodeResult.getAddress());
                }
            }
            registrationManage.setRecUserId(registration.getCreateUserId());
            useRegistrationManageService.updateById(registrationManage);
        }else{
            registrationManage = new  JgUseRegistrationManage();
            registrationManage.setSequenceNbr(sequence.nextId());
            registrationManage.setUseRegistrationCode(useRegistrationCode);
            //修改使用登记证管理使用单位地址、使用单位代码、接收机构、接收机构公司代码、办理日期、使用单位地址
            registrationManage.setUseUnitName(registration.getNewUseUnitName());
            registrationManage.setUseUnitCreditCode(registration.getNewUseUnitCreditCode());
            registrationManage.setReceiveOrgName(registration.getReceiveOrgName());
            registrationManage.setReceiveCompanyCode(registration.getReceiveOrgCode());
            registrationManage.setAuditPassDate(registration.getAuditPassDate());
            registrationManage.setVersion(1);
            registrationManage.setRegType("0");
            registrationManage.setChangeReason(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getName());
            if (!ObjectUtils.isEmpty(registration.getNewUseUnitCreditCode())) {
                TzBaseEnterpriseInfo useCodeResult = tzBaseEnterpriseInfoMapper.selectOne(new QueryWrapper<TzBaseEnterpriseInfo>().eq("use_unit_code", registration.getNewUseUnitCreditCode()));
                if (useCodeResult != null) {
                    registrationManage.setUseUnitAddress(useCodeResult.getAddress());
                }
            }
            registrationManage.setRecUserId(registration.getCreateUserId());
            useRegistrationManageService.save(registrationManage);
        }
        // 发送数据变更消息
        this.sendDataRefreshMsg(registration);
        //新增使用登记证和设备变更记录
        this.saveRecord(registration);
        //创建设备履历
        this.createResume(records, registration, taskV2Model.getRoutePath());
        JgChangeRegistrationUnitMapper.updateById(registration);
        // redis流程实时数据更新
        commonServiceImpl.saveExecuteFlowData2Redis(dto.getInstanceId(), this.buildInstanceRuntimeData(registration));
        this.delRepeatUseEquipData(registration.getApplyNo(), registration.getStatus(), registration.getNewUseUnitCreditCode());
    }

    /**
     * 处理气瓶单位变更三级审核完成后数据
     */
    private void dealCylinderAuditPassData(JgChangeRegistrationUnitDto dto, String nextTaskId, JgChangeRegistrationUnit registration, String op) {
        List<Map<String, Object>> deviceList = dto.getDeviceList();
        if (CollUtil.isEmpty(deviceList)) {
            throw new BadRequest("设备列表不能为空！");
        }

        // 1. 更新代办
        registration.setNextTaskId(nextTaskId);
        TaskV2Model taskV2Model = updateTaskModel(registration, op);

        // 2. 确定使用登记证代码（优先复用已有，否者生成）
        AtomicReference<Boolean> existedRegistrationFlag = new AtomicReference<>(Boolean.FALSE);
        String useRegistrationCode = findOrGenerateUseRegistrationCode(dto, registration, deviceList, existedRegistrationFlag);

        // 3. ES批量更新参数 & 需要变更的设备列表
        List<String> records = new ArrayList<>(deviceList.size());
        Map<String, Map<String, Object>> esUpdateMap = new HashMap<>();

        // 4. 更新产权单位信息
        TzBaseEnterpriseInfo enterpriseInfo = Optional.ofNullable(dto.getEstateUnitSeq())
                .filter(ObjectUtils::isNotEmpty)
                .map(tzBaseEnterpriseInfoMapper::selectById)
                .orElse(null);
        // 提取设备信息（只取第一个）
        if (CollUtil.isNotEmpty(deviceList)) {
            Map<String, Object> first = deviceList.get(0);
            registration.setEquCategory(MapUtils.getString(first, "equCategoryName"));
            registration.setEquDefine(MapUtils.getString(first, "equDefineName"));
            registration.setEquDefineCode(MapUtils.getString(first, "equDefine"));
        }
        // 5.更新useInfo、属地、注册信息，准备ES更新内容
        for (Map<String, Object> device : deviceList) {
            String record = MapUtils.getString(device, RECORD);
            records.add(record);
            IdxBizJgUseInfo useInfo = fetchUseInfoByRecord(record);
            if (useInfo != null) {
                updateUseInfoFields(useInfo, dto, registration, enterpriseInfo);
                useInfoMapper.updateById(useInfo);
            }

            // 更新属地
            if (!ObjectUtils.isEmpty(dto.getOrgBranchCode())) {
                updateSupervisionInfo(record, registration);
            }
            // 更新注册表中 useOrgCode
            updateRegisterInfo(record, useRegistrationCode);
            // 准备 ES 更新参数
            Map<String, Object> esParams = buildEsUpdateParams(useInfo, registration, useRegistrationCode);
            esUpdateMap.put(record, esParams);
        }

        // 6. 批量调用ES更新
        if (!esUpdateMap.isEmpty()) {
            tzsServiceFeignClient.commonUpdateEsDataByIds(esUpdateMap);
        }

        // 7. 新增或更新使用登记证管理表
        upsertUseRegistrationManage(registration, useRegistrationCode, existedRegistrationFlag.get());

        // 8. 发送变更消息、保存变更记录、创建设备履历等后置处理
        this.sendDataRefreshMsg(registration);
        this.saveRecord(registration);
        this.createResume(records, registration, taskV2Model.getRoutePath());

        // 9. 更新主表并刷新 redis 实时流程数据
        JgChangeRegistrationUnitMapper.updateById(registration);
        commonServiceImpl.saveExecuteFlowData2Redis(dto.getInstanceId(), this.buildInstanceRuntimeData(registration));

        // 10. 删除重复使用标识
        this.delRepeatUseEquipData(registration.getApplyNo(), registration.getStatus(), registration.getNewUseUnitCreditCode());
    }

    private String findOrGenerateUseRegistrationCode(JgChangeRegistrationUnitDto dto, JgChangeRegistrationUnit registration,
                                                     List<Map<String, Object>> deviceList, AtomicReference<Boolean> existedFlag) {

        // 查询新单位存在的已登记使用登记证（只取以 "瓶30" 开头的）系统生成的证号
        JgUseRegistrationManageDto queryDto = new JgUseRegistrationManageDto();
        queryDto.setCertificateStatus(CertificateStatusEnum.YIDENGJI.getName());
        queryDto.setUseUnitCreditCode(registration.getNewUseUnitCreditCode());
        List<JgUseRegistrationManageDto> found = jgUseRegistrationManageMapper.queryByUseUnitCreditCode(queryDto);
        if (!CollectionUtils.isEmpty(found)) {
            for (JgUseRegistrationManageDto m : found) {
                String code = m.getUseRegistrationCode();
                if (StringUtils.hasText(code) && code.startsWith("瓶30")) {
                    existedFlag.set(Boolean.TRUE);
                    return code;
                }
            }
        }

        // 若企业没有系统生成的使用登记证，则生成（使用第一个设备ID）
        String firstRecord = Objects.toString(deviceList.get(0).get(RECORD), null);
        return commonServiceImpl.generateRegistrationCode(firstRecord, dto.getReceiveCompanyCode(), true);
    }

    private IdxBizJgUseInfo fetchUseInfoByRecord(String record) {
        LambdaQueryWrapper<IdxBizJgUseInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IdxBizJgUseInfo::getRecord, record);
        return useInfoMapper.selectOne(wrapper);
    }

    private void updateUseInfoFields(IdxBizJgUseInfo useInfo, JgChangeRegistrationUnitDto dto, JgChangeRegistrationUnit registration,
                                     TzBaseEnterpriseInfo enterpriseInfo) {
        if (enterpriseInfo != null) {
            useInfo.setEstateUnitCreditCode(enterpriseInfo.getUseUnitCode())
                    .setEstateUnitName(enterpriseInfo.getUseUnit());
        }

        String safetyManager = Optional.ofNullable(dto.getSafetyManager())
                .map(s -> s.split(","))
                .filter(arr -> arr.length > 1)
                .map(arr -> arr[1])
                .orElse("");

        useInfo.setPhone(dto.getPhone());
        useInfo.setUseUnitCreditCode(registration.getNewUseUnitCreditCode());
        useInfo.setUseUnitName(registration.getNewUseUnitName());
        useInfo.setSafetyManager(safetyManager);

        // 地区/地址字段回填（保持原字段赋值顺序与语义）
        useInfo.setProvinceName("陕西省")
                .setProvince(dto.getProvince())
                .setCity(dto.getCity())
                .setCityName(getRegionNameByCode("city", dto.getCity()))
                .setCounty(dto.getCounty())
                .setCountyName(getRegionNameByCode("region", dto.getCounty()))
                .setFactoryUseSiteStreet(dto.getStreet())
                .setStreetName(getRegionNameByCode("street", dto.getStreet()))
                .setAddress(dto.getFullAddress())
                .setUseDate(dto.getUseDate());
    }

    private void updateSupervisionInfo(String record, JgChangeRegistrationUnit registration) {
        // 查询上级公司信息（保留原逻辑）
        HashMap<String, Object> parentMessage = (HashMap<String, Object>) Privilege.companyClient.queryByOrgcode(registration.getOrgBranchCode()).getResult();
        CompanyModel parentModel = JSON.parseObject(JSON.toJSONString(parentMessage.get("compnay")), CompanyModel.class);

        IdxBizJgSupervisionInfo supervision = new IdxBizJgSupervisionInfo();
        supervision.setOrgBranchCode(registration.getOrgBranchCode());
        supervision.setOrgBranchName(registration.getOrgBranchName());
        supervision.setCompanyOrgBranchCode(parentModel.getCompanyCode());

        LambdaQueryWrapper<IdxBizJgSupervisionInfo> eq = new QueryWrapper<IdxBizJgSupervisionInfo>().lambda()
                .eq(IdxBizJgSupervisionInfo::getRecord, record);
        idxBizJgSupervisionInfoMapper.update(supervision, eq);
    }

    private void updateRegisterInfo(String record, String useRegistrationCode) {
        LambdaUpdateWrapper<IdxBizJgRegisterInfo> update = new UpdateWrapper<IdxBizJgRegisterInfo>().lambda()
                .eq(IdxBizJgRegisterInfo::getRecord, record)
                .set(true, IdxBizJgRegisterInfo::getUseOrgCode, useRegistrationCode);
        idxBizJgRegisterInfoService.update(update);
    }

    private Map<String, Object> buildEsUpdateParams(IdxBizJgUseInfo useInfo, JgChangeRegistrationUnit registration, String useRegistrationCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("USE_UNIT_CREDIT_CODE", registration.getNewUseUnitCreditCode());
        params.put("USE_UNIT_NAME", registration.getNewUseUnitName());
        params.put("REC_DATE", new Date());
        if (useInfo != null) {
            params.put("USE_PLACE", joinWithSeparator("/", useInfo.getProvinceName(), useInfo.getCityName(), useInfo.getCountyName(), useInfo.getStreetName()));
            params.put("USE_PLACE_CODE", joinWithSeparator("#", useInfo.getProvince(), useInfo.getCity(), useInfo.getCounty(), useInfo.getFactoryUseSiteStreet()));
        }
        params.put("ORG_BRANCH_CODE", registration.getOrgBranchCode());
        params.put("ORG_BRANCH_NAME", registration.getOrgBranchName());
        params.put("USE_ORG_CODE", useRegistrationCode);
        return params;
    }

    private void upsertUseRegistrationManage(JgChangeRegistrationUnit registration, String useRegistrationCode, boolean existed) {
        Map<String, String> equType = new HashMap<>();
        equType.put("equListCode", registration.getEquListCode());
        equType.put("equCategoryCode", registration.getEquCategoryCode());
        if (existed) {
            // 查询并更新已有管理表记录
            LambdaQueryWrapper<JgUseRegistrationManage> q = new LambdaQueryWrapper<>();
            q.eq(JgUseRegistrationManage::getUseRegistrationCode, useRegistrationCode)
                    .isNotNull(JgUseRegistrationManage::getUseRegistrationCode)
                    .eq(JgUseRegistrationManage::getCertificateStatus, "已登记")
                    .eq(BaseEntity::getIsDelete, false);
            JgUseRegistrationManage manage = useRegistrationManageService.getBaseMapper().selectOne(q);
            if (manage != null) {
                manage.setAuditPassDate(registration.getAuditPassDate());
                manage.setVersion(manage.getVersion() + 1);
                manage.setChangeReason(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getName());
                manage.setRecUserId(registration.getCreateUserId());
                useRegistrationManageService.updateById(manage);
            }
        } else {
            // 新增证管理信息
            JgUseRegistrationManage newManage = new JgUseRegistrationManage();
            newManage.setSequenceNbr(sequence.nextId());
            newManage.setUseRegistrationCode(useRegistrationCode);
            newManage.setUseUnitName(registration.getNewUseUnitName());
            newManage.setUseUnitCreditCode(registration.getNewUseUnitCreditCode());
            newManage.setReceiveOrgName(registration.getReceiveOrgName());
            newManage.setReceiveCompanyCode(registration.getReceiveOrgCode());
            newManage.setAuditPassDate(registration.getAuditPassDate());
            newManage.setVersion(1);
            newManage.setRegType("使用登记");
            newManage.setManageType("unit");
            newManage.setApplyNo(registration.getApplyNo());
            newManage.setEquUseAddress(registration.getEquAddress());
            newManage.setRegDate(new Date());
            newManage.setEquList(registration.getEquList());
            newManage.setEquListCode(registration.getEquListCode());
            newManage.setEquCategory(registration.getEquCategory());
            newManage.setEquCategoryCode(registration.getEquCategoryCode());
            newManage.setEquDefine(registration.getEquDefine());
            newManage.setEquDefineCode(registration.getEquDefineCode());
            newManage.setCertificateStatus(CertificateStatusEnum.YIDENGJI.getName());
            newManage.setChangeReason(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getName());
            newManage.setRecUserName(registration.getCreateUserName());
            newManage.setRecUserId(registration.getCreateUserId());
            newManage.setRecDate(new Date());
            newManage.setCreateUserId(registration.getCreateUserId());
            newManage.setCreateDate(new Date());
            if (!ObjectUtils.isEmpty(registration.getNewUseUnitCreditCode())) {
                TzBaseEnterpriseInfo info = tzBaseEnterpriseInfoMapper.selectOne(new QueryWrapper<TzBaseEnterpriseInfo>().eq("use_unit_code", registration.getNewUseUnitCreditCode()));
                if (info != null) {
                    newManage.setUseUnitAddress(info.getAddress());
                }
            }
            newManage.setSuperviseOrgCode(registration.getOrgBranchCode());
            newManage.setSuperviseOrgName(registration.getOrgBranchName());
            newManage.setCertificateNo(commonServiceImpl.generateCertificateNo(equType, new Date(), registration.getReceiveCompanyCode()));
            useRegistrationManageService.save(newManage);
        }
    }

    private void updateUseInfoAddress(IdxBizJgUseInfo useInfo, JgChangeRegistrationUnit registration) {
        useInfo.setProvince(registration.getProvince());
        useInfo.setCity(registration.getCity());
        useInfo.setCounty(registration.getCounty());
        useInfo.setFactoryUseSiteStreet(registration.getStreet());
        useInfo.setAddress(registration.getFullAddress());
        useInfo.setProvinceName(getRegionNameByCode("province", useInfo.getProvince()));
        useInfo.setCityName(getRegionNameByCode("city", useInfo.getCity()));
        useInfo.setCountyName(getRegionNameByCode("region", useInfo.getCounty()));
        useInfo.setStreetName(getRegionNameByCode("street", useInfo.getFactoryUseSiteStreet()));
    }

    private String joinWithSeparator(String separator, String... values) {
        return Arrays.stream(values)
                .map(v -> ObjectUtils.defaultIfNull(v, ""))
                .collect(Collectors.joining(separator));
    }

    private void createResume(JgChangeRegistrationUnit registration, List<JgChangeRegistrationUnitEq> jgChangeRegistrationUnitEqs, String routePath) {
        jgResumeInfoService.saveBatchResume(jgChangeRegistrationUnitEqs.stream().map(eq-> JgResumeInfoDto.builder().applyNo(registration.getApplyNo())
                .businessType(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getName())
                .businessId(String.valueOf(registration.getSequenceNbr()))
                .equId(eq.getEquId())
                .approvalUnit(registration.getReceiveOrgName())
                .approvalUnitCode(registration.getReceiveCompanyCode())
                .status("正常")
                .changeContent(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getName() + "业务办理")
                .routePath(routePath)
                .build()).collect(Collectors.toList())
        );
    }

    private void sendDataRefreshMsg(JgChangeRegistrationUnit registration) {
        LambdaQueryWrapper<JgChangeRegistrationUnitEq> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(JgChangeRegistrationUnitEq::getUnitChangeRegistrationId, registration.getSequenceNbr());
        queryWrapper.select(JgChangeRegistrationUnitEq::getEquId);
        List<String> records = JgChangeRegistrationUnitEqMapper.selectList(queryWrapper).stream().map(JgChangeRegistrationUnitEq::getEquId).filter(Objects::nonNull).collect(Collectors.toList());
        eventPublisher.publish(new DataRefreshEvent(this, records, DataRefreshEvent.DataType.equipment.name(), DataRefreshEvent.Operation.UPDATE));
    }

    /**
     * 处理工业管道
     *
     * @param dto
     * @param nextTaskId
     * @param registration
     */
    private void dealProjectContraption(JgChangeRegistrationUnitDto dto, String nextTaskId, JgChangeRegistrationUnit registration, String op) {
        List<Map<String, Object>> projectContraptionList = dto.getProjectContraptionList();
        if (CollUtil.isEmpty(projectContraptionList)) {
            throw new BadRequest("工程/装置列表为空");
        }
        registration.setNextTaskId(nextTaskId);
        TaskV2Model taskV2Model = updateTaskModel(registration, op);
        List<String> projectContraptionIds = new ArrayList<>();
        for (Map<String, Object> map : projectContraptionList) {
            String useRegistrationCode = (String) map.get("useRegistrationCode");
            String sequenceNbr = (String) map.get(SEQUENCE_NBR);
            projectContraptionIds.add(sequenceNbr);
            String estateUnitSeq = String.valueOf(map.get("estateUnitName"));
            TzBaseEnterpriseInfo enterpriseInfo = (estateUnitSeq != null && !estateUnitSeq.isEmpty())
                    ? tzBaseEnterpriseInfoMapper.selectById(estateUnitSeq)
                    : null;
            LambdaQueryWrapper<IdxBizJgProjectContraption> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(IdxBizJgProjectContraption::getUseRegistrationCode, useRegistrationCode)
                    .isNotNull(IdxBizJgProjectContraption::getUseRegistrationCode)
                    .eq(BaseEntity::getIsDelete, false);
            List<IdxBizJgProjectContraption> jgProjectContraptions = projectContraptionService.list(wrapper);
            if (CollectionUtils.isEmpty(jgProjectContraptions)) {
                throw new BadRequest("使用登记证编号：" + useRegistrationCode + "对应的工程/装置列表不存在");
            }
            //更新产权信息
            if (enterpriseInfo != null) {
                idxBizJgUseInfoService.update(
                        new IdxBizJgUseInfo().setEstateUnitCreditCode(enterpriseInfo.getUseUnitCode())
                                .setEstateUnitName(enterpriseInfo.getUseUnit()),
                        new LambdaQueryWrapper<IdxBizJgUseInfo>().eq(IdxBizJgUseInfo::getProjectContraptionId, sequenceNbr)
                );
            }

            //如果工程装置对应一个证就直接变更给新单位 否则重新生成一个登记证
            if (jgProjectContraptions.size() == 1) {
                IdxBizJgProjectContraption idxBizJgProjectContraption = jgProjectContraptions.get(0);
                idxBizJgProjectContraption.setUseUnitName(registration.getNewUseUnitName());
                idxBizJgProjectContraption.setUseUnitCreditCode(registration.getNewUseUnitCreditCode());
                idxBizJgProjectContraption.setRecUserId(registration.getCreateUserId());
                projectContraptionService.updateById(idxBizJgProjectContraption);
                //获取使用登记证
                LambdaQueryWrapper<JgUseRegistrationManage> useRegistrationManageWrapper = new LambdaQueryWrapper<>();
                useRegistrationManageWrapper.eq(JgUseRegistrationManage::getUseRegistrationCode, useRegistrationCode)
                        .isNotNull(JgUseRegistrationManage::getUseRegistrationCode)
                        .eq(JgUseRegistrationManage::getCertificateStatus, "已登记")
                        .eq(BaseEntity::getIsDelete, false);
                JgUseRegistrationManage registrationManage = useRegistrationManageService.getBaseMapper().selectOne(useRegistrationManageWrapper);
                //修改使用登记证管理使用单位地址、使用单位代码、接收机构、接收机构公司代码、办理日期、使用单位地址
                registrationManage.setUseUnitName(registration.getNewUseUnitName());
                registrationManage.setUseUnitCreditCode(registration.getNewUseUnitCreditCode());
                registrationManage.setReceiveOrgName(registration.getReceiveOrgName());
                registrationManage.setReceiveCompanyCode(registration.getReceiveOrgCode());
                registrationManage.setAuditPassDate(registration.getAuditPassDate());
                registrationManage.setVersion(registrationManage.getVersion() + 1);
                registrationManage.setChangeReason(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getName());
                if (!ObjectUtils.isEmpty(registration.getNewUseUnitCreditCode())) {//回填新单位地址
                    TzBaseEnterpriseInfo useCodeResult = tzBaseEnterpriseInfoMapper.selectOne(new QueryWrapper<TzBaseEnterpriseInfo>().eq("use_unit_code", registration.getNewUseUnitCreditCode()));
                    if (useCodeResult != null) {
                        registrationManage.setUseUnitAddress(useCodeResult.getAddress());
                    }
                }
                registrationManage.setRecUserId(registration.getCreateUserId());
                useRegistrationManageService.updateById(registrationManage);
                //新增使用登记证和设备变更记录
                this.saveRecordByProjectContraption(registrationManage, registration);

                //通过装置Id查询出设备进行更新 更新其他关联表
                this.updateRelationTable(idxBizJgProjectContraption.getSequenceNbr(), registration, Boolean.FALSE, null);
            } else {
                IdxBizJgProjectContraption idxBizJgProjectContraption = projectContraptionService.getBaseMapper().selectById(Long.valueOf(sequenceNbr));
                //查询使用登记表eq 查询和装置关联的管道，取第一个用于生成登记编号
                LambdaQueryWrapper<JgChangeRegistrationUnitEq> registrationUnitEqWrapper = new LambdaQueryWrapper<>();
                registrationUnitEqWrapper.eq(JgChangeRegistrationUnitEq::getProjectContraptionId, idxBizJgProjectContraption.getSequenceNbr())
                        .eq(JgChangeRegistrationUnitEq::getUnitChangeRegistrationId, registration.getSequenceNbr())
                        .eq(JgChangeRegistrationUnitEq::getIsDelete, false);
                List<JgChangeRegistrationUnitEq> changeRegistrationUnitEqs = registrationUnitEqService.list(registrationUnitEqWrapper);
                if (CollUtil.isEmpty(changeRegistrationUnitEqs)) {
                    throw new BadRequest("使用登记表没有找到工程/装置对应的信息");
                }
                //重新生成使用登记证
                LambdaQueryWrapper<JgUseRegistrationManage> useRegistrationManageWrapper = new LambdaQueryWrapper<>();
                useRegistrationManageWrapper.eq(JgUseRegistrationManage::getUseRegistrationCode, useRegistrationCode)
                        .isNotNull(JgUseRegistrationManage::getUseRegistrationCode)
                        .eq(JgUseRegistrationManage::getCertificateStatus, "已登记")
                        .eq(BaseEntity::getIsDelete, false);
                List<JgUseRegistrationManage> registrationManages = useRegistrationManageService.getBaseMapper().selectList(useRegistrationManageWrapper);
                if (CollUtil.isEmpty(registrationManages)) {
                    throw new BadRequest("使用登记编号为：" + useRegistrationCode + "不存在");
                }
                if (registrationManages.size() != 1) {
                    throw new BadRequest("使用登记编号为：" + useRegistrationCode + "有重复数据,请排查");
                }
                JgUseRegistrationManage registrationManage = registrationManages.get(0);
                registrationManage.setSequenceNbr(sequence.nextId());
                String code = commonServiceImpl.generateRegistrationCode(changeRegistrationUnitEqs.get(0).getEquId(), registrationManage.getReceiveCompanyCode(), true);
                registrationManage.setUseRegistrationCode(code);
                //修改使用登记证管理使用单位地址、使用单位代码、接收机构、接收机构公司代码、办理日期、使用单位地址
                registrationManage.setUseUnitName(registration.getNewUseUnitName());
                registrationManage.setUseUnitCreditCode(registration.getNewUseUnitCreditCode());
                registrationManage.setReceiveOrgName(registration.getReceiveOrgName());
                registrationManage.setReceiveCompanyCode(registration.getReceiveOrgCode());
                registrationManage.setAuditPassDate(registration.getAuditPassDate());
                registrationManage.setVersion(1);
                registrationManage.setChangeReason(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getName());
                if (!ObjectUtils.isEmpty(registration.getNewUseUnitCreditCode())) {//回填新单位地址
                    TzBaseEnterpriseInfo useCodeResult = tzBaseEnterpriseInfoMapper.selectOne(new QueryWrapper<TzBaseEnterpriseInfo>().eq("use_unit_code", registration.getNewUseUnitCreditCode()));
                    if (useCodeResult != null) {
                        registrationManage.setUseUnitAddress(useCodeResult.getAddress());
                    }
                }
                registrationManage.setRecUserId(registration.getCreateUserId());
                useRegistrationManageService.save(registrationManage);
                //更新装置表
                idxBizJgProjectContraption.setUseRegistrationCode(code);
                idxBizJgProjectContraption.setUseUnitName(registration.getNewUseUnitName());
                idxBizJgProjectContraption.setUseUnitCreditCode(registration.getNewUseUnitCreditCode());
                idxBizJgProjectContraption.setRecUserId(registration.getCreateUserId());
                projectContraptionService.updateById(idxBizJgProjectContraption);
                //新增使用登记证和设备变更记录
                this.saveRecordByProjectContraption(registrationManage, registration);
                //通过装置Id查询出设备进行更新 更新其他关联表
                this.updateRelationTable(idxBizJgProjectContraption.getSequenceNbr(), registration, Boolean.TRUE, code);
            }
        }
        //  创建设备履历
        this.createResume(projectContraptionIds, registration, taskV2Model.getRoutePath());
    }

    private void createResume(List<String> projectContraptionIds, JgChangeRegistrationUnit registration, String routePath) {
        jgResumeInfoService.saveBatchResume(projectContraptionIds.stream().map(projectContraptionId-> JgResumeInfoDto.builder().applyNo(registration.getApplyNo())
                .businessType(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getName())
                .businessId(String.valueOf(registration.getSequenceNbr()))
                .equId(projectContraptionId)
                .approvalUnit(registration.getReceiveOrgName())
                .approvalUnitCode(registration.getReceiveCompanyCode())
                .status("正常")
                .changeContent(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getName() + "业务办理")
                .routePath(routePath)
                .build()).collect(Collectors.toList())
        );
    }

    /**
     * 更新关系表
     *
     * @param sequenceNbr
     * @param registration
     * @param isNewUseOrgCode
     * @param useOrgCode
     */
    private void updateRelationTable(Long sequenceNbr, JgChangeRegistrationUnit registration, Boolean isNewUseOrgCode, String useOrgCode) {
        LambdaQueryWrapper<JgChangeRegistrationUnitEq> unitEqWrapper = new LambdaQueryWrapper<>();
        unitEqWrapper.eq(JgChangeRegistrationUnitEq::getProjectContraptionId, sequenceNbr)
                .eq(JgChangeRegistrationUnitEq::getUnitChangeRegistrationId, registration.getSequenceNbr())
                .eq(JgChangeRegistrationUnitEq::getIsDelete, false);
        List<JgChangeRegistrationUnitEq> changeRegistrationUnitEqs = registrationUnitEqService.list(unitEqWrapper);
        if (!CollectionUtils.isEmpty(changeRegistrationUnitEqs)) {
            Map<String, Map<String, Object>> resultMap = new HashMap<>();
            for (JgChangeRegistrationUnitEq jgChangeRegistrationUnitEq : changeRegistrationUnitEqs) {
                String equipId = jgChangeRegistrationUnitEq.getEquId();
                LambdaQueryWrapper<IdxBizJgUseInfo> queryWrapper2 = new LambdaQueryWrapper<>();
                queryWrapper2.eq(IdxBizJgUseInfo::getRecord, equipId);
                IdxBizJgUseInfo useInfo = useInfoMapper.selectOne(queryWrapper2);
                if (ObjectUtils.isNotEmpty(useInfo)) {
                    useInfo.setUseUnitName(registration.getNewUseUnitName());
                    useInfo.setUseUnitCreditCode(registration.getNewUseUnitCreditCode());
                    useInfoMapper.updateById(useInfo);
                }
                //修改ES中使用单位信息和使用单位代码
                Map<String, Object> map1 = new HashMap<>();
                String[] companyCode = registration.getNewUseUnitCreditCode().split("_");
                map1.put("USE_UNIT_CREDIT_CODE", companyCode.length > 1 ? companyCode[1] : registration.getNewUseUnitCreditCode());
                map1.put("USE_UNIT_NAME", registration.getNewUseUnitName());
                map1.put("REC_DATE", new Date());// 更新时间
                if (Boolean.TRUE.equals(isNewUseOrgCode)) {
                    map1.put("USE_ORG_CODE", useOrgCode);
                }
                resultMap.put(equipId, map1);
                //修改各类告知列表，置为废弃
                this.updataInvalidStatusByHistory(equipId, registration.getApplyNo());
                tzsServiceFeignClient.commonUpdateEsDataByIds(resultMap);
            }
        }
    }

    /**
     * 保存装置单位变更记录
     *
     * @param registrationManage
     */
    private void saveRecordByProjectContraption(JgUseRegistrationManage registrationManage, JgChangeRegistrationUnit registrationUnit) {
        List<JgChangeRegistrationUnitEq> eqList = registrationUnitEqService.lambdaQuery().eq(JgChangeRegistrationUnitEq::getUnitChangeRegistrationId, registrationManage.getSequenceNbr()).list();
        List<JgCertificateChangeRecordEq> changeRecordEqList = new ArrayList<>();
        JgCertificateChangeRecord changeRecord = new JgCertificateChangeRecord();
        long nextId = sequence.nextId();
        changeRecord.setApplyNo(registrationManage.getApplyNo());//申请编号
        changeRecord.setReceiveOrgName(registrationManage.getReceiveOrgName());//接收机构/登记机关
        changeRecord.setAuditPassDate(registrationUnit.getApplyDate());//办理日期
        changeRecord.setRegType("单位变更登记");//登记类别
        changeRecord.setRegDate(registrationUnit.getCreateDate());//申请日期
        changeRecord.setChangeContent(this.buildRecordContent(registrationUnit));//变更内容
        changeRecord.setUseRegistrationCode(registrationManage.getUseRegistrationCode());//使用登记编号
        changeRecord.setReceiveCompanyCode(registrationUnit.getReceiveCompanyCode());//接收机构公司代码
        changeRecord.setCertificateNo(registrationManage.getCertificateNo());//登记证书唯一码
        changeRecord.setUseUnitCreditCode(registrationUnit.getNewUseUnitCreditCode());//使用单位统一信用代码
        changeRecord.setUseUnitName(registrationUnit.getNewUseUnitName());//使用单位名称
        changeRecord.setEquCategory(registrationManage.getEquCategory());//设备类别编码
        changeRecord.setCreateDate(new Date());
        changeRecord.setSequenceNbr(nextId);
        for (JgChangeRegistrationUnitEq unitEq : eqList) {
            JgCertificateChangeRecordEq changeRecordEq = new JgCertificateChangeRecordEq();
            changeRecordEq.setChangeRecordId(String.valueOf(nextId));//登记证记录主键
            changeRecordEq.setEquId(unitEq.getEquId());//设备主键
            changeRecordEqList.add(changeRecordEq);
        }
        certificateChangeRecordService.save(changeRecord);
        if (!changeRecordEqList.isEmpty()) {
            certificateChangeRecordEqService.saveBatch(changeRecordEqList);
        }
    }

    /**
     * 删除 redis校验重复引用设备的数据
     */
    private void delRepeatUseEquipData(String applyNo, String status, String companyCode) {
        JgRegistrationHistory history = registrationHistoryService.lambdaQuery()
                .eq(JgRegistrationHistory::getCurrentDocumentId, applyNo)
                .eq(JgRegistrationHistory::getIsDelete, false)
                .one();
        // 获取使用登记证列表
        JSONArray registrationList = JSON.parseArray(history.getChangeData());
        if (CollectionUtils.isEmpty(registrationList)) {
            return;
        }
        List<Long> registrationIdList = new ArrayList<>();
        List<String> projectContraptionIdList = new ArrayList<>();
        //根据使用登记证查询设备
        for (Object o : registrationList) {
            JSONObject parseObject = JSON.parseObject(o.toString());
            if (parseObject.containsKey("sequenceNbr") && ObjectUtils.isNotEmpty(parseObject.get("sequenceNbr")) &&
                    canConvertToLong(parseObject.get("sequenceNbr").toString())) {
                registrationIdList.add(Long.parseLong(parseObject.get("sequenceNbr").toString()));
                projectContraptionIdList.add(parseObject.get("sequenceNbr").toString());
            }
        }
        List<String> records = new ArrayList<>();
        JSONObject firstObj = registrationList.stream()
                .findFirst()
                .map(Object::toString)
                .map(JSON::parseObject)
                .orElse(new JSONObject());
        String equListCode = Optional.ofNullable(firstObj.getString("equList"))
                .filter(ObjectUtils::isNotEmpty)
                .orElse(null);
        String equCategoryCode = Optional.ofNullable(firstObj.getString("equCategory"))
                .filter(ObjectUtils::isNotEmpty)
                .orElse(null);

        if (PipelineEnum.PRESSURE_PIPELINE.getCode().equals(equListCode)) {
            JgChangeRegistrationUnit registrationUnit = this.lambdaQuery().eq(JgChangeRegistrationUnit::getApplyNo, applyNo)
                    .eq(JgChangeRegistrationUnit::getIsDelete, false)
                    .one();
            if (Objects.nonNull(registrationUnit)) {
                LambdaQueryWrapper<JgChangeRegistrationUnitEq> wrapper = new LambdaQueryWrapper<>();
                wrapper.in(JgChangeRegistrationUnitEq::getProjectContraptionId, projectContraptionIdList)
                        .eq(JgChangeRegistrationUnitEq::getUnitChangeRegistrationId, registrationUnit.getSequenceNbr())
                        .eq(JgChangeRegistrationUnitEq::getIsDelete, false);
                List<JgChangeRegistrationUnitEq> changeRegistrationUnitEqs = registrationUnitEqService.list(wrapper);
                if (CollUtil.isNotEmpty(changeRegistrationUnitEqs)) {
                    changeRegistrationUnitEqs.forEach(item -> records.add(item.getEquId()));
                }
            }
        } else if (CylinderTypeEnum.CYLINDER.getCode().equals(equCategoryCode)) {
            records.addAll(registrationList.stream()
                            .map(Object::toString)
                            .map(JSON::parseObject)
                            .map(obj -> obj.getString("sequenceNbr"))
                            .filter(ObjectUtils::isNotEmpty)
                            .collect(Collectors.toList())
            );
        } else {
            List<JSONObject> equList = registrationManageService.queryEquByCertificateSeqList(registrationIdList);
            // 获取单位变更设备列表
            if (CollectionUtils.isEmpty(equList)) {
                return;
            }

            for (JSONObject jsonObject : equList) {
                records.add(jsonObject.getString("SEQUENCE_NBR"));
            }
        }
        if (NOT_FLOWING_STATE.contains(status)) {
            EquipUsedCheckStrategyContext.getUsedStrategy(PROCESS_DEFINITION_KEY)
                    .delDataForCheckEquipRepeatUsed(records, companyCode);
        }
    }

    private Integer getTaskCodeByName(String auditStatus) {
        return commonServiceImpl.getDictionaryCodeByName(auditStatus);
    }


    /**
     * 修改各类告知列表，置为废弃
     *
     * @param equId           设备id
     * @param equipTransferId 当前告知单Id
     */
    public void updataInvalidStatusByHistory(String equId, String equipTransferId) {
        //改造变更登记
        jgChangeRegistrationReformEqMapper.updateEquipIsVaildByEquipIdAndCurrentDocumentId(equId, equipTransferId);
        //移装变更登记
        jgChangeRegistrationTransferEqMapper.updateEquipIsVaildByEquipIdAndCurrentDocumentId(equId, equipTransferId);
        //使用登记
        jgUseRegistrationEqMapper.updateEquipIsVaildByEquipIdAndCurrentDocumentId(equId, equipTransferId);
        //单位变更登记
        jgChangeRegistrationUnitEqMapper.updateEquipIsVaildByEquipIdAndCurrentDocumentId(equId, equipTransferId);
        //更名变更登记
        jgChangeRegistrationNameEqMapper.updateEquipIsVaildByEquipIdAndCurrentDocumentId(equId, equipTransferId);
    }

    /**
     * 导出使用登记证
     *
     * @param sequenceNbr
     */
    public void exportUseRegistrationCertificate(String sequenceNbr, HttpServletResponse response, String printType) {
        Map<String, Object> exportParamsMap = new HashMap<>();
        //查询移装变更详情
        JgChangeRegistrationUnit transfer = this.getById(sequenceNbr);
        LambdaQueryWrapper<JgChangeRegistrationUnitEq> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(JgChangeRegistrationUnitEq::getUnitChangeRegistrationId, transfer.getSequenceNbr());
        JgChangeRegistrationUnitEq jgChangeRegistrationUnitEq = JgChangeRegistrationUnitEqMapper.selectOne(queryWrapper);
        if (ValidationUtil.isEmpty(transfer) || ValidationUtil.isEmpty(jgChangeRegistrationUnitEq)) {
            throw new BadRequest("使用登记证导出失败,请稍后重试!");
        }
        //是否废弃
        exportParamsMap.put("isInvalid", ValidationUtil.isEmpty(jgChangeRegistrationUnitEq.getIsInvalid()) ? "0" : jgChangeRegistrationUnitEq.getIsInvalid());
        //登记机关
        exportParamsMap.put("receiveOrgName", transfer.getReceiveOrgName());
        //使用登记证编号
        exportParamsMap.put("useRegistrationCode", ValidationUtil.isEmpty(transfer.getUseRegistCode()) ? "" : transfer.getUseRegistCode());
        //使用单位名称
        exportParamsMap.put("useUnitName", transfer.getNewUseUnitName());
        //监管码
        LambdaQueryWrapper<OtherInfo> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(OtherInfo::getRecord, jgChangeRegistrationUnitEq.getEquId());
        OtherInfo tzsJgOtherInfo = tzsJgOtherInfoMapper.selectOne(queryWrapper1);
        exportParamsMap.put("supervisoryCode", tzsJgOtherInfo.getSupervisoryCode());
        getAuditPassedDate(transfer.getAuditPassDate(), exportParamsMap);
        //查询设备注册信息
        IdxBizJgRegisterInfo registerInfo = idxBizJgRegisterInfoService.getOne(new QueryWrapper<IdxBizJgRegisterInfo>().eq("RECORD", jgChangeRegistrationUnitEq.getEquId()));
        if (!ValidationUtil.isEmpty(registerInfo)) {
            //设备种类、类别、品种
            String equList = registerInfo.getEquList();//设备种类
            String equCategory = registerInfo.getEquCategory();//设备类别
            String equDefine = registerInfo.getEquDefine();//设备品种
            List<EquipmentCategory> categoryList0 = commonService.getEquipmentCategoryList(equList, null);
            List<EquipmentCategory> categoryList1 = commonService.getEquipmentCategoryList(equCategory, null);
            if (org.apache.commons.collections.CollectionUtils.isNotEmpty(categoryList1)) {
                exportParamsMap.put("equList", categoryList0.get(0).getName());
            }
            if (org.apache.commons.collections.CollectionUtils.isNotEmpty(categoryList1)) {
                exportParamsMap.put("equCategory", categoryList1.get(0).getName());
            }
            if (!ValidationUtil.isEmpty(equDefine)) {
                List<EquipmentCategory> categoryList2 = commonService.getEquipmentCategoryList(equDefine, null);
                if (org.apache.commons.collections.CollectionUtils.isNotEmpty(categoryList2)) {
                    exportParamsMap.put("equDefine", categoryList2.get(0).getName());
                }
            }
            //设备代码
            exportParamsMap.put("equCode", registerInfo.getEquCode());
        }

        //单位内部编码
        IdxBizJgUseInfo useInfo = idxBizJgUseInfoService.getOneData(jgChangeRegistrationUnitEq.getEquId());
        if (!ValidationUtil.isEmpty(useInfo)) {
            exportParamsMap.put("useInnerCode", useInfo.getUseInnerCode());

            String fullAddress = "";
            String province = ValidationUtil.isEmpty(useInfo.getProvinceName()) ? "" : useInfo.getProvinceName();
            String city = ValidationUtil.isEmpty(useInfo.getCityName()) ? "" : useInfo.getCityName();
            String county = ValidationUtil.isEmpty(useInfo.getCountyName()) ? "" : useInfo.getCountyName();
            String street = ValidationUtil.isEmpty(useInfo.getStreetName()) ? "" : useInfo.getStreetName();
            String address = ValidationUtil.isEmpty(useInfo.getAddress()) ? "" : useInfo.getAddress();
            fullAddress = province + city + county + street + address;
            exportParamsMap.put("fullAddress", fullAddress);
        }
        IdxBizJgFactoryInfo factoryInfo = idxBizJgFactoryInfoService.getOneData(jgChangeRegistrationUnitEq.getEquId());
        //产品编号(出厂编号)
        if (!ValidationUtil.isEmpty(factoryInfo)) {
            exportParamsMap.put("factoryNum", factoryInfo.getFactoryNum());
        }
        //调用生成使用登记证
        exportParamsMap.put("excelType", "变更登记");
        exportParamsMap.put("tableName", "特种设备单位变更登记");
        exportParamsMap.put("applyNo", transfer.getApplyNo());
        if ("0".equals(printType)) {
            //调用生成使用登记证
            commonService.generateCertificateReport(exportParamsMap, response);
        } else {
            commonService.generatePdfPrint(exportParamsMap, response);
        }
    }


    private String buildTaskContent(JgChangeRegistrationUnit obj) {
        //发起电梯登记业务申请，【申请单号SYDJ202400001】
        return "发起了【单位变更登记】业务申请，【申请单号" + obj.getApplyNo() + "】";
    }

    private String buildRecordContent(JgChangeRegistrationUnit obj) {
        FeignClientResult<AgencyUserModel> agencyUserModelFeignClientResult = Privilege.agencyUserClient.queryByUserId(obj.getCreateUserId());
        //张三办理了【单位变更】 ，单号【DWBG202407050001】，原恒星电梯单位变更为新恒星电梯有限公司，办理日期2024-07-05
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");

        return obj.getNewUseUnitName() + "的" + agencyUserModelFeignClientResult.getResult().getRealName() + "办理了【单位变更登记】，单号【"
                + obj.getApplyNo() + "】，原" + obj.getUseUnitName() + "变更为新"
                + obj.getNewUseUnitName() + "，申请日期" + simpleDateFormat.format(obj.getCreateDate());
    }

    private void createTaskModel(JgChangeRegistrationUnit dto, String taskName, String submitType, String nextUserIds) {
        ReginParams reginParams = JSON.parseObject(redisUtils.get(RedisKey.buildReginKey(RequestContext.getExeUserId(), RequestContext.getToken())).toString(), ReginParams.class);
        if ("1".equals(submitType)) {
            TaskModelDto taskModelDto = new TaskModelDto();
            taskModelDto.setFlowCreateDate(dto.getApplyDate());
            taskModelDto.setTaskName(taskName);
            taskModelDto.setTaskCode(dto.getApplyNo());
            taskModelDto.setTaskContent(this.buildTaskContent(dto));
            taskModelDto.setTaskDesc("");
            taskModelDto.setTaskType(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getCode());
            taskModelDto.setTaskTypeLabel(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getName());
            taskModelDto.setRelationId(dto.getInstanceId());
            taskModelDto.setExecuteUserIds(nextUserIds);
            taskModelDto.setFlowStatus(this.getTaskStatusByName(dto.getStatus()));
            taskModelDto.setFlowStatusLabel(dto.getStatus());
            taskModelDto.setTaskStatusLabel(dto.getAuditStatus());
            taskModelDto.setStartUserId(RequestContext.getExeUserId());
            taskModelDto.setStartUser(reginParams.getUserModel().getRealName());
            taskModelDto.setStartUserCompanyName(dto.getUseUnitName());
            taskModelDto.setNextExecuteUser(dto.getNextExecutorIds());
            taskModelDto.setStartDate(dto.getCreateDate());
            TaskMessageDto obj = new TaskMessageDto();
            obj.setInstanceId(dto.getInstanceId());
            obj.setStatus(dto.getStatus());
            obj.setPromoter(dto.getPromoter());
            obj.setNextExecuteUserIds(dto.getNextExecuteUserIds());
            obj.setSequenceNbr(dto.getSequenceNbr());
            obj.setNextTaskId(dto.getNextTaskId());
            taskModelDto.setModel(obj);
            commonServiceImpl.buildTaskModel(Collections.singletonList(taskModelDto));
        }
    }

    private void buildRoleList(List<ProcessTaskDTO> processTasks, List<String> roleListNext, List<String> roleListAll) {
        processTasks.forEach(p -> {
            p.getNextCandidateGroups().values().forEach(e -> {
                roleListNext.addAll(e);
                roleListAll.addAll(e);
            });
            roleListAll.addAll(p.getCandidateGroups());
        });
    }

    private void executeOneStep(JgChangeRegistrationUnit jgChangeRegistrationName, String taskName, String nextUserIds, String operate) {
        // 先更新之前的待办
        TaskV2Model taskV2Model = updateTaskModel(jgChangeRegistrationName, operate);
        taskV2Model.setTaskDesc("");
        taskV2Model.setTaskName(taskName);
        taskV2Model.setTaskDesc("");
        taskV2Model.setTaskCode(jgChangeRegistrationName.getApplyNo());
        taskV2Model.setExecuteUserIds(nextUserIds);
        taskV2Model.setRelationId(jgChangeRegistrationName.getInstanceId());
        FlowStatusEnum flowStatusEnum = this.getTaskStatus(operate);
        taskV2Model.setTaskStatus(this.getTaskStatusByName(jgChangeRegistrationName.getStatus()));
        taskV2Model.setTaskStatusLabel(jgChangeRegistrationName.getStatus());
        taskV2Model.setFlowStatus(this.getTaskStatusByName(jgChangeRegistrationName.getStatus()));
        taskV2Model.setFlowStatusLabel(jgChangeRegistrationName.getStatus());
        taskV2Model.setExecuteUserIds(nextUserIds);
        TaskModelDto modelDto = new TaskModelDto();
        BeanUtil.copyProperties(taskV2Model, modelDto);
        modelDto.setNextExecuteUser(jgChangeRegistrationName.getNextExecutorIds());
        if (jgChangeRegistrationName.getStatus().equals(WorkFlowStatusEnum.UNIT_RENAME_SUBMIT.getReject())) {
            modelDto.setPageType("edit");
        }
        TaskMessageDto obj = new TaskMessageDto();
        obj.setInstanceId(jgChangeRegistrationName.getInstanceId());
        obj.setStatus(jgChangeRegistrationName.getStatus());
        obj.setPromoter(jgChangeRegistrationName.getPromoter());
        obj.setNextExecuteUserIds(jgChangeRegistrationName.getNextExecuteUserIds());
        obj.setSequenceNbr(jgChangeRegistrationName.getSequenceNbr());
        obj.setNextTaskId(jgChangeRegistrationName.getNextTaskId());
        modelDto.setModel(obj);
        // 再插入新代办、数据待定
        commonServiceImpl.buildTaskModel(Collections.singletonList(modelDto));
    }


    private TaskV2Model updateTaskModel(JgChangeRegistrationUnit jgChangeRegistrationName, String operate) {
        Map<String, Object> params = new HashMap<>();
        FlowStatusEnum flowStatusEnum = this.getTaskStatus(operate);
        params.put("relationId", jgChangeRegistrationName.getInstanceId());
        params.put("taskStatus", flowStatusEnum.getCode());
        params.put("taskStatusLabel", flowStatusEnum.getName());
        params.put("flowStatus", this.getTaskStatusByName(jgChangeRegistrationName.getStatus()));
        params.put("flowStatusLabel", jgChangeRegistrationName.getStatus());
        params.put("model", jgChangeRegistrationName);
        return commonServiceImpl.updateTaskModel(params);
    }

    private FlowStatusEnum getTaskStatus(String operate) {
        if ("0".equals(operate)) {
            return FlowStatusEnum.TO_BE_FINISHED;
        } else {
            return FlowStatusEnum.REJECTED;
        }
    }


    private String getNextUserOrgCode(String operate, JgChangeRegistrationUnit jgChangeRegistrationName) {
        if ("0".equals(operate)) {
            // 执行通过节点，单位code为接收机构单位信息
            return jgChangeRegistrationName.getReceiveOrgCode();
        }
        if (jgChangeRegistrationName.getStatus().equals(WorkFlowStatusEnum.UNIT_RENAME_RECEIVE.getPass())
                || jgChangeRegistrationName.getStatus().equals(WorkFlowStatusEnum.UNIT_RENAME_RECEIVE.getReject())
                || jgChangeRegistrationName.getStatus().equals(WorkFlowStatusEnum.UNIT_RENAME_RECEIVE.getRollBack())) {
            // 驳回且当前节点为一键节点时，单位code为发起人单位信息
            return jgChangeRegistrationName.getUseUnitCreditCode();
        }
        // 2、3级驳回时，单位code为接收单位code
        return jgChangeRegistrationName.getReceiveOrgCode();
    }

    private Integer getTaskStatusByName(String auditStatus) {
        return commonServiceImpl.getDictionaryCodeByName(auditStatus);
    }

    @Override
    public boolean beforeCheck(JgChangeRegistrationUnit jgChangeRegistrationUnit) {
        return jgChangeRegistrationUnit.getInstanceId() != null && !jgChangeRegistrationUnit.getStatus().equals(FlowStatusEnum.TO_BE_FINISHED.getName()) && !jgChangeRegistrationUnit.getStatus().equals(FlowStatusEnum.TO_BE_DISCARD.getName());
    }

    @Override
    public void compensate(JgChangeRegistrationUnit jgChangeRegistrationUnit) {
        commonServiceImpl.saveExecuteFlowData2Redis(jgChangeRegistrationUnit.getInstanceId(), this.buildInstanceRuntimeData(jgChangeRegistrationUnit));
    }

    /**
     * bug24304脏数据处理（单位变更登记一勾选过多，部分变更设备失败，现在将变更失败的设备变更过去）
     */
    @Transactional(rollbackFor = Exception.class)
    public void bug24304Handle(String businessType, String applyNos) {
        /* // 查询单位变更登记的历史数据
        List<JgRegistrationHistory> historyList = jgRegistrationHistoryMapper.selectList(new LambdaQueryWrapper<JgRegistrationHistory>()
                .eq(JgRegistrationHistory::getRegistrationClass, "单位变更登记"));
        // 过滤出异常数据
        List<JgRegistrationHistory> targetHistory = historyList
                .stream()
                .filter(item -> {
                    try {
                        return JSONArray.parseArray(item.getChangeData()).size() > 10;
                    } catch (Exception ex) {
                        return false;
                    }
                })
                .collect(Collectors.toList()); */

        List<String> applyNoList = Arrays.asList(applyNos.split(","));
        switch (businessType) {
            case "1":
                changeRegistrationUnitHandler(applyNoList);
                break;
            case "2":
                scrapCancelHandler(applyNoList);
                break;
            default:
                break;
        }

    }

    /**
     * 注销-数据处理
     *
     * @param applyNoList 申请单号list
     */
    private void scrapCancelHandler(List<String> applyNoList) {
        applyNoList.forEach(applyNo -> {
            // 查询注销数据
            JgScrapCancel scrapCancel = jgScrapCancelMapper.selectOne(new LambdaQueryWrapper<JgScrapCancel>()
                    .eq(JgScrapCancel::getApplyNo, applyNo));
            // 查询注销的历史数据
            JgRegistrationHistory history = jgRegistrationHistoryMapper.selectOne(new LambdaQueryWrapper<JgRegistrationHistory>()
                    .eq(JgRegistrationHistory::getRegistrationClass, "注销")
                    .eq(JgRegistrationHistory::getCurrentDocumentId, scrapCancel.getSequenceNbr()));
            // 查询已经入库的设备id
            List<String> inEqTableEquId = jgScrapCancelEqMapper.selectList(new LambdaQueryWrapper<JgScrapCancelEq>()
                            .eq(JgScrapCancelEq::getEquipTransferId, scrapCancel.getSequenceNbr()))
                    .stream()
                    .map(JgScrapCancelEq::getEquId)
                    .collect(Collectors.toList());
            // 查询单子下的所有使用登记证的seq
            List<Long> certificateSeq = JSONArray.parseArray(JSON.toJSONString(JSONObject.parseObject(history.getChangeData()).get("certificates"))).stream().map(item -> Long.valueOf((String) JSONObject.parseObject(JSONObject.toJSONString(item)).get(SEQUENCE_NBR))).collect(Collectors.toList());
            // 使用登记证下的所有设备
            List<JSONObject> equJSONList = registrationManageService.queryEquByCertificateSeqList(certificateSeq);
            // 过滤出未入库的设备
            List<JSONObject> toBeProcessedEq = equJSONList.stream().filter(item -> !inEqTableEquId.contains(item.getString("SEQUENCE_NBR"))).collect(Collectors.toList());
            // 未入库的设备record
            List<String> toBeProcessedEqRecord = toBeProcessedEq.stream().map(item -> String.valueOf(item.get("SEQUENCE_NBR"))).collect(Collectors.toList());
            // 保存到对应的eq表
            ArrayList<JgScrapCancelEq> jgScrapCancelEqs = new ArrayList<>();
            toBeProcessedEqRecord.forEach(eq -> {
                IdxBizJgRegisterInfo jgRegisterInfo = idxBizJgRegisterInfoService.getBaseMapper().selectOne(new LambdaQueryWrapper<IdxBizJgRegisterInfo>().eq(IdxBizJgRegisterInfo::getRecord, eq));
                JgScrapCancelEq cancelEq = new JgScrapCancelEq();
                cancelEq.setEquipTransferId(String.valueOf(scrapCancel.getSequenceNbr()));
                cancelEq.setEquId(eq);
                cancelEq.setIsDelete(Boolean.FALSE);
                cancelEq.setEquCode(jgRegisterInfo.getEquCode());
                cancelEq.setUseRegistrationCode(jgRegisterInfo.getUseOrgCode());
                jgScrapCancelEqs.add(cancelEq);
                jgScrapCancelEqMapper.insert(cancelEq);
            });
            // 修改监督管理信息
            SuperviseInfo superviseInfo = new SuperviseInfo();
            superviseInfo.setOrgBranchCode("");
            superviseInfo.setOrgBranchName("");
            LambdaQueryWrapper<SuperviseInfo> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(SuperviseInfo::getRecord, toBeProcessedEqRecord);
            superviseInfoMapper.update(superviseInfo, wrapper);
            // 修改设备状态
            idxBizJgUseInfoMapper.batchUpdateUseInfo(toBeProcessedEqRecord, EquimentEnum.ZHUXIAO.getCode());
            // 修改证管理表中的证的状态
            List<JgUseRegistrationManage> jgUseRegistrationManages = jgUseRegistrationManageMapper.selectList(new LambdaQueryWrapper<JgUseRegistrationManage>()
                    .in(JgUseRegistrationManage::getSequenceNbr, certificateSeq)
                    .eq(JgUseRegistrationManage::getCertificateStatus, CertificateStatusEnum.YIDENGJI.getName()));
            LambdaUpdateWrapper<JgUseRegistrationManage> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.in(JgUseRegistrationManage::getSequenceNbr, jgUseRegistrationManages.stream().map(BaseEntity::getSequenceNbr).collect(Collectors.toList()));
            updateWrapper.set(JgUseRegistrationManage::getCertificateStatus, CertificateStatusEnum.YIZHUXIAO.getName());
            jgUseRegistrationManageMapper.update(null, updateWrapper);
            // 修改es数据
            Map<String, Object> map = new HashMap<>();
            map.put("ORG_BRANCH_CODE", "");
            map.put("ORG_BRANCH_NAME", "");
            map.put("EQU_STATE", String.valueOf(EquimentEnum.ZHUXIAO.getCode()));
            Map<String, Map<String, Object>> resultMap = new HashMap<>();
            for (String equipId : toBeProcessedEqRecord) {
                resultMap.put(equipId, map);
            }
            tzsServiceFeignClient.commonUpdateEsDataByIds(resultMap);
            // 添加流水记录
            String userId = scrapCancel.getCreateUserId();
            String realName = Privilege.agencyUserClient.queryByUserId(userId).getResult().getRealName();
            String content = scrapCancel.getUseUnitName() + "的" + realName + "办理了【" + CancelTypeEnum.getNameByType(scrapCancel.getCancelType()) + "】" +
                    "单号【" + scrapCancel.getApplyNo() + "】，办理日期" + DateUtils.getDateNowShortStr();
            List<JgCertificateChangeRecord> certificateChangeRecords = new ArrayList<>();
            List<JgCertificateChangeRecordEq> changeRecordEqList = new ArrayList<>();
            Optional<JgCertificateChangeRecord> first = certificateChangeRecordService.getBaseMapper().selectList(new LambdaQueryWrapper<JgCertificateChangeRecord>().eq(JgCertificateChangeRecord::getApplyNo, applyNo)).stream().findFirst();
            for (JgUseRegistrationManage jgUseRegistrationManage : jgUseRegistrationManages) {
                long seq = sequence.nextId();
                JgCertificateChangeRecord changeRecord = new JgCertificateChangeRecord();
                changeRecord.setSequenceNbr(seq);
                changeRecord.setApplyNo(scrapCancel.getApplyNo());
                changeRecord.setReceiveOrgName(scrapCancel.getReceiveOrgName());
                changeRecord.setAuditPassDate(new Date());
                changeRecord.setRegType(CancelTypeEnum.getNameByType(scrapCancel.getCancelType()));
                changeRecord.setRegDate(scrapCancel.getCreateDate());
                changeRecord.setChangeContent(content);
                changeRecord.setUseRegistrationCode(jgUseRegistrationManage.getUseRegistrationCode());
                changeRecord.setReceiveCompanyCode(scrapCancel.getReceiveOrgCode());
                changeRecord.setUseUnitCreditCode(scrapCancel.getUseUnitCode());
                changeRecord.setUseUnitName(scrapCancel.getUseUnitName());
                changeRecord.setEquCategory(jgUseRegistrationManage.getEquCategoryCode());
                changeRecord.setCreateDate(new Date());
                first.ifPresent(item -> changeRecord.setRoutePath(item.getRoutePath()));
                changeRecord.setCreateUserId(scrapCancel.getCreateUserId());
                changeRecord.setCertificateNo(jgUseRegistrationManage.getCertificateNo());
                certificateChangeRecords.add(changeRecord);
                jgScrapCancelEqs.stream().filter(eq -> eq.getUseRegistrationCode().equals(jgUseRegistrationManage.getUseRegistrationCode())).forEach(e -> {
                    JgCertificateChangeRecordEq changeRecordEq = new JgCertificateChangeRecordEq();
                    changeRecordEq.setChangeRecordId(changeRecord.getSequenceNbr().toString()); // 登记证记录主键
                    changeRecordEq.setEquId(e.getEquId()); // 设备主键
                    changeRecordEq.setProductCode(e.getEquCode()); // 设备代码
                    changeRecordEqList.add(changeRecordEq);
                });
            }
            if (!certificateChangeRecords.isEmpty()) {
                certificateChangeRecordService.saveBatch(certificateChangeRecords);
            }
            if (!changeRecordEqList.isEmpty()) {
                jgCertificateChangeRecordEqServiceImpl.saveBatch(changeRecordEqList);
            }
        });
    }

    /**
     * 单位变更数据处理
     *
     * @param applyNoList 申请编号list
     */
    private void changeRegistrationUnitHandler(List<String> applyNoList) {
        applyNoList.forEach(applyNo -> {
            // 查询单位变更登记的历史数据
            JgRegistrationHistory history = jgRegistrationHistoryMapper.selectOne(new LambdaQueryWrapper<JgRegistrationHistory>()
                    .eq(JgRegistrationHistory::getRegistrationClass, "单位变更登记")
                    .eq(JgRegistrationHistory::getCurrentDocumentId, applyNo));
            // 查询变更数据
            JgChangeRegistrationUnit registrationUnit = this.baseMapper.selectOne(new LambdaQueryWrapper<JgChangeRegistrationUnit>()
                    .eq(JgChangeRegistrationUnit::getApplyNo, history.getCurrentDocumentId()));
            // 查询已经入库的设备id
            List<String> inEqTableEquId = jgChangeRegistrationUnitEqMapper.selectList(new LambdaQueryWrapper<JgChangeRegistrationUnitEq>()
                            .eq(JgChangeRegistrationUnitEq::getUnitChangeRegistrationId, registrationUnit.getSequenceNbr()))
                    .stream()
                    .map(JgChangeRegistrationUnitEq::getEquId)
                    .collect(Collectors.toList());

            // 查询单子下的所有使用登记证的seq
            List<Long> certificateSeq = JSONArray.parseArray(history.getChangeData()).stream().map(item -> Long.valueOf((String) JSONObject.parseObject(JSONObject.toJSONString(item)).get(SEQUENCE_NBR))).collect(Collectors.toList());
            // 使用登记证下的所有设备
            List<JSONObject> equJSONList = registrationManageService.queryEquByCertificateSeqList(certificateSeq);
            // 过滤出未入库的设备
            List<JSONObject> toBeProcessedEq = equJSONList.stream().filter(item -> !inEqTableEquId.contains(item.getString("SEQUENCE_NBR"))).collect(Collectors.toList());
            // 将未入库设备 保存到eq表
            this.changeRegistrationUnitSaveLackEqu(toBeProcessedEq, registrationUnit);
            // 将未入库设备 更新es和otherInfo表的使用单位信息
            this.changeRegistrationUnitUpdateLackEquForES(toBeProcessedEq, registrationUnit);
            this.changeRegistrationUnitUpdateLackEquForUseInfo(toBeProcessedEq, registrationUnit);
        });
    }

    /**
     * 单位变更-保存漏掉的设备
     */
    public void changeRegistrationUnitSaveLackEqu(List<JSONObject> deviceList, JgChangeRegistrationUnit registrationUnit) {
        List<JgChangeRegistrationUnitEq> jgRelationEquipList = new ArrayList<>();
        deviceList.forEach(device -> {
            JgChangeRegistrationUnitEq unitEq = new JgChangeRegistrationUnitEq();
            if (device.containsKey("USE_ORG_CODE")) {
                unitEq.setRegistrationCertificate(device.getString("USE_ORG_CODE"));//使用登记证主键
            }
            if (device.containsKey("SEQUENCE_NBR")) {
                unitEq.setEquId(device.getString("SEQUENCE_NBR"));//设备主键
            }
            unitEq.setUnitChangeRegistrationId(registrationUnit.getSequenceNbr().toString());//单位变更业务主键
            unitEq.setDeviceInfo(JSONObject.toJSONString(device));//设备信息json
            jgRelationEquipList.add(unitEq);
        });
        registrationUnitEqService.saveBatch(jgRelationEquipList);
    }

    /**
     * 单位变更-修改es漏掉的设备
     */
    public void changeRegistrationUnitUpdateLackEquForES(List<JSONObject> deviceList, JgChangeRegistrationUnit registration) {
        deviceList.forEach(device -> {
            Map<String, Map<String, Object>> resultMap = new HashMap<>();
            Map<String, Object> map1 = new HashMap<>();
            String[] companyCode = registration.getNewUseUnitCreditCode().split("_");
            map1.put("USE_UNIT_CREDIT_CODE", companyCode.length > 1 ? companyCode[1] : registration.getNewUseUnitCreditCode());
            map1.put("USE_UNIT_NAME", registration.getNewUseUnitName());
            map1.put("REC_DATE", registration.getCreateDate());
            resultMap.put(device.getString("SEQUENCE_NBR"), map1);
            tzsServiceFeignClient.commonUpdateEsDataByIds(resultMap);
        });
    }

    /**
     * 单位变更-修改使用信息表中漏掉的设备
     */
    public void changeRegistrationUnitUpdateLackEquForUseInfo(List<JSONObject> deviceList, JgChangeRegistrationUnit registration) {
        deviceList.forEach(device -> {
            LambdaQueryWrapper<IdxBizJgUseInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(IdxBizJgUseInfo::getRecord, device.getString("SEQUENCE_NBR"));
            IdxBizJgUseInfo useInfo = useInfoMapper.selectOne(queryWrapper);
            if (ObjectUtils.isNotEmpty(useInfo)) {
                useInfo.setUseUnitName(registration.getNewUseUnitName());
                useInfo.setUseUnitCreditCode(registration.getNewUseUnitCreditCode());
                useInfoMapper.updateById(useInfo);
            }
        });
    }

    /**
     * 保存工业管道的单位变更
     *
     * @param submitType
     * @param map
     * @param reginParams
     * @return
     */
    public List<JgChangeRegistrationUnitDto> saveUnitByProjectContraption(String submitType, JSONObject map, ReginParams reginParams) {
        JgChangeRegistrationUnitDto model = BeanUtil.toBeanIgnoreError(map.get("changeRegisInfo"), JgChangeRegistrationUnitDto.class);
        String[] taskName = new String[]{"流程结束"};
        // 字段转换
        convertField(model);
        // 获取单位变更装置列表
        List<Map<String, Object>> projectContraptionList = model.getProjectContraptionList();
        if (CollectionUtils.isEmpty(projectContraptionList)) {
            throw new BadRequest("工程/装置列表不能为空");
        }
        //根据使用登记证查询设备
        List<String> registrationNoList = new ArrayList<>();//使用登记证编号
        Map<String, String> useRegistrationCodeMap = new HashMap<>();
        for (Map<String, Object> objectMap : projectContraptionList) {
            if (objectMap.containsKey("useRegistrationCode") && ObjectUtils.isNotEmpty(objectMap.get("useRegistrationCode"))) {
                registrationNoList.add(objectMap.get("useRegistrationCode").toString());
                useRegistrationCodeMap.put(objectMap.get(SEQUENCE_NBR).toString(), objectMap.get("useRegistrationCode").toString());
            }
        }
        //查询装置下的管道信息
        List<Map<String, Object>> deviceList = this.queryPipelineList(projectContraptionList);
        if (CollUtil.isEmpty(deviceList)) {
            throw new BadRequest("工程/装置下没有管道信息");
        }
        if (SUBMIT_TYPE_FLOW.equals(submitType)) {//放在最后面防止前面有异常
            //如果是提交，用于校验设备是否已经在流程中，如果不在标记设备已经在流程中
            CompanyBo company = reginParams.getCompany();
            deviceList.forEach(jsonObject -> EquipUsedCheckStrategyContext.getUsedStrategy(PROCESS_DEFINITION_KEY)
                    .equipRepeatUsedCheck(String.valueOf(jsonObject.get("record")),
                            CompanyTypeEnum.INDIVIDUAL.getName().equals(company.getCompanyType()) ?
                                    company.getCompanyCode().split("_")[1] :
                                    company.getCompanyCode()));
        }

        // 获取单位变更单号
        ResponseModel<List<String>> listResponseModel = tzsServiceFeignClient.applicationFormCode(ApplicationFormTypeEnum.DWBG.getCode(), 1);
        List<String> applyNoList = listResponseModel.getResult();
        if (CollectionUtils.isEmpty(applyNoList)) {
            log.error("生成申请变更单位编码失败");
            return new ArrayList<>();
        }
        ArrayList<String> roleListFirst = new ArrayList<>();
        List<String> instanceIdList = new ArrayList<>();
        List<String> nextExecutorUserIds = new ArrayList<>();
        List<String> taskIds = new ArrayList<>();
        List<WorkflowResultDto> workflowResultDtos = new ArrayList<>();
        String nextExecutorRoleIds = "";
        // 判断当前是否为提交
        if (SUBMIT_TYPE_FLOW.equals(submitType)) {
            //发起流程
            ActWorkflowBatchDTO actWorkflowBatchDTO = new ActWorkflowBatchDTO();
            List<ActWorkflowStartDTO> list = new ArrayList<>();
            ActWorkflowStartDTO dto = new ActWorkflowStartDTO();
            dto.setProcessDefinitionKey(PROCESS_DEFINITION_KEY);
            dto.setBusinessKey(String.valueOf(new Date().getTime()));
            dto.setNextExecuteUserCompanyCode(model.getReceiveCompanyCode());
            dto.setCompleteFirstTask(true);
            list.add(dto);
            actWorkflowBatchDTO.setProcess(list);
            List<ProcessTaskDTO> processTaskDTOS = cmWorkflowService.startBatch(actWorkflowBatchDTO);
            workflowResultDtos = commonServiceImpl.buildWorkFlowInfo(processTaskDTOS);
            nextExecutorRoleIds = workflowResultDtos.get(0).getNextExecutorRoleIds();
            nextExecutorUserIds.add(workflowResultDtos.get(0).getNextExecutorUserIds());
            for (WorkflowResultDto processTaskDTO : workflowResultDtos) {
                instanceIdList.add(processTaskDTO.getInstanceId());
                taskIds.add(processTaskDTO.getNextTaskId());
            }
        }

        String finalNextExecutorRoleIds = nextExecutorRoleIds;
        CompanyBo companyBo = commonServiceImpl.getOneCompany(model.getReceiveCompanyCode());
        model.setApplyNo(applyNoList.get(0));
        model.setReceiveCompanyOrgCode(companyBo.getOrgCode());
        model.setCreateUserId(RequestContext.getExeUserId());
        model.setApplyDate(new Date());
        if (SUBMIT_TYPE_FLOW.equals(submitType)) {
            model.setNextExecutorIds(finalNextExecutorRoleIds);
            model.setInstanceStatus(String.join(",", roleListFirst));
            model.setPromoter(reginParams.getUserModel().getUserId());
            model.setStatus(taskName[0]);
        }
        if (!CollectionUtils.isEmpty(nextExecutorUserIds)) {
            model.setNextExecuteUserIds(nextExecutorUserIds.get(0));
        }
        if (!CollectionUtils.isEmpty(instanceIdList)) {
            model.setInstanceId(instanceIdList.get(0));
            model.setStatus(WorkFlowStatusEnum.UNITCHANGE_RECEIVE.getPass());
        } else {
            model.setStatus(WorkFlowStatusEnum.UNITCHANGE_SUBMIT.getPass());
        }
        if (!CollectionUtils.isEmpty(taskIds)) {
            model.setNextTaskId(taskIds.get(0));
        }
        model.setSequenceNbr(sequence.nextId());
        model.setCreateDate(new Date());
        model.setCreateUserName(reginParams.getUserModel().getRealName());
        model.setUseRegistCode(String.join("，", registrationNoList));
        model.setEquList(projectContraptionList.get(0).get("equListName").toString());
        JgChangeRegistrationUnit registrationUnit = new JgChangeRegistrationUnit();
        BeanUtils.copyProperties(model, registrationUnit);
        setNewUnitInfo(reginParams, registrationUnit);
        JgChangeRegistrationUnitMapper.insert(registrationUnit);

        List<TaskModelDto> modelDtos = new ArrayList<>();
        if (SUBMIT_TYPE_FLOW.equals(submitType)) {
            TaskModelDto taskModelDto = new TaskModelDto();
            TaskMessageDto ta = new TaskMessageDto();
            ta.setInstanceId(registrationUnit.getInstanceId());
            ta.setStatus(registrationUnit.getStatus());
            ta.setPromoter(registrationUnit.getPromoter());
            ta.setNextExecuteUserIds(registrationUnit.getNextExecuteUserIds());
            ta.setSequenceNbr(registrationUnit.getSequenceNbr());
            ta.setNextTaskId(registrationUnit.getNextTaskId());
            taskModelDto.setModel(ta);
            taskModelDto.setFlowCreateDate(new Date());
            taskModelDto.setTaskName(workflowResultDtos.get(0).getNextTaskName());
            taskModelDto.setFlowCode(registrationUnit.getNextTaskId());
            taskModelDto.setTaskContent(this.buildTaskContent(registrationUnit));
            taskModelDto.setTaskCode(registrationUnit.getApplyNo());
            taskModelDto.setTaskType(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getCode());
            taskModelDto.setTaskTypeLabel(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getName());
            taskModelDto.setRelationId(registrationUnit.getInstanceId());
            taskModelDto.setExecuteUserIds(workflowResultDtos.get(0).getNextExecutorUserIds());
            taskModelDto.setTaskStatusLabel("未处理");
            taskModelDto.setFlowStatus(commonServiceImpl.getDictionaryCodeByName(WorkFlowStatusEnum.UNITCHANGE_RECEIVE.getPass()));
            taskModelDto.setFlowStatusLabel(WorkFlowStatusEnum.UNITCHANGE_RECEIVE.getPass());
            taskModelDto.setStartUserId(RequestContext.getExeUserId());
            taskModelDto.setStartUser(workflowResultDtos.get(0).getStartUserName());
            taskModelDto.setStartUserCompanyName(reginParams.getCompany().getCompanyName());
            taskModelDto.setStartDate(new Date());
            taskModelDto.setNextExecuteUser(workflowResultDtos.get(0).getNextExecutorRoleIds());
            modelDtos.add(taskModelDto);
        } else {
            TaskModelDto taskModelDto = new TaskModelDto();
            //行数据
            taskModelDto.setModel(registrationUnit);
            //摘要 按原有规则组装
            taskModelDto.setTaskContent(this.buildTaskContent(registrationUnit));
            //申请单号
            taskModelDto.setTaskCode(registrationUnit.getApplyNo());
            //业务类型枚举code值
            taskModelDto.setTaskType(BusinessTypeEnum.JG_COMPANY_CHANGE_REGISTRATION.getCode());
            ////业务主键
            taskModelDto.setRelationId(registrationUnit.getSequenceNbr() + "");
            modelDtos.add(taskModelDto);
//            }
        }
        commonServiceImpl.saveExecuteFlowData2Redis(registrationUnit.getInstanceId(), buildInstanceRuntimeData(registrationUnit));
        commonServiceImpl.buildTaskModel(modelDtos);

        List<JgChangeRegistrationUnitEq> jgRelationEquipList = new ArrayList<>();
        deviceList.forEach(device -> {
            JgChangeRegistrationUnitEq unitEq = new JgChangeRegistrationUnitEq();
            if (device.containsKey("projectContraptionId")) {
                String projectContraptionId = (String) device.get("projectContraptionId");
                unitEq.setRegistrationCertificate(useRegistrationCodeMap.get(projectContraptionId));
                unitEq.setProjectContraptionId(projectContraptionId);
            }
            if (device.containsKey("record")) {
                unitEq.setEquId((String) device.get("record"));//设备主键
            }
            unitEq.setUnitChangeRegistrationId(registrationUnit.getSequenceNbr().toString());//单位变更业务主键
            unitEq.setDeviceInfo(JSON.toJSONString(device));//设备信息json
            jgRelationEquipList.add(unitEq);
        });
        JgRegistrationHistory jgRegistrationHistory = new JgRegistrationHistory();
        //登记类别
        jgRegistrationHistory.setRegistrationClass("单位变更登记");
        jgRegistrationHistory.setCurrentDocumentId(registrationUnit.getApplyNo());
        //使用登记证历史数据
        jgRegistrationHistory.setChangeData(JSON.toJSONString(projectContraptionList));
        //先删除之前设备关系数据，在插入最新设备关系数据
        registrationUnitEqService.getBaseMapper().delete(new LambdaQueryWrapper<JgChangeRegistrationUnitEq>()
                .eq(JgChangeRegistrationUnitEq::getUnitChangeRegistrationId, registrationUnit.getSequenceNbr()));
        registrationUnitEqService.saveBatch(jgRelationEquipList);
        //先删除之前未提交的历史使用登记证信息，在插入最新的使用登记证信息
        registrationHistoryService.getBaseMapper().delete(new LambdaQueryWrapper<JgRegistrationHistory>()
                .eq(JgRegistrationHistory::getCurrentDocumentId, registrationUnit.getApplyNo()));
        registrationHistoryService.save(jgRegistrationHistory);
        return Collections.singletonList(model);
    }

    /**
     * 获取装置下的管道信息
     *
     * @param projectContraptionList
     * @return
     */
    public List<Map<String, Object>> queryPipelineList(List<Map<String, Object>> projectContraptionList) {
        List<Map<String, Object>> result = new ArrayList<>();
        projectContraptionList.forEach(item -> result.addAll(projectContraptionService.getBaseMapper().selectEquipList((String) item.get(SEQUENCE_NBR))));
        return result;
    }

    public Map<String, Map<String, Object>> detailsByProjectContraption(String sequenceNbr, String applyNo) {
        String[] jsonFields = {"productPhoto", "otherAccessories", "productQualificationCertificate", "startLatitudeLongitude",
                "endLatitudeLongitude"};
        Map<String, Map<String, Object>> resultMap = new HashMap<>();
        if (StringUtils.isEmpty(sequenceNbr)) {
            resultMap.put("equipInfo", Collections.emptyMap());
            return resultMap;
        }
        JgChangeRegistrationUnit registrationUnit = this.lambdaQuery().eq(JgChangeRegistrationUnit::getApplyNo, applyNo)
                .eq(JgChangeRegistrationUnit::getIsDelete, false)
                .one();
        if (Objects.isNull(registrationUnit)) {
            resultMap.put("equipInfo", Collections.emptyMap());
            return resultMap;
        }
        //如果单位变更关系表没有管道数据返回空
        LambdaQueryWrapper<JgChangeRegistrationUnitEq> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JgChangeRegistrationUnitEq::getProjectContraptionId, sequenceNbr)
                .eq(JgChangeRegistrationUnitEq::getUnitChangeRegistrationId, registrationUnit.getSequenceNbr())
                .eq(JgChangeRegistrationUnitEq::getIsDelete, false);
        List<JgChangeRegistrationUnitEq> changeRegistrationUnitEqs = registrationUnitEqService.list(wrapper);
        if (CollUtil.isEmpty(changeRegistrationUnitEqs)) {
            resultMap.put("equipInfo", Collections.emptyMap());
            return resultMap;
        }
        JgChangeRegistrationUnit jgChangeRegistrationUnit = this.baseMapper.selectById(changeRegistrationUnitEqs.get(0).getUnitChangeRegistrationId());
        //如果历史表没有数据返回空
        Map<String, Object> projectContraptionDetail = new HashMap<>();
        JgRegistrationHistory history = registrationHistoryService.lambdaQuery()
                .eq(JgRegistrationHistory::getCurrentDocumentId, jgChangeRegistrationUnit.getApplyNo())
                .eq(JgRegistrationHistory::getIsDelete, false)
                .one();
        JSONArray historyJson = JSON.parseArray(history.getChangeData());
        if (CollUtil.isEmpty(historyJson)) {
            resultMap.put("equipInfo", Collections.emptyMap());
            return resultMap;
        }
        historyJson.forEach(item -> {
            JSONObject jsonObject = JSON.parseObject(item.toString());
            if (sequenceNbr.equals(jsonObject.getString(SEQUENCE_NBR))) {
                projectContraptionDetail.putAll(jsonObject);
            }
        });
        Map<String, Object> projectContraptionMap = Optional.of(projectContraptionDetail)
                .map(map -> {
                    commonServiceImpl.convertStringToJsonobject(map, jsonFields);
                    List<JSONObject> pipelineList = new ArrayList<>();
                    changeRegistrationUnitEqs.forEach(item -> {
                        JSONObject jsonObject = JSON.parseObject(item.getDeviceInfo());
                        //如果日期格式是时间戳的话需要处理
                        if (jsonObject.containsKey("uscDate") && !StringUtils.isEmpty(jsonObject.get("uscDate")) && !jsonObject.getString("uscDate").contains("-")) {
                            // 转换为 Instant 并指定时区（默认系统时区）
                            Instant instant = Instant.ofEpochMilli(Long.parseLong(jsonObject.getString("uscDate")));
                            ZoneId zoneId = ZoneId.systemDefault();
                            // 定义日期格式
                            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
                                    .withZone(zoneId);
                            jsonObject.put("uscDate", formatter.format(instant));
                        }
                        pipelineList.add(jsonObject);
                    });
                    map.put("pipelineList", pipelineList);
                    return map;
                }).orElse(Collections.emptyMap());

        resultMap.put("equipInfo", projectContraptionMap);
        return resultMap;
    }
}