package com.ruoyi.buyerapi.service.impl;

import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.buyerapi.domain.BuyerFileInfo;
import com.ruoyi.buyerapi.domain.BuyerGoods;
import com.ruoyi.buyerapi.mapper.BuyerFileInfoMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.buyerapi.mapper.BuyerIntangibleAssetsMapper;
import com.ruoyi.buyerapi.domain.BuyerIntangibleAssets;
import com.ruoyi.buyerapi.service.IBuyerIntangibleAssetsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 无形资产摊销记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-22
 */
@Service
public class BuyerIntangibleAssetsServiceImpl implements IBuyerIntangibleAssetsService {
    @Autowired
    private BuyerIntangibleAssetsMapper buyerIntangibleAssetsMapper;

    @Autowired
    private BuyerFileInfoMapper buyerFileInfoMapper;

    /**
     * 查询无形资产摊销记录
     *
     * @param id 无形资产摊销记录主键
     * @return 无形资产摊销记录
     */
    @Transactional
    @Override
    public BuyerIntangibleAssets selectBuyerIntangibleAssetsById(String id) {
        BuyerIntangibleAssets buyerIntangibleAssets = buyerIntangibleAssetsMapper.selectBuyerIntangibleAssetsById(id);
        // 查询关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(id);
        buyerIntangibleAssets.setBuyerFileInfos(buyerFileInfos);
        return buyerIntangibleAssets;
    }

    /**
     * 查询无形资产摊销记录列表
     *
     * @param buyerIntangibleAssets 无形资产摊销记录
     * @return 无形资产摊销记录
     */
    @Transactional
    @Override
    public List<BuyerIntangibleAssets> selectBuyerIntangibleAssetsList(BuyerIntangibleAssets buyerIntangibleAssets) {
        buyerIntangibleAssets.setDeptId(SecurityUtils.getCompanyId());
        List<BuyerIntangibleAssets> buyerIntangibleAssetss = buyerIntangibleAssetsMapper.selectBuyerIntangibleAssetsList(buyerIntangibleAssets);
        // 查询关联文件
        for (BuyerIntangibleAssets intangibleAssets : buyerIntangibleAssetss) {
            List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(intangibleAssets.getId());
            intangibleAssets.setBuyerFileInfos(buyerFileInfos);
        }
        return buyerIntangibleAssetss;
    }

    /**
     * 统计无形资产摊销记录列表
     *
     * @param buyerIntangibleAssets 无形资产摊销记录
     * @return 无形资产摊销记录
     */
    @Override
    public List<BuyerIntangibleAssets> selectStatistBuyerIntangibleAssetsList(BuyerIntangibleAssets buyerIntangibleAssets) {
        buyerIntangibleAssets.setDeptId(SecurityUtils.getCompanyId());
        return buyerIntangibleAssetsMapper.selectStatistBuyerIntangibleAssetsList(buyerIntangibleAssets);
    }

    /**
     * 新增无形资产摊销记录
     *
     * @param buyerIntangibleAssets 无形资产摊销记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBuyerIntangibleAssets(BuyerIntangibleAssets buyerIntangibleAssets) {
        buyerIntangibleAssets.setCreateTime(DateUtils.getNowDate());
        buyerIntangibleAssets.setDeptId(SecurityUtils.getCompanyId());
        // 新增关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerIntangibleAssets.getBuyerFileInfos();
        if (buyerFileInfos != null && buyerFileInfos.size()>0) {
            for (BuyerFileInfo buyerFileInfo : buyerFileInfos) {
                buyerFileInfo.setId(IdUtil.simpleUUID());
                buyerFileInfo.setCreateTime(DateUtils.getNowDate());
                buyerFileInfo.setCreateBy(SecurityUtils.getUsername());
                buyerFileInfo.setLinkId(buyerIntangibleAssets.getId());
            }
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        }
        return buyerIntangibleAssetsMapper.insertBuyerIntangibleAssets(buyerIntangibleAssets);
    }

    /**
     * 修改无形资产摊销记录
     *
     * @param buyerIntangibleAssets 无形资产摊销记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBuyerIntangibleAssets(BuyerIntangibleAssets buyerIntangibleAssets) {
        buyerIntangibleAssets.setUpdateTime(DateUtils.getNowDate());
        // 修改关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerIntangibleAssets.getBuyerFileInfos();
        if (buyerFileInfos != null && buyerFileInfos.size()>0) {
            for (BuyerFileInfo buyerFileInfo : buyerFileInfos) {
                buyerFileInfo.setId(IdUtil.simpleUUID());
                buyerFileInfo.setUpdateTime(DateUtils.getNowDate());
                buyerFileInfo.setUpdateBy(SecurityUtils.getUsername());
                buyerFileInfo.setLinkId(buyerIntangibleAssets.getId());
            }
            buyerFileInfoMapper.deleteBuyerFileInfoByLinkId(buyerIntangibleAssets.getId());
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        }else if (buyerFileInfos != null && buyerFileInfos.size() == 0) {
            buyerFileInfoMapper.deleteBuyerFileInfoByLinkId(buyerIntangibleAssets.getId());
        }
        return buyerIntangibleAssetsMapper.updateBuyerIntangibleAssets(buyerIntangibleAssets);
    }

    /**
     * 批量删除无形资产摊销记录
     *
     * @param ids 需要删除的无形资产摊销记录主键
     * @return 结果
     */
    @Override
    public int deleteBuyerIntangibleAssetsByIds(String[] ids) {
        return buyerIntangibleAssetsMapper.deleteBuyerIntangibleAssetsByIds(ids);
    }

    /**
     * 删除无形资产摊销记录信息
     *
     * @param id 无形资产摊销记录主键
     * @return 结果
     */
    @Override
    public int deleteBuyerIntangibleAssetsById(String id) {
        return buyerIntangibleAssetsMapper.deleteBuyerIntangibleAssetsById(id);
    }
}
