package com.ruoyi.tc.service.impl;

import cn.hutool.core.bean.BeanUtil;
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.exception.ServiceException;
import com.ruoyi.tc.entity.AssetApp;
import com.ruoyi.tc.entity.AssetTask;
import com.ruoyi.tc.entity.Unit;
import com.ruoyi.tc.entity.po.AssetAppCpPo;
import com.ruoyi.tc.entity.po.AssetOfficialAccountCpPo;
import com.ruoyi.tc.entity.request.AssetAppPageRequest;
import com.ruoyi.tc.mapper.AssetAppMapper;
import com.ruoyi.tc.service.AssetAppCpService;
import com.ruoyi.tc.service.AssetAppService;
import com.ruoyi.tc.service.AssetTaskService;
import com.ruoyi.tc.service.UnitService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 资产移动应用程序表(asset_app)表服务实现类
 *
 * @author makejava
 * @since 2024-11-28 17:13:07
 */
@Service("assetAppService")
public class AssetAppServiceImpl extends ServiceImpl<AssetAppMapper, AssetApp> implements AssetAppService {
    @Resource
    private UnitService unitService;

    @Resource
    private AssetAppCpService assetAppCpService;

    @Resource
    private AssetTaskService assetTaskService;



    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param as   查询实体
     * @return 所有数据
     */
    @Override
    public Page<AssetApp> page(Page<AssetApp> page, AssetAppPageRequest as) {
        return baseMapper.page(page, as);
    }

    /**
     * 逻辑删除
     */
    @Override
    public void deleteById(Long id) {
        baseMapper.deleteById(id);
        QueryWrapper<AssetAppCpPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("asset_id", id);
        assetAppCpService.remove(queryWrapper);
    }


    /**
     * 查询所有数据
     *
     * @param as 查询实体
     * @return 所有数据
     */
    @Override
    public List<AssetApp> getAllList(AssetAppPageRequest as) {
        return baseMapper.page(as);
    }

    @Override
    public void deleteIdList(List<Long> idList) {
        baseMapper.deleteIdList(idList);
    }

    @Override
    public Integer findBydwmc(String part) {
        return baseMapper.findBydwmc(part);
    }


    /**
     * 新增数据
     *
     * @param assetApp 实体
     * @return 新增结果
     */
    @Override
    public Boolean add(AssetApp assetApp) {
        List<Unit> list = unitService.lambdaQuery().eq(Unit::getNickName, assetApp.getSsdw()).eq(Unit::getDelFlag, 0).list();
        if (list == null || list.isEmpty()) {
            throw new ServiceException("请选择已有单位!");
        }

        List<AssetApp> list1 = lambdaQuery().eq(AssetApp::getSsdw, assetApp.getSsdw())
                .eq(AssetApp::getAppName, assetApp.getAppName()).list();
        if (list1 != null&& !list1.isEmpty()){
            throw new ServiceException("不允许新增重复资产!");
        }
        save(assetApp);
        AssetAppCpPo a = new AssetAppCpPo();
        BeanUtil.copyProperties(assetApp, a);
        Integer i = unitService.selectTaskId(assetApp.getSsdw(), "4");
        if (i != null) {
            a.setTaskId(i);
            a.setAssetId(assetApp.getId());
            assetAppCpService.save(a);
        }
        return true;
    }


    /**
     * 编辑数据
     *
     * @param assetApp 实体
     * @return 编辑结果
     */
    @Override
    public Boolean edit(AssetApp assetApp) {
        AssetApp byId = getById(assetApp.getId());

        List<Unit> list2 = unitService.lambdaQuery().eq(Unit::getNickName, assetApp.getSsdw()).eq(Unit::getDelFlag, 0).list();
        if (list2 == null || list2.isEmpty()) {
            throw new ServiceException("请选择已有单位!");
        }


        updateById(assetApp);


        List<AssetAppCpPo> list = assetAppCpService.lambdaQuery()
                .eq(AssetAppCpPo::getAssetId, assetApp.getId()).list();
        list.forEach(x -> {
            Integer status = x.getStatus();
            Integer taskId = x.getTaskId();
            Long appId = x.getAppId();
            String auditState = x.getAuditState();
            String auditYy = x.getAuditYy();
            BeanUtil.copyProperties(assetApp, x);
            x.setStatus(status);
            x.setTaskId(taskId);
            x.setAssetId(assetApp.getId());
            x.setAppId(appId);
            x.setAuditYy(auditYy);
            x.setAuditState(auditState);
            assetAppCpService.updateById(x);
        });

        //根据修改的单位名称查询任务表中是否有此单位的任务，如果有修改资产中的任务id，如果没有删除
        AssetTask assetTask = assetTaskService.findByDwmc(assetApp.getAppName());
        if (assetTask != null) {
            assetAppCpService.deletByAssetId(assetApp.getId());
        } else {
            //根据资产id查询旧资产
            AssetAppCpPo assetAppCpPo = assetAppCpService.findDwmc(assetApp.getId());
            if (assetAppCpPo!=null){
                assetAppCpPo.setAppName(assetApp.getAppName());
                assetAppCpPo.setTaskId(assetTask.getId());
                //修改
                assetAppCpService.updateById(assetAppCpPo);
            }

        }
        return true;
    }

}
