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.template.CsExpandInteractTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cs.mapper.CsExpandInteractMapper;
import com.ruoyi.cs.domain.CsExpandInteract;
import com.ruoyi.cs.service.ICsExpandInteractService;

import javax.annotation.Resource;

/**
 * 拓展交流Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-07
 */
@Service
public class CsExpandInteractServiceImpl implements ICsExpandInteractService
{
    @Resource
    private CsExpandInteractMapper csExpandInteractMapper;

    /**
     * 查询拓展交流
     *
     * @param expandInteractId 拓展交流主键
     * @return 拓展交流
     */
    @Override
    public CsExpandInteract selectCsExpandInteractByExpandInteractId(String expandInteractId)
    {
        return csExpandInteractMapper.selectCsExpandInteractByExpandInteractId(expandInteractId);
    }

    /**
     * 查询拓展交流列表
     *
     * @param csExpandInteract 拓展交流
     * @return 拓展交流
     */
    @Override
    public List<CsExpandInteract> selectCsExpandInteractList(CsExpandInteract csExpandInteract)
    {
        return csExpandInteractMapper.selectCsExpandInteractList(csExpandInteract);
    }

    /**
     * 新增拓展交流
     *
     * @param csExpandInteract 拓展交流
     * @return 结果
     */
    @Override
    public int insertCsExpandInteract(CsExpandInteract csExpandInteract)
    {
        csExpandInteract.setExpandInteractId(IdUtil.getSnowflakeNextIdStr());
        csExpandInteract.setCreateAt(DateUtils.getNowDate());
        csExpandInteract.setDelFlag("0");
        return csExpandInteractMapper.insertCsExpandInteract(csExpandInteract);
    }

    /**
     * 修改拓展交流
     *
     * @param csExpandInteract 拓展交流
     * @return 结果
     */
    @Override
    public int updateCsExpandInteract(CsExpandInteract csExpandInteract)
    {
        csExpandInteract.setUpdateAt(DateUtils.getNowDate());
        return csExpandInteractMapper.updateCsExpandInteract(csExpandInteract);
    }

    /**
     * 批量删除拓展交流
     *
     * @param expandInteractIds 需要删除的拓展交流主键
     * @return 结果
     */
    @Override
    public int deleteCsExpandInteractByExpandInteractIds(String[] expandInteractIds)
    {
        return csExpandInteractMapper.deleteCsExpandInteractByExpandInteractIds(expandInteractIds);
    }

    /**
     * 删除拓展交流信息
     *
     * @param expandInteractId 拓展交流主键
     * @return 结果
     */
    @Override
    public int deleteCsExpandInteractByExpandInteractId(String expandInteractId)
    {
        return csExpandInteractMapper.deleteCsExpandInteractByExpandInteractId(expandInteractId);
    }

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

        List<CsExpandInteractTemplate> insertCsExpandInteractList = new ArrayList<>();
        List<CsExpandInteractTemplate> updateCsExpandInteractList = new ArrayList<>();

        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (CsExpandInteractTemplate item : list)
        {
            try
            {
                item.setExpandInteractId(IdUtil.getSnowflakeNextIdStr());
                item.setCreateAt(DateUtils.getNowDate());
                item.setDelFlag("0");
                insertCsExpandInteractList.add(item);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("拓展交流记录").append(item.getCompanyContacts()).append(" 导入成功");

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "拓展交流记录" + item.getCompanyContacts() + " 导入失败：";
                failureMsg.append(msg).append(e.getMessage());
            }
        }

        if (insertCsExpandInteractList.size() > 0) {
            this.insertCsExpandInteractBatch(insertCsExpandInteractList);
        }

        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 insertCsExpandInteractBatch(List<CsExpandInteractTemplate> list) {
        return csExpandInteractMapper.insertCsExpandInteractBatch(list);
    }
}
