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.CsCustomerFollowTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cs.mapper.CsCustomerFollowMapper;
import com.ruoyi.cs.domain.CsCustomerFollow;
import com.ruoyi.cs.service.ICsCustomerFollowService;

import javax.annotation.Resource;

/**
 * 回访客户记录Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-07
 */
@Service
public class CsCustomerFollowServiceImpl implements ICsCustomerFollowService
{
    @Resource
    private CsCustomerFollowMapper csCustomerFollowMapper;

    /**
     * 查询回访客户记录
     *
     * @param customerFollowId 回访客户记录主键
     * @return 回访客户记录
     */
    @Override
    public CsCustomerFollow selectCsCustomerFollowByCustomerFollowId(String customerFollowId)
    {
        return csCustomerFollowMapper.selectCsCustomerFollowByCustomerFollowId(customerFollowId);
    }

    /**
     * 查询回访客户记录列表
     *
     * @param csCustomerFollow 回访客户记录
     * @return 回访客户记录
     */
    @Override
    public List<CsCustomerFollow> selectCsCustomerFollowList(CsCustomerFollow csCustomerFollow)
    {
        return csCustomerFollowMapper.selectCsCustomerFollowList(csCustomerFollow);
    }

    /**
     * 新增回访客户记录
     *
     * @param csCustomerFollow 回访客户记录
     * @return 结果
     */
    @Override
    public int insertCsCustomerFollow(CsCustomerFollow csCustomerFollow)
    {
        csCustomerFollow.setCustomerFollowId(IdUtil.getSnowflakeNextIdStr());
        csCustomerFollow.setCreateAt(DateUtils.getNowDate());
        csCustomerFollow.setDelFlag("0");
        return csCustomerFollowMapper.insertCsCustomerFollow(csCustomerFollow);
    }

    /**
     * 修改回访客户记录
     *
     * @param csCustomerFollow 回访客户记录
     * @return 结果
     */
    @Override
    public int updateCsCustomerFollow(CsCustomerFollow csCustomerFollow)
    {
        csCustomerFollow.setUpdateAt(DateUtils.getNowDate());
        return csCustomerFollowMapper.updateCsCustomerFollow(csCustomerFollow);
    }

    /**
     * 批量删除回访客户记录
     *
     * @param customerFollowIds 需要删除的回访客户记录主键
     * @return 结果
     */
    @Override
    public int deleteCsCustomerFollowByCustomerFollowIds(String[] customerFollowIds)
    {
        return csCustomerFollowMapper.deleteCsCustomerFollowByCustomerFollowIds(customerFollowIds);
    }

    /**
     * 删除回访客户记录信息
     *
     * @param customerFollowId 回访客户记录主键
     * @return 结果
     */
    @Override
    public int deleteCsCustomerFollowByCustomerFollowId(String customerFollowId)
    {
        return csCustomerFollowMapper.deleteCsCustomerFollowByCustomerFollowId(customerFollowId);
    }

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

        List<CsCustomerFollowTemplate> insertCsCustomerFollowList = new ArrayList<>();
        List<CsCustomerFollowTemplate> updateCsCustomerFollowList = new ArrayList<>();

        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (CsCustomerFollowTemplate item : list)
        {
            try
            {
                item.setCustomerFollowId(IdUtil.getSnowflakeNextIdStr());
                item.setCreateAt(DateUtils.getNowDate());
                item.setDelFlag("0");
                insertCsCustomerFollowList.add(item);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("回访客户记录").append(item.getCustomerName()).append(" 导入成功");

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "回访客户记录" + item.getCustomerName() + " 导入失败：";
                failureMsg.append(msg).append(e.getMessage());
            }
        }

        if (insertCsCustomerFollowList.size() > 0) {
            this.insertCsCustomerFollowBatch(insertCsCustomerFollowList);
        }

        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 insertCsCustomerFollowBatch(List<CsCustomerFollowTemplate> list)
    {
        return csCustomerFollowMapper.insertCsCustomerFollowBatch(list);
    }
}
