package com.fs.company.service.impl;

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

import com.fs.common.core.domain.entity.SysUser;
import com.fs.common.exception.CustomException;
import com.fs.common.utils.StringUtils;
import com.fs.company.param.CompanyVoiceCallerImportParam;
import com.fs.company.param.CompanyVoiceCallerParam;
import com.fs.company.vo.CompanyVoiceCallerListVO;
import com.fs.crm.domain.CrmCustomer;
import com.fs.crm.param.CrmLineCustomerImportParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.company.mapper.CompanyVoiceCallerMapper;
import com.fs.company.domain.CompanyVoiceCaller;
import com.fs.company.service.ICompanyVoiceCallerService;

/**
 * 企业用户坐席Service业务层处理
 *
 * @author fs
 * @date 2023-02-23
 */
@Service
public class CompanyVoiceCallerServiceImpl implements ICompanyVoiceCallerService
{
    @Autowired
    private CompanyVoiceCallerMapper companyVoiceCallerMapper;

    /**
     * 查询企业用户坐席
     *
     * @param callingId 企业用户坐席ID
     * @return 企业用户坐席
     */
    @Override
    public CompanyVoiceCaller selectCompanyVoiceCallerById(Long callingId)
    {
        return companyVoiceCallerMapper.selectCompanyVoiceCallerById(callingId);
    }


    /**
     * 查询企业用户坐席列表
     *
     * @param companyVoiceCaller 企业用户坐席
     * @return 企业用户坐席
     */
    @Override
    public List<CompanyVoiceCaller> selectCompanyVoiceCallerList(CompanyVoiceCaller companyVoiceCaller)
    {
        return companyVoiceCallerMapper.selectCompanyVoiceCallerList(companyVoiceCaller);
    }

    /**
     * 新增企业用户坐席
     *
     * @param companyVoiceCaller 企业用户坐席
     * @return 结果
     */
    @Override
    public int insertCompanyVoiceCaller(CompanyVoiceCaller companyVoiceCaller)
    {
        return companyVoiceCallerMapper.insertCompanyVoiceCaller(companyVoiceCaller);
    }

    /**
     * 修改企业用户坐席
     *
     * @param companyVoiceCaller 企业用户坐席
     * @return 结果
     */
    @Override
    public int updateCompanyVoiceCaller(CompanyVoiceCaller companyVoiceCaller)
    {
        return companyVoiceCallerMapper.updateCompanyVoiceCaller(companyVoiceCaller);
    }

    /**
     * 批量删除企业用户坐席
     *
     * @param callingIds 需要删除的企业用户坐席ID
     * @return 结果
     */
    @Override
    public int deleteCompanyVoiceCallerByIds(Long[] callingIds)
    {
        return companyVoiceCallerMapper.deleteCompanyVoiceCallerByIds(callingIds);
    }

    /**
     * 删除企业用户坐席信息
     *
     * @param callingId 企业用户坐席ID
     * @return 结果
     */
    @Override
    public int deleteCompanyVoiceCallerById(Long callingId)
    {
        return companyVoiceCallerMapper.deleteCompanyVoiceCallerById(callingId);
    }

    @Override
    public List<CompanyVoiceCallerListVO> selectCompanyVoiceCallerListVO(CompanyVoiceCallerParam param) {
        return companyVoiceCallerMapper.selectCompanyVoiceCallerListVO(param);
    }

    @Override
    public CompanyVoiceCaller selectCompanyVoiceCallerByUserId(Long companyId, Long companyUserId) {
        return companyVoiceCallerMapper.selectCompanyVoiceCallerByUserId(companyId,companyUserId);
    }

    @Override
    public String importCompanyVoiceCaller(List<CompanyVoiceCallerImportParam> list) {
        if (StringUtils.isNull(list) || list.size() == 0)
        {
            throw new CustomException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (CompanyVoiceCallerImportParam item : list)
        {

            try
            {
                CompanyVoiceCaller caller = companyVoiceCallerMapper.selectCompanyVoiceCallerByCallerNo(item.getCallerNo());
                if (caller == null) {
                    caller=new CompanyVoiceCaller();
                    caller.setStatus(1);
                    caller.setCallerNo(item.getCallerNo());
                    companyVoiceCallerMapper.insertCompanyVoiceCaller(caller);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、号码 " + item.getCallerNo() + " 导入成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、号码 " + item.getCallerNo() + " 已存在");
                }


            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、号码 " +  item.getCallerNo() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            //throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<CompanyVoiceCaller> selectCompanyVoiceCallerByEnable() {
        return companyVoiceCallerMapper.selectCompanyVoiceCallerByEnable();
    }

    @Override
    public Integer selectCompanyVoiceCallerCountsByCompanyId(Long companyId) {
        return companyVoiceCallerMapper.selectCompanyVoiceCallerCountsByCompanyId(companyId);
    }
}
