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.AssetEmail;
import com.ruoyi.tc.entity.Unit;
import com.ruoyi.tc.entity.po.AssetEmailCpPo;
import com.ruoyi.tc.entity.po.AssetEmailJyPo;
import com.ruoyi.tc.entity.po.AssetMiniProgramsCpPo;
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.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;


    /**
     * 分页查询所有数据
     *
     * @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("请选择已有单位!");
        }
        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) {
        updateById(assetEmail);
        AssetEmailCpPo one = assetEmailCpService.lambdaQuery()
                .eq(AssetEmailCpPo::getAssetId, assetEmail.getId())
                .ne(AssetEmailCpPo::getStatus, 5)
                .isNotNull(AssetEmailCpPo::getTaskId).one();
        if (one != null) {
            Integer status = one.getStatus();
            Integer taskId = one.getTaskId();
            Long appId = one.getEmailId();
            BeanUtil.copyProperties(assetEmail, one);
            one.setStatus(status);
            one.setTaskId(taskId);
            one.setAssetId(assetEmail.getId());
            one.setEmailId(appId);
            return assetEmailCpService.updateById(one);
        }else {
            return true;
        }
    }
}
