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.AssetMiniPrograms;
import com.ruoyi.tc.entity.po.AssetMiniProgramsCpPo;
import com.ruoyi.tc.entity.request.AssetMiniProgramsPageRequest;
import com.ruoyi.tc.mapper.AssetMiniProgramsMapper;
import com.ruoyi.tc.service.AssetMiniProgramsCpService;
import com.ruoyi.tc.service.AssetMiniProgramsService;
import com.ruoyi.tc.service.UnitService;
import org.springframework.stereotype.Service;

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

/**
 * 小程序资产(asset_mini_programs)表服务实现类
 *
 * @author makejava
 * @since 2024-11-28 17:13:07
 */
@Service("assetMiniProgramsService")
public class AssetMiniProgramsServiceImpl extends ServiceImpl<AssetMiniProgramsMapper, AssetMiniPrograms> implements AssetMiniProgramsService{

    @Resource
    private AssetMiniProgramsCpService assetMiniProgramsCpService;

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

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


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

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

    /**
     * 新增小程序数据
     *
     * @param assetMiniPrograms 实体
     * @return 新增结果
     */
    @Override
    public Boolean add(AssetMiniPrograms assetMiniPrograms) {
        save(assetMiniPrograms);
        AssetMiniProgramsCpPo assetMiniProgramsCpPo = new AssetMiniProgramsCpPo();
        BeanUtil.copyProperties(assetMiniPrograms,assetMiniProgramsCpPo);
        assetMiniProgramsCpPo.setTaskId(unitService.selectTaskId(assetMiniPrograms.getSsdw(),"1"));
        assetMiniProgramsCpPo.setAssetId(assetMiniPrograms.getId());
        return assetMiniProgramsCpService.save(assetMiniProgramsCpPo);
    }

    /**
     * 编辑小程序数据
     *
     * @param assetMiniPrograms 实体
     * @return 编辑结果
     */
    @Override
    public Boolean edit(AssetMiniPrograms assetMiniPrograms) {
        updateById(assetMiniPrograms);
        AssetMiniProgramsCpPo one = assetMiniProgramsCpService.lambdaQuery().eq(AssetMiniProgramsCpPo::getAssetId, assetMiniPrograms.getId()).one();
        BeanUtil.copyProperties(assetMiniPrograms,one);
        one.setTaskId(unitService.selectTaskId(assetMiniPrograms.getSsdw(),"1"));
        one.setAssetId(assetMiniPrograms.getId());
        return assetMiniProgramsCpService.updateById(one);
    }


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