package com.ruoyi.tc.service.impl;

import cn.hutool.core.collection.CollectionUtil;
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.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.AssetdwHcResponse;
import com.ruoyi.tc.mapper.AssetTaskMapper;
import com.ruoyi.tc.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
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;

    /**
     * 通过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) {
        int a = 0;
        //遍历单位
        String[] taskParts = assetTask.getDwmc().split(",");
        for (String part : taskParts) {
            //根据单位查询所有资产
            //web
            //  遍历资产类型字段
            String[] uParts = assetTask.getType().split(",");
            int current = 0;
            int xcx = 0;
            int gzh = 0;
            int email = 0;
            int app = 0;
            int totalCount = 0;
            for (String qq : uParts) {

                if (qq.equals("0")) {
                    current = assetCurrentService.findBydwmc(part);
                    totalCount += current;
                } else if (qq.equals("1")) {
                    xcx = assetMiniProgramsService.findBydwmc(part);
                    totalCount += xcx;
                } else if (qq.equals("2")) {
                    gzh = assetOfficialAccountService.findBydwmc(part);
                    totalCount += gzh;
                } else if (qq.equals("3")) {
                    email = assetEmailService.findBydwmc(part);
                    totalCount += email;
                } else if (qq.equals("4")) {
                    app = assetAppService.findBydwmc(part);
                    totalCount += app;
                }

            }
            assetTask.setTotalCount(totalCount);
        }
        //任务新增
        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);
                    for (AssetCurrentCpPo assetCurrentResponse : list) {
                        assetCurrentResponse.setTaskId(id);
                    }
                    assetCurrentCpService.saveBatch(list);
                    List<AssetBusinessFormCpPo> assetBusinessFormCpResponseList = new ArrayList<>();
                    for (AssetCurrentCpPo assetCurrentResponse : list) {
                        //新增新监管业务形态
                        //根据资产id查询新监管业务形态主表id
                        List<AssetBusinessFormCpPo> bussList = assetBusinessFormCpService.findByassetId(assetCurrentResponse.getId());
                        if (bussList != null) {
                            for (AssetBusinessFormCpPo assetBusinessFormCpResponse : bussList) {
                                if (assetCurrentResponse.getXjgywxt() != null) {
                                    assetCurrentResponse.getXjgywxt().setAssetId(assetBusinessFormCpResponse.getAssetId());
                                    assetCurrentResponse.getXjgywxt().setTaskId(id);
                                    assetBusinessFormCpResponseList.add(assetCurrentResponse.getXjgywxt());
                                }
                            }
                            assetBusinessFormCpService.saveBatch(assetBusinessFormCpResponseList);
                        }
                        //根据资产id查询供应链主表id-
                        List<AssetSupplyChainCpPo> supplyChainList = assetSupplyChainCpService.findByassetId(assetCurrentResponse.getId());
                        if (supplyChainList != null) {
                            for (AssetSupplyChainCpPo items : supplyChainList) {
                                items.setAssetId(assetCurrentResponse.getId());
                                items.setSupplyId(items.getSupplyId());
                                items.setTaskId(id);
                                //新增供应链
                            }
                            assetSupplyChainCpService.saveBatch(supplyChainList);
                        }
                        List<AssetBasicNetworkCpPo> BasicNetworkCpList = assetBasicNetworkCpService.findByassetId(assetCurrentResponse.getId());
                        if (BasicNetworkCpList != null) {
                            for (AssetBasicNetworkCpPo items : BasicNetworkCpList) {
                                //根据资产id查询基础网络主表id
                                items.setAssetId(assetCurrentResponse.getId());
                                items.setNetworkId(items.getNetworkId());
                                items.setTaskId(id);
                                //新增基础网络
                            }
                            assetBasicNetworkCpService.saveBatch(BasicNetworkCpList);
                        }
                        List<UnitOtherConcatCpPo> unitOtherConcatList = unitOtherConcatCpService.findByassetId(assetCurrentResponse.getId());
                        if (unitOtherConcatList != null) {
                            for (UnitOtherConcatCpPo items : unitOtherConcatList) {
                                //根据资产id查询其他联系人主表id
                                items.setAssetId(assetCurrentResponse.getId());
                                items.setConcatId(items.getConcatId());
                                items.setTaskId(id);
                                //新增其他联系人
                            }
                            unitOtherConcatCpService.saveBatch(unitOtherConcatList);
                        }
                    }
                }
            } 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("isbf", 0);
                    List<AssetMiniPrograms> list = assetMiniProgramsService.list(queryWrapper);
                    List<AssetMiniProgramsCpPo> miniProgramsNewList = list.stream()
                            .map(cpPo -> {
                                AssetMiniProgramsCpPo assetMiniProgramsCpPo = new AssetMiniProgramsCpPo();
                                assetMiniProgramsCpPo.setTaskId(id);
                                assetMiniProgramsCpPo.setStatus(0);
                                assetMiniProgramsCpPo.setAssetId(cpPo.getId());
                                BeanUtils.copyProperties(cpPo, assetMiniProgramsCpPo); // 复制属性
                                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("isbf", 0);
                    List<AssetOfficialAccount> list = assetOfficialAccountService.list(queryWrapper);
                    List<AssetOfficialAccountCpPo> officialAccountProgramsNewList = list.stream()
                            .map(cpPo -> {
                                AssetOfficialAccountCpPo assetOfficialAccountCpPo = new AssetOfficialAccountCpPo();
                                assetOfficialAccountCpPo.setTaskId(id);
                                assetOfficialAccountCpPo.setStatus(0);
                                assetOfficialAccountCpPo.setAssetId(cpPo.getId());
                                BeanUtils.copyProperties(cpPo, assetOfficialAccountCpPo); // 复制属性
                                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("isbf", 0);
                    List<AssetEmail> list = assetEmailService.list(queryWrapper);
                    List<AssetEmailCpPo> emailCpNewList = list.stream()
                            .map(cpPo -> {
                                AssetEmailCpPo emailCpPo = new AssetEmailCpPo();
                                emailCpPo.setTaskId(id);
                                emailCpPo.setAssetId(cpPo.getId());
                                emailCpPo.setStatus(0);
                                BeanUtils.copyProperties(cpPo, emailCpPo); // 复制属性
                                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("isbf", 0);
                    List<AssetApp> list = assetAppService.list(queryWrapper);
                    List<AssetAppCpPo> appCpNewList = list.stream()
                            .map(cpPo -> {
                                AssetAppCpPo appCpPo = new AssetAppCpPo();
                                appCpPo.setTaskId(id);
                                appCpPo.setAssetId(cpPo.getId());
                                appCpPo.setStatus(0);
                                BeanUtils.copyProperties(cpPo, appCpPo); // 复制属性
                                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());
        List<Acomma> a1 = new ArrayList<>();
        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<>();
        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 AssetOfficialAccountCpPo officetaskSh(int taskId, int assetId) {
        QueryWrapper<AssetOfficialAccountCpPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("asset_id", assetId);
        queryWrapper.eq("task_id", taskId);
        return assetOfficialAccountCpService.getOne(queryWrapper);
    }

    @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 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 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) {
        //根据taskid获取当前任务的count数量
        AssetTask assetTask = assetTaskDao.getByTaskId(req.getTaskId());
        if (req.getType() == 0) {
            //web资产
            //是否超期
            ifCq(req, assetTask);
            //创建任务流程
            getTaskStatus(req);
            assetTaskDao.sh(req);
        } else if (req.getType() == 1) {
            //小程序资产
            //创建任务流程
            ifCq(req, assetTask);
            getTaskStatus(req);
            assetTaskDao.xcxsh(req);
        } else if (req.getType() == 2) {
            //公众号资产
            //创建任务流程
            ifCq(req, assetTask);
            getTaskStatus(req);
            assetTaskDao.gzhsh(req);
        } else if (req.getType() == 3) {
            //电子邮件
            //创建任务流程
            ifCq(req, assetTask);
            getTaskStatus(req);
            assetTaskDao.emailsh(req);
        } else if (req.getType() == 4) {
            //app
            //创建任务流程
            ifCq(req, assetTask);
            getTaskStatus(req);
            assetTaskDao.appsh(req);
        }
        AssetTask finalTask = assetTaskDao.getByTaskId(req.getTaskId());
        //判断当前任务资产是否全部审核完成
        int deadlineCount = Optional.ofNullable(finalTask.getDeadlineCount()).orElse(0);
        int count = Optional.ofNullable(finalTask.getCount()).orElse(0);
        int totalCount = Optional.ofNullable(finalTask.getTotalCount()).orElse(0);
        int ygt = Optional.ofNullable(finalTask.getYgt()).orElse(0);
        LocalDateTime finishTime = null;
        if (ygt + count >= totalCount && deadlineCount == 0) {
            //关停和通过数量=全部数量并且超期完成数量为0 状态是正常完成 status=2
            finalTask.setTaskStatus(2);
            finishTime = LocalDateTime.now();
            finalTask.setTaskFinishTime(finishTime);
            assetTaskService.update(finalTask);
            //同步主表数据 web
            taskSaveOrDelete(req);
            syb(req);

        } else if (deadlineCount + count + ygt >= totalCount && deadlineCount != 0) {
            //超期完成和通过数量=全部数量并且超期完成数量不为0 状态是超期完成 status=3
            finalTask.setTaskStatus(3);
            finishTime = LocalDateTime.now();
            finalTask.setTaskFinishTime(finishTime);
            assetTaskService.update(finalTask);

            //同步主表数据 web
            taskSaveOrDelete(req);
            syb(req);
        } else {
            // 状态是进行中 status=1
            finalTask.setTaskStatus(1);
            String btgyy = req.getBtgyy();
            assetTaskService.update(finalTask);
            //修改不通过原因
            if (req.getType()==0){
                assetCurrentCpService.updateByAssetId(req.getAssetId(), req.getTaskId(), btgyy);
            }else if(req.getType()==1){
                assetMiniProgramsCpService.updateByAssetId(req.getAssetId(), req.getTaskId(), btgyy);
            }else if(req.getType()==2){
                assetOfficialAccountCpService.updateByAssetId(req.getAssetId(), req.getTaskId(), btgyy);
            }else if(req.getType()==3){
                assetEmailCpService.updateByAssetId(req.getAssetId(), req.getTaskId(), btgyy);
            }else if(req.getType()==4){
            assetAppCpService.updateByAssetId(req.getAssetId(), req.getTaskId(), btgyy);
        }


        }
        return 0;
    }

    private void syb(AssetCurrentShRequest req) {
        //app 小程序 邮件 公众号
        List<AssetAppCpPo> appList = assetAppCpService.findByTaskId(req.getTaskId());
        List<AssetEmailCpPo> emailCpPoListList = assetEmailCpService.findByTaskId(req.getTaskId());
        List<AssetMiniProgramsCpPo> miniProgramsCpPoListList = assetMiniProgramsCpService.findByTaskId(req.getTaskId());
        List<AssetOfficialAccountCpPo> officialAccountCpPoListList = assetOfficialAccountCpService.findByTaskId(req.getTaskId());
        //app
        List<Long> appidList = appList.stream()
                .map(AssetAppCpPo::getAssetId)
                .collect(Collectors.toList());
        //删除原有数据
        if (CollectionUtil.isNotEmpty(appidList)) {
            assetAppService.deleteIdList(appidList);
        }
        //将子表数据复制到主表中
        List<AssetApp> appNewList = appList.stream()
                .map(cpPo -> {
                    AssetApp current = new AssetApp();
                    BeanUtils.copyProperties(cpPo, current); // 复制属性
                    return current; // 返回新对象
                })
                .collect(Collectors.toList()); // 收集到列表中
        assetAppService.saveBatch(appNewList);

        //邮件
        List<Long> emailidList = emailCpPoListList.stream()
                .map(AssetEmailCpPo::getAssetId)
                .collect(Collectors.toList());
        //删除原有数据
        if (CollectionUtil.isNotEmpty(emailidList)) {
            assetEmailService.deleteIdList(emailidList);
        }
        //将子表数据复制到主表中
        List<AssetEmail> emailNewList = appList.stream()
                .map(cpPo -> {
                    AssetEmail current = new AssetEmail();
                    BeanUtils.copyProperties(cpPo, current); // 复制属性
                    return current; // 返回新对象
                })
                .collect(Collectors.toList()); // 收集到列表中
        assetEmailService.saveBatch(emailNewList);

        //小程序
        List<Long> miniidList = miniProgramsCpPoListList.stream()
                .map(AssetMiniProgramsCpPo::getAssetId)
                .collect(Collectors.toList());
        //删除原有数据
        if (CollectionUtil.isNotEmpty(miniidList)) {
            assetMiniProgramsService.deleteIdList(miniidList);
        }
        //将子表数据复制到主表中
        List<AssetMiniPrograms> miniNewList = appList.stream()
                .map(cpPo -> {
                    AssetMiniPrograms current = new AssetMiniPrograms();
                    BeanUtils.copyProperties(cpPo, current); // 复制属性
                    return current; // 返回新对象
                })
                .collect(Collectors.toList()); // 收集到列表中
        assetMiniProgramsService.saveBatch(miniNewList);


        //公众号
        List<Long> officialidList = officialAccountCpPoListList.stream()
                .map(AssetOfficialAccountCpPo::getAssetId)
                .collect(Collectors.toList());
        //删除原有数据
        if (CollectionUtil.isNotEmpty(officialidList)) {
            assetOfficialAccountService.deleteIdList(officialidList);
        }
        //将子表数据复制到主表中
        List<AssetOfficialAccount> officialNewList = appList.stream()
                .map(cpPo -> {
                    AssetOfficialAccount current = new AssetOfficialAccount();
                    BeanUtils.copyProperties(cpPo, current); // 复制属性
                    return current; // 返回新对象
                })
                .collect(Collectors.toList()); // 收集到列表中
        assetOfficialAccountService.saveBatch(officialNewList);
    }

    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;
        // 获取当前时间
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDateTime = formatter.format(now);
        //获取任务截至时间
        String deadLine = assetTask.getTaskDeadline();
        // 转换为 LocalDateTime
        LocalDate deadlineDate = LocalDate.parse(deadLine, formatter);
        LocalDate currentDate = LocalDate.parse(formattedDateTime, formatter);
        if (deadlineDate.isBefore(currentDate)) {
            //超期
            status = 1;
        } else {
            //未超期
            status = 2;
        }
        return status;
    }

    //web
    private void taskSaveOrDelete(AssetCurrentShRequest req) {
        // 根据taskid查询五张数据
        List<AssetCurrentCpPo> currentList = assetCurrentCpService.findByTaskId(req.getTaskId());
        List<Long> idList = currentList.stream()
                .map(AssetCurrentCpPo::getId)
                .collect(Collectors.toList());
        //删除原有数据
        if (CollectionUtil.isNotEmpty(idList)) {
            assetCurrentService.deleteIdList(idList);
        }

        //将子表数据复制到主表中
        List<AssetCurrent> currentNewList = currentList.stream()
                .map(cpPo -> {
                    AssetCurrent current = new AssetCurrent();
                    BeanUtils.copyProperties(cpPo, current); // 复制属性
                    return current; // 返回新对象
                })
                .collect(Collectors.toList()); // 收集到列表中
        assetCurrentService.saveBatch(currentNewList);
        //AssetBusinessFormCpPo
        List<AssetBusinessFormCpPo> bussinessList = assetBusinessFormCpService.findByTaskId(req.getTaskId());
        List<Long> bussinessidList = bussinessList.stream()
                .map(AssetBusinessFormCpPo::getAssetId)
                .collect(Collectors.toList());
        //删除原有数据
        if (CollectionUtil.isNotEmpty(bussinessidList)) {
            assetBusinessFormService.deleteIdList(bussinessidList);
        }

        //将子表数据复制到主表中
        List<AssetBusinessForm> bussinessNewList = bussinessList.stream()
                .map(cpPo -> {
                    AssetBusinessForm businessForm = new AssetBusinessForm();
                    BeanUtils.copyProperties(cpPo, businessForm); // 复制属性
                    return businessForm; // 返回新对象
                })
                .collect(Collectors.toList()); // 收集到列表中
        assetBusinessFormService.saveBatch(bussinessNewList);
        //AssetBasicNetworkCpPo
        List<AssetBasicNetworkCpPo> netWorkList = assetBasicNetworkCpService.findByTaskId(req.getTaskId());
        List<Long> netWorkidList = netWorkList.stream()
                .map(AssetBasicNetworkCpPo::getAssetId)
                .collect(Collectors.toList());
        //删除原有数据
        if (CollectionUtil.isNotEmpty(netWorkidList)) {
            assetBasicNetworkService.deleteIdList(netWorkidList);
        }

        //将子表数据复制到主表中
        List<AssetBasicNetwork> netWorkNewList = netWorkList.stream()
                .map(cpPo -> {
                    AssetBasicNetwork network = new AssetBasicNetwork();
                    BeanUtils.copyProperties(cpPo, network); // 复制属性
                    return network; // 返回新对象
                })
                .collect(Collectors.toList()); // 收集到列表中
        assetBasicNetworkService.saveBatch(netWorkNewList);
        //AssetSupplyChainCpPo
        List<AssetSupplyChainCpPo> SupplyChainList = assetSupplyChainCpService.findByTaskId(req.getTaskId());
        List<Long> SupplyChainidList = SupplyChainList.stream()
                .map(AssetSupplyChainCpPo::getAssetId)
                .collect(Collectors.toList());
        //删除原有数据
        if (CollectionUtil.isNotEmpty(SupplyChainidList)) {
            assetSupplyChainService.deleteIdList(SupplyChainidList);
        }

        List<AssetSupplyChain> SupplyChainNewList = SupplyChainList.stream()
                .map(cpPo -> {
                    AssetSupplyChain supplyChain = new AssetSupplyChain();
                    supplyChain.setId(null);
                    BeanUtils.copyProperties(cpPo, supplyChain); // 复制属性
                    return supplyChain; // 返回新对象
                })
                .collect(Collectors.toList());
        assetSupplyChainService.saveBatch(SupplyChainNewList);
        //UnitOtherConcatCpPo
        List<UnitOtherConcatCpPo> UnitOtherConcatList = unitOtherConcatCpService.findByTaskId(req.getTaskId());
        List<Long> UnitOtherConcatidList = UnitOtherConcatList.stream()
                .map(UnitOtherConcatCpPo::getAssetId)
                .collect(Collectors.toList());
        //删除原有数据
        if (CollectionUtil.isNotEmpty(UnitOtherConcatidList)) {
            unitOtherConcatService.deleteIdList(UnitOtherConcatidList);
        }
        List<UnitOtherConcat> UnitOtherConcatNewList = UnitOtherConcatList.stream()
                .map(cpPo -> {
                    UnitOtherConcat unitOtherConcat = new UnitOtherConcat();
                    BeanUtils.copyProperties(cpPo, unitOtherConcat); // 复制属性
                    return unitOtherConcat; // 返回新对象
                })
                .collect(Collectors.toList());
        unitOtherConcatService.saveBatch(UnitOtherConcatNewList);
    }

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

        return assetTaskDao.dwHc(assetdwHcResponsePage, req);
    }

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

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

    //创建任务流程
    private void getTaskStatus(AssetCurrentShRequest req) {
        AssetLc assetLc = new AssetLc();
        int taskStatus = 0;
        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);
        }

    }


    // //单位端资产审核，更新任务表状态
    //            //查询当前任务下的所有资产状态和完成时间
    //            List<AssetTaskStatusResponse> list = assetTaskDao.findByTaskId(req.getTaskId());
    //            // 检查状态
    //            boolean hasPendingApproval = list.stream().anyMatch(item -> item.getStatus() == 0 || item.getStatus() == 1);
    //            boolean allApproved = list.stream().allMatch(item -> item.getStatus() == 3);
    //            boolean hasExpired = list.stream().anyMatch(item -> item.getDeadstatus() == 6);
    //            boolean hasRejected = list.stream().anyMatch(item -> item.getStatus() == 4);
    //            boolean hased = list.stream().anyMatch(item -> item.getStatus() == 5);
    //            LocalDateTime finishTime = null;
    //            String btgyy = null;
    //            if (hasPendingApproval) {
    //                // 资产状态存在待审批，任务状态为进行中
    //                taskStatus = 1;
    //            } else if (allApproved && !hasPendingApproval && !hasRejected && !hasExpired) {
    //                // 资产状态全部为已通过中并且全部资产完成时间在任务期限之前
    //                taskStatus = 2;
    //                finishTime = LocalDateTime.now();
    //                taskSaveOrDelete(req);
    //            } else if (allApproved && !hasPendingApproval && !hasRejected && hasExpired) {
    //                // 资产状态全部为已通过，资产完成时间有超期的为超期完成
    //                taskStatus = 3;
    //                finishTime = LocalDateTime.now();
    //                taskSaveOrDelete(req);
    //            } else if (hasRejected) {
    //                // 审核不通过
    //                taskStatus = 1;
    //                btgyy = req.getBtgyy();
    //                //修改不通过原因
    //                assetCurrentCpService.updateByAssetId(req.getAssetId(), req.getTaskId(), btgyy);
    //            } else if (hased && !hasRejected && !hasExpired) {
    //                //存在已报废并且其他都已完成 且未超期
    //                taskStatus = 2;
    //                finishTime = LocalDateTime.now();
    //                taskSaveOrDelete(req);
    //            } else if (hased && !hasRejected && hasExpired) {
    //                //存在已报废并且其他都已完成 且未超期
    //                taskStatus = 3;
    //                finishTime = LocalDateTime.now();
    //                taskSaveOrDelete(req);
    //            } else if (hased && !hasPendingApproval && !allApproved && !hasExpired && !hasRejected) {
    //                taskStatus = 3;
    //            }
    //            //修改主表任务状态
    //            assetTaskDao.updateByTaskId(taskStatus, req.getTaskId(), finishTime);
}