package com.ruoyi.cs.service.impl;

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

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.cs.domain.CsAnnex;
import com.ruoyi.cs.domain.template.CsContractDetailsTemplate;
import com.ruoyi.cs.domain.vo.CsAnnexApplyVO;
import com.ruoyi.cs.mapper.CsAnnexMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.cs.mapper.CsContractDetailsMapper;
import com.ruoyi.cs.domain.CsContractDetails;
import com.ruoyi.cs.service.ICsContractDetailsService;

import javax.annotation.Resource;

/**
 * 合同信息详情Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-29
 */
@Service
public class CsContractDetailsServiceImpl implements ICsContractDetailsService
{
    @Resource
    private CsContractDetailsMapper csContractDetailsMapper;
    @Resource
    private CsAnnexMapper csAnnexMapper;

    /**
     * 查询合同信息详情
     *
     * @param contractDetailsId 合同信息详情主键
     * @return 合同信息详情
     */
    @Override
    public CsContractDetails selectCsContractDetailsByContractDetailsId(String contractDetailsId)
    {
        CsContractDetails csContractDetails = csContractDetailsMapper.selectCsContractDetailsByContractDetailsId(contractDetailsId);
        CsAnnex csAnnex = new CsAnnex();
        csAnnex.setResId(contractDetailsId);
        csAnnex.setDelFlag("0");
        List<CsAnnex> csAnnexList = csAnnexMapper.selectCsAnnexList(csAnnex);
        csContractDetails.setAnnexList(csAnnexList);
        return csContractDetails;
    }

    /**
     * 查询合同信息详情列表
     *
     * @param csContractDetails 合同信息详情
     * @return 合同信息详情
     */
    @Override
    public List<CsContractDetails> selectCsContractDetailsList(CsContractDetails csContractDetails)
    {
        return csContractDetailsMapper.selectCsContractDetailsList(csContractDetails);
    }

    /**
     * 新增合同信息详情
     *
     * @param csContractDetails 合同信息详情
     * @return 结果
     */
    @Override
    public int insertCsContractDetails(CsContractDetails csContractDetails)
    {
        String idStr = IdUtil.getSnowflakeNextIdStr();
        csContractDetails.setContractDetailsId(idStr);
        csContractDetails.setCreateAt(DateUtils.getTime());
        csContractDetails.setDelFlag("0");
        csContractDetails.setStatus("0");// 默认正常
        List<CsAnnex> annexList = csContractDetails.getAnnexList();
        if (!CollectionUtils.isEmpty(annexList) && annexList.size() > 0) {
            for (CsAnnex csAnnex : annexList) {
                csAnnex.setAnnexId(IdUtil.getSnowflakeNextIdStr());
                csAnnex.setResId(idStr);
                csAnnex.setResType("2");
            }
            csAnnexMapper.batchInsertCsAnnexList(annexList);
        }
        return csContractDetailsMapper.insertCsContractDetails(csContractDetails);
    }

    /**
     * 修改合同信息详情
     *
     * @param csContractDetails 合同信息详情
     * @return 结果
     */
    @Override
    public int updateCsContractDetails(CsContractDetails csContractDetails)
    {
        csAnnexMapper.batchDeleteCsAnnexByResId(csContractDetails.getContractDetailsId());
        List<CsAnnex> annexList = csContractDetails.getAnnexList();
        if (!CollectionUtils.isEmpty(annexList) && annexList.size() > 0) {
            for (CsAnnex csAnnex : annexList) {
                csAnnex.setAnnexId(IdUtil.getSnowflakeNextIdStr());
                csAnnex.setResId(csContractDetails.getContractDetailsId());
                csAnnex.setResType("2");
            }
            csAnnexMapper.batchInsertCsAnnexList(annexList);
        }
        csContractDetails.setUpdateAt(DateUtils.getTime());
        return csContractDetailsMapper.updateCsContractDetails(csContractDetails);
    }

    /**
     * 批量删除合同信息详情
     *
     * @param contractDetailsIds 需要删除的合同信息详情主键
     * @return 结果
     */
    @Override
    public int deleteCsContractDetailsByContractDetailsIds(String[] contractDetailsIds)
    {
        return csContractDetailsMapper.deleteCsContractDetailsByContractDetailsIds(contractDetailsIds);
    }

    /**
     * 删除合同信息详情信息
     *
     * @param contractDetailsId 合同信息详情主键
     * @return 结果
     */
    @Override
    public int deleteCsContractDetailsByContractDetailsId(String contractDetailsId)
    {
        return csContractDetailsMapper.deleteCsContractDetailsByContractDetailsId(contractDetailsId);
    }

    /** 导入合同信息记录
     *
     * @param list
     * @param updateSupport
     * @return
     */
    @Override
    public String importCsContractDetails(List<CsContractDetailsTemplate> list, boolean updateSupport) {
        if (StringUtils.isNull(list) || list.size() == 0)
        {
            throw new ServiceException("导入合同信息记录不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;

        List<CsContractDetailsTemplate> insertCsContractDetailsList = new ArrayList<>();
        List<CsContractDetailsTemplate> updateCsContractDetailsList = new ArrayList<>();

        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (CsContractDetailsTemplate item : list)
        {
            try
            {
                item.setContractDetailsId(IdUtil.getSnowflakeNextIdStr());
                item.setCreateAt(DateUtils.getTime());
                item.setDelFlag("0");
                insertCsContractDetailsList.add(item);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("合同信息记录").append(item.getDepartmentContractNumber()).append(" 导入成功");

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "合同信息记录" + item.getDepartmentContractNumber() + " 导入失败：";
                failureMsg.append(msg).append(e.getMessage());
            }
        }

        if (insertCsContractDetailsList.size() > 0) {
            this.insertCsContractDetailsBatch(insertCsContractDetailsList);
        }

        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 批量新增合同信息记录
     *
     * @param list 合同信息记录
     * @return 结果
     */
    @Override
    public int insertCsContractDetailsBatch(List<CsContractDetailsTemplate> list) {
        return csContractDetailsMapper.insertCsContractDetailsBatch(list);
    }
}
