package com.ruoyi.tc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.tc.entity.*;
import com.ruoyi.tc.entity.history.*;
import com.ruoyi.tc.entity.po.*;
import com.ruoyi.tc.entity.request.*;
import com.ruoyi.tc.entity.response.AssestTaskXqresponse;
import com.ruoyi.tc.entity.response.AssetTaskResponse;
import com.ruoyi.tc.entity.response.AssetdwHcBlResponse;
import com.ruoyi.tc.entity.response.AssetdwHcResponse;
import com.ruoyi.tc.entity.zztb.AssetSjZztbPo;
import com.ruoyi.tc.historyService.*;
import com.ruoyi.tc.mapper.AssetTaskMapper;
import com.ruoyi.tc.service.*;
import com.ruoyi.tc.zztbService.AssetAssetSjZztbService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 任务主表(AssetTask)表服务实现类
 *
 * @author makejava
 * @since 2024-11-18 13:23:09
 */
@Service("assetTaskService")
public class AssetTaskServiceImpl extends ServiceImpl<AssetTaskMapper, AssetTask> implements AssetTaskService {
    @Resource
    private AssetTaskMapper assetTaskDao;

    @Resource
    private AssetCurrentCpService assetCurrentCpService;

    @Resource
    private AssetBusinessFormCpService assetBusinessFormCpService;

    @Resource
    private AssetSupplyChainCpService assetSupplyChainCpService;


    @Resource
    private AssetBasicNetworkCpService assetBasicNetworkCpService;


    @Resource
    private UnitOtherConcatCpService unitOtherConcatCpService;

    @Resource
    private AssetLcService assetLcService;

    @Resource
    private AssetCurrentService assetCurrentService;

    @Resource
    private AssetOfficialAccountService assetOfficialAccountService;
    @Resource
    private AssetBusinessFormService assetBusinessFormService;

    @Resource
    private AssetBasicNetworkService assetBasicNetworkService;


    @Resource
    private AssetSupplyChainService assetSupplyChainService;

    @Resource
    private UnitOtherConcatService unitOtherConcatService;


    @Resource
    private AssetMiniProgramsService assetMiniProgramsService;

    @Resource
    private AssetMiniProgramsCpService assetMiniProgramsCpService;

    @Resource
    private AssetOfficialAccountCpService assetOfficialAccountCpService;


    @Resource
    private AssetEmailCpService assetEmailCpService;

    @Resource
    private AssetEmailService assetEmailService;
    @Resource
    private AssetAppService assetAppService;

    @Resource
    private AssetAppCpService assetAppCpService;


    @Resource
    private UnitService unitService;

    @Resource
    private AssetTaskService assetTaskService;


    @Resource
    private AssetCurrentHistoryService assetCurrentHistoryService;


    @Resource
    private AssetAppHistoryService assetAppHistoryService;


    @Resource
    private AssetBasicNetworkHistoryService assetBasicNetworkHistoryService;

    @Resource
    private AssetBusinessFormHistoryService assetBusinessFormHistoryService;

    @Resource
    private AssetEmailHistoryService assetEmailHistoryService;


    @Resource
    private AssetMiniProgramsHistoryService assetMiniProgramsHistoryService;


    @Resource
    private AssetOfficialAccountHistoryService assetOfficialAccountHistoryService;

    @Resource
    private AssetSupplyChainHistoryService assetSupplyChainHistoryService;

    @Resource
    private UnitOtherConcatHistoryService unitOtherConcatHistoryService;


    @Resource
    private AssetAssetSjCpService assetAssetSjCpService;


    @Resource
    private AssetAssetSjZztbService assetAssetSjZztbService;

    @Resource
    private AssetAssetSjService assetAssetSjService;




    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public AssetTaskResponse queryById(Integer id) {
        return assetTaskDao.queryById(id);
    }


