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.BuyerOtherCostMapper;
import com.ruoyi.buyerapi.domain.BuyerOtherCost;
import com.ruoyi.buyerapi.service.IBuyerOtherCostService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目其他费用记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-22
 */
@Service
public class BuyerOtherCostServiceImpl implements IBuyerOtherCostService {
    @Autowired
    private BuyerOtherCostMapper buyerOtherCostMapper;

    @Autowired
    private BuyerFileInfoMapper buyerFileInfoMapper;

    /**
     * 查询项目其他费用记录
     *
     * @param id 项目其他费用记录主键
     * @return 项目其他费用记录
     */
    @Transactional
    @Override
    public BuyerOtherCost selectBuyerOtherCostById(String id) {
        BuyerOtherCost buyerOtherCost = buyerOtherCostMapper.selectBuyerOtherCostById(id);
        // 查询关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(id);
        buyerOtherCost.setBuyerFileInfos(buyerFileInfos);
        return buyerOtherCost;
    }

    /**
     * 查询项目其他费用记录列表
     *
     * @param buyerOtherCost 项目其他费用记录
     * @return 项目其他费用记录
     */
    @Transactional
    @Override
    public List<BuyerOtherCost> selectBuyerOtherCostList(BuyerOtherCost buyerOtherCost) {
        buyerOtherCost.setDeptId(SecurityUtils.getCompanyId());
        if(null != buyerOtherCost && null != buyerOtherCost.getParams()
                && buyerOtherCost.getParams().containsKey("searchField")){
            if ("projectName".equals(buyerOtherCost.getParams().get("searchField"))) {
                buyerOtherCost.setProjectName(buyerOtherCost.getSearchValue());
            }else if("info".equals(buyerOtherCost.getParams().get("searchField"))){
                buyerOtherCost.setInfo(buyerOtherCost.getSearchValue());
            }
        }
        List<BuyerOtherCost> buyerOtherCosts = buyerOtherCostMapper.selectBuyerOtherCostList(buyerOtherCost);
        // 查询关联文件
        for (BuyerOtherCost otherCost : buyerOtherCosts) {
            List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(otherCost.getId());
            otherCost.setBuyerFileInfos(buyerFileInfos);
        }
        return buyerOtherCosts;
    }

    /**
     * 统计项目其他费用记录列表
     *
     * @param buyerOtherCost 项目其他费用记录
     * @return 项目其他费用记录
     */
    @Transactional
    @Override
    public List<BuyerOtherCost> selectStatistBuyerOtherCostList(BuyerOtherCost buyerOtherCost) {
        buyerOtherCost.setDeptId(SecurityUtils.getCompanyId());
        return buyerOtherCostMapper.selectStatistBuyerOtherCostList(buyerOtherCost);
    }

    /**
     * 新增项目其他费用记录
     *
     * @param buyerOtherCost 项目其他费用记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBuyerOtherCost(BuyerOtherCost buyerOtherCost) {
        buyerOtherCost.setCreateTime(DateUtils.getNowDate());
        buyerOtherCost.setDeptId(SecurityUtils.getCompanyId());
        // 新增关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerOtherCost.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(buyerOtherCost.getId());
            }
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        }
        return buyerOtherCostMapper.insertBuyerOtherCost(buyerOtherCost);
    }

    /**
     * 修改项目其他费用记录
     *
     * @param buyerOtherCost 项目其他费用记录
     * @return 结果
     */
    @Override
    public int updateBuyerOtherCost(BuyerOtherCost buyerOtherCost) {
        buyerOtherCost.setUpdateTime(DateUtils.getNowDate());
        // 修改关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerOtherCost.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(buyerOtherCost.getId());
            }
            buyerFileInfoMapper.deleteBuyerFileInfoByLinkId(buyerOtherCost.getId());
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        }else if (buyerFileInfos != null && buyerFileInfos.size() == 0) {
            buyerFileInfoMapper.deleteBuyerFileInfoByLinkId(buyerOtherCost.getId());
        }
        return buyerOtherCostMapper.updateBuyerOtherCost(buyerOtherCost);
    }

    /**
     * 批量删除项目其他费用记录
     *
     * @param ids 需要删除的项目其他费用记录主键
     * @return 结果
     */
    @Override
    public int deleteBuyerOtherCostByIds(String[] ids) {
        return buyerOtherCostMapper.deleteBuyerOtherCostByIds(ids);
    }

    /**
     * 删除项目其他费用记录信息
     *
     * @param id 项目其他费用记录主键
     * @return 结果
     */
    @Override
    public int deleteBuyerOtherCostById(String id) {
        return buyerOtherCostMapper.deleteBuyerOtherCostById(id);
    }
}
