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.AssetApp;
import com.ruoyi.tc.entity.po.AssetAppCpPo;
import com.ruoyi.tc.entity.po.AssetEmailCpPo;
import com.ruoyi.tc.entity.request.AssetAppPageRequest;
import com.ruoyi.tc.mapper.AssetAppMapper;
import com.ruoyi.tc.service.AssetAppCpService;
import com.ruoyi.tc.service.AssetAppService;
import com.ruoyi.tc.service.UnitService;
import org.springframework.stereotype.Service;

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

/**
 * 资产移动应用程序表(asset_app)表服务实现类
 *
 * @author makejava
 * @since 2024-11-28 17:13:07
 */
@Service("assetAppService")
public class AssetAppServiceImpl extends ServiceImpl<AssetAppMapper, AssetApp> implements AssetAppService{
    @Resource
    private UnitService unitService;

    @Resource
    private AssetAppCpService assetAppCpService;

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

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


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


    /**
     * 新增数据
     *
     * @param assetApp 实体
     * @return 新增结果
     */
    @Override
    public Boolean add(AssetApp assetApp) {
        save(assetApp);
        AssetAppCpPo a = new AssetAppCpPo();
        BeanUtil.copyProperties(assetApp, a);
        a.setTaskId(unitService.selectTaskId(assetApp.getSsdw(), "4"));
        a.setAssetId(assetApp.getId());
        return assetAppCpService.save(a);
    }


    /**
     * 编辑数据
     *
     * @param assetApp 实体
     * @return 编辑结果
     */
    @Override
    public Boolean edit(AssetApp assetApp) {
        updateById(assetApp);
        AssetAppCpPo one = assetAppCpService.lambdaQuery().eq(AssetAppCpPo::getAssetId, assetApp.getId()).one();
        BeanUtil.copyProperties(assetApp, one);
        one.setTaskId(unitService.selectTaskId(assetApp.getSsdw(), "4"));
        one.setAssetId(assetApp.getId());
        return assetAppCpService.updateById(one);
    }


}