    /**
     * 分页查询
     *
     * @param assetTask   筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<AssetTask> queryByPage(Page<AssetTask> assetTask, AssetTaskPageRequest pageRequest) {
        return assetTaskDao.queryByPage(assetTask, pageRequest);
    }

    /**
     * 新增数据
     *
     * @param assetTask 实例对象
     */
    @Override
    public void insert(AssetTask assetTask) {
        //任务新增
        assetTaskDao.insert(assetTask);
        int id = assetTask.getId();
        //新增流程节点
        AssetLc assetLc = new AssetLc();
        assetLc.setTaskId(id);
        assetLc.setXfTime(LocalDateTime.now());
        assetLc.setAssetName("管理员任务下发");
        assetLcService.insert(assetLc);
        //  遍历资产类型字段
        String[] assetParts = assetTask.getType().split(",");
        for (String assetPart : assetParts) {
            if (assetPart.equals("0")) {
                //遍历单位
                String[] parts = assetTask.getDwmc().split(",");
                for (String part : parts) {
                    //根据单位名称查询资产
                    List<AssetCurrentCpPo> list = assetTaskDao.findByDwmcAssetCurrent(part);
                    list.forEach(assetCurrentResponse -> {
                        assetCurrentResponse.setTaskId(id);
                        if (assetCurrentResponse.getXtzt() != null && assetCurrentResponse.getXtzt().equals("5")) {
                            assetCurrentResponse.setStatus(5);
                            assetCurrentResponse.setBfyy(null);
                            assetCurrentResponse.setBtgyy(null);
                            assetCurrentResponse.setUpdateTime(null);
                        } else {
                            assetCurrentResponse.setStatus(0);
                            assetCurrentResponse.setBfyy(null);
                            assetCurrentResponse.setBtgyy(null);
                            assetCurrentResponse.setUpdateTime(null);
                        }

                        //新增新监管业务形态
                        List<AssetBusinessFormCpPo> bussList = assetBusinessFormCpService.findByassetId(assetCurrentResponse.getId());
                        bussList.stream()
                                .forEach(assetBusinessFormCp -> {
                                    assetBusinessFormCp.setBusinessId(null);
                                    assetBusinessFormCp.setTaskId(id);
                                });
                        assetBusinessFormCpService.saveBatch(bussList);
                        //新增供应
                        List<AssetSupplyChainCpPo> supplyChainList = assetSupplyChainCpService.findByassetId(assetCurrentResponse.getId());
                        supplyChainList.stream()
                                .forEach(assetSupplyChainCpPo ->{
                                    assetSupplyChainCpPo.setSupplyId(null);
                                    assetSupplyChainCpPo.setTaskId(id);
                                } );
                        assetSupplyChainCpService.saveBatch(supplyChainList);
                        //新增基础网络
                        List<AssetBasicNetworkCpPo> BasicNetworkCpList = assetBasicNetworkCpService.findByassetId(assetCurrentResponse.getId());
                        BasicNetworkCpList.stream()
                                .forEach(assetBasicNetworkCpPo -> {
                                    assetBasicNetworkCpPo.setNetworkId(null);
                                    assetBasicNetworkCpPo.setTaskId(id);
                                });
                        assetBasicNetworkCpService.saveBatch(BasicNetworkCpList);
                        //新增其他联系人
                        List<UnitOtherConcatCpPo> unitOtherConcatList = unitOtherConcatCpService.findByassetId(assetCurrentResponse.getId());
                        unitOtherConcatList.stream()
                                .forEach(unitOtherConcatCpPo -> {
                                    unitOtherConcatCpPo.setConcatId(null);
                                    unitOtherConcatCpPo.setTaskId(id);
                                });
                        unitOtherConcatCpService.saveBatch(unitOtherConcatList);

                        //新增数据资产
                        List<AssetSj> assetSjZztbPoList = assetAssetSjService.lambdaQuery().eq(AssetSj::getAssetId,assetCurrentResponse.getId()).list();
                        List<AssetSjCpPo> assetSjCpPoList = assetSjZztbPoList.stream().map(cpPo -> {
                            AssetSjCpPo assetSjCpPo = new AssetSjCpPo();
                           BeanUtils.copyProperties(cpPo, assetSjCpPo); // 复制属性
                            assetSjCpPo.setSjId(null);
                            assetSjCpPo.setTaskId(id);
                            return assetSjCpPo; // 返回新对象
                        }).collect(Collectors.toList());
                        assetAssetSjCpService.saveBatch(assetSjCpPoList);
                    });



                    assetCurrentCpService.saveBatch(list);

                }
            } else if (assetPart.equals("1")) {
                //遍历小程序资产单位
                String[] parts = assetTask.getDwmc().split(",");
                for (String part : parts) {
                    //根据单位名称查询资产
                    QueryWrapper<AssetMiniPrograms> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("ssdw", part);
                    queryWrapper.eq("del_flag", 0);
                    List<AssetMiniPrograms> list = assetMiniProgramsService.lambdaQuery().eq(AssetMiniPrograms::getSsdw, part).eq(AssetMiniPrograms::getDelFlag, 0).list();
                    List<AssetMiniProgramsCpPo> miniProgramsNewList = list.stream().map(cpPo -> {
                        AssetMiniProgramsCpPo assetMiniProgramsCpPo = new AssetMiniProgramsCpPo();
                        assetMiniProgramsCpPo.setTaskId(id);
                        assetMiniProgramsCpPo.setAssetId(cpPo.getId());
                        BeanUtils.copyProperties(cpPo, assetMiniProgramsCpPo); // 复制属性
                        // 根据 xtzt 设置 status
                        if (cpPo.getState() != null && cpPo.getState().equals("7")) {
                            assetMiniProgramsCpPo.setStatus(5);
                            assetMiniProgramsCpPo.setBfyy(null);
                            assetMiniProgramsCpPo.setBtgyy(null);
                            assetMiniProgramsCpPo.setUpdateTime(null);
                        } else {
                            assetMiniProgramsCpPo.setStatus(0);
                            assetMiniProgramsCpPo.setBfyy(null);
                            assetMiniProgramsCpPo.setBtgyy(null);
                            assetMiniProgramsCpPo.setUpdateTime(null);
                        }

                        return assetMiniProgramsCpPo; // 返回新对象
                    }).collect(Collectors.toList()); // 收集到列表中
                    assetMiniProgramsCpService.saveBatch(miniProgramsNewList);
                }
            } else if (assetPart.equals("2")) {
                //遍历公众号资产单位
                String[] parts = assetTask.getDwmc().split(",");
                for (String part : parts) {
                    //根据单位名称查询资产
                    QueryWrapper<AssetOfficialAccount> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("ssdw", part);
                    queryWrapper.eq("del_flag", 0);
                    List<AssetOfficialAccount> list = assetOfficialAccountService.lambdaQuery().eq(AssetOfficialAccount::getSsdw, part).eq(AssetOfficialAccount::getDelFlag, 0).list();
                    List<AssetOfficialAccountCpPo> officialAccountProgramsNewList = list.stream().map(cpPo -> {
                        AssetOfficialAccountCpPo assetOfficialAccountCpPo = new AssetOfficialAccountCpPo();
                        assetOfficialAccountCpPo.setTaskId(id);
                        assetOfficialAccountCpPo.setAssetId(cpPo.getId());
                        BeanUtils.copyProperties(cpPo, assetOfficialAccountCpPo); // 复制属性
                        // 根据 xtzt 设置 status
                        if (cpPo.getGzhzt() != null && cpPo.getGzhzt().equals("7")) {
                            assetOfficialAccountCpPo.setStatus(5);
                            assetOfficialAccountCpPo.setBfyy(null);
                            assetOfficialAccountCpPo.setBtgyy(null);
                            assetOfficialAccountCpPo.setUpdateTime(null);
                        } else {
                            assetOfficialAccountCpPo.setStatus(0);
                            assetOfficialAccountCpPo.setBfyy(null);
                            assetOfficialAccountCpPo.setBtgyy(null);
                            assetOfficialAccountCpPo.setUpdateTime(null);
                        }

                        return assetOfficialAccountCpPo; // 返回新对象
                    }).collect(Collectors.toList()); // 收集到列表中
                    assetOfficialAccountCpService.saveBatch(officialAccountProgramsNewList);
                }
            } else if (assetPart.equals("3")) {
                //遍历电子邮件资产单位
                String[] parts = assetTask.getDwmc().split(",");
                for (String part : parts) {
                    //根据单位名称查询资产
                    QueryWrapper<AssetEmail> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("ssdw", part);
                    queryWrapper.eq("del_flag", 0);
                    List<AssetEmail> list = assetEmailService.lambdaQuery().eq(AssetEmail::getSsdw, part).eq(AssetEmail::getDelFlag, 0).list();
                    ;
                    List<AssetEmailCpPo> emailCpNewList = list.stream().map(cpPo -> {
                        AssetEmailCpPo emailCpPo = new AssetEmailCpPo();
                        emailCpPo.setTaskId(id);
                        emailCpPo.setAssetId(cpPo.getId());
                        BeanUtils.copyProperties(cpPo, emailCpPo); // 复制属性
                        // 根据 xtzt 设置 status
                        if (cpPo.getYjxtzc() != null && cpPo.getYjxtzc().equals("2")) {
                            emailCpPo.setStatus(5);
                            emailCpPo.setBfyy(null);
                            emailCpPo.setBtgyy(null);
                            emailCpPo.setUpdateTime(null);
                        } else {
                            emailCpPo.setStatus(0);
                            emailCpPo.setBfyy(null);
                            emailCpPo.setBtgyy(null);
                            emailCpPo.setUpdateTime(null);
                        }
                        return emailCpPo; // 返回新对象
                    }).collect(Collectors.toList()); // 收集到列表中
                    assetEmailCpService.saveBatch(emailCpNewList);
                }
            } else if (assetPart.equals("4")) {
                //遍历资产移动应用程序资产单位
                String[] parts = assetTask.getDwmc().split(",");
                for (String part : parts) {
                    //根据单位名称查询资产
                    QueryWrapper<AssetApp> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("ssdw", part);
                    queryWrapper.eq("del_flag", 0);
                    List<AssetApp> list = assetAppService.lambdaQuery().eq(AssetApp::getSsdw, part).eq(AssetApp::getDelFlag, 0).list();
                    List<AssetAppCpPo> appCpNewList = list.stream().map(cpPo -> {
                        AssetAppCpPo appCpPo = new AssetAppCpPo();
                        appCpPo.setTaskId(id);
                        appCpPo.setAssetId(cpPo.getId());

                        BeanUtils.copyProperties(cpPo, appCpPo); // 复制属性
                        // 根据 xtzt 设置 status
                        if (cpPo.getAppState() != null && cpPo.getAppState().equals("7")) {
                            appCpPo.setStatus(5);
                            appCpPo.setBfyy(null);
                            appCpPo.setBtgyy(null);
                            appCpPo.setUpdateTime(null);
                        } else {
                            appCpPo.setStatus(0);
                            appCpPo.setBfyy(null);
                            appCpPo.setBtgyy(null);
                            appCpPo.setUpdateTime(null);
                        }
                        return appCpPo; // 返回新对象
                    }).collect(Collectors.toList()); // 收集到列表中
                    assetAppCpService.saveBatch(appCpNewList);
                }
            }

        }
    }

