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.tc.entity.AssetEmail;
import com.ruoyi.tc.entity.po.AssetEmailCpPo;
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) {
        save(assetEmail);
        AssetEmailCpPo a = new AssetEmailCpPo();
        BeanUtil.copyProperties(assetEmail, a);
        a.setTaskId(unitService.selectTaskId(assetEmail.getSsdw(), "3"));
        a.setAssetId(assetEmail.getId());
        return assetEmailCpService.save(a);
    }

    /**
     * 编辑email数据
     *
     * @param assetEmail 实体
     * @return 编辑结果
     */
    @Override
    public Boolean edit(AssetEmail assetEmail) {
        updateById(assetEmail);
        AssetEmailCpPo one = assetEmailCpService.lambdaQuery().eq(AssetEmailCpPo::getAssetId, assetEmail.getId()).one();
        BeanUtil.copyProperties(assetEmail, one);
        one.setTaskId(unitService.selectTaskId(assetEmail.getSsdw(), "3"));
        one.setAssetId(assetEmail.getId());
        return assetEmailCpService.updateById(one);
    }
}
