package com.ruoyi.tc.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.tc.entity.*;
import com.ruoyi.tc.entity.po.*;
import com.ruoyi.tc.entity.request.Acomma;
import com.ruoyi.tc.entity.request.AssetAuditPageRequest;
import com.ruoyi.tc.entity.request.AssetAuditRequest;
import com.ruoyi.tc.entity.request.AssetCurrentPageRequest;
import com.ruoyi.tc.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 现有资产表(AssetCurrent)表控制层
 *
 * @author makejava
 * @since 2024-11-15 10:03:56
 */
@Api(tags = "现有资产表")
@RestController
@RequestMapping("/tc/assetCurrent")
public class AssetCurrentController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private AssetCurrentService assetCurrentService;
    @Resource
    private AssetSupplyChainService assetSupplyChainService;

    @Resource
    private AssetBasicNetworkService assetBasicNetworkService;

    @Resource
    private AssetBusinessFormService assetBusinessFormService;

    @Resource
    private UnitOtherConcatService unitOtherConcatService;

    @Resource
    private AssetTaskService assetTaskService;

    @Resource
    private AssetCurrentCpService assetCurrentCpService;

    @Resource
    private AssetBusinessFormCpService assetBusinessFormCpService;

    @Resource
    private AssetSupplyChainCpService assetSupplyChainCpService;


    @Resource
    private AssetBasicNetworkCpService assetBasicNetworkCpService;


    @Resource
    private UnitOtherConcatCpService unitOtherConcatCpService;

    @Resource
    private UnitService unitService;

    @Resource
    private AssetCurrentJyService assetCurrentJyService;


    @Resource
    private AssetBusinessFormJyService assetBusinessFormJyService;

    @Resource
    private AssetSupplyChainJyService assetSupplyChainJyService;

    @Resource
    private UnitOtherConcatJyservice unitOtherConcatJyservice;

    @Resource
    private AssetBasicNetworkJyService assetBasicNetworkJyService;


    /**
     * 分页查询所有数据
     *
     * @param as 查询实体
     * @return 所有数据
     */
    @ApiOperation(value = "分页查询所有数据", response = AssetCurrent.class)
    @GetMapping
    public AjaxResult selectAll(AssetCurrentPageRequest as) {
        Page<AssetCurrent> page = new Page<>();
        page.setSize(as.getSize());
        page.setCurrent(as.getCurrent());
        try {
            if (!SecurityUtils.getLoginUser().getUser().isAdmin() && !SecurityUtils.hasRole("common")) {
                as.setDwmc(SecurityUtils.getLoginUser().getUser().getNickName());
            }
        } catch (Exception e) {
            throw new ServiceException("获取用户信息异常");
        }
        return success(assetCurrentService.page(page, as));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "通过主键查询单条数据", response = AssetCurrent.class)
    @GetMapping("{id}")
    public AjaxResult selectOne(@PathVariable Serializable id) {
        return success(assetCurrentService.selectOne(id));
    }

    /**
     * 新增数据
     *
     * @param assetCurrent 实体对象
     * @return 新增结果
     */
    @ApiOperation(value = "新增数据")
    @PostMapping
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insert(@RequestBody @Valid AssetCurrent assetCurrent) {
        List<Unit> list = unitService.lambdaQuery().eq(Unit::getNickName, assetCurrent.getDwmc())
                .eq(Unit::getDelFlag, 0).list();
        if (list == null || list.isEmpty()) {
            throw new ServiceException("请选择已有单位!");
        }
        StringBuilder a = new StringBuilder();
        if (assetCurrent.getGlymList() != null) {
            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();
        if (assetCurrent.getGlIpList() != null) {
            assetCurrent.getGlIpList().forEach(x -> {
                if (!Objects.equals(x.getKey(), "") && x.getKey() != null) {
                    b.append(x.getKey());
                    b.append(",");
                }
            });
        }
        assetCurrent.setGlIp(b.toString());
        assetCurrentService.save(assetCurrent);
        //新增新监管业务形态
        if (assetCurrent.getXjgywxt() != null) {
            assetCurrent.getXjgywxt().setAssetId(assetCurrent.getId());
            assetBusinessFormService.save(assetCurrent.getXjgywxt());
        }
        if (assetCurrent.getGylxxList() != null) {
            for (AssetSupplyChain items : assetCurrent.getGylxxList()) {
                items.setAssetId(assetCurrent.getId());
            }
            //新增供应链
            assetSupplyChainService.saveBatch(assetCurrent.getGylxxList());
        }
        if (assetCurrent.getJcwlList() != null) {
            for (AssetBasicNetwork items : assetCurrent.getJcwlList()) {
                items.setAssetId(assetCurrent.getId());
            }
            //新增基础网络
            assetBasicNetworkService.saveBatch(assetCurrent.getJcwlList());
        }
        if (assetCurrent.getOtherConcat() != null) {
            for (UnitOtherConcat items : assetCurrent.getOtherConcat()) {
                items.setAssetId(assetCurrent.getId());
            }
            //新增其他联系人
            unitOtherConcatService.saveBatch(assetCurrent.getOtherConcat());
        }

        // 单位有进行中的任务时，管理端新增该单位下的资产则需同步至当前进行中的任务中。
        //获取新增资产id
        Long id = assetCurrent.getId();
        Integer i = unitService.selectTaskId(assetCurrent.getDwmc(), "0");
        if (i != null) {
            //新增资产复制主表
            AssetCurrentCpPo assetCurrentCpPo = new AssetCurrentCpPo();
            BeanUtil.copyProperties(assetCurrent, assetCurrentCpPo);
            assetCurrentCpPo.setTaskId(i);
            assetCurrentCpService.save(assetCurrentCpPo);
            //新增新监管业务形态
            //根据资产id查询新监管业务形态主表id
            if (assetCurrent.getXjgywxt() != null) {
                assetCurrentCpPo.getXjgywxt().setAssetId(id);
                assetCurrentCpPo.getXjgywxt().setTaskId(i);
                assetBusinessFormCpService.save(assetCurrentCpPo.getXjgywxt());
            }
            if (assetCurrent.getGylxxList() != null) {
                for (AssetSupplyChainCpPo items : assetCurrentCpPo.getGylxxList()) {
                    items.setAssetId(id);
                    items.setTaskId(i);
                }
                //新增供应链
                assetSupplyChainCpService.saveBatch(assetCurrentCpPo.getGylxxList());
            }
            if (assetCurrent.getJcwlList() != null) {
                for (AssetBasicNetworkCpPo items : assetCurrentCpPo.getJcwlList()) {
                    items.setAssetId(assetCurrent.getId());
                    items.setTaskId(i);
                }
                //新增基础网络
                assetBasicNetworkCpService.saveBatch(assetCurrentCpPo.getJcwlList());
            }
            if (assetCurrent.getOtherConcat() != null) {
                for (UnitOtherConcatCpPo items : assetCurrentCpPo.getOtherConcat()) {
                    items.setAssetId(assetCurrent.getId());
                    items.setTaskId(i);
                }
                //新增其他联系人
                unitOtherConcatCpService.saveBatch(assetCurrentCpPo.getOtherConcat());
            }
        }
        return

                success();

    }
    //=================================================

    /**
     * 单位自编辑进行校验
     */
    @ApiOperation(value = "单位自编辑进行校验")
    @PreAuthorize("@ss.hasAnyRoles('unit')")
    @PostMapping("/unitEdit")
    public AjaxResult unitEdit(@RequestBody @Valid AssetCurrentJyPo assetCurrentJyPo) {
        AssetCurrentJyPo one = assetCurrentJyService.lambdaQuery().eq(AssetCurrentJyPo::getXtmc, assetCurrentJyPo.getXtmc())
                .eq(AssetCurrentJyPo::getDwmc, assetCurrentJyPo.getDwmc()).isNull(AssetCurrentJyPo::getTaskId).one();
        assetCurrentJyPo.setAuditState("1");
        assetCurrentJyPo.setAuditYy(null);
        if (one != null) {
            assetCurrentJyPo.setCurrentId(one.getCurrentId());
            assetCurrentJyService.updateById(assetCurrentJyPo);

            QueryWrapper<AssetSupplyChainJyPo> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("asset_id", assetCurrentJyPo.getId());
            queryWrapper2.isNull("task_id");
            assetSupplyChainJyService.remove(queryWrapper2);
            QueryWrapper<AssetBasicNetworkJyPo> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("asset_id", assetCurrentJyPo.getId());
            queryWrapper3.isNull("task_id");
            assetBasicNetworkJyService.remove(queryWrapper3);
            QueryWrapper<AssetBusinessFormJyPo> queryWrapper4 = new QueryWrapper<>();
            queryWrapper4.eq("asset_id", assetCurrentJyPo.getId());
            queryWrapper4.isNull("task_id");
            assetBusinessFormJyService.remove(queryWrapper4);
            QueryWrapper<UnitOtherConcatJyPo> queryWrapper5 = new QueryWrapper<>();
            queryWrapper5.isNull("task_id");
            queryWrapper5.eq("asset_id", assetCurrentJyPo.getId());
            unitOtherConcatJyservice.remove(queryWrapper5);
            if (assetCurrentJyPo.getXjgywxt() != null) {
                assetCurrentJyPo.getXjgywxt().setAssetId(assetCurrentJyPo.getId());
                assetBusinessFormJyService.save(assetCurrentJyPo.getXjgywxt());
            }
            if (assetCurrentJyPo.getGylxxList() != null) {
                for (AssetSupplyChainJyPo items : assetCurrentJyPo.getGylxxList()) {
                    items.setAssetId(assetCurrentJyPo.getId());
                }
                //新增供应链
                assetSupplyChainJyService.saveBatch(assetCurrentJyPo.getGylxxList());
            }
            if (assetCurrentJyPo.getJcwlList() != null) {
                for (AssetBasicNetworkJyPo items : assetCurrentJyPo.getJcwlList()) {
                    items.setAssetId(assetCurrentJyPo.getId());
                }
                //新增基础网络
                assetBasicNetworkJyService.saveBatch(assetCurrentJyPo.getJcwlList());
            }
            if (assetCurrentJyPo.getOtherConcat() != null) {
                for (UnitOtherConcatJyPo items : assetCurrentJyPo.getOtherConcat()) {
                    items.setAssetId(assetCurrentJyPo.getId());
                }
                //新增其他联系人
                unitOtherConcatJyservice.saveBatch(assetCurrentJyPo.getOtherConcat());
            }

        } else {
            AssetCurrentCpPo acc = new AssetCurrentCpPo();
            BeanUtil.copyProperties(assetCurrentJyPo, acc);
            assetCurrentCpService.save(acc);
            if (acc.getXjgywxt() != null) {
                acc.getXjgywxt().setAssetId(acc.getId());
                assetBusinessFormCpService.save(acc.getXjgywxt());
            }
            if (acc.getGylxxList() != null) {
                for (AssetSupplyChainCpPo items : acc.getGylxxList()) {
                    items.setAssetId(acc.getId());
                }
                //新增供应链
                assetSupplyChainCpService.saveBatch(acc.getGylxxList());
            }
            if (acc.getJcwlList() != null) {
                for (AssetBasicNetworkCpPo items : acc.getJcwlList()) {
                    items.setAssetId(acc.getId());
                }
                //新增基础网络
                assetBasicNetworkCpService.saveBatch(acc.getJcwlList());
            }
            if (acc.getOtherConcat() != null) {
                for (UnitOtherConcatCpPo items : acc.getOtherConcat()) {
                    items.setAssetId(acc.getId());
                }
                //新增其他联系人
                unitOtherConcatCpService.saveBatch(acc.getOtherConcat());
            }
        }

        return success();
    }

    /**
     * 分页查询管理端或者单位端审核列表
     *
     * @param as 查询实体
     * @return 所有数据
     */
    @ApiOperation(value = "分页查询管理端或者单位端审核列表", response = AssetCurrentCpPo.class)
    @GetMapping("/getAuditList")
    public AjaxResult getAuditList(AssetAuditPageRequest as) {
        Page<AssetCurrentJyPo> page = new Page<>();
        page.setSize(as.getSize());
        page.setCurrent(as.getCurrent());
        try {
            if (!SecurityUtils.getLoginUser().getUser().isAdmin() && !SecurityUtils.hasRole("common")) {
                as.setDwmc(SecurityUtils.getLoginUser().getUser().getNickName());
            }
        } catch (Exception e) {
            throw new ServiceException("获取用户信息异常");
        }
        return success(assetCurrentJyService.getAuditList(page, as));
    }

    /**
     * 查看中间数据详情
     */
    @ApiOperation(value = "单位端查看中间数据详情", response = AssetCurrentCpPo.class)
    @GetMapping("/lookInfo/{id}")
    public AjaxResult lookInfo(@PathVariable Long id) {
        AssetCurrentCpPo byId = assetCurrentCpService.getById(id);
        byId.setGylxxList(assetSupplyChainCpService.lambdaQuery().eq(AssetSupplyChainCpPo::getAssetId, byId.getId()).isNull(AssetSupplyChainCpPo::getTaskId).list());
        byId.setJcwlList(assetBasicNetworkCpService.lambdaQuery().eq(AssetBasicNetworkCpPo::getAssetId, byId.getId()).isNull(AssetBasicNetworkCpPo::getTaskId).list());
        byId.setXjgywxt(assetBusinessFormCpService.lambdaQuery().eq(AssetBusinessFormCpPo::getAssetId, byId.getId()).isNull(AssetBusinessFormCpPo::getTaskId).one());
        byId.setOtherConcat(unitOtherConcatCpService.lambdaQuery().eq(UnitOtherConcatCpPo::getAssetId, byId.getId()).isNull(UnitOtherConcatCpPo::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 success(byId);
    }

    /**
     * 管理端审核
     *
     * @param as 查询实体
     * @return 所有数据
     */
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @ApiOperation(value = "管理端审核")
    @PostMapping("/audit")
    public AjaxResult audit(@Valid @RequestBody AssetAuditRequest as) {
        AssetCurrentCpPo ass = new AssetCurrentCpPo();
        BeanUtil.copyProperties(as, ass);
        assetCurrentCpService.updateById(ass);
        //通过数据同步
        if ("2".equals(as.getAuditState())) {
            AssetCurrentCpPo byId = assetCurrentCpService.getById(as.getCurrentId());
            AssetCurrent assetCurrent = new AssetCurrent();
            BeanUtil.copyProperties(byId, assetCurrent);
            //修改主表
            assetCurrentService.updateById(assetCurrent);
            //删除副表数据然后再添加
            assetSupplyChainService.deleteByAssetIds(assetCurrent.getId());
            assetBasicNetworkService.deleteByAssetIds(assetCurrent.getId());
            assetBusinessFormService.deleteByAssetIds(assetCurrent.getId());
            unitOtherConcatService.deleteByAssetIds(assetCurrent.getId());

            List<AssetSupplyChainCpPo> list = assetSupplyChainCpService.lambdaQuery().eq(AssetSupplyChainCpPo::getAssetId, byId.getId()).isNull(AssetSupplyChainCpPo::getTaskId).list();
            list.forEach(x -> {
                AssetSupplyChain a1 = new AssetSupplyChain();
                BeanUtil.copyProperties(x, a1);
                assetSupplyChainService.save(a1);
            });

            List<AssetBasicNetworkCpPo> list1 = assetBasicNetworkCpService.lambdaQuery().eq(AssetBasicNetworkCpPo::getAssetId, byId.getId()).isNull(AssetBasicNetworkCpPo::getTaskId).list();
            list1.forEach(x -> {
                AssetBasicNetwork a2 = new AssetBasicNetwork();
                BeanUtil.copyProperties(x, a2);
                assetBasicNetworkService.save(a2);
            });

            List<AssetBusinessFormCpPo> list2 = assetBusinessFormCpService.lambdaQuery().eq(AssetBusinessFormCpPo::getAssetId, byId.getId()).isNull(AssetBusinessFormCpPo::getTaskId).list();
            list2.forEach(x -> {
                AssetBusinessForm a3 = new AssetBusinessForm();
                BeanUtil.copyProperties(x, a3);
                assetBusinessFormService.save(a3);
            });

            List<UnitOtherConcatCpPo> list3 = unitOtherConcatCpService.lambdaQuery().eq(UnitOtherConcatCpPo::getAssetId, byId.getId()).isNull(UnitOtherConcatCpPo::getTaskId).list();
            list3.forEach(x -> {
                UnitOtherConcat a4 = new UnitOtherConcat();
                BeanUtil.copyProperties(x, a4);
                unitOtherConcatService.save(a4);
            });
            //如果有任务也修改任务这边
            AssetCurrentCpPo one = assetCurrentCpService.lambdaQuery()
                    .isNotNull(AssetCurrentCpPo::getTaskId)
                    .ne(AssetCurrentCpPo::getStatus, 5)
                    .eq(AssetCurrentCpPo::getId, byId.getId()).one();
            if (one != null) {
                Integer taskId = one.getTaskId();
                Long appId = one.getCurrentId();
                Integer status = one.getStatus();
                BeanUtil.copyProperties(byId, one);
                one.setCurrentId(appId);
                one.setTaskId(taskId);
                one.setAuditState(null);
                one.setAuditYy(null);
                one.setStatus(status);
                assetCurrentCpService.updateById(one);

                QueryWrapper<AssetSupplyChainCpPo> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("asset_id", one.getId());
                queryWrapper2.eq("task_id", taskId);
                assetSupplyChainCpService.remove(queryWrapper2);
                QueryWrapper<AssetBasicNetworkCpPo> queryWrapper3 = new QueryWrapper<>();
                queryWrapper3.eq("asset_id", one.getId());
                queryWrapper3.eq("task_id", taskId);
                assetBasicNetworkCpService.remove(queryWrapper3);
                QueryWrapper<AssetBusinessFormCpPo> queryWrapper4 = new QueryWrapper<>();
                queryWrapper4.eq("asset_id", one.getId());
                queryWrapper4.eq("task_id", taskId);
                assetBusinessFormCpService.remove(queryWrapper4);
                QueryWrapper<UnitOtherConcatCpPo> queryWrapper5 = new QueryWrapper<>();
                queryWrapper5.eq("task_id", taskId);
                queryWrapper5.eq("asset_id", one.getId());
                unitOtherConcatCpService.remove(queryWrapper5);
                list.forEach(x -> {
                    x.setTaskId(taskId);
                    assetSupplyChainCpService.save(x);
                });
                list1.forEach(x -> {
                    x.setTaskId(taskId);
                    assetBasicNetworkCpService.save(x);
                });
                list2.forEach(x -> {
                    x.setTaskId(taskId);
                    assetBusinessFormCpService.save(x);
                });
                list3.forEach(x -> {
                    x.setTaskId(taskId);
                    unitOtherConcatCpService.save(x);
                });
            }
        }
        return success();
    }
    //=================================================


    /**
     * 修改数据
     *
     * @param assetCurrent 实体对象
     * @return 修改结果
     */
    @ApiOperation(value = "修改数据")
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @PutMapping
    public AjaxResult update(@RequestBody @Valid AssetCurrent assetCurrent) {
        StringBuilder a = new StringBuilder();
        if (assetCurrent.getGlymList() != null) {
            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();
        if (assetCurrent.getGlIpList() != null) {
            assetCurrent.getGlIpList().forEach(x -> {
                if (!Objects.equals(x.getKey(), "") && x.getKey() != null) {
                    b.append(x.getKey());
                    b.append(",");
                }
            });
        }
        assetCurrent.setGlIp(b.toString());
        assetCurrentService.updateById(assetCurrent);
        assetSupplyChainService.deleteByAssetIds(assetCurrent.getId());
        assetBasicNetworkService.deleteByAssetIds(assetCurrent.getId());
        assetBusinessFormService.deleteByAssetIds(assetCurrent.getId());
        unitOtherConcatService.deleteByAssetIds(assetCurrent.getId());
        if (assetCurrent.getXjgywxt() != null) {
            assetCurrent.getXjgywxt().setAssetId(assetCurrent.getId());
            assetCurrent.getXjgywxt().setId(null);
            assetBusinessFormService.save(assetCurrent.getXjgywxt());
        }
        if (assetCurrent.getGylxxList() != null) {
            for (AssetSupplyChain items : assetCurrent.getGylxxList()) {
                items.setAssetId(assetCurrent.getId());
                items.setId(null);
            }
            //新增供应链
            assetSupplyChainService.saveBatch(assetCurrent.getGylxxList());
        }
        if (assetCurrent.getJcwlList() != null) {
            for (AssetBasicNetwork items : assetCurrent.getJcwlList()) {
                items.setAssetId(assetCurrent.getId());
                items.setId(null);
            }
            //新增基础网络
            assetBasicNetworkService.saveBatch(assetCurrent.getJcwlList());
        }
        if (assetCurrent.getOtherConcat() != null) {
            for (UnitOtherConcat items : assetCurrent.getOtherConcat()) {
                items.setAssetId(assetCurrent.getId());
                items.setConcatId(null);
            }
            //新增其他联系人
            unitOtherConcatService.saveBatch(assetCurrent.getOtherConcat());
        }
        //================================

        Integer i = unitService.selectTaskId(assetCurrent.getDwmc(), "0");
        if (i != null) {
            QueryWrapper<AssetCurrentCpPo> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("id", assetCurrent.getId());
            queryWrapper1.eq("task_id", i);
            queryWrapper1.ne("status", 5);
            AssetCurrentCpPo x = assetCurrentCpService.getOne(queryWrapper1);

            Integer status = x.getStatus();
            Long currentId = x.getCurrentId();
            AssetCurrentCpPo assetCurrentCpPo = new AssetCurrentCpPo();
            BeanUtil.copyProperties(assetCurrent, assetCurrentCpPo);
            assetCurrentCpPo.setTaskId(i);
            assetCurrentCpPo.setCurrentId(currentId);
            assetCurrentCpPo.setStatus(status);
            assetCurrentCpService.updateById(assetCurrentCpPo);

            QueryWrapper<AssetSupplyChainCpPo> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("asset_id", assetCurrent.getId());
            queryWrapper2.eq("task_id", i);
            assetSupplyChainCpService.remove(queryWrapper2);
            QueryWrapper<AssetBasicNetworkCpPo> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("task_id", i);
            queryWrapper3.eq("asset_id", assetCurrent.getId());
            assetBasicNetworkCpService.remove(queryWrapper3);
            QueryWrapper<AssetBusinessFormCpPo> queryWrapper4 = new QueryWrapper<>();
            queryWrapper4.eq("task_id", i);
            queryWrapper4.eq("asset_id", assetCurrent.getId());
            assetBusinessFormCpService.remove(queryWrapper4);
            QueryWrapper<UnitOtherConcatCpPo> queryWrapper5 = new QueryWrapper<>();
            queryWrapper5.eq("asset_id", assetCurrent.getId());
            queryWrapper5.eq("task_id", i);
            unitOtherConcatCpService.remove(queryWrapper5);

            if (assetCurrent.getXjgywxt() != null) {
                assetCurrentCpPo.getXjgywxt().setAssetId(assetCurrent.getId());
                assetCurrentCpPo.getXjgywxt().setTaskId(i);
                assetBusinessFormCpService.save(assetCurrentCpPo.getXjgywxt());
            }
            if (assetCurrent.getGylxxList() != null) {
                for (AssetSupplyChainCpPo items : assetCurrentCpPo.getGylxxList()) {
                    items.setAssetId(assetCurrent.getId());
                    items.setTaskId(i);
                }
                //新增供应链
                assetSupplyChainCpService.saveBatch(assetCurrentCpPo.getGylxxList());
            }
            if (assetCurrent.getJcwlList() != null) {
                for (AssetBasicNetworkCpPo items : assetCurrentCpPo.getJcwlList()) {
                    items.setAssetId(assetCurrent.getId());
                    items.setTaskId(i);
                }
                //新增基础网络
                assetBasicNetworkCpService.saveBatch(assetCurrentCpPo.getJcwlList());
            }
            if (assetCurrent.getOtherConcat() != null) {
                for (UnitOtherConcatCpPo items : assetCurrentCpPo.getOtherConcat()) {
                    items.setAssetId(assetCurrent.getId());
                    items.setTaskId(i);
                }
                //新增其他联系人
                unitOtherConcatCpService.saveBatch(assetCurrentCpPo.getOtherConcat());
            }
        }
        return success();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @ApiOperation(value = "删除数据")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        assetSupplyChainService.deleteByAssetIds(id);
        assetBasicNetworkService.deleteByAssetIds(id);
        assetBusinessFormService.deleteByAssetIds(id);
        unitOtherConcatService.deleteByAssetIds(id);
        assetCurrentService.deleteByUnitIds(id);

        QueryWrapper<AssetCurrentCpPo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("id", id);
        assetCurrentCpService.remove(queryWrapper1);
        QueryWrapper<AssetSupplyChainCpPo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("asset_id", id);
        assetSupplyChainCpService.remove(queryWrapper2);
        QueryWrapper<AssetBasicNetworkCpPo> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("asset_id", id);
        assetBasicNetworkCpService.remove(queryWrapper3);
        QueryWrapper<AssetBusinessFormCpPo> queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.eq("asset_id", id);
        assetBusinessFormCpService.remove(queryWrapper4);
        QueryWrapper<UnitOtherConcatCpPo> queryWrapper5 = new QueryWrapper<>();
        queryWrapper5.eq("asset_id", id);
        unitOtherConcatCpService.remove(queryWrapper5);


        return success();
    }

    /**
     * 下载资产导入模板
     */
    @ApiOperation(value = "下载资产导入模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<AssetExport> util = new ExcelUtil<>(AssetExport.class);
        util.importTemplateExcel(response, "资产导入模板");
    }


    /**
     * 导出现有资产
     */
    @ApiOperation(value = "导出现有资产")
    @PostMapping("/export")
    public void exportProject(HttpServletResponse response, AssetCurrentPageRequest as) {
        try {
            if (!SecurityUtils.getLoginUser().getUser().isAdmin() && !SecurityUtils.hasRole("common")) {
                as.setDwmc(SecurityUtils.getLoginUser().getUser().getNickName());
            }
        } catch (Exception e) {
            throw new ServiceException("获取用户信息异常");
        }
        List<AssetExport> list = assetCurrentService.page1(as);
        ExcelUtil<AssetExport> util = new ExcelUtil<>(AssetExport.class);
        util.exportExcel(response, list, "现有资产记录");
    }

    /**
     * 导入现有资产
     */
    @Transactional(rollbackFor = Exception.class)
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @ApiOperation(value = "导入现有资产")
    @PostMapping(value = "/import", consumes = "multipart/form-data")
    public AjaxResult export(@RequestPart("file") MultipartFile file) throws Exception {
        //读取所有的字段
        ExcelUtil<AssetExport> util = new ExcelUtil<>(AssetExport.class);
        List<AssetExport> proList = util.importExcel(file.getInputStream());
        int j = 0;
        for (AssetExport items : proList) {
            j++;
            AssetCurrent as = new AssetCurrent();
            //copy新增到新的现有资产表里面
            BeanUtil.copyProperties(items, as);
            AssetCurrent one = assetCurrentService.lambdaQuery().eq(AssetCurrent::getXtmc, as.getXtmc())
                    .eq(AssetCurrent::getDwmc, as.getDwmc()).one();
            if (one != null) {
                as.setId(one.getId());
                assetCurrentService.updateById(as);
                List<Long> a1 = new ArrayList<>();
                a1.add(one.getId());
                assetSupplyChainService.deleteIdList(a1);
                assetBasicNetworkService.deleteIdList(a1);
            } else {
                List<Unit> list = unitService.lambdaQuery().eq(Unit::getNickName, items.getDwmc())
                        .eq(Unit::getDelFlag, 0).list();
                if (list == null || list.isEmpty()) {
                    throw new ServiceException(j + "行单位未入库!");
                }
                assetCurrentService.save(as);
            }

            //新增系统建设单位
            AssetSupplyChain s1 = new AssetSupplyChain();
            BeanUtil.copyProperties(items, s1);
            s1.setType(5);
            s1.setAssetId(as.getId());
            assetSupplyChainService.save(s1);
            //新增系统运营单位
            AssetSupplyChain b6 = new AssetSupplyChain();
            b6.setGyszcdz(items.getGyszcdz1());
            b6.setLxr(items.getLxr1());
            b6.setLxdh(items.getLxdh1());
            b6.setSfwtc(items.getSfwtc1());
            b6.setName(items.getName1());
            b6.setTyshxydm(items.getTyshxydm1());
            b6.setType(7);
            b6.setAssetId(as.getId());
            assetSupplyChainService.save(b6);
            //新增服务器信息
            AssetBasicNetwork a1 = new AssetBasicNetwork();
            BeanUtil.copyProperties(items, a1);
            a1.setType(1);
            a1.setAssetId(as.getId());
            assetBasicNetworkService.save(a1);
            //新增网络设备
            AssetBasicNetwork a2 = new AssetBasicNetwork();
            a2.setSblx(items.getWlsblx());
            a2.setPp(items.getWlpp());
            a2.setSbIp(items.getWlsbIp());
            a2.setYjxh(items.getWlyjxh());
            a2.setYjxlh(items.getWlyjxlh());
            a2.setYjbbxx(items.getWlyjbbxx());
            a2.setYjyt(items.getWlyjyt());
            a2.setYjbsxx(items.getWlyjbsxx());
            a2.setType(2);
            a2.setAssetId(as.getId());
            assetBasicNetworkService.save(a2);
            //新增安全设备
            AssetBasicNetwork a3 = new AssetBasicNetwork();
            a3.setSblx(items.getAqwlsblx());
            a3.setPp(items.getAqwlpp());
            a3.setSbIp(items.getAqwlsbIp());

            a3.setYjxh(items.getAqyjxh());
            a3.setYjxlh(items.getAqyjxlh());
            a3.setYjbbxx(items.getAqyjbbxx());
            a3.setYjyt(items.getAqyjyt());
            a3.setYjbsxx(items.getAqyjbswz());


            a3.setType(3);
            a3.setAssetId(as.getId());
            assetBasicNetworkService.save(a3);

            //=============
            Long id = as.getId();
            Integer i = unitService.selectTaskId(as.getDwmc(), "0");
            if (i != null) {
                //新增资产复制主表
                AssetCurrentCpPo assetCurrentCpPo = new AssetCurrentCpPo();
                BeanUtil.copyProperties(as, assetCurrentCpPo);
                assetCurrentCpPo.setTaskId(i);
                assetCurrentCpPo.setStatus(0);
                AssetCurrentCpPo two = assetCurrentCpService.lambdaQuery().eq(AssetCurrentCpPo::getXtmc, as.getXtmc())
                        .eq(AssetCurrentCpPo::getDwmc, as.getDwmc()).isNotNull(AssetCurrentCpPo::getTaskId).one();
                if (two != null) {
                    assetCurrentCpPo.setId(two.getId());
                    assetCurrentCpService.updateById(assetCurrentCpPo);
                    assetSupplyChainCpService.deletByAssetIdandTaskId(id, i);
                    assetBasicNetworkCpService.deletByAssetIdandTaskId(id, i);
                } else {
                    assetCurrentCpService.save(assetCurrentCpPo);
                }
                //新增系统建设单位
                AssetSupplyChainCpPo co1 = new AssetSupplyChainCpPo();
                BeanUtil.copyProperties(items, co1);
                co1.setType(5);
                co1.setAssetId(id);
                co1.setTaskId(i);
                assetSupplyChainCpService.save(co1);

                //新增系统运营单位
                AssetSupplyChainCpPo co2 = new AssetSupplyChainCpPo();
                co2.setGyszcdz(items.getGyszcdz1());
                co2.setLxr(items.getLxr1());
                co2.setLxdh(items.getLxdh1());
                co2.setSfwtc(items.getSfwtc1());
                co2.setName(items.getName1());
                co2.setTyshxydm(items.getTyshxydm1());
                co2.setType(7);
                co2.setAssetId(as.getId());
                co2.setTaskId(i);
                assetSupplyChainCpService.save(co2);

                //新增服务器信息
                AssetBasicNetworkCpPo co3 = new AssetBasicNetworkCpPo();
                BeanUtil.copyProperties(items, co3);
                co3.setType(1);
                co3.setAssetId(id);
                co3.setTaskId(i);
                assetBasicNetworkCpService.save(co3);

                //新增网络设备
                AssetBasicNetworkCpPo co4 = new AssetBasicNetworkCpPo();
                co4.setSblx(items.getWlsblx());
                co4.setPp(items.getWlpp());
                co4.setSbIp(items.getWlsbIp());
                co4.setYjxh(items.getWlyjxh());
                co4.setYjxlh(items.getWlyjxlh());
                co4.setYjbbxx(items.getWlyjbbxx());
                co4.setYjyt(items.getWlyjyt());
                co4.setYjbsxx(items.getWlyjbsxx());
                co4.setType(2);
                co4.setAssetId(id);
                co4.setTaskId(i);
                assetBasicNetworkCpService.save(co4);
                //新增安全设备
                AssetBasicNetworkCpPo co5 = new AssetBasicNetworkCpPo();
                co5.setYjxh(items.getAqyjxh());
                co5.setYjxlh(items.getAqyjxlh());
                co5.setYjbbxx(items.getAqyjbbxx());
                co5.setYjyt(items.getAqyjyt());
                co5.setYjbsxx(items.getAqyjbswz());
                co5.setSblx(items.getAqwlsblx());
                co5.setPp(items.getAqwlpp());
                co5.setSbIp(items.getAqwlsbIp());
                co5.setType(3);
                co5.setAssetId(id);
                co5.setTaskId(i);
                assetBasicNetworkCpService.save(co5);

            }
        }
        return AjaxResult.success();
    }

}