    /**
     * 修改数据
     *
     * @param assetTask 实例对象
     * @return 实例对象
     */
    @Override
    public int update(AssetTask assetTask) {
        return this.assetTaskDao.update(assetTask);

    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.assetTaskDao.deleteById(id) > 0;
    }

    @Override
    public AssetCurrentCpPo taskSh(int taskId, int assetId) {

        AssetCurrentCpPo byId = assetTaskDao.findBytaskIdandAssestId(taskId, assetId);
        byId.setGylxxList(assetSupplyChainCpService.lambdaQuery().eq(AssetSupplyChainCpPo::getAssetId, assetId).eq(AssetSupplyChainCpPo::getDelFlag, "0").eq(AssetSupplyChainCpPo::getTaskId, taskId).list());
        byId.setJcwlList(assetBasicNetworkCpService.lambdaQuery().eq(AssetBasicNetworkCpPo::getAssetId, assetId).eq(AssetBasicNetworkCpPo::getDelFlag, "0").eq(AssetBasicNetworkCpPo::getTaskId, taskId).list());
        byId.setXjgywxt(assetBusinessFormCpService.lambdaQuery().eq(AssetBusinessFormCpPo::getAssetId, assetId).eq(AssetBusinessFormCpPo::getDelFlag, "0").eq(AssetBusinessFormCpPo::getTaskId, taskId).one());
        byId.setOtherConcat(unitOtherConcatCpService.lambdaQuery().eq(UnitOtherConcatCpPo::getAssetId, assetId).eq(UnitOtherConcatCpPo::getDelFlag, "0").eq(UnitOtherConcatCpPo::getTaskId, taskId).list());
        byId.setSjzcList(assetAssetSjCpService.lambdaQuery().eq(AssetSjCpPo::getAssetId, assetId).eq(AssetSjCpPo::getDelFlag, "0").eq(AssetSjCpPo::getTaskId, taskId).list());



        List<Acomma> a1 = new ArrayList<>();
        if (byId.getGlym() != null) {
            Arrays.asList(byId.getGlym().split(",")).forEach(x -> {
                Acomma acomma = new Acomma();
                acomma.setKey(x);
                a1.add(acomma);
            });
            byId.setGlymList(a1);
        }
        List<Acomma> a2 = new ArrayList<>();
        if (byId.getGlIp() != null && !byId.getGlIp().isEmpty()) {
            for (Object o : JSONArray.parseArray(byId.getGlIp())) {
                Acomma acomma = new Acomma();
                JSONObject jsonObject = JSONObject.parseObject(o.toString());
                if (BeanUtil.isNotEmpty(jsonObject.get("key"))) {
                    acomma.setKey(jsonObject.get("key").toString());
                } else {
                    acomma.setKey("");
                }
                if (BeanUtil.isNotEmpty(jsonObject.get("ipType"))) {
                    acomma.setIpType(jsonObject.get("ipType").toString());
                } else {
                    acomma.setIpType("");
                }
                Object portValue = jsonObject.get("port");
                if (portValue != null && !portValue.toString().isEmpty()) {
                    acomma.setPort(portValue.toString());
                }else{
                    acomma.setPort("");
                }
                a2.add(acomma);
            }
            byId.setGlIpList(a2);
        } else {
            byId.setGlIpList(new ArrayList<>());
        }

        return byId;


    }

    @Override
    public AssetCurrentHistory webHistory(Long id) {
        QueryWrapper<AssetCurrentHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("current_id", id);
        AssetCurrentHistory byId = assetCurrentHistoryService.getOne(queryWrapper);
        byId.setGylxxList(assetSupplyChainCpService.lambdaQuery().eq(AssetSupplyChainCpPo::getAssetId, byId.getId()).eq(AssetSupplyChainCpPo::getDelFlag, "0").eq(AssetSupplyChainCpPo::getTaskId, byId.getTaskId()).list());
        byId.setJcwlList(assetBasicNetworkCpService.lambdaQuery().eq(AssetBasicNetworkCpPo::getAssetId, byId.getId()).eq(AssetBasicNetworkCpPo::getDelFlag, "0").eq(AssetBasicNetworkCpPo::getTaskId, byId.getTaskId()).list());
        byId.setXjgywxt(assetBusinessFormCpService.lambdaQuery().eq(AssetBusinessFormCpPo::getAssetId, byId.getId()).eq(AssetBusinessFormCpPo::getDelFlag, "0").eq(AssetBusinessFormCpPo::getTaskId, byId.getTaskId()).one());
        byId.setOtherConcat(unitOtherConcatCpService.lambdaQuery().eq(UnitOtherConcatCpPo::getAssetId, byId.getId()).eq(UnitOtherConcatCpPo::getDelFlag, "0").eq(UnitOtherConcatCpPo::getTaskId, byId.getTaskId()).list());
        byId.setSjzcList(assetAssetSjCpService.lambdaQuery().eq(AssetSjCpPo::getAssetId, byId.getId()).eq(AssetSjCpPo::getDelFlag, "0").eq(AssetSjCpPo::getTaskId, byId.getTaskId()).list());

        List<Acomma> a1 = new ArrayList<>();
        if (byId.getGlym() != null) {
            Arrays.asList(byId.getGlym().split(",")).forEach(x -> {
                Acomma acomma = new Acomma();
                acomma.setKey(x);
                a1.add(acomma);
            });
            byId.setGlymList(a1);
        }
        List<Acomma> a2 = new ArrayList<>();
        if (byId.getGlIp() != null) {
            Arrays.asList(byId.getGlIp().split(",")).forEach(x -> {
                Acomma acomma = new Acomma();
                acomma.setKey(x);
                a2.add(acomma);
            });
            byId.setGlIpList(a2);
        }
        return byId;
    }

    @Override
    public AssetMiniProgramsCpPo minitaskSh(int taskId, int assetId) {
        QueryWrapper<AssetMiniProgramsCpPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("asset_id", assetId);
        queryWrapper.eq("task_id", taskId);
        return assetMiniProgramsCpService.getOne(queryWrapper);
    }

