package com.fs.company.service.impl;

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

import com.fs.common.exception.CustomException;
import com.fs.common.utils.OrderUtils;
import com.fs.common.utils.StringUtils;
import com.fs.company.vo.CompanyVoiceMobileVO;
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.CompanyVoiceMobileMapper;
import com.fs.company.domain.CompanyVoiceMobile;
import com.fs.company.service.ICompanyVoiceMobileService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 中间号Service业务层处理
 * 
 * @author fs
 * @date 2021-10-04
 */
@Service
public class CompanyVoiceMobileServiceImpl implements ICompanyVoiceMobileService 
{
    @Autowired
    private CompanyVoiceMobileMapper companyVoiceMobileMapper;

    /**
     * 查询中间号
     * 
     * @param mobileId 中间号ID
     * @return 中间号
     */
    @Override
    public CompanyVoiceMobile selectCompanyVoiceMobileById(Long mobileId)
    {
        return companyVoiceMobileMapper.selectCompanyVoiceMobileById(mobileId);
    }

    /**
     * 查询中间号列表
     * 
     * @param companyVoiceMobile 中间号
     * @return 中间号
     */
    @Override
    public List<CompanyVoiceMobile> selectCompanyVoiceMobileList(CompanyVoiceMobile companyVoiceMobile)
    {
        return companyVoiceMobileMapper.selectCompanyVoiceMobileList(companyVoiceMobile);
    }

    /**
     * 新增中间号
     * 
     * @param companyVoiceMobile 中间号
     * @return 结果
     */
    @Override
    public int insertCompanyVoiceMobile(CompanyVoiceMobile companyVoiceMobile)
    {
        return companyVoiceMobileMapper.insertCompanyVoiceMobile(companyVoiceMobile);
    }

    /**
     * 修改中间号
     * 
     * @param companyVoiceMobile 中间号
     * @return 结果
     */
    @Override
    public int updateCompanyVoiceMobile(CompanyVoiceMobile companyVoiceMobile)
    {
        return companyVoiceMobileMapper.updateCompanyVoiceMobile(companyVoiceMobile);
    }

    /**
     * 批量删除中间号
     * 
     * @param mobileIds 需要删除的中间号ID
     * @return 结果
     */
    @Override
    public int deleteCompanyVoiceMobileByIds(Long[] mobileIds)
    {
        return companyVoiceMobileMapper.deleteCompanyVoiceMobileByIds(mobileIds);
    }

    /**
     * 删除中间号信息
     * 
     * @param mobileId 中间号ID
     * @return 结果
     */
    @Override
    public int deleteCompanyVoiceMobileById(Long mobileId)
    {
        return companyVoiceMobileMapper.deleteCompanyVoiceMobileById(mobileId);
    }

    @Override
    public List<CompanyVoiceMobileVO> selectCompanyVoiceMobileVOList(CompanyVoiceMobile companyVoiceMobile) {
        return companyVoiceMobileMapper.selectCompanyVoiceMobileVOList(companyVoiceMobile);
    }



    @Override
    public CompanyVoiceMobile selectEmptyCompanyVoiceMobile(Long companyId,Long apiId) {
        return companyVoiceMobileMapper.selectEmptyCompanyVoiceMobile(companyId,apiId);
    }

    @Override
    public Integer selectCompanyVoiceMobileCount() {
        return companyVoiceMobileMapper.selectCompanyVoiceMobileCount();
    }

    @Override
    public CompanyVoiceMobile selectCompanyVoiceMobileByMobile(String mobile) {
        return companyVoiceMobileMapper.selectCompanyVoiceMobileByMobile(mobile);
    }

    @Override
    @Transactional
    public String importMobile(List<CompanyVoiceMobile> 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 (CompanyVoiceMobile item : list)
        {
            try
            {
                CompanyVoiceMobile mobile=companyVoiceMobileMapper.selectCompanyVoiceMobileByMobile(item.getMobile().trim());
                if(mobile!=null){
                    failureNum++;
                    String msg = "<br/>" + failureNum + "、手机号 " + item.getMobile() + " 已存在";
                    failureMsg.append(msg );
                }
                else{
                    mobile=new CompanyVoiceMobile();
                    mobile.setMobile(item.getMobile());
                    mobile.setMobileType(1);
                    mobile.setStatus(1);
                    mobile.setApiId(item.getApiId());
                    companyVoiceMobileMapper.insertCompanyVoiceMobile(mobile);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、手机号 " + item.getMobile() + " 导入成功");
                }

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、手机号 " + item.getMobile() + " 导入失败：";
                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();
    }
}
