package com.ruoyi.tc.controller;

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.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
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.*;
import com.ruoyi.tc.historyService.*;
import com.ruoyi.tc.mapper.AssetTaskMapper;
import com.ruoyi.tc.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 任务主表(AssetTask)表控制层
 *
 * @author makejava
 * @since 2024-11-18 13:22:58
 */
@Api(tags = "任务主表")
@RestController
@Transactional(rollbackFor = Exception.class)
@RequestMapping("/unit/assetTask")
public class AssetTaskController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private AssetTaskService assetTaskService;
    @Resource
    private AssetMiniProgramsService assetMiniProgramsService;
    @Resource
    private AssetCurrentCpService assetCurrentCpService;

    @Resource
    private AssetBusinessFormCpService assetBusinessFormCpService;

    @Resource
    private AssetBasicNetworkCpService assetBasicNetworkCpService;
    @Resource
    private AssetSupplyChainCpService assetSupplyChainCpService;

    @Resource
    private UnitOtherConcatCpService unitOtherConcatCpService;

    @Resource
    private AssetCurrentJyService assetCurrentJyService;

    @Resource
    private AssetBusinessFormJyService assetBusinessFormJyService;

    @Resource
    private AssetSupplyChainJyService assetSupplyChainJyService;

    @Resource
    AssetBasicNetworkJyService assetBasicNetworkJyService;

    @Resource
    private UnitOtherConcatJyservice unitOtherConcatJyservice;


    @Resource
    private AssetLcService assetLcService;


    @Resource
    private AssetCurrentService assetCurrentService;

    @Resource
    private AssetMiniProgramsCpService assetMiniProgramsCpService;

    @Resource
    private AssetOfficialAccountCpService assetOfficialAccountCpService;

    @Resource
    private AssetAppCpService assetAppCpService;


    @Resource
    private AssetEmailCpService assetEmailCpService;


    @Resource
    private AssetMiniProgramsJyService assetMiniProgramsJyService;

    @Resource
    private AssetOfficialAccountJyService assetOfficialAccountJyService;

    @Resource
    private AssetAppJyService assetAppJyService;

    @Resource
    private AssetEmailJyService assetEmailJyService;

    @Resource
    private AssetOfficialAccountService assetOfficialAccountService;

    @Resource
    private AssetAppService assetAppService;

    @Resource
    private AssetEmailService assetEmailService;

    @Resource
    private AssetTaskMapper assetTaskDao;


    @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 AssetAssetSjJyService assetAssetSjJyService;
    /**
     * 分页查询
     *
     * @param assetTask   筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @ApiOperation(value = "分页查询", response = AssetTask.class)
    @GetMapping("/page")
    public AjaxResult queryByPage(Page<AssetTask> assetTask, AssetTaskPageRequest pageRequest) {
        return AjaxResult.success(this.assetTaskService.queryByPage(assetTask, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "通过主键查询单条数据", response = AssetTaskIdResponse.class)
    @GetMapping("{id}")
    public AjaxResult queryById(@PathVariable("id") Integer id) {
        return AjaxResult.success(this.assetTaskService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param assetTask 实体
     * @return 新增结果
     */
    @ApiOperation(value = "新增数据")
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(@RequestBody @Valid AssetTask assetTask) {
        assetTask.setTaskTime(LocalDateTime.now());
        this.assetTaskService.insert(assetTask);
        return AjaxResult.success();
    }

    /**
     * 编辑数据
     *
     * @param assetTask 实体
     * @return 编辑结果
     */
    @ApiOperation(value = "编辑数据")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody @Valid AssetTask assetTask) {
        return AjaxResult.success(this.assetTaskService.update(assetTask));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @ApiOperation(value = "删除数据")
    @PostMapping("/deleteById/{id}")
    public AjaxResult deleteById(@PathVariable("id") Integer id) {
        return AjaxResult.success(this.assetTaskService.deleteById(id));
    }


    /**
     * web资产任务审核详情信息
     *
     * @param taskId  任务id
     * @param assetId 资产id
     * @return
     */
    @ApiOperation(value = "web资产任务审核详情信息", response = AssetCurrent.class)
    @GetMapping("/taskSh")
    public AjaxResult taskSh(@RequestParam("taskId") int taskId, @RequestParam("assetId") int assetId) {

        return AjaxResult.success(assetTaskService.taskSh(taskId, assetId));
    }


    /**
     * 历史详情信息
     *
     * @param id id
     * @return
     */
    @ApiOperation(value = "历史详情信息", response = AssetHistoryResponse.class)
    @GetMapping("/history")
    public AjaxResult webHistory(@RequestParam("id") Long id, @RequestParam("type") Integer type) {
        AssetHistoryResponse assetHistoryResponse = new AssetHistoryResponse();
        if (type == 0) {
            //web
            assetHistoryResponse.setAssetCurrentHistory(assetTaskService.webHistory(id));
        } else if (type == 1) {
            //小程序资产
            assetHistoryResponse.setAssetMiniProgramsHistory(assetTaskService.miniHistory(id));
        } else if (type == 2) {
            //公众号资产

            assetHistoryResponse.setAssetOfficialAccountHistory(assetTaskService.officeHistory(id));
        } else if (type == 3) {
            //电子邮件资产
            assetHistoryResponse.setAssetEmailHistory(assetTaskService.emailHistory(id));
        } else if (type == 4) {
            //移动应用程序资产
            assetHistoryResponse.setAssetAppHistory(assetTaskService.appHistory(id));
        }
        return AjaxResult.success(assetHistoryResponse);
    }

    /**
     * 小程序资产任务审核详情信息
     *
     * @param taskId  任务id
     * @param assetId 资产id
     * @return
     */
    @ApiOperation(value = "小程序资产任务审核详情信息", response = AssetCurrent.class)
    @GetMapping("/minitaskSh")
    public AjaxResult minitaskSh(@RequestParam("taskId") int taskId, @RequestParam("assetId") int assetId) {

        return AjaxResult.success(assetTaskService.minitaskSh(taskId, assetId));
    }

    /**
     * 公众号资产任务审核详情信息
     *
     * @param taskId  任务id
     * @param assetId 资产id
     * @return
     */
    @ApiOperation(value = "公众号资产任务审核详情信息", response = AssetCurrent.class)
    @GetMapping("/officetaskSh")
    public AjaxResult officetaskSh(@RequestParam("taskId") int taskId, @RequestParam("assetId") int assetId) {

        return AjaxResult.success(assetTaskService.officetaskSh(taskId, assetId));
    }


    /**
     * 电子邮件资产任务审核详情信息
     *
     * @param taskId  任务id
     * @param assetId 资产id
     * @return
     */
    @ApiOperation(value = "电子邮件资产任务审核详情信息", response = AssetCurrent.class)
    @GetMapping("/emailtaskSh")
    public AjaxResult emailtaskSh(@RequestParam("taskId") int taskId, @RequestParam("assetId") int assetId) {

        return AjaxResult.success(assetTaskService.emailtaskSh(taskId, assetId));
    }

    /**
     * 移动应用程序资产任务审核详情信息
     *
     * @param taskId  任务id
     * @param assetId 资产id
     * @return
     */
    @ApiOperation(value = "移动应用程序资产", response = AssetAppCpPo.class)
    @GetMapping("/apptaskSh")
    public AjaxResult apptaskSh(@RequestParam("taskId") int taskId, @RequestParam("assetId") int assetId) {

        return AjaxResult.success(assetTaskService.apptaskSh(taskId, assetId));
    }

    /**
     * 资产核查信息
     *
     * @param page        分页参数
     * @param pageRequest 查询条件
     * @return
     */
    @ApiOperation(value = "资产核查信息", response = AssestTaskXqresponse.class)
    @GetMapping("/zcHc")
    public AjaxResult zcHc(Page<AssestTaskXqresponse> page, AssestTaskXqRequest pageRequest) {
        return AjaxResult.success(assetTaskService.zcHc(page, pageRequest));
    }


    /**
     * web单位端暂存报废
     *
     * @param assetCurrent 现有资产复制表
     * @return
     */
    @ApiOperation(value = "web单位端暂存报废")
    @PostMapping("/zc")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult zcHc(@RequestBody AssetCurrentCpPo assetCurrent) {
        //根据资产id和任务id删除五张表数据
        assetCurrentCpService.deletByAssetIdandTaskId(assetCurrent.getId(), assetCurrent.getTaskId());
        assetBusinessFormCpService.deletByAssetIdandTaskId(assetCurrent.getId(), assetCurrent.getTaskId());
        assetSupplyChainCpService.deletByAssetIdandTaskId(assetCurrent.getId(), assetCurrent.getTaskId());
        unitOtherConcatCpService.deletByAssetIdandTaskId(assetCurrent.getId(), assetCurrent.getTaskId());
        assetBasicNetworkCpService.deletByAssetIdandTaskId(assetCurrent.getId(), assetCurrent.getTaskId());
        assetAssetSjCpService.deletByAssetIdandTaskId(assetCurrent.getId(), assetCurrent.getTaskId());
        //根据taskid获取当前任务的count数量
        AssetTask assetTask = assetTaskDao.getByTaskId(assetCurrent.getTaskId());
        int ygt = Optional.ofNullable(assetTask.getYgt()).orElse(0);
        AssetCurrent ac = new AssetCurrent();
        if (StringUtils.isBlank(assetCurrent.getBfyy())) {
            //待审批
            assetCurrent.setStatus(0);
        } else {
            //已报废
            assetCurrent.setStatus(5);
            assetCurrent.setXtzt("5");
            //已关停
            ygt++;
            assetTaskService.updateYgtByTaskId(assetCurrent.getTaskId(), ygt);

        }
        StringBuilder a = new StringBuilder();
        if (CollectionUtil.isNotEmpty(assetCurrent.getGlymList())) {
            assetCurrent.getGlymList().forEach(x -> {
                if (!Objects.equals(x.getKey(), "") && x.getKey() != null) {
                    a.append(x.getKey());
                    a.append(",");
                }
            });
        }
        assetCurrent.setGlym(a.toString());
        StringBuilder b = new StringBuilder();
        List<Acomma> a2 = new ArrayList<>();
        if (assetCurrent.getGlIpList() != null) {
            if (assetCurrent.getGlIp() != null && !assetCurrent.getGlIp().isEmpty()) {
                for (Object o : JSONArray.parseArray(assetCurrent.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);
                }
                assetCurrent.setGlIpList(a2);
            }

        } else {
            assetCurrent.setGlIpList(new ArrayList<>());
        }
//        assetCurrent.setCurrentId(null);
        BeanUtils.copyProperties(assetCurrent, ac);
        assetCurrentService.updateById(ac);
        assetCurrentCpService.save(assetCurrent);
        //新增新监管业务形态
        if (assetCurrent.getXjgywxt() != null) {
            assetCurrent.getXjgywxt().setAssetId(assetCurrent.getId());
            assetCurrent.getXjgywxt().setTaskId(assetCurrent.getTaskId());
            assetBusinessFormCpService.save(assetCurrent.getXjgywxt());
        }
        if (!assetCurrent.getGylxxList().isEmpty()) {
            for (AssetSupplyChainCpPo items : assetCurrent.getGylxxList()) {
                items.setAssetId(assetCurrent.getId());
                items.setTaskId(assetCurrent.getTaskId());

            }
            //新增供应链
            assetSupplyChainCpService.saveBatch(assetCurrent.getGylxxList());
        }
        if (!assetCurrent.getJcwlList().isEmpty()) {
            for (AssetBasicNetworkCpPo items : assetCurrent.getJcwlList()) {
                items.setAssetId(assetCurrent.getId());
                items.setTaskId(assetCurrent.getTaskId());

            }
            //新增基础网络
            assetBasicNetworkCpService.saveBatch(assetCurrent.getJcwlList());
        }
        if (!assetCurrent.getOtherConcat().isEmpty()) {
            for (UnitOtherConcatCpPo items : assetCurrent.getOtherConcat()) {
                items.setAssetId(assetCurrent.getId());
                items.setTaskId(assetCurrent.getTaskId());

            }
            //新增其他联系人
            unitOtherConcatCpService.saveBatch(assetCurrent.getOtherConcat());
        }

        if (!assetCurrent.getSjzcList().isEmpty()) {
            for (AssetSjCpPo items : assetCurrent.getSjzcList()) {
                items.setAssetId(assetCurrent.getId());
                items.setTaskId(assetCurrent.getTaskId());

            }
            //新增其他联系人
            assetAssetSjCpService.saveBatch(assetCurrent.getSjzcList());
        }
        // 获取当前时间
        LocalDateTime localDateTime = LocalDateTime.now();
        // 转换为 Date
        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        //新增历史记录
        StringBuilder c = new StringBuilder();
        if (CollectionUtil.isNotEmpty(assetCurrent.getGlymList())) {
            assetCurrent.getGlymList().forEach(x -> {
                if (!Objects.equals(x.getKey(), "") && x.getKey() != null) {
                    c.append(x.getKey());
                    c.append(",");
                }
            });
        }
        assetCurrent.setGlym(a.toString());
        StringBuilder d = new StringBuilder();
        if (CollectionUtil.isNotEmpty(assetCurrent.getGlIpList())) {
            assetCurrent.getGlIpList().forEach(x -> {
                if (!Objects.equals(x.getKey(), "") && x.getKey() != null) {
                    d.append(x.getKey());
                    d.append(",");
                }
            });
        }
        assetCurrent.setGlIp(b.toString());
        assetCurrent.setStatus(assetCurrent.getStatus());
        AssetCurrentHistory assetCurrentHistory = new AssetCurrentHistory();
        // 设置更新时间
        assetCurrent.setUpdateTime(date);
        BeanUtils.copyProperties(assetCurrent, assetCurrentHistory);
        assetCurrentHistory.setCurrentId(null);
        assetCurrentHistoryService.save(assetCurrentHistory);

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

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

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

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

    /**
     * 单位端校验提交
     *
     * @param assetCurrent 现有资产复制表
     * @return
     */
    @ApiOperation(value = "单位端校验提交")
    @PostMapping("/jyTj")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult tj(@RequestBody @Validated AssetCurrentJyPo assetCurrent) {
//
        //根据资产id和任务id删除五张表数据
        assetCurrentCpService.deletByAssetIdandTaskId(assetCurrent.getId(), assetCurrent.getTaskId());
        assetBusinessFormCpService.deletByAssetIdandTaskId(assetCurrent.getId(), assetCurrent.getTaskId());
        assetSupplyChainCpService.deletByAssetIdandTaskId(assetCurrent.getId(), assetCurrent.getTaskId());
        unitOtherConcatCpService.deletByAssetIdandTaskId(assetCurrent.getId(), assetCurrent.getTaskId());
        assetBasicNetworkCpService.deletByAssetIdandTaskId(assetCurrent.getId(), assetCurrent.getTaskId());
        assetAssetSjCpService.deletByAssetIdandTaskId(assetCurrent.getId(), assetCurrent.getTaskId());

        //新增流程节点时间
        AssetLc assetLc = new AssetLc();
        assetLc.setTaskId(assetCurrent.getTaskId());
        assetLc.setAssetName(assetCurrent.getXtmc());
        //当审核状态为审核不通过 单位端重新提交
//        if (assetCurrent.getStatus().equals(4)) {
//            assetLc.setTaskId(assetCurrent.getTaskId());
//            assetLc.setXfTime(LocalDateTime.now());
//            assetLc.setAssetName("单位重新核查提交时间" + assetCurrent.getXtmc());
//            assetLcService.insert(assetLc);
//        }

        StringBuilder a = new StringBuilder();
        if (CollectionUtil.isNotEmpty(assetCurrent.getGlymList())) {
            assetCurrent.getGlymList().forEach(x -> {
                if (!Objects.equals(x.getKey(), "") && x.getKey() != null) {
                    a.append(x.getKey());
                    a.append(",");
                }
            });
        }
        assetCurrent.setGlym(a.toString());
        StringBuilder b = new StringBuilder();
        List<Acomma> a2 = new ArrayList<>();
        if (assetCurrent.getGlIpList() != null) {
            if (assetCurrent.getGlIp() != null && !assetCurrent.getGlIp().isEmpty()) {
                for (Object o : JSONArray.parseArray(assetCurrent.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);
                }
                assetCurrent.setGlIpList(a2);
            }

        } else {
            assetCurrent.setGlIpList(new ArrayList<>());
        }
        assetCurrent.setStatus(1);
        Date date = new Date();
        assetCurrent.setUpdateTime(date);
        //当审核状态为带待核查
        if (assetCurrent.getStatus().equals(1)) {
            assetLc.setXfTime(LocalDateTime.now());
            assetLc.setTaskId(assetCurrent.getTaskId());
            assetLc.setAssetName("单位核查完成提交" + assetCurrent.getXtmc());
            assetLcService.insert(assetLc);
        }
        assetCurrentJyService.save(assetCurrent);
        //新增新监管业务形态
        if (assetCurrent.getXjgywxt() != null) {
            assetCurrent.getXjgywxt().setAssetId(assetCurrent.getId());
            assetCurrent.getXjgywxt().setTaskId(assetCurrent.getTaskId());
            assetBusinessFormJyService.save(assetCurrent.getXjgywxt());
        }
        if (CollectionUtil.isNotEmpty(assetCurrent.getGylxxList())) {
            for (AssetSupplyChainJyPo items : assetCurrent.getGylxxList()) {
                items.setAssetId(assetCurrent.getId());
                items.setTaskId(assetCurrent.getTaskId());
            }
            //新增供应链
            assetSupplyChainJyService.saveBatch(assetCurrent.getGylxxList());
        }
        if (CollectionUtil.isNotEmpty(assetCurrent.getJcwlList())) {
            for (AssetBasicNetworkJyPo items : assetCurrent.getJcwlList()) {
                items.setAssetId(assetCurrent.getId());
                items.setTaskId(assetCurrent.getTaskId());
            }
            //新增基础网络
            assetBasicNetworkJyService.saveBatch(assetCurrent.getJcwlList());
        }
        if (!assetCurrent.getOtherConcat().isEmpty()) {
            for (UnitOtherConcatJyPo items : assetCurrent.getOtherConcat()) {
                items.setAssetId(assetCurrent.getId());
                items.setTaskId(assetCurrent.getTaskId());
            }
            //新增其他联系人
            unitOtherConcatJyservice.saveBatch(assetCurrent.getOtherConcat());
        }
        if (!assetCurrent.getSjzcList().isEmpty()) {
            for (AssetSjJyPo items : assetCurrent.getSjzcList()) {
                items.setAssetId(assetCurrent.getId());
                items.setTaskId(assetCurrent.getTaskId());
            }
            //新增其他联系人
            assetAssetSjJyService.saveBatch(assetCurrent.getSjzcList());
        }
        // 获取当前时间
        LocalDateTime localDateTime = LocalDateTime.now();
        // 转换为 Date
        date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        //新增历史记录
        StringBuilder c = new StringBuilder();
        if (CollectionUtil.isNotEmpty(assetCurrent.getGlymList())) {
            assetCurrent.getGlymList().forEach(x -> {
                if (!Objects.equals(x.getKey(), "") && x.getKey() != null) {
                    c.append(x.getKey());
                    c.append(",");
                }
            });
        }
        assetCurrent.setGlym(c.toString());
        StringBuilder d = new StringBuilder();
        if (CollectionUtil.isNotEmpty(assetCurrent.getGlIpList())) {
            assetCurrent.getGlIpList().forEach(x -> {
                if (!Objects.equals(x.getKey(), "") && x.getKey() != null) {
                    d.append(x.getKey());
                    d.append(",");
                }
            });
        }
        assetCurrent.setGlIp(d.toString());
        assetCurrent.setStatus(assetCurrent.getStatus());
        AssetCurrentHistory assetCurrentHistory = new AssetCurrentHistory();
        // 设置更新时间
        assetCurrent.setUpdateTime(date);
        BeanUtils.copyProperties(assetCurrent, assetCurrentHistory);
        assetCurrentHistory.setCurrentId(null);

        assetCurrentHistoryService.save(assetCurrentHistory);

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

        }
        if (!assetCurrent.getGylxxList().isEmpty()) {
            for (AssetSupplyChainJyPo items : assetCurrent.getGylxxList()) {
                items.setAssetId(assetCurrent.getId());
                items.setTaskId(assetCurrent.getTaskId());

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

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


        return success();
    }


    /**
     * 管理端审核
     *
     * @param req 请求类
     * @return
     */
    @ApiOperation(value = "管理端审核")
    @PostMapping("/sh")
    public AjaxResult sh(@RequestBody @Validated AssetCurrentShRequest req) throws ParseException {
        return AjaxResult.success(assetTaskService.sh(req));
    }


    /**
     * 管理端-单位核查情况
     *
     * @param req 请求类
     * @return
     */
    @ApiOperation(value = "管理端-单位核查情况", response = AssetdwHcResponse.class)
    @GetMapping("/dwHc")
    public AjaxResult dwHc(Page<AssetdwHcResponse> assetdwHcResponsePage, AssetdwHcRequest req) {
        return AjaxResult.success(assetTaskService.dwHc(assetdwHcResponsePage, req));
    }


    /**
     * 导出管理端-单位核查情况
     *
     * @param req 请求类
     * @return
     */
    @ApiOperation(value = "导出管理端-单位核查情况")
    @PostMapping("/export")
    public void export(HttpServletResponse response, AssetdwHcRequest req) {
        List<AssetdwHcResponse> list = assetTaskService.export(req);
        list.forEach(x -> {
            String formattedRatio = x.getCheckedRatio() + "%";
            x.setCheckedRatio(formattedRatio);
        });
        ExcelUtil<AssetdwHcResponse> util = new ExcelUtil<AssetdwHcResponse>(AssetdwHcResponse.class);
        util.exportExcel(response, list, "单位完成情况");

    }

    /**
     * 小程序单位端暂存报废
     *
     * @param assetMiniProgramsCpPo 小程序复制表
     * @return
     */
    @ApiOperation(value = "小程序单位端暂存报废")
    @PostMapping("/xcxzc")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult xcxzc(@RequestBody AssetMiniProgramsCpPo assetMiniProgramsCpPo) {
        //根据资产id和任务id删除数据
        assetMiniProgramsCpService.deletByAssetIdandTaskId(assetMiniProgramsCpPo.getAssetId(), assetMiniProgramsCpPo.getTaskId());
        //根据taskid获取当前任务的count数量
        AssetTask assetTask = assetTaskDao.getByTaskId(assetMiniProgramsCpPo.getTaskId());
        int ygt = Optional.ofNullable(assetTask.getYgt()).orElse(0);
        AssetMiniPrograms ac = new AssetMiniPrograms();
        if (StringUtils.isBlank(assetMiniProgramsCpPo.getBfyy())) {
            //待审批
            assetMiniProgramsCpPo.setStatus(0);
        } else {
            //已报废
            assetMiniProgramsCpPo.setStatus(5);
            ac.setIsbf(2);
            ac.setState("7");
            ac.setId(assetMiniProgramsCpPo.getAssetId());
            assetMiniProgramsService.updateById(ac);
            //已关停
            ygt++;
            assetTaskService.updateYgtByTaskId(assetMiniProgramsCpPo.getTaskId(), ygt);
        }
        assetMiniProgramsCpService.save(assetMiniProgramsCpPo);
        //新增历史记录表
        AssetMiniProgramsHistory assetMiniProgramsHistory = new AssetMiniProgramsHistory();
        BeanUtils.copyProperties(assetMiniProgramsCpPo, assetMiniProgramsHistory);
        assetMiniProgramsHistory.setMiniId(null);
        assetMiniProgramsHistoryService.save(assetMiniProgramsHistory);
        return success();
    }


    /**
     * 公众号单位端暂存报废
     *
     * @param assetOfficialAccountCpPo 公众号复制表
     * @return
     */
    @ApiOperation(value = "公众号单位端暂存报废")
    @PostMapping("/gzhzc")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult gzhzc(@RequestBody AssetOfficialAccountCpPo assetOfficialAccountCpPo) {
        //根据资产id和任务id删除数据
        assetOfficialAccountCpService.deletByAssetIdandTaskId(assetOfficialAccountCpPo.getAssetId(), assetOfficialAccountCpPo.getTaskId());
        if (assetOfficialAccountCpPo.getCdList() != null && !assetOfficialAccountCpPo.getCdList().isEmpty()) {
            StringBuilder a = new StringBuilder();
            StringBuilder b = new StringBuilder();
            StringBuilder c = new StringBuilder();
            assetOfficialAccountCpPo.getCdList().forEach(x -> {
                if (!Objects.equals(x.getCdmc(), "") && x.getCdmc() != null) {
                    a.append(x.getCdmc()).append("|");
                }
                if (!Objects.equals(x.getCdlj(), "") && x.getCdlj() != null) {
                    b.append(x.getCdlj()).append("|");
                }
                if (!Objects.equals(x.getCdlx(), "") && x.getCdlx() != null) {
                    c.append(x.getCdlx()).append("|");
                }
            });
            assetOfficialAccountCpPo.setCdmc(a.toString());
            assetOfficialAccountCpPo.setCdlj(b.toString());
            assetOfficialAccountCpPo.setCdlx(c.toString());
        }
        //根据taskid获取当前任务的count数量
        AssetTask assetTask = assetTaskDao.getByTaskId(assetOfficialAccountCpPo.getTaskId());
        int ygt = Optional.ofNullable(assetTask.getYgt()).orElse(0);
        AssetOfficialAccount ac = new AssetOfficialAccount();
        if (StringUtils.isBlank(assetOfficialAccountCpPo.getBfyy())) {
            //待审批
            assetOfficialAccountCpPo.setStatus(0);
        } else {
            //已报废
            assetOfficialAccountCpPo.setStatus(5);
            ac.setIsbf(2);
            ac.setGzhzt("7");
            ac.setId(assetOfficialAccountCpPo.getAssetId());
            assetOfficialAccountService.updateById(ac);
            //已关停
            ygt++;
            assetTaskService.updateYgtByTaskId(assetOfficialAccountCpPo.getTaskId(), ygt);
        }

        assetOfficialAccountCpService.save(assetOfficialAccountCpPo);
        //新增历史记录表
        AssetOfficialAccountHistory assetOfficialAccountHistory = new AssetOfficialAccountHistory();
        BeanUtils.copyProperties(assetOfficialAccountCpPo, assetOfficialAccountHistory);
        assetOfficialAccountHistory.setAccountId(null);
        assetOfficialAccountHistoryService.save(assetOfficialAccountHistory);
        return success();
    }


    /**
     * 资产移动应用程序单位端暂存报废
     *
     * @param assetAppCpPo 资产移动应用程序复制表
     * @return
     */
    @ApiOperation(value = "资产移动应用程序单位端暂存报废")
    @PostMapping("/appzc")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult appzc(@RequestBody AssetAppCpPo assetAppCpPo) {
        //根据资产id和任务id删除数据
        assetAppCpService.deletByAssetIdandTaskId(assetAppCpPo.getAssetId(), assetAppCpPo.getTaskId());
        //根据taskid获取当前任务的count数量
        AssetTask assetTask = assetTaskDao.getByTaskId(assetAppCpPo.getTaskId());
        int ygt = Optional.ofNullable(assetTask.getYgt()).orElse(0);
        AssetApp ac = new AssetApp();
        if (StringUtils.isBlank(assetAppCpPo.getBfyy())) {
            //待审批
            assetAppCpPo.setStatus(0);
        } else {
            //已报废
            assetAppCpPo.setStatus(5);
            ac.setIsbf(2);
            ac.setAppState("7");
            ac.setId(assetAppCpPo.getAssetId());
            assetAppService.updateById(ac);
            //已关停
            ygt++;
            assetTaskService.updateYgtByTaskId(assetAppCpPo.getTaskId(), ygt);
        }

        assetAppCpService.save(assetAppCpPo);
        //新增历史记录表
        AssetAppHistory assetAppHistory = new AssetAppHistory();
        BeanUtils.copyProperties(assetAppCpPo, assetAppHistory);
        assetAppHistory.setAppId(null);
        assetAppHistoryService.save(assetAppHistory);
        return success();
    }


    /**
     * 电子邮件单位端暂存报废
     *
     * @param assetEmailCpPo 电子邮件复制表
     * @return
     */
    @ApiOperation(value = "电子邮件单位端暂存报废")
    @PostMapping("/emailzc")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult emailzc(@RequestBody AssetEmailCpPo assetEmailCpPo) {
        //根据资产id和任务id删除数据
        assetEmailCpService.deletByAssetIdandTaskId(assetEmailCpPo.getAssetId(), assetEmailCpPo.getTaskId());
        //根据taskid获取当前任务的count数量
        AssetTask assetTask = assetTaskDao.getByTaskId(assetEmailCpPo.getTaskId());
        int ygt = Optional.ofNullable(assetTask.getYgt()).orElse(0);
        AssetEmail ac = new AssetEmail();
        if (StringUtils.isBlank(assetEmailCpPo.getBfyy())) {
            //待审批
            assetEmailCpPo.setStatus(0);
        } else {
            //已报废
            assetEmailCpPo.setStatus(5);
            ac.setIsbf(2);
            ac.setYjxtzc("2");
            ac.setId(assetEmailCpPo.getAssetId());
            assetEmailService.updateById(ac);
            //已关停
            ygt++;
            assetTaskService.updateYgtByTaskId(assetEmailCpPo.getTaskId(), ygt);
        }

        assetEmailCpService.save(assetEmailCpPo);
        //新增历史记录表
        AssetEmailHistory assetEmailHistory = new AssetEmailHistory();
        BeanUtils.copyProperties(assetEmailCpPo, assetEmailHistory);
        assetEmailHistory.setEmailId(null);
        assetEmailHistoryService.save(assetEmailHistory);
        return success();
    }


    /**
     * 小程序单位端校验提交
     *
     * @param assetMiniProgramsJyPo 小程序资产复制表
     * @return
     */
    @ApiOperation(value = "小程序单位端校验提交")
    @PostMapping("/xcxjyTj")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult xcxJyTj(@RequestBody @Validated AssetMiniProgramsJyPo assetMiniProgramsJyPo) {

//        //判断是否是驳回后提交
//        QueryWrapper<AssetMiniProgramsJyPo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("mini_id", assetMiniProgramsJyPo.getMiniId());
//        AssetMiniProgramsJyPo assetCurrentJyPo = assetMiniProgramsJyService.getOne(queryWrapper);
//        if (assetCurrentJyPo.getStatus()==4){
//            //新增历史记录表
//            AssetMiniProgramsHistory assetMiniProgramsHistory = new AssetMiniProgramsHistory();
//            BeanUtils.copyProperties(assetMiniProgramsJyPo, assetMiniProgramsHistory);
//            assetMiniProgramsHistory.setMiniId(null);
//            assetMiniProgramsHistory.setStatus(4);
//            assetMiniProgramsHistoryService.save(assetMiniProgramsHistory);
//        }

        assetMiniProgramsCpService.deletByAssetIdandTaskId(assetMiniProgramsJyPo.getAssetId(), assetMiniProgramsJyPo.getTaskId());
        //新增流程节点时间
        AssetLc assetLc = new AssetLc();
        assetLc.setTaskId(assetMiniProgramsJyPo.getTaskId());
        assetLc.setAssetName(assetMiniProgramsJyPo.getXcxmc());
        assetMiniProgramsJyPo.setStatus(1);
        // 获取当前时间
        LocalDateTime localDateTime = LocalDateTime.now();
        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        assetMiniProgramsJyPo.setUpdateTime(date);
        //当审核状态为带待核查
        if (assetMiniProgramsJyPo.getStatus().equals(1)) {
            assetLc.setXfTime(LocalDateTime.now());
            assetLc.setTaskId(assetMiniProgramsJyPo.getTaskId());
            assetLc.setAssetName("单位核查完成提交" + assetMiniProgramsJyPo.getXcxmc());
            assetLcService.insert(assetLc);
        }
        assetMiniProgramsJyService.save(assetMiniProgramsJyPo);

        //新增历史记录表
        AssetMiniProgramsHistory assetMiniProgramsHistory = new AssetMiniProgramsHistory();
        BeanUtils.copyProperties(assetMiniProgramsJyPo, assetMiniProgramsHistory);
        assetMiniProgramsHistory.setMiniId(null);
        assetMiniProgramsHistoryService.save(assetMiniProgramsHistory);


        return success();
    }


    /**
     * 公众号单位端校验提交
     *
     * @param assetOfficialAccountJyPo 公众号资产复制表
     * @return
     */
    @ApiOperation(value = "公众号单位端校验提交")
    @PostMapping("/gzhjyTj")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult gzhjyTj(@RequestBody @Validated AssetOfficialAccountJyPo assetOfficialAccountJyPo) {

//        //判断是否是驳回后提交
//        QueryWrapper<AssetOfficialAccountJyPo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("account_id", assetOfficialAccountJyPo.getAccountId());
//        AssetOfficialAccountJyPo assetCurrentJyPo = assetOfficialAccountJyService.getOne(queryWrapper);
//        if (assetCurrentJyPo.getStatus()==4){
//            //新增历史记录表
//            AssetOfficialAccountHistory assetOfficialAccountHistory = new AssetOfficialAccountHistory();
//            BeanUtils.copyProperties(assetOfficialAccountJyPo, assetOfficialAccountHistory);
//            assetOfficialAccountHistory.setAccountId(null);
//            assetOfficialAccountHistory.setStatus(4);
//            assetOfficialAccountHistoryService.save(assetOfficialAccountHistory);
//
//        }
        assetOfficialAccountCpService.deletByAssetIdandTaskId(assetOfficialAccountJyPo.getAssetId(), assetOfficialAccountJyPo.getTaskId());
        if (assetOfficialAccountJyPo.getCdList() != null && !assetOfficialAccountJyPo.getCdList().isEmpty()) {
            StringBuilder a = new StringBuilder();
            StringBuilder b = new StringBuilder();
            StringBuilder c = new StringBuilder();
            assetOfficialAccountJyPo.getCdList().forEach(x -> {
                if (!Objects.equals(x.getCdmc(), "") && x.getCdmc() != null) {
                    a.append(x.getCdmc()).append("|");
                }
                if (!Objects.equals(x.getCdlj(), "") && x.getCdlj() != null) {
                    b.append(x.getCdlj()).append("|");
                }
                if (!Objects.equals(x.getCdlx(), "") && x.getCdlx() != null) {
                    c.append(x.getCdlx()).append("|");
                }
            });
            assetOfficialAccountJyPo.setCdmc(a.toString());
            assetOfficialAccountJyPo.setCdlj(b.toString());
            assetOfficialAccountJyPo.setCdlx(c.toString());
        }
        //新增流程节点时间
        AssetLc assetLc = new AssetLc();
        assetLc.setTaskId(assetOfficialAccountJyPo.getTaskId());
        assetLc.setAssetName(assetOfficialAccountJyPo.getGzhmc());
        assetOfficialAccountJyPo.setStatus(1);
        //当审核状态为带待核查
        if (assetOfficialAccountJyPo.getStatus().equals(1)) {
            assetLc.setXfTime(LocalDateTime.now());
            assetLc.setTaskId(assetOfficialAccountJyPo.getTaskId());
            assetLc.setAssetName("单位核查完成提交" + assetOfficialAccountJyPo.getGzhmc());
            assetLcService.insert(assetLc);
        }
        // 获取当前时间
        LocalDateTime localDateTime = LocalDateTime.now();
        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

        assetOfficialAccountJyPo.setUpdateTime(date);
        assetOfficialAccountJyService.save(assetOfficialAccountJyPo);
        //新增历史记录表
        AssetOfficialAccountHistory assetOfficialAccountHistory = new AssetOfficialAccountHistory();
        BeanUtils.copyProperties(assetOfficialAccountJyPo, assetOfficialAccountHistory);
        assetOfficialAccountHistory.setAccountId(null);
        assetOfficialAccountHistoryService.save(assetOfficialAccountHistory);
        return success();
    }


    /**
     * 资产移动应用程序单位端校验提交
     *
     * @param assetAppJyPo 资产移动应用程序资产复制表
     * @return
     */
    @ApiOperation(value = "资产移动应用程序单位端校验提交")
    @PostMapping("/appjyTj")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult appjyTj(@RequestBody @Validated AssetAppJyPo assetAppJyPo) {

//        //判断是否是驳回后提交
//        QueryWrapper<AssetAppJyPo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("app_id", assetAppJyPo.getAppId());
//        AssetAppJyPo assetCurrentJyPo = assetAppJyService.getOne(queryWrapper);
//        if (assetCurrentJyPo.getStatus()==4){
//            //新增历史记录表
//            AssetAppHistory assetAppHistory = new AssetAppHistory();
//            BeanUtils.copyProperties(assetAppJyPo, assetAppHistory);
//            assetAppHistory.setAppId(null);
//            assetAppHistory.setStatus(4);
//            assetAppHistoryService.save(assetAppHistory);
//        }

        //根据资产id和任务id删除数据
        assetAppCpService.deletByAssetIdandTaskId(assetAppJyPo.getAssetId(), assetAppJyPo.getTaskId());
        //新增流程节点时间
        AssetLc assetLc = new AssetLc();
        assetLc.setTaskId(assetAppJyPo.getTaskId());
        assetLc.setAssetName(assetAppJyPo.getAppName());
        assetAppJyPo.setStatus(1);
        //当审核状态为带待核查
        if (assetAppJyPo.getStatus().equals(1)) {
            assetLc.setXfTime(LocalDateTime.now());
            assetLc.setTaskId(assetAppJyPo.getTaskId());
            assetLc.setAssetName("单位核查完成提交" + assetAppJyPo.getAppName());
            assetLcService.insert(assetLc);
        }
        // 获取当前时间
        LocalDateTime localDateTime = LocalDateTime.now();
        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        assetAppJyPo.setUpdateTime(date);
        assetAppJyService.save(assetAppJyPo);
        //新增历史记录表

        AssetAppHistory assetAppHistory = new AssetAppHistory();
        BeanUtils.copyProperties(assetAppJyPo, assetAppHistory);
        assetAppHistory.setAppId(null);
        assetAppHistoryService.save(assetAppHistory);


        return success();
    }


    /**
     * 电子邮件单位端校验提交
     *
     * @param assetEmailJyPo 电子邮件资产复制表
     * @return
     */
    @ApiOperation(value = "电子邮件单位端校验提交")
    @PostMapping("/emailjyTj")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult emailjyTj(@RequestBody @Validated AssetEmailJyPo assetEmailJyPo) {

//        //判断是否是驳回后提交
//        QueryWrapper<AssetEmailJyPo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("email_id", assetEmailJyPo.getEmailId());
//        AssetEmailJyPo assetCurrentJyPo = assetEmailJyService.getOne(queryWrapper);
//        if (assetCurrentJyPo.getStatus()==4){
//            //新增历史记录表
//            AssetEmailHistory assetEmailHistory = new AssetEmailHistory();
//            BeanUtils.copyProperties(assetEmailJyPo, assetEmailHistory);
//            assetEmailHistory.setEmailId(null);
//            assetEmailHistory.setStatus(4);
//            assetEmailHistoryService.save(assetEmailHistory);
//        }


        //根据资产id和任务id删除数据
        assetEmailCpService.deletByAssetIdandTaskId(assetEmailJyPo.getAssetId(), assetEmailJyPo.getTaskId());
        //新增流程节点时间
        AssetLc assetLc = new AssetLc();
        assetLc.setTaskId(assetEmailJyPo.getTaskId());
        assetLc.setAssetName(assetEmailJyPo.getDzyxhz());
        assetEmailJyPo.setStatus(1);
        //当审核状态为带待核查
        if (assetEmailJyPo.getStatus().equals(1)) {
            assetLc.setXfTime(LocalDateTime.now());
            assetLc.setTaskId(assetEmailJyPo.getTaskId());
            assetLc.setAssetName("单位核查完成提交" + assetEmailJyPo.getDzyxhz());
            assetLcService.insert(assetLc);
        }
        // 获取当前时间
        LocalDateTime localDateTime = LocalDateTime.now();
        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        assetEmailJyPo.setUpdateTime(date);
        assetEmailJyService.save(assetEmailJyPo);
        //新增历史记录表
        AssetEmailHistory assetEmailHistory = new AssetEmailHistory();
        BeanUtils.copyProperties(assetEmailJyPo, assetEmailHistory);
        assetEmailHistory.setEmailId(null);
        assetEmailHistoryService.save(assetEmailHistory);
        return success();
    }


    /**
     * 管理端-关闭
     *
     * @param taskName 任务名称
     * @param taskId   任务id
     * @return
     */
    @ApiOperation(value = "管理端-关闭")
    @GetMapping("/close")
    public AjaxResult close(@RequestParam(value = "taskName") String taskName, @RequestParam(value = "taskId") Integer taskId) {
        AssetLc assetLc = new AssetLc();
        assetLc.setAssetName("管理员关闭" + taskName + "任务");
        assetLc.setTaskId(taskId);
        assetLc.setXfTime(LocalDateTime.now());
        assetLcService.insert(assetLc);
        assetTaskService.close(taskId);
        return AjaxResult.success();
    }


    /**
     * 管理端-单位核查比例图
     *
     * @param req 请求类
     * @return
     */
    @ApiOperation(value = "管理端-单位核查比例图", response = AssetdwHcBlResponse.class)
    @PostMapping("/dwHcBl")
    public AjaxResult dwHcBl(@RequestBody AssetdwHcRequest req) {
        return AjaxResult.success(assetTaskService.dwHcBl(req));
    }

    /**
     * 资产提交记录
     *
     * @param req
     * @return
     */
    @ApiOperation(value = "资产提交记录", response = AssetzcTjResponse.class)
    @PostMapping("/zcTj")
    public AjaxResult zcTj(@RequestBody AssetzcTjRequest req) {

        List<AssetzcTjResponse> list = null;
        if (req.getType() == 0) {
            //web
            list = assetCurrentHistoryService.findByTaskIdandAssetId(req);

        } else if (req.getType() == 1) {
            //小程序资产
            list = assetMiniProgramsHistoryService.findByTaskIdandAssetId(req);

        } else if (req.getType() == 2) {
            //公众号资产
            list = assetOfficialAccountHistoryService.findByTaskIdandAssetId(req);
        } else if (req.getType() == 3) {
            //电子邮件资产
            list = assetEmailHistoryService.findByTaskIdandAssetId(req);
        } else if (req.getType() == 4) {
            //移动应用程序资产
            list = assetAppHistoryService.findByTaskIdandAssetId(req);

        }
        return AjaxResult.success(list);
    }

}