    @Override
    public AssetMiniProgramsHistory miniHistory(Long id) {
        QueryWrapper<AssetMiniProgramsHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mini_id", id);
        return assetMiniProgramsHistoryService.getOne(queryWrapper);
    }

    @Override
    public AssetOfficialAccountCpPo officetaskSh(int taskId, int assetId) {
        QueryWrapper<AssetOfficialAccountCpPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("asset_id", assetId);
        queryWrapper.eq("task_id", taskId);

        AssetOfficialAccountCpPo byId = assetOfficialAccountCpService.getOne(queryWrapper);
        List<AssetOfficialAccountMenu> a1 = new ArrayList<>();
        String[] s1 = byId.getCdmc().split("\\|");
        String[] s2 = byId.getCdlj().split("\\|");
        String[] s3 = byId.getCdlx().split("\\|");
        for (int i = 0; i < s1.length; i++) {
            AssetOfficialAccountMenu as = new AssetOfficialAccountMenu();
            if (s1[i] != null && !Objects.equals(s1[i], "")) {
                as.setCdmc(s1[i]);
            }
            if (s2[i] != null && !Objects.equals(s2[i], "")) {
                as.setCdlj(s2[i]);
            }
            if (s3[i] != null && !Objects.equals(s3[i], "")) {
                as.setCdlx(s3[i]);
            }
            a1.add(as);
        }
        byId.setCdList(a1);
        return byId;
    }

    @Override
    public AssetOfficialAccountHistory officeHistory(Long id) {
        QueryWrapper<AssetOfficialAccountHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id", id);
        AssetOfficialAccountHistory byId = assetOfficialAccountHistoryService.getOne(queryWrapper);
        List<AssetOfficialAccountMenu> a1 = new ArrayList<>();
        String[] s1 = byId.getCdmc().split("\\|");
        String[] s2 = byId.getCdlj().split("\\|");
        String[] s3 = byId.getCdlx().split("\\|");
        for (int i = 0; i < s1.length; i++) {
            AssetOfficialAccountMenu as = new AssetOfficialAccountMenu();
            if (s1[i] != null && !Objects.equals(s1[i], "")) {
                as.setCdmc(s1[i]);
            }
            if (s2[i] != null && !Objects.equals(s2[i], "")) {
                as.setCdlj(s2[i]);
            }
            if (s3[i] != null && !Objects.equals(s3[i], "")) {
                as.setCdlx(s3[i]);
            }
            a1.add(as);
        }
        byId.setCdList(a1);
        return byId;
    }

    @Override
    public AssetEmailCpPo emailtaskSh(int taskId, int assetId) {
        QueryWrapper<AssetEmailCpPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("asset_id", assetId);
        queryWrapper.eq("task_id", taskId);
        return assetEmailCpService.getOne(queryWrapper);
    }

    @Override
    public AssetEmailHistory emailHistory(Long id) {
        QueryWrapper<AssetEmailHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("eamil_id", id);

        return assetEmailHistoryService.getOne(queryWrapper);
    }

    @Override
    public AssetAppCpPo apptaskSh(int taskId, int assetId) {
        QueryWrapper<AssetAppCpPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("asset_id", assetId);
        queryWrapper.eq("task_id", taskId);
        return assetAppCpService.getOne(queryWrapper);
    }

    @Override
    public AssetAppHistory appHistory(Long id) {
        QueryWrapper<AssetAppHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("app_id", id);
        return assetAppHistoryService.getOne(queryWrapper);

    }

    @Override
    public Page<AssestTaskXqresponse> zcHc(Page<AssestTaskXqresponse> assetTask, AssestTaskXqRequest pageRequest) {
        assetTask.setOptimizeCountSql(false);
        Page<AssestTaskXqresponse> result = null;
        if (pageRequest.getType().equals("0")) {
            result = assetTaskDao.zcHc(assetTask, pageRequest);
        } else if (pageRequest.getType().equals("1")) {
            result = assetTaskDao.xcxZcHc(assetTask, pageRequest);
        } else if (pageRequest.getType().equals("2")) {
            result = assetTaskDao.gzhZcHc(assetTask, pageRequest);
        } else if (pageRequest.getType().equals("3")) {
            result = assetTaskDao.emailZcHc(assetTask, pageRequest);
        } else if (pageRequest.getType().equals("4")) {
            result = assetTaskDao.appZcHc(assetTask, pageRequest);
        }
        // 确保 records 不为 null
        if (result.getRecords() == null) {
            result.setRecords(new ArrayList<>()); // 设置为空数组
        }
        return result;
    }


    @Override
    public int sh(AssetCurrentShRequest req) throws ParseException {

        // 获取当前时间
        LocalDateTime localDateTime = LocalDateTime.now();
        // 转换为 Date
        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        if (req.getType() == 0) {
            //web资产
            //是否超期
//            ifCq(req, assetTask);
            //创建任务流程
            getTaskStatus(req);
            if (req.getBtgyy() != null) {
                assetTaskDao.sh(req);
//                //修改子表和新增历史表
                updateZiandHistory(req, date);

            } else {
                //修改子表和新增历史表
                updateZiandHistory(req, date);
                //同步主表
                taskSaveOrDelete(req);


            }
        } else if (req.getType() == 1) {
            //小程序资产
            //创建任务流程
//            ifCq(req, assetTask);
            // //创建任务流程
            getTaskStatus(req);
            if (req.getBtgyy() != null) {
                assetTaskDao.xcxsh(req);
//                //修改子表和新增历史表
                updateMinZiandHistory(req, date);

            } else {
                //修改子表和新增历史表
                updateMinZiandHistory(req, date);
                //同步主表
                syb(req);
            }

        } else if (req.getType() == 2) {
            //公众号资产
            //创建任务流程
//            ifCq(req, assetTask);
            //创建任务流程
            getTaskStatus(req);
            if (req.getBtgyy() != null) {
                assetTaskDao.gzhsh(req);
//                //修改子表和历史表新增
                updateGzhZiandHistory(req, date);

            } else {
                //修改子表和历史表新增
                updateGzhZiandHistory(req, date);
                //同步主表
                syb(req);
            }

        } else if (req.getType() == 3) {
            //电子邮件
            //创建任务流程
//            ifCq(req, assetTask);
            //创建任务流程
            getTaskStatus(req);
            if (req.getBtgyy() != null) {
                assetTaskDao.emailsh(req);
//                //修改子表和历史表新增
                updateEmailZiandHistory(req, date);
            } else {
                //修改子表和历史表新增
                updateEmailZiandHistory(req, date);
                //同步主表
                syb(req);
            }

        } else if (req.getType() == 4) {
            //app
            //创建任务流程
//            ifCq(req, assetTask);
            //创建任务流程
            getTaskStatus(req);
            if (req.getBtgyy() != null) {
                assetTaskDao.appsh(req);
//                //修改子表和历史表新增
                updateAppZiandHistory(req, date);

            } else {
                //修改子表和历史表新增
                updateAppZiandHistory(req, date);
                //同步主表
                syb(req);
            }

        }
        AssetTask finalTask = assetTaskDao.getByTaskId(req.getTaskId());
        LocalDateTime finishTime = null;
        //根据任务id查询是否进行中
        Integer jxz = assetTaskDao.getJxz(req.getTaskId());
        if (jxz > 0) {
            //进行中
            finalTask.setTaskStatus(1);
            finalTask.setTaskFinishTime(finishTime);
            assetTaskService.update(finalTask);
        } else {
            //根据任务id查询是否超期
            String ifcq = assetTaskDao.getifcq(req.getTaskId());
            String taskDeadline = finalTask.getTaskDeadline(); // 定义日期格式，确保格式与字符串一致
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 根据你的时间格式调整
            // 将字符串转换为日期对象
            Date ifcqDate = dateFormat.parse(ifcq);
            Date taskDeadlineDate = dateFormat.parse(taskDeadline);
            // 比较两个日期
            if (ifcqDate.before(taskDeadlineDate)) {
                //超期完成
                finalTask.setTaskStatus(2);
                finishTime = LocalDateTime.now();
                finalTask.setTaskFinishTime(finishTime);
                assetTaskService.update(finalTask);
            } else if (ifcqDate.after(taskDeadlineDate)) {
                //正常完成

                finalTask.setTaskStatus(3);
                finishTime = LocalDateTime.now();
                finalTask.setTaskFinishTime(finishTime);
                assetTaskService.update(finalTask);
            }
        }

        return 0;
    }

