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.AssetOfficialAccount;
import com.ruoyi.tc.entity.po.AssetOfficialAccountCpPo;
import com.ruoyi.tc.entity.request.AssetOfficialAccountPageRequest;
import com.ruoyi.tc.mapper.AssetOfficialAccountMapper;
import com.ruoyi.tc.service.AssetOfficialAccountCpService;
import com.ruoyi.tc.service.AssetOfficialAccountService;
import com.ruoyi.tc.service.UnitService;
import org.springframework.stereotype.Service;

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

/**
 * 公众号资产(asset_official_account)表服务实现类
 *
 * @author makejava
 * @since 2024-11-28 17:13:07
 */
@Service("assetOfficialAccountService")
public class AssetOfficialAccountServiceImpl extends ServiceImpl<AssetOfficialAccountMapper, AssetOfficialAccount> implements AssetOfficialAccountService {


    @Resource
    private UnitService unitService;

    @Resource
    private AssetOfficialAccountCpService assetOfficialAccountCpService;

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

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


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


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

    /**
     * 新增公众号数据
     *
     * @param assetOfficialAccount 实体
     * @return 新增结果
     */
    @Override
    public Boolean add(AssetOfficialAccount assetOfficialAccount) {
        save(assetOfficialAccount);
        AssetOfficialAccountCpPo a = new AssetOfficialAccountCpPo();
        BeanUtil.copyProperties(assetOfficialAccount, a);
        a.setTaskId(unitService.selectTaskId(assetOfficialAccount.getSsdw(), "2"));
        a.setAssetId(assetOfficialAccount.getId());
        return assetOfficialAccountCpService.save(a);
    }

    /**
     * 编辑公众号数据
     *
     * @param assetOfficialAccount 实体
     * @return 编辑结果
     */
    @Override
    public Boolean edit(AssetOfficialAccount assetOfficialAccount) {
        updateById(assetOfficialAccount);
        AssetOfficialAccountCpPo one = assetOfficialAccountCpService.lambdaQuery().eq(AssetOfficialAccountCpPo::getAssetId, assetOfficialAccount.getId()).one();
        BeanUtil.copyProperties(assetOfficialAccount, one);
        one.setTaskId(unitService.selectTaskId(assetOfficialAccount.getSsdw(), "2"));
        one.setAssetId(assetOfficialAccount.getId());
        return assetOfficialAccountCpService.updateById(one);
    }


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