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.CsIncomeDetailsTemplate;
import com.ruoyi.cs.mapper.CsAnnexMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cs.mapper.CsIncomeDetailsMapper;
import com.ruoyi.cs.domain.CsIncomeDetails;
import com.ruoyi.cs.service.ICsIncomeDetailsService;

import javax.annotation.Resource;

/**
 * 收入明细Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-07
 */
@Service
public class CsIncomeDetailsServiceImpl implements ICsIncomeDetailsService
{
    @Resource
    private CsIncomeDetailsMapper csIncomeDetailsMapper;
    @Resource
    private CsAnnexMapper csAnnexMapper;

    /**
     * 查询收入明细
     *
     * @param incomeDetailsId 收入明细主键
     * @return 收入明细
     */
    @Override
    public CsIncomeDetails selectCsIncomeDetailsByIncomeDetailsId(String incomeDetailsId)
    {
        CsIncomeDetails csIncomeDetails = csIncomeDetailsMapper.selectCsIncomeDetailsByIncomeDetailsId(incomeDetailsId);
        CsAnnex csAnnex = new CsAnnex();
        csAnnex.setResId(incomeDetailsId);
        csAnnex.setDelFlag("0");
        List<CsAnnex> csAnnexList = csAnnexMapper.selectCsAnnexList(csAnnex);
        csIncomeDetails.setAnnexList(csAnnexList);
        return csIncomeDetails;
    }

    /**
     * 查询收入明细列表
     *
     * @param csIncomeDetails 收入明细
     * @return 收入明细
     */
    @Override
    public List<CsIncomeDetails> selectCsIncomeDetailsList(CsIncomeDetails csIncomeDetails)
    {
        return csIncomeDetailsMapper.selectCsIncomeDetailsList(csIncomeDetails);
    }

    /**
     * 新增收入明细
     *
     * @param csIncomeDetails 收入明细
     * @return 结果
     */
    @Override
    public int insertCsIncomeDetails(CsIncomeDetails csIncomeDetails)
    {
        String idStr = IdUtil.getSnowflakeNextIdStr();
        csIncomeDetails.setIncomeDetailsId(idStr);
        csIncomeDetails.setCreateAt(DateUtils.getNowDate());
        csIncomeDetails.setDelFlag("0");

        List<CsAnnex> annexList = csIncomeDetails.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 csIncomeDetailsMapper.insertCsIncomeDetails(csIncomeDetails);
    }

    /**
     * 修改收入明细
     *
     * @param csIncomeDetails 收入明细
     * @return 结果
     */
    @Override
    public int updateCsIncomeDetails(CsIncomeDetails csIncomeDetails)
    {
        csAnnexMapper.batchDeleteCsAnnexByResId(csIncomeDetails.getIncomeDetailsId());
        List<CsAnnex> annexList = csIncomeDetails.getAnnexList();
        if (!CollectionUtils.isEmpty(annexList) && annexList.size() > 0) {
            for (CsAnnex csAnnex : annexList) {
                csAnnex.setAnnexId(IdUtil.getSnowflakeNextIdStr());
                csAnnex.setResId(csIncomeDetails.getIncomeDetailsId());
                csAnnex.setResType("2");
            }
            csAnnexMapper.batchInsertCsAnnexList(annexList);
        }
        csIncomeDetails.setUpdateAt(DateUtils.getNowDate());
        return csIncomeDetailsMapper.updateCsIncomeDetails(csIncomeDetails);
    }

    /**
     * 批量删除收入明细
     *
     * @param incomeDetailsIds 需要删除的收入明细主键
     * @return 结果
     */
    @Override
    public int deleteCsIncomeDetailsByIncomeDetailsIds(String[] incomeDetailsIds)
    {
        return csIncomeDetailsMapper.deleteCsIncomeDetailsByIncomeDetailsIds(incomeDetailsIds);
    }

    /**
     * 删除收入明细信息
     *
     * @param incomeDetailsId 收入明细主键
     * @return 结果
     */
    @Override
    public int deleteCsIncomeDetailsByIncomeDetailsId(String incomeDetailsId)
    {
        return csIncomeDetailsMapper.deleteCsIncomeDetailsByIncomeDetailsId(incomeDetailsId);
    }

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

        List<CsIncomeDetailsTemplate> insertCsIncomeDetailsList = new ArrayList<>();
        List<CsIncomeDetailsTemplate> updateCsIncomeDetailsList = new ArrayList<>();

        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (CsIncomeDetailsTemplate item : list)
        {
            try
            {
                item.setIncomeDetailsId(IdUtil.getSnowflakeNextIdStr());
                if (item.getCreateAt() == null) {
                    item.setCreateAt(DateUtils.getNowDate());
                }
                item.setDelFlag("0");
                insertCsIncomeDetailsList.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 (insertCsIncomeDetailsList.size() > 0) {
            this.insertCsIncomeDetailsBatch(insertCsIncomeDetailsList);
        }

        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 insertCsIncomeDetailsBatch(List<CsIncomeDetailsTemplate> list) {
        return csIncomeDetailsMapper.insertCsIncomeDetailsBatch(list);
    }
}
