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.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.BuyerProcessFileMapper;
import com.ruoyi.buyerapi.domain.BuyerProcessFile;
import com.ruoyi.buyerapi.service.IBuyerProcessFileService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目管理过程文件Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-22
 */
@Service
public class BuyerProcessFileServiceImpl implements IBuyerProcessFileService {
    @Autowired
    private BuyerProcessFileMapper buyerProcessFileMapper;

    @Autowired
    private BuyerFileInfoMapper buyerFileInfoMapper;

    /**
     * 查询项目管理过程文件
     *
     * @param id 项目管理过程文件主键
     * @return 项目管理过程文件
     */
    @Transactional
    @Override
    public BuyerProcessFile selectBuyerProcessFileById(String id) {
        BuyerProcessFile buyerProcessFile = buyerProcessFileMapper.selectBuyerProcessFileById(id);
        // 查询关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(id);
        buyerProcessFile.setBuyerFileInfos(buyerFileInfos);
        return buyerProcessFile;
    }

    /**
     * 查询项目管理过程文件列表
     *
     * @param buyerProcessFile 项目管理过程文件
     * @return 项目管理过程文件
     */
    @Transactional
    @Override
    public List<BuyerProcessFile> selectBuyerProcessFileList(BuyerProcessFile buyerProcessFile) {
        buyerProcessFile.setDeptId(SecurityUtils.getCompanyId());
        List<BuyerProcessFile> buyerProcessFiles = buyerProcessFileMapper.selectBuyerProcessFileList(buyerProcessFile);
        for (BuyerProcessFile processFile : buyerProcessFiles) {
            List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(processFile.getId());
            processFile.setBuyerFileInfos(buyerFileInfos);
        }
        return buyerProcessFiles;
    }

    /**
     * 新增项目管理过程文件
     *
     * @param buyerProcessFile 项目管理过程文件
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBuyerProcessFile(BuyerProcessFile buyerProcessFile) {
        buyerProcessFile.setCreateTime(DateUtils.getNowDate());
        buyerProcessFile.setDeptId(SecurityUtils.getCompanyId());
        // 新增关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerProcessFile.getBuyerFileInfos();
        if (buyerFileInfos != null && buyerFileInfos.size() > 0) {
            for (BuyerFileInfo buyerFileInfo : buyerFileInfos) {
                buyerFileInfo.setId(IdUtil.simpleUUID());
                buyerFileInfo.setCreateTime(DateUtils.getNowDate());
                buyerFileInfo.setCreateBy(SecurityUtils.getUsername());
                buyerFileInfo.setUpdateTime(DateUtils.getNowDate());
                buyerFileInfo.setUpdateBy(SecurityUtils.getUsername());
                buyerFileInfo.setLinkId(buyerProcessFile.getId());
            }
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        }
        return buyerProcessFileMapper.insertBuyerProcessFile(buyerProcessFile);
    }

    /**
     * 修改项目管理过程文件
     *
     * @param buyerProcessFile 项目管理过程文件
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBuyerProcessFile(BuyerProcessFile buyerProcessFile) {
        buyerProcessFile.setUpdateTime(DateUtils.getNowDate());
        // 新增关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerProcessFile.getBuyerFileInfos();
        if (buyerFileInfos != null && buyerFileInfos.size() > 0) {
            for (BuyerFileInfo buyerFileInfo : buyerFileInfos) {
                buyerFileInfo.setId(IdUtil.simpleUUID());
                buyerFileInfo.setCreateTime(DateUtils.getNowDate());
                buyerFileInfo.setCreateBy(SecurityUtils.getUsername());
                buyerFileInfo.setUpdateTime(DateUtils.getNowDate());
                buyerFileInfo.setUpdateBy(SecurityUtils.getUsername());
                buyerFileInfo.setLinkId(buyerProcessFile.getId());
            }
            buyerFileInfoMapper.deleteBuyerFileInfoByLinkId(buyerProcessFile.getId());
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        }
        return buyerProcessFileMapper.updateBuyerProcessFile(buyerProcessFile);
    }

    /**
     * 批量删除项目管理过程文件
     *
     * @param ids 需要删除的项目管理过程文件主键
     * @return 结果
     */
    @Override
    public int deleteBuyerProcessFileByIds(String[] ids) {
        return buyerProcessFileMapper.deleteBuyerProcessFileByIds(ids);
    }

    /**
     * 删除项目管理过程文件信息
     *
     * @param id 项目管理过程文件主键
     * @return 结果
     */
    @Override
    public int deleteBuyerProcessFileById(String id) {
        return buyerProcessFileMapper.deleteBuyerProcessFileById(id);
    }
}