    private void updateAppZiandHistory(AssetCurrentShRequest req, Date date) {
        req.getAssetAppCpPo().setStatus(req.getStatus());
        req.getAssetAppCpPo().setUpdateTime(date);
        req.getAssetAppCpPo().setBtgyy(req.getBtgyy());
        assetAppCpService.updateById(req.getAssetAppCpPo());
        //新增历史记录表
//        AssetAppHistory assetAppHistory = new AssetAppHistory();
//        BeanUtils.copyProperties(req.getAssetAppCpPo(), assetAppHistory);
//        assetAppHistory.setAppId(null);
//        assetAppHistoryService.save(assetAppHistory);
    }

    private void updateEmailZiandHistory(AssetCurrentShRequest req, Date date) {
        req.getAssetEmailCpPo().setStatus(req.getStatus());
        req.getAssetEmailCpPo().setUpdateTime(date);
        req.getAssetEmailCpPo().setBtgyy(req.getBtgyy());
        assetEmailCpService.updateById(req.getAssetEmailCpPo());
//        //新增历史记录表
//        AssetEmailHistory assetEmailHistory = new AssetEmailHistory();
//        BeanUtils.copyProperties(req.getAssetEmailCpPo(), assetEmailHistory);
//        assetEmailHistory.setEmailId(null);
//        assetEmailHistoryService.save(assetEmailHistory);
    }

    private void updateGzhZiandHistory(AssetCurrentShRequest req, Date date) {
        if (req.getAssetOfficialAccountCpPo().getCdList() != null && !req.getAssetOfficialAccountCpPo().getCdList().isEmpty()) {
            StringBuilder a = new StringBuilder();
            StringBuilder b = new StringBuilder();
            StringBuilder c = new StringBuilder();
            req.getAssetOfficialAccountCpPo().getCdList().forEach(x -> {
                a.append(x.getCdmc()).append("|");
                b.append(x.getCdlj()).append("|");
                c.append(x.getCdlx()).append("|");
            });
            req.getAssetOfficialAccountCpPo().setCdmc(a.toString());
            req.getAssetOfficialAccountCpPo().setCdlj(b.toString());
            req.getAssetOfficialAccountCpPo().setCdlx(c.toString());
        }
        req.getAssetOfficialAccountCpPo().setStatus(req.getStatus());
        req.getAssetOfficialAccountCpPo().setUpdateTime(date);
        req.getAssetOfficialAccountCpPo().setBtgyy(req.getBtgyy());
        assetOfficialAccountCpService.updateById(req.getAssetOfficialAccountCpPo());
//        //新增历史记录表
//        AssetOfficialAccountHistory assetOfficialAccountHistory = new AssetOfficialAccountHistory();
//        BeanUtils.copyProperties(req.getAssetOfficialAccountCpPo(), assetOfficialAccountHistory);
//        assetOfficialAccountHistory.setAccountId(null);
//        assetOfficialAccountHistoryService.save(assetOfficialAccountHistory);
    }

    private void updateMinZiandHistory(AssetCurrentShRequest req, Date date) {
        req.getAssetMiniProgramsCpPo().setStatus(req.getStatus());
        req.getAssetMiniProgramsCpPo().setUpdateTime(date);
        req.getAssetMiniProgramsCpPo().setBtgyy(req.getBtgyy());
        assetMiniProgramsCpService.updateById(req.getAssetMiniProgramsCpPo());
//        //新增历史记录表
//        AssetMiniProgramsHistory assetMiniProgramsHistory = new AssetMiniProgramsHistory();
//        BeanUtils.copyProperties(req.getAssetMiniProgramsCpPo(), assetMiniProgramsHistory);
//        assetMiniProgramsHistory.setMiniId(null);
//        assetMiniProgramsHistoryService.save(assetMiniProgramsHistory);
    }

