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.AssetCurrent;
import com.ruoyi.tc.entity.AssetEmail;
import com.ruoyi.tc.entity.AssetTask;
import com.ruoyi.tc.entity.Unit;
import com.ruoyi.tc.entity.po.AssetCurrentCpPo;
import com.ruoyi.tc.entity.po.AssetEmailCpPo;
import com.ruoyi.tc.entity.request.AssetEmailPageRequest;
import com.ruoyi.tc.mapper.AssetEmailMapper;
import com.ruoyi.tc.service.AssetEmailCpService;
import com.ruoyi.tc.service.AssetEmailService;
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_email)表服务实现类
 *
 * @author makejava
 * @since 2024-11-28 17:13:07
 */
@Service("assetEmailService")
public class AssetEmailServiceImpl extends ServiceImpl<AssetEmailMapper, AssetEmail> implements AssetEmailService {
    @Resource
    private UnitService unitService;

    @Resource
    private AssetEmailCpService assetEmailCpService;

    @Resource
    private AssetTaskService assetTaskService;


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

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


    /**
     * 查询所有数据
     *
     * @param as 查询实体
     * @return 所有数据
     */
    @Override
    public List<AssetEmail> getAllList(AssetEmailPageRequest 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);
    }

    /**
     * 新增email数据
     *
     * @param assetEmail 实体
     * @return 新增结果
     */
    @Override
    public Boolean add(AssetEmail assetEmail) {


        List<Unit> list = unitService.lambdaQuery().eq(Unit::getNickName, assetEmail.getSsdw())
                .eq(Unit::getDelFlag, 0).list();
        if (list == null || list.isEmpty()) {
            throw new ServiceException("请选择已有单位!");
        }
        List<AssetEmail> list1 = lambdaQuery().eq(AssetEmail::getSsdw, assetEmail.getSsdw())
                .eq(AssetEmail::getDzyxhz, assetEmail.getDzyxhz()).list();
        if (list1 != null&& !list1.isEmpty()){
            throw new ServiceException("不允许新增重复资产!");
        }
        save(assetEmail);
        AssetEmailCpPo a = new AssetEmailCpPo();
        BeanUtil.copyProperties(assetEmail, a);
        Integer i = unitService.selectTaskId(assetEmail.getSsdw(), "3");
        if (i != null) {
            a.setTaskId(i);
            a.setAssetId(assetEmail.getId());
            assetEmailCpService.save(a);
        }
        return true;
    }

    /**
     * 编辑email数据
     *
     * @param assetEmail 实体
     * @return 编辑结果
     */
    @Override
    public Boolean edit(AssetEmail assetEmail) {

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

        updateById(assetEmail);


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

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

        }
        return true;
    }


}
