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.BuyerDeptMaterialMapper;
import com.ruoyi.buyerapi.domain.BuyerDeptMaterial;
import com.ruoyi.buyerapi.service.IBuyerDeptMaterialService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目材料领用记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-22
 */
@Service
public class BuyerDeptMaterialServiceImpl implements IBuyerDeptMaterialService {
    @Autowired
    private BuyerDeptMaterialMapper buyerDeptMaterialMapper;

    @Autowired
    private BuyerFileInfoMapper buyerFileInfoMapper;

    /**
     * 查询项目材料领用记录
     *
     * @param id 项目材料领用记录主键
     * @return 项目材料领用记录
     */
    @Transactional
    @Override
    public BuyerDeptMaterial selectBuyerDeptMaterialById(String id) {
        BuyerDeptMaterial buyerDeptMaterial = buyerDeptMaterialMapper.selectBuyerDeptMaterialById(id);
        // 查询关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(id);
        buyerDeptMaterial.setBuyerFileInfos(buyerFileInfos);
        return buyerDeptMaterial;
    }

    /**
     * 查询项目材料领用记录列表
     *
     * @param buyerDeptMaterial 项目材料领用记录
     * @return 项目材料领用记录
     */
    @Transactional
    @Override
    public List<BuyerDeptMaterial> selectBuyerDeptMaterialList(BuyerDeptMaterial buyerDeptMaterial) {
        buyerDeptMaterial.setDeptId(SecurityUtils.getCompanyId());
        List<BuyerDeptMaterial> buyerDeptMaterials = buyerDeptMaterialMapper.selectBuyerDeptMaterialList(buyerDeptMaterial);
        // 查询关联文件
        for (BuyerDeptMaterial deptMaterial : buyerDeptMaterials) {
            List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(deptMaterial.getId());
            deptMaterial.setBuyerFileInfos(buyerFileInfos);
        }
        return buyerDeptMaterials;
    }

    /**
     * 统计项目材料领用记录列表
     *
     * @param buyerDeptMaterial 项目材料领用记录
     * @return 项目材料领用记录
     */
    @Override
    public List<BuyerDeptMaterial> selectStatistBuyerDeptMaterialList(BuyerDeptMaterial buyerDeptMaterial) {
        buyerDeptMaterial.setDeptId(SecurityUtils.getCompanyId());
        return buyerDeptMaterialMapper.selectStatistBuyerDeptMaterialList(buyerDeptMaterial);
    }

    /**
     * 新增项目材料领用记录
     *
     * @param buyerDeptMaterial 项目材料领用记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBuyerDeptMaterial(BuyerDeptMaterial buyerDeptMaterial) {
        buyerDeptMaterial.setCreateTime(DateUtils.getNowDate());
        buyerDeptMaterial.setDeptId(SecurityUtils.getCompanyId());
        // 新增关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerDeptMaterial.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(buyerDeptMaterial.getId());
            }
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        }
        return buyerDeptMaterialMapper.insertBuyerDeptMaterial(buyerDeptMaterial);
    }

    /**
     * 修改项目材料领用记录
     *
     * @param buyerDeptMaterial 项目材料领用记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBuyerDeptMaterial(BuyerDeptMaterial buyerDeptMaterial) {
        buyerDeptMaterial.setUpdateTime(DateUtils.getNowDate());
        // 修改关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerDeptMaterial.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(buyerDeptMaterial.getId());
            }
            buyerFileInfoMapper.deleteBuyerFileInfoByLinkId(buyerDeptMaterial.getId());
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        }
        else if (buyerFileInfos != null && buyerFileInfos.size() == 0) {
            buyerFileInfoMapper.deleteBuyerFileInfoByLinkId(buyerDeptMaterial.getId());
        }
        return buyerDeptMaterialMapper.updateBuyerDeptMaterial(buyerDeptMaterial);
    }

    /**
     * 批量删除项目材料领用记录
     *
     * @param ids 需要删除的项目材料领用记录主键
     * @return 结果
     */
    @Override
    public int deleteBuyerDeptMaterialByIds(String[] ids) {
        return buyerDeptMaterialMapper.deleteBuyerDeptMaterialByIds(ids);
    }

    /**
     * 删除项目材料领用记录信息
     *
     * @param id 项目材料领用记录主键
     * @return 结果
     */
    @Override
    public int deleteBuyerDeptMaterialById(String id) {
        return buyerDeptMaterialMapper.deleteBuyerDeptMaterialById(id);
    }
}