    private void updateZiandHistory(AssetCurrentShRequest req, Date date) {
//        //根据资产id和任务id删除五张表数据
        assetCurrentCpService.deletByAssetIdandTaskId(req.getAssetCurrentCpPo().getId(), req.getAssetCurrentCpPo().getTaskId());
        assetBusinessFormCpService.deletByAssetIdandTaskId(req.getAssetCurrentCpPo().getId(), req.getAssetCurrentCpPo().getTaskId());
        assetSupplyChainCpService.deletByAssetIdandTaskId(req.getAssetCurrentCpPo().getId(), req.getAssetCurrentCpPo().getTaskId());
        unitOtherConcatCpService.deletByAssetIdandTaskId(req.getAssetCurrentCpPo().getId(), req.getAssetCurrentCpPo().getTaskId());
        assetBasicNetworkCpService.deletByAssetIdandTaskId(req.getAssetCurrentCpPo().getId(), req.getAssetCurrentCpPo().getTaskId());
        assetAssetSjCpService.deletByAssetIdandTaskId(req.getAssetCurrentCpPo().getId(), req.getAssetCurrentCpPo().getTaskId());


        StringBuilder a = new StringBuilder();
        if (CollectionUtil.isNotEmpty(req.getAssetCurrentCpPo().getGlymList())) {
            req.getAssetCurrentCpPo().getGlymList().forEach(x -> {
                if (!Objects.equals(x.getKey(), "") && x.getKey() != null) {
                    a.append(x.getKey());
                    a.append(",");
                }
            });
        }
        req.getAssetCurrentCpPo().setGlym(a.toString());
        StringBuilder b = new StringBuilder();
        if (CollectionUtil.isNotEmpty(req.getAssetCurrentCpPo().getGlIpList())) {
            req.getAssetCurrentCpPo().setGlIp(req.getAssetCurrentCpPo().getGlIpList().toString());
        } else {
            req.getAssetCurrentCpPo().setGlIp("");
        }
        req.getAssetCurrentCpPo().setStatus(req.getStatus());

        // 设置更新时间
        req.getAssetCurrentCpPo().setUpdateTime(date);
        req.getAssetCurrentCpPo().setStatus(req.getStatus());
        req.getAssetCurrentCpPo().setBtgyy(req.getBtgyy());
        assetCurrentCpService.save(req.getAssetCurrentCpPo());
        //新增新监管业务形态
        if (req.getAssetCurrentCpPo().getXjgywxt() != null) {
            req.getAssetCurrentCpPo().getXjgywxt().setAssetId(req.getAssetCurrentCpPo().getId());
            req.getAssetCurrentCpPo().getXjgywxt().setTaskId(req.getAssetCurrentCpPo().getTaskId());
            assetBusinessFormCpService.save(req.getAssetCurrentCpPo().getXjgywxt());
        }
        if (CollectionUtil.isNotEmpty(req.getAssetCurrentCpPo().getGylxxList())) {
            for (AssetSupplyChainCpPo items : req.getAssetCurrentCpPo().getGylxxList()) {
                items.setAssetId(req.getAssetCurrentCpPo().getId());
                items.setTaskId(req.getAssetCurrentCpPo().getTaskId());

            }
            //新增供应链
            assetSupplyChainCpService.saveBatch(req.getAssetCurrentCpPo().getGylxxList());
        }
        if (CollectionUtil.isNotEmpty(req.getAssetCurrentCpPo().getJcwlList())) {
            for (AssetBasicNetworkCpPo items : req.getAssetCurrentCpPo().getJcwlList()) {
                items.setAssetId(req.getAssetCurrentCpPo().getId());
                items.setTaskId(req.getAssetCurrentCpPo().getTaskId());

            }
            //新增基础网络
            assetBasicNetworkCpService.saveBatch(req.getAssetCurrentCpPo().getJcwlList());
        }
        if (CollectionUtil.isNotEmpty(req.getAssetCurrentCpPo().getOtherConcat())) {
            for (UnitOtherConcatCpPo items : req.getAssetCurrentCpPo().getOtherConcat()) {
                items.setAssetId(req.getAssetCurrentCpPo().getId());
                items.setTaskId(req.getAssetCurrentCpPo().getTaskId());
            }
            //新增其他联系人
            unitOtherConcatCpService.saveBatch(req.getAssetCurrentCpPo().getOtherConcat());
        }
        if (CollectionUtil.isNotEmpty(req.getAssetCurrentCpPo().getSjzcList())) {
            for (AssetSjCpPo items : req.getAssetCurrentCpPo().getSjzcList()) {
                items.setAssetId(req.getAssetCurrentCpPo().getId());
                items.setTaskId(req.getAssetCurrentCpPo().getTaskId());
            }
            //新增数据资产
            assetAssetSjCpService.saveBatch(req.getAssetCurrentCpPo().getSjzcList());
        }
        //新增历史记录
        //  saveHistory(req, a, b, date, assetCurrentHistory);
    }

    private void saveHistory(AssetCurrentShRequest req, StringBuilder a, StringBuilder b, Date date, AssetCurrentHistory assetCurrentHistory) {
        //新增历史记录
        StringBuilder c = new StringBuilder();
        if (CollectionUtil.isNotEmpty(req.getAssetCurrentCpPo().getGlymList())) {
            req.getAssetCurrentCpPo().getGlymList().forEach(x -> {
                if (!Objects.equals(x.getKey(), "") && x.getKey() != null) {
                    c.append(x.getKey());
                    c.append(",");
                }
            });
        }
        req.getAssetCurrentCpPo().setGlym(a.toString());
        StringBuilder d = new StringBuilder();
        if (CollectionUtil.isNotEmpty(req.getAssetCurrentCpPo().getGlIpList())) {
            req.getAssetCurrentCpPo().setGlIp(req.getAssetCurrentCpPo().getGlIpList().toString());
        } else {
            req.getAssetCurrentCpPo().setGlIp("");
        }

        req.getAssetCurrentCpPo().setStatus(req.getStatus());

        // 设置更新时间
        req.getAssetCurrentCpPo().setUpdateTime(date);
        BeanUtils.copyProperties(req.getAssetCurrentCpPo(), assetCurrentHistory);
        assetCurrentHistory.setCurrentId(null);
        assetCurrentHistoryService.save(assetCurrentHistory);

        //新增新监管业务形态历史表
        if (req.getAssetCurrentCpPo().getXjgywxt() != null) {
            req.getAssetCurrentCpPo().getXjgywxt().setAssetId(req.getAssetCurrentCpPo().getId());
            req.getAssetCurrentCpPo().getXjgywxt().setTaskId(req.getAssetCurrentCpPo().getTaskId());
            AssetBusinessFormHistory assetBusinessFormHistory = new AssetBusinessFormHistory();
            assetBusinessFormHistory.setCopyId(req.getAssetCurrentCpPo().getCurrentId());
            BeanUtils.copyProperties(req.getAssetCurrentCpPo().getXjgywxt(), assetBusinessFormHistory);
            assetBusinessFormHistory.setBusinessId(null);
            assetBusinessFormHistoryService.save(assetBusinessFormHistory);

        }
        if (!req.getAssetCurrentCpPo().getGylxxList().isEmpty()) {
            for (AssetSupplyChainCpPo items : req.getAssetCurrentCpPo().getGylxxList()) {
                items.setAssetId(req.getAssetCurrentCpPo().getId());
                items.setTaskId(req.getAssetCurrentCpPo().getTaskId());

            }
            //新增供应链历史表
            List<AssetSupplyChainHistory> assetSupplyChainHistoryList = req.getAssetCurrentCpPo().getGylxxList().stream().map(cpPo -> {
                AssetSupplyChainHistory assetSupplyChainHistory = new AssetSupplyChainHistory();
                assetSupplyChainHistory.setCopyId(req.getAssetCurrentCpPo().getCurrentId());
                BeanUtils.copyProperties(cpPo, assetSupplyChainHistory); // 复制属性
                assetSupplyChainHistory.setSupplyId(null);
                return assetSupplyChainHistory; // 返回新对象
            }).collect(Collectors.toList()); // 收集到列表中
            assetSupplyChainHistoryService.saveBatch(assetSupplyChainHistoryList);
        }
        if (!req.getAssetCurrentCpPo().getJcwlList().isEmpty()) {
            for (AssetBasicNetworkCpPo items : req.getAssetCurrentCpPo().getJcwlList()) {
                items.setAssetId(req.getAssetCurrentCpPo().getId());
                items.setTaskId(req.getAssetCurrentCpPo().getTaskId());

            }
            //新增基础网络历史表
            List<AssetBasicNetworkHistory> assetSupplyChainHistoryList = req.getAssetCurrentCpPo().getJcwlList().stream().map(cpPo -> {
                AssetBasicNetworkHistory assetBasicNetworkHistory = new AssetBasicNetworkHistory();
                assetBasicNetworkHistory.setCopyId(req.getAssetCurrentCpPo().getCurrentId());
                BeanUtils.copyProperties(cpPo, assetBasicNetworkHistory); // 复制属性
                assetBasicNetworkHistory.setNetworkId(null);
                return assetBasicNetworkHistory; // 返回新对象
            }).collect(Collectors.toList()); // 收集到列表中
            assetBasicNetworkHistoryService.saveBatch(assetSupplyChainHistoryList);
        }
        if (!req.getAssetCurrentCpPo().getOtherConcat().isEmpty()) {
            for (UnitOtherConcatCpPo items : req.getAssetCurrentCpPo().getOtherConcat()) {
                items.setAssetId(req.getAssetCurrentCpPo().getId());
                items.setTaskId(req.getAssetCurrentCpPo().getTaskId());
            }
            //新增其他联系人
            List<UnitOtherConcatHistory> unitOtherConcatHistoryList = req.getAssetCurrentCpPo().getOtherConcat().stream().map(cpPo -> {
                UnitOtherConcatHistory unitOtherConcatHistory = new UnitOtherConcatHistory();
                unitOtherConcatHistory.setCopyId(req.getAssetCurrentCpPo().getCurrentId());
                BeanUtils.copyProperties(cpPo, unitOtherConcatHistory); // 复制属性
                unitOtherConcatHistory.setUnitId(null);
                return unitOtherConcatHistory; // 返回新对象
            }).collect(Collectors.toList()); // 收集到列表中
            unitOtherConcatHistoryService.saveBatch(unitOtherConcatHistoryList);
        }
    }

