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.BuyerOutsourcingMapper;
import com.ruoyi.buyerapi.domain.BuyerOutsourcing;
import com.ruoyi.buyerapi.service.IBuyerOutsourcingService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 委外研发记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-22
 */
@Service
public class BuyerOutsourcingServiceImpl implements IBuyerOutsourcingService {
    @Autowired
    private BuyerOutsourcingMapper buyerOutsourcingMapper;

    @Autowired
    private BuyerFileInfoMapper buyerFileInfoMapper;

    /**
     * 查询委外研发记录
     *
     * @param id 委外研发记录主键
     * @return 委外研发记录
     */
    @Transactional
    @Override
    public BuyerOutsourcing selectBuyerOutsourcingById(String id) {
        BuyerOutsourcing buyerOutsourcing = buyerOutsourcingMapper.selectBuyerOutsourcingById(id);
        // 查询关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(id);
        buyerOutsourcing.setBuyerFileInfos(buyerFileInfos);
        return buyerOutsourcing;
    }

    /**
     * 查询委外研发记录列表
     *
     * @param buyerOutsourcing 委外研发记录
     * @return 委外研发记录
     */
    @Transactional
    @Override
    public List<BuyerOutsourcing> selectBuyerOutsourcingList(BuyerOutsourcing buyerOutsourcing) {
        buyerOutsourcing.setDeptId(SecurityUtils.getCompanyId());
        List<BuyerOutsourcing> buyerOutsourcings = buyerOutsourcingMapper.selectBuyerOutsourcingList(buyerOutsourcing);
        // 查询关联文件
        for (BuyerOutsourcing outsourcing : buyerOutsourcings) {
            List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(outsourcing.getId());
            outsourcing.setBuyerFileInfos(buyerFileInfos);
        }
        return buyerOutsourcings;
    }

    /**
     * 统计委外研发记录列表
     *
     * @param buyerOutsourcing 委外研发记录
     * @return 委外研发记录
     */
    @Override
    public List<BuyerOutsourcing> selectStatistBuyerOutsourcingList(BuyerOutsourcing buyerOutsourcing) {
        buyerOutsourcing.setDeptId(SecurityUtils.getCompanyId());
        return buyerOutsourcingMapper.selectStatistBuyerOutsourcingList(buyerOutsourcing);
    }

    /**
     * 新增委外研发记录
     *
     * @param buyerOutsourcing 委外研发记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBuyerOutsourcing(BuyerOutsourcing buyerOutsourcing) {
        buyerOutsourcing.setCreateTime(DateUtils.getNowDate());
        buyerOutsourcing.setDeptId(SecurityUtils.getCompanyId());
        // 新增关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerOutsourcing.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(buyerOutsourcing.getId());
            }
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        }
        return buyerOutsourcingMapper.insertBuyerOutsourcing(buyerOutsourcing);
    }

    /**
     * 修改委外研发记录
     *
     * @param buyerOutsourcing 委外研发记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBuyerOutsourcing(BuyerOutsourcing buyerOutsourcing) {
        buyerOutsourcing.setUpdateTime(DateUtils.getNowDate());
        // 修改关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerOutsourcing.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(buyerOutsourcing.getId());
            }
            buyerFileInfoMapper.deleteBuyerFileInfoByLinkId(buyerOutsourcing.getId());
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        }else if (buyerFileInfos != null && buyerFileInfos.size() == 0) {
            buyerFileInfoMapper.deleteBuyerFileInfoByLinkId(buyerOutsourcing.getId());
        }
        return buyerOutsourcingMapper.updateBuyerOutsourcing(buyerOutsourcing);
    }

    /**
     * 批量删除委外研发记录
     *
     * @param ids 需要删除的委外研发记录主键
     * @return 结果
     */
    @Override
    public int deleteBuyerOutsourcingByIds(String[] ids) {
        return buyerOutsourcingMapper.deleteBuyerOutsourcingByIds(ids);
    }

    /**
     * 删除委外研发记录信息
     *
     * @param id 委外研发记录主键
     * @return 结果
     */
    @Override
    public int deleteBuyerOutsourcingById(String id) {
        return buyerOutsourcingMapper.deleteBuyerOutsourcingById(id);
    }
}
