package com.ruoyi.buyerapi.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.buyerapi.domain.*;
import com.ruoyi.buyerapi.mapper.*;
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.service.IBuyerProjectService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 立项管理Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-22
 */
@Service
public class BuyerProjectServiceImpl implements IBuyerProjectService {
    @Autowired
    private BuyerProjectMapper buyerProjectMapper;

    @Autowired
    private BuyerProjectMemberMapper buyerProjectMemberMapper;

    @Autowired
    private BuyerFileInfoMapper buyerFileInfoMapper;

    @Autowired
    private BuyerVoucherProjectMapper buyerVoucherProjectMapper;

    @Autowired
    private BuyerProcessFileMapper buyerProcessFileMapper;

    @Autowired
    private BuyerProjectAcceptMapper buyerProjectAcceptMapper;

    @Autowired
    private BuyerDeptEmployeeServiceImpl buyerDeptEmployeeService;

    /**
     * 查询立项管理
     *
     * @param id 立项管理主键
     * @return 立项管理
     */
    @Transactional
    @Override
    public BuyerProject selectBuyerProjectById(String id) {
        BuyerProject buyerProject = buyerProjectMapper.selectBuyerProjectById(id);
        // 查询项目组成员
        BuyerProjectMember buyerProjectMember = new BuyerProjectMember();
        buyerProjectMember.setProjectId(buyerProject.getId());
        buyerProject.setBuyerProjectMemberList(buyerProjectMemberMapper.selectBuyerProjectMemberList(buyerProjectMember));
        List<BuyerProjectMember> list = buyerProjectMemberMapper.selectBuyerProjectMemberList(buyerProjectMember);
        List<String> buyerProjectMemberArray = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (BuyerProjectMember projectMember : list) {
                buyerProjectMemberArray.add(projectMember.getDeptEmployeeId());
            }
        }
        buyerProject.setBuyerProjectMemberArray(buyerProjectMemberArray);
        // 查询关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(id);
        buyerProject.setBuyerFileInfos(buyerFileInfos);
        return buyerProject;
    }

    /**
     * 查询立项管理列表
     *
     * @param buyerProject 立项管理
     * @return 立项管理
     */
    @Transactional
    @Override
    public List<BuyerProject> selectBuyerProjectList(BuyerProject buyerProject) {
        buyerProject.setCompanyId(SecurityUtils.getCompanyId());
        List<BuyerProject> buyerProjects = buyerProjectMapper.selectBuyerProjectList(buyerProject);
        for (BuyerProject project : buyerProjects) {
            // 查询项目成员
            BuyerProjectMember buyerProjectMember = new BuyerProjectMember();
            buyerProjectMember.setProjectId(project.getId());
            project.setBuyerProjectMemberList(buyerProjectMemberMapper.selectBuyerProjectMemberList(buyerProjectMember));
            List<BuyerProjectMember> list = buyerProjectMemberMapper.selectBuyerProjectMemberList(buyerProjectMember);
            List<String> buyerProjectMemberArray = new ArrayList<>();
            if (list != null && list.size() > 0) {
                for (BuyerProjectMember projectMember : list) {
                    buyerProjectMemberArray.add(projectMember.getDeptEmployeeId());
                }
            }
            project.setBuyerProjectMemberArray(buyerProjectMemberArray);
            // 查询关联文件
            List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(project.getId());
            project.setBuyerFileInfos(buyerFileInfos);
        }
        return buyerProjects;
    }

    /**
     * 查询项目关联过程文件
     *
     * @param buyerProject 立项管理
     * @return 立项管理
     */
    @Transactional
    @Override
    public List<BuyerProject> selectBuyerProjectProcessFileList(BuyerProject buyerProject) {
        if(StrUtil.isEmpty(SecurityUtils.getCompanyId())){
            return new ArrayList<>();
        }
        buyerProject.setCompanyId(SecurityUtils.getCompanyId());
        return buyerProjectMapper.selectBuyerProjectProcessFileList(buyerProject);
    }

    /**
     * 查询项目关联验收资料
     *
     * @param buyerProject 立项管理
     * @return 立项管理
     */
    @Transactional
    @Override
    public List<BuyerProject> selectBuyerProjectAcceptFileList(BuyerProject buyerProject) {
        buyerProject.setCompanyId(SecurityUtils.getCompanyId());
        return buyerProjectMapper.selectBuyerProjectAcceptFileList(buyerProject);
    }

    @Override
    public List<BuyerProject> listForVoucher(BuyerProject buyerProject) {
        buyerProject.setCompanyId(SecurityUtils.getCompanyId());
        List<BuyerProject> buyerProjects = buyerProjectMapper.listForVoucher(buyerProject);
        for (BuyerProject project : buyerProjects) {
            // 查询项目组成员
            BuyerProjectMember buyerProjectMember = new BuyerProjectMember();
            buyerProjectMember.setProjectId(project.getId());
            project.setBuyerProjectMemberList(buyerProjectMemberMapper.selectBuyerProjectMemberList(buyerProjectMember));

            // 关联凭证数
            BuyerVoucherProject buyerVoucherProject = new BuyerVoucherProject();
            buyerVoucherProject.setProjectId(project.getId());
            project.setVoucherProjects(buyerVoucherProjectMapper.selectBuyerVoucherProjectList(buyerVoucherProject));
        }
        return buyerProjects;
    }

    /**
     * 新增立项管理
     *
     * @param buyerProject 立项管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBuyerProject(BuyerProject buyerProject) {
        buyerProject.setCreateTime(DateUtils.getNowDate());
        buyerProject.setUpdateTime(DateUtils.getNowDate());
        buyerProject.setCompanyId(SecurityUtils.getCompanyId());
        // 新增项目组成员
        List<String> buyerProjectMemberArray = buyerProject.getBuyerProjectMemberArray();
        if (buyerProjectMemberArray != null && buyerProjectMemberArray.size() > 0) {
            for (String buyerProjectMemberId : buyerProjectMemberArray) {
                BuyerProjectMember buyerProjectMemberEntity = new BuyerProjectMember();
                buyerProjectMemberEntity.setId(IdUtil.simpleUUID());
                buyerProjectMemberEntity.setProjectId(buyerProject.getId());
                buyerProjectMemberEntity.setDeptEmployeeId(buyerProjectMemberId);
                buyerProjectMemberMapper.insertBuyerProjectMember(buyerProjectMemberEntity);
            }
        }
        // 新增关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerProject.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(buyerProject.getId());
            }
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        }
        // 新增过程文件
        BuyerProcessFile buyerProcessFile = new BuyerProcessFile();
        buyerProcessFile.setId(IdUtil.simpleUUID());
        buyerProcessFile.setProjectId(buyerProject.getId());
        buyerProcessFile.setDeptId(SecurityUtils.getCompanyId());
        buyerProcessFile.setCreateTime(DateUtils.getNowDate());
        buyerProcessFile.setCreateBy(SecurityUtils.getUsername());
        buyerProcessFileMapper.insertBuyerProcessFile(buyerProcessFile);
        // 新增验收资料
        BuyerProjectAccept buyerProjectAccept = new BuyerProjectAccept();
        buyerProjectAccept.setId(IdUtil.simpleUUID());
        buyerProjectAccept.setProjectId(buyerProject.getId());
        buyerProjectAccept.setDeptId(SecurityUtils.getCompanyId());
        buyerProjectAccept.setCreateTime(DateUtils.getNowDate());
        buyerProjectAccept.setCreateBy(SecurityUtils.getUsername());
        buyerProjectAcceptMapper.insertBuyerProjectAccept(buyerProjectAccept);
        // 新增立项管理
        // 设置项目中间时间
        Long days = DateUtil.between(buyerProject.getStartDate(), buyerProject.getEndDate(), DateUnit.DAY);
        Date middleDate = DateUtil.offset(buyerProject.getStartDate(), DateField.DAY_OF_YEAR, Integer.parseInt(days.toString()) / 2);
        buyerProject.setMiddleDate(middleDate);
        return buyerProjectMapper.insertBuyerProject(buyerProject);
    }

    /**
     * 修改立项管理
     *
     * @param buyerProject 立项管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBuyerProject(BuyerProject buyerProject) {
        buyerProject.setUpdateTime(DateUtils.getNowDate());
        // 删除原项目组成员
        buyerProjectMemberMapper.deleteBuyerProjectMemberByProjectId(buyerProject.getId());
        // 新增项目组成员
        List<String> buyerProjectMemberArray = buyerProject.getBuyerProjectMemberArray();
        if (buyerProjectMemberArray != null && buyerProjectMemberArray.size() > 0) {
            for (String buyerProjectMemberId : buyerProjectMemberArray) {
                BuyerProjectMember buyerProjectMemberEntity = new BuyerProjectMember();
                buyerProjectMemberEntity.setId(IdUtil.simpleUUID());
                buyerProjectMemberEntity.setProjectId(buyerProject.getId());
                buyerProjectMemberEntity.setDeptEmployeeId(buyerProjectMemberId);
                buyerProjectMemberMapper.insertBuyerProjectMember(buyerProjectMemberEntity);
            }
        }
        // 修改关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerProject.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(buyerProject.getId());
            }
            buyerFileInfoMapper.deleteBuyerFileInfoByLinkId(buyerProject.getId());
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        } else if (buyerFileInfos != null && buyerFileInfos.size() == 0) {
            buyerFileInfoMapper.deleteBuyerFileInfoByLinkId(buyerProject.getId());
        }
        // 修改项目信息
        // 修改项目中间时间
        Long days = DateUtil.between(buyerProject.getStartDate(), buyerProject.getEndDate(), DateUnit.DAY);
        Date middleDate = DateUtil.offset(buyerProject.getStartDate(), DateField.DAY_OF_YEAR, Integer.parseInt(days.toString()) / 2);
        buyerProject.setMiddleDate(middleDate);
        return buyerProjectMapper.updateBuyerProject(buyerProject);
    }

    /**
     * 批量删除立项管理
     *
     * @param ids 需要删除的立项管理主键
     * @return 结果
     */
    @Override
    public int deleteBuyerProjectByIds(String[] ids) {
        int result = buyerProjectMapper.deleteBuyerProjectByIds(ids);
        // 删除相关的过程文件和验收资料
        if(result >= 0){
            for(String id : ids){
                buyerProcessFileMapper.deleteBuyerProcessFileByProjectId(id);
                buyerProjectAcceptMapper.deleteBuyerProjectAcceptById(id);
            }
        }
        return result;
    }

    /**
     * 删除立项管理信息
     *
     * @param id 立项管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBuyerProjectById(String id) {
        // 删除过程文件
        buyerProcessFileMapper.deleteBuyerProcessFileByProjectId(id);
        // 删除验收资料
        buyerProjectAcceptMapper.deleteBuyerProjectAcceptByProjectId(id);
        // 删除项目
        return buyerProjectMapper.deleteBuyerProjectById(id);
    }
}