    private void syb(AssetCurrentShRequest req) {
        if (req.getType() == 1) {
            //小程序
            AssetMiniProgramsCpPo assetMiniProgramsCpPo = assetMiniProgramsCpService.findByTaskId(req.getAssetId(), req.getTaskId());
            //删除原有数据
            if (assetMiniProgramsCpPo != null) {
                assetMiniProgramsService.deleteIdList(Collections.singletonList(assetMiniProgramsCpPo.getAssetId()));
                //将子表数据复制到主表中
                AssetMiniPrograms assetMiniPrograms = new AssetMiniPrograms();
                BeanUtils.copyProperties(assetMiniProgramsCpPo, assetMiniPrograms); // 复制属性
                assetMiniPrograms.setId(null);
                assetMiniProgramsService.save(assetMiniPrograms);
            }

        }
        if (req.getType() == 2) {
            //公众号资产
            AssetOfficialAccountCpPo assetOfficialAccountCpPo = assetOfficialAccountCpService.findByTaskId(req.getAssetId(), req.getTaskId());
            //删除原有数据
            if (assetOfficialAccountCpPo != null) {
                assetOfficialAccountService.deleteIdList(Collections.singletonList(assetOfficialAccountCpPo.getAssetId()));
                //将子表数据复制到主表中
                AssetOfficialAccount assetOfficialAccount = new AssetOfficialAccount();
                BeanUtils.copyProperties(assetOfficialAccountCpPo, assetOfficialAccount); // 复制属性
                assetOfficialAccount.setId(null);
                assetOfficialAccountService.save(assetOfficialAccount);
            }


        }
        if (req.getType() == 3) {
            //电子邮件资产
            //根据assetId查询查询数据
            AssetEmailCpPo assetEmailCpPo = assetEmailCpService.findByTaskId(req.getAssetId(), req.getTaskId());
            //删除原有数据
            if (assetEmailCpPo != null) {
                assetEmailService.deleteIdList(Collections.singletonList(assetEmailCpPo.getAssetId()));
                //将子表数据复制到主表中
                AssetEmail assetEmail = new AssetEmail();
                BeanUtils.copyProperties(assetEmailCpPo, assetEmail); // 复制属性
                assetEmail.setId(null);
                assetEmailService.save(assetEmail);
            }
        }
        if (req.getType() == 4) {
            //移动应用程序资产
            //根据资产id查询
            AssetAppCpPo assetAppCpPo = assetAppCpService.findByTaskId(req.getAssetId(), req.getTaskId());
            //删除原有数据
            if (assetAppCpPo != null) {
                assetAppService.deleteIdList(Collections.singletonList(assetAppCpPo.getAssetId()));
                //将子表数据复制到主表中
                AssetApp assetApp = new AssetApp();
                BeanUtils.copyProperties(assetAppCpPo, assetApp); // 复制属性
                assetApp.setId(null);
                assetAppService.save(assetApp);
            }


        }


    }

    private void ifCq(AssetCurrentShRequest req, AssetTask assetTask) {

        if (req.getStatus().equals(3)) {
            AssetTask newAssetTask = new AssetTask();
            //比较是否超期
            int deadlineStatus = extracted(assetTask);
            int deadlineCount = Optional.ofNullable(assetTask.getDeadlineCount()).orElse(0);
            int count = Optional.ofNullable(assetTask.getCount()).orElse(0);
            BeanUtils.copyProperties(assetTask, newAssetTask);
            if (deadlineStatus == 1) {
                //超期
                newAssetTask.setDeadlineCount(deadlineCount + 1);
                assetTaskService.update(newAssetTask);
            } else {
                newAssetTask.setCount(count + 1);
                assetTaskService.update(newAssetTask);
            }

        }
    }

    //当前时间喝任务截至时间比较
    private int extracted(AssetTask assetTask) {
        int status = 0;
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();

        // 创建格式化器，时间部分使用冒号
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化当前的日期和时间
        String formattedDateTime = now.format(formatter);
        //获取任务截至时间
        String deadLine = assetTask.getTaskDeadline();
        // 转换为 LocalDateTime，使用相同的格式
        LocalDateTime deadlineDate = LocalDateTime.parse(deadLine, formatter);
        LocalDateTime currentDate = LocalDateTime.parse(formattedDateTime, formatter);
        if (deadlineDate.isBefore(currentDate)) {
            //超期
            status = 1;
        } else {
            //未超期
            status = 2;
        }
        return status;
    }

