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.domain.BuyerProject;
import com.ruoyi.buyerapi.mapper.BuyerFileInfoMapper;
import com.ruoyi.buyerapi.mapper.BuyerProjectMapper;
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.BuyerProjectAcceptMapper;
import com.ruoyi.buyerapi.domain.BuyerProjectAccept;
import com.ruoyi.buyerapi.service.IBuyerProjectAcceptService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 立项管理验收资料Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-22
 */
@Service
public class BuyerProjectAcceptServiceImpl implements IBuyerProjectAcceptService {
    @Autowired
    private BuyerProjectAcceptMapper buyerProjectAcceptMapper;

    @Autowired
    private BuyerFileInfoMapper buyerFileInfoMapper;

    @Autowired
    private BuyerProjectMapper buyerProjectMapper;

    /**
     * 查询立项管理验收资料
     *
     * @param id 立项管理验收资料主键
     * @return 立项管理验收资料
     */
    @Transactional
    @Override
    public BuyerProjectAccept selectBuyerProjectAcceptById(String id) {
        BuyerProjectAccept buyerProjectAccept = buyerProjectAcceptMapper.selectBuyerProjectAcceptById(id);
        // 查询关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(id);
        buyerProjectAccept.setBuyerFileInfos(buyerFileInfos);
        return buyerProjectAccept;
    }

    /**
     * 查询立项管理验收资料列表
     *
     * @param buyerProjectAccept 立项管理验收资料
     * @return 立项管理验收资料
     */
    @Transactional
    @Override
    public List<BuyerProjectAccept> selectBuyerProjectAcceptList(BuyerProjectAccept buyerProjectAccept) {
        buyerProjectAccept.setDeptId(SecurityUtils.getCompanyId());
        List<BuyerProjectAccept> buyerProjectAccepts = buyerProjectAcceptMapper.selectBuyerProjectAcceptList(buyerProjectAccept);
        // 查询关联文件
        for (BuyerProjectAccept projectAccept : buyerProjectAccepts) {
            List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(projectAccept.getId());
            projectAccept.setBuyerFileInfos(buyerFileInfos);
        }
        return buyerProjectAccepts;
    }

    /**
     * 新增立项管理验收资料
     *
     * @param buyerProjectAccept 立项管理验收资料
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBuyerProjectAccept(BuyerProjectAccept buyerProjectAccept) {
        buyerProjectAccept.setCreateTime(DateUtils.getNowDate());
        buyerProjectAccept.setDeptId(SecurityUtils.getCompanyId());
        // 新增关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerProjectAccept.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(buyerProjectAccept.getId());
            }
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        }
        return buyerProjectAcceptMapper.insertBuyerProjectAccept(buyerProjectAccept);
    }

    /**
     * 修改立项管理验收资料
     *
     * @param buyerProjectAccept 立项管理验收资料
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBuyerProjectAccept(BuyerProjectAccept buyerProjectAccept) {
        buyerProjectAccept.setUpdateTime(DateUtils.getNowDate());
        // 修改关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerProjectAccept.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(buyerProjectAccept.getId());
            }
            buyerFileInfoMapper.deleteBuyerFileInfoByLinkId(buyerProjectAccept.getId());
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        } else if (buyerFileInfos != null && buyerFileInfos.size() == 0) {
            buyerFileInfoMapper.deleteBuyerFileInfoByLinkId(buyerProjectAccept.getId());
        }
        // 如果项目验收状态改变，修改项目表状态
        if (buyerProjectAccept.getStatus().equals("1")) {
            // 将项目状态改为已完成
            BuyerProject buyerProject = new BuyerProject();
            buyerProject.setId(buyerProjectAccept.getProjectId());
            buyerProject.setStatus("1");
            buyerProjectMapper.updateBuyerProject(buyerProject);
        }else if(buyerProjectAccept.getStatus().equals("2")){
            // 将项目状态改为终止
            BuyerProject buyerProject = new BuyerProject();
            buyerProject.setId(buyerProjectAccept.getProjectId());
            buyerProject.setStatus("3");
            buyerProjectMapper.updateBuyerProject(buyerProject);
        }
        return buyerProjectAcceptMapper.updateBuyerProjectAccept(buyerProjectAccept);
    }

    /**
     * 批量删除立项管理验收资料
     *
     * @param ids 需要删除的立项管理验收资料主键
     * @return 结果
     */
    @Override
    public int deleteBuyerProjectAcceptByIds(String[] ids) {
        return buyerProjectAcceptMapper.deleteBuyerProjectAcceptByIds(ids);
    }

    /**
     * 删除立项管理验收资料信息
     *
     * @param id 立项管理验收资料主键
     * @return 结果
     */
    @Override
    public int deleteBuyerProjectAcceptById(String id) {
        return buyerProjectAcceptMapper.deleteBuyerProjectAcceptById(id);
    }
}