    //web
    private void taskSaveOrDelete(AssetCurrentShRequest req) {
        // 根据资产id查询五张数据
        AssetCurrentCpPo currentCpPo = assetCurrentCpService.findByTaskId(req.getAssetId(), req.getTaskId());
        //删除主表原有数据
        if (currentCpPo != null) {

            //将子表数据复制到主表中

            ///BeanUtils.copyProperties(currentCpPo, current); // 复制属性
            AssetCurrent assetCurrent = BeanUtil.copyProperties(currentCpPo, AssetCurrent.class);
            assetCurrentService.updateById(assetCurrent);
        }

        //AssetBusinessFormCpPo
        AssetBusinessFormCpPo businessFormCpPo = assetBusinessFormCpService.findByTaskId(req.getAssetId(), req.getTaskId());
        //删除原有数据
        if (businessFormCpPo != null) {
            assetBusinessFormService.deleteIdList(Collections.singletonList(businessFormCpPo.getAssetId()));
            //将子表数据复制到主表中
            AssetBusinessForm assetBusinessForm = new AssetBusinessForm();
            BeanUtils.copyProperties(businessFormCpPo, assetBusinessForm); // 复制属性
            assetBusinessForm.setId(null);
            assetBusinessFormService.save(assetBusinessForm);
        } else {
            //将子表数据复制到主表中
            AssetBusinessForm assetBusinessForm = new AssetBusinessForm();
            BeanUtils.copyProperties(businessFormCpPo, assetBusinessForm); // 复制属性
            assetBusinessForm.setId(null);
            assetBusinessFormService.save(assetBusinessForm);
        }


        //AssetBasicNetworkCpPo
        List<AssetBasicNetworkCpPo> networkCpPo = assetBasicNetworkCpService.findByTaskId(req.getAssetId(), req.getTaskId());
        List<Long> netWorkList = networkCpPo.stream().map(AssetBasicNetworkCpPo::getAssetId).collect(Collectors.toList());
        //删除原有数据
        if (CollectionUtil.isNotEmpty(netWorkList)) {
            assetBasicNetworkService.deleteIdList(netWorkList);
            List<AssetBasicNetwork> networksCpPoList = req.getAssetCurrentCpPo().getJcwlList().stream().map(cpPo -> {
                AssetBasicNetwork assetBasicNetwork = new AssetBasicNetwork();
                BeanUtils.copyProperties(cpPo, assetBasicNetwork); // 复制属性
                assetBasicNetwork.setId(null);
                return assetBasicNetwork; // 返回新对象
            }).collect(Collectors.toList());
            assetBasicNetworkService.saveBatch(networksCpPoList);

        } else {
            List<AssetBasicNetwork> networksCpPoList = req.getAssetCurrentCpPo().getJcwlList().stream().map(cpPo -> {
                AssetBasicNetwork assetBasicNetwork = new AssetBasicNetwork();
                BeanUtils.copyProperties(cpPo, assetBasicNetwork); // 复制属性
                assetBasicNetwork.setId(null);
                return assetBasicNetwork; // 返回新对象
            }).collect(Collectors.toList());
            assetBasicNetworkService.saveBatch(networksCpPoList);
        }


        //AssetSupplyChainCpPo
        List<AssetSupplyChainCpPo> supplyChainCpPo = assetSupplyChainCpService.findByTaskId(req.getAssetId(), req.getTaskId());
        List<Long> supplyList = supplyChainCpPo.stream().map(AssetSupplyChainCpPo::getAssetId).collect(Collectors.toList());
        //删除原有数据
        if (CollectionUtil.isNotEmpty(supplyList)) {
            assetSupplyChainService.deleteIdList(supplyList);
            List<AssetSupplyChain> supplyChainList = req.getAssetCurrentCpPo().getGylxxList().stream().map(cpPo -> {
                AssetSupplyChain assetSupplyChain = new AssetSupplyChain();
                BeanUtils.copyProperties(cpPo, assetSupplyChain); // 复制属性
                assetSupplyChain.setId(null);
                return assetSupplyChain; // 返回新对象
            }).collect(Collectors.toList());
            assetSupplyChainService.saveBatch(supplyChainList);
        } else {
            List<AssetSupplyChain> supplyChainList = req.getAssetCurrentCpPo().getGylxxList().stream().map(cpPo -> {
                AssetSupplyChain assetSupplyChain = new AssetSupplyChain();
                BeanUtils.copyProperties(cpPo, assetSupplyChain); // 复制属性
                assetSupplyChain.setId(null);
                return assetSupplyChain; // 返回新对象
            }).collect(Collectors.toList());
            assetSupplyChainService.saveBatch(supplyChainList);
        }
        //UnitOtherConcatCpPo
        List<UnitOtherConcatCpPo> unitOtherConcatCpPo = unitOtherConcatCpService.findByTaskId(req.getAssetId(), req.getTaskId());
        List<Long> unitList = unitOtherConcatCpPo.stream().map(UnitOtherConcatCpPo::getAssetId).collect(Collectors.toList());
        //删除原有数据
        if (CollectionUtil.isNotEmpty(unitList)) {
            unitOtherConcatService.deleteIdList(unitList);
            List<UnitOtherConcat> unitOtherConcatList = req.getAssetCurrentCpPo().getOtherConcat().stream().map(cpPo -> {
                UnitOtherConcat unitOtherConcat = new UnitOtherConcat();
                BeanUtils.copyProperties(cpPo, unitOtherConcat); // 复制属性
                unitOtherConcat.setConcatId(null);
                return unitOtherConcat; // 返回新对象
            }).collect(Collectors.toList());
            unitOtherConcatService.saveBatch(unitOtherConcatList);
        } else {
            List<UnitOtherConcat> unitOtherConcatList = req.getAssetCurrentCpPo().getOtherConcat().stream().map(cpPo -> {
                UnitOtherConcat unitOtherConcat = new UnitOtherConcat();
                BeanUtils.copyProperties(cpPo, unitOtherConcat); // 复制属性
                unitOtherConcat.setConcatId(null);
                return unitOtherConcat; // 返回新对象
            }).collect(Collectors.toList());
            unitOtherConcatService.saveBatch(unitOtherConcatList);
        }
    }

    @Override
    public Page<AssetdwHcResponse> dwHc(Page<AssetdwHcResponse> assetdwHcResponsePage, AssetdwHcRequest req) {

        return assetTaskDao.dwHc(assetdwHcResponsePage, req);
    }

    @Override
    public List<AssetdwHcResponse> export(AssetdwHcRequest req) {
        return assetTaskDao.export(req);
    }

    @Override
    public List<AssetTask> taskList(String dwmc) {
        return assetTaskDao.taskList(dwmc);
    }

    @Override
    public void updateYgtByTaskId(Integer taskId, Integer count) {
        assetTaskDao.updateYgtByTaskId(taskId, count);
    }

    @Override
    public void close(Integer taskId) {
        assetTaskDao.close(taskId);
    }

    @Override
    public List<AssetdwHcBlResponse> dwHcBl(AssetdwHcRequest req) {
        return assetTaskDao.dwHcBl(req);
    }

    @Override
    public AssetAppCpPo findByDwmc(String dwmc) {
        return assetTaskDao.findByDwmc(dwmc);
    }

    @Override
    public AssetTask findByTaskId(Integer taskId) {
        return assetTaskDao.findBytaskId(taskId);
    }

    //创建任务流程
    private void getTaskStatus(AssetCurrentShRequest req) {
        AssetLc assetLc = new AssetLc();

        if (req.getStatus() == 3) {
            assetLc.setTaskId(req.getTaskId());
            assetLc.setXfTime(LocalDateTime.now());
            assetLc.setAssetName("管理员审核通过" + req.getXtmc());
            assetLcService.insert(assetLc);
        } else if (req.getStatus() == 4) {
            assetLc.setTaskId(req.getTaskId());
            assetLc.setAssetName("管理员审核不通过" + req.getXtmc());
            assetLc.setXfTime(LocalDateTime.now());
            assetLcService.insert(assetLc);

        }
    }


}