package com.fs.company.service.impl;

import com.fs.common.utils.DateUtils;
import com.fs.company.domain.CompanyVoiceRoboticCallBlacklist;
import com.fs.company.mapper.CompanyVoiceRoboticCallBlacklistMapper;
import com.fs.company.param.CompanyVoiceRoboticCallBlacklistCheckParam;
import com.fs.company.service.ICompanyVoiceRoboticCallBlacklistService;
import com.fs.company.vo.CompanyVoiceRoboticCallBlacklistCheckVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 黑名单Service业务层处理
 * 
 * @author fs
 * @date 2023-02-23
 */
@Service
public class CompanyVoiceRoboticCallBlacklistServiceImpl implements ICompanyVoiceRoboticCallBlacklistService
{
    @Autowired
    private CompanyVoiceRoboticCallBlacklistMapper companyVoiceRoboticCallBlacklistMapper;

    /**
     * 查询黑名单
     * 
     * @param blacklistId 黑名单ID
     * @return 黑名单
     */
    @Override
    public CompanyVoiceRoboticCallBlacklist selectCompanyVoiceRoboticCallBlacklistById(Long blacklistId)
    {
        return companyVoiceRoboticCallBlacklistMapper.selectCompanyVoiceBlacklistById(blacklistId);
    }

    /**
     * 查询黑名单列表
     * 
     * @param companyCallBlacklist 黑名单
     * @return 黑名单
     */
    @Override
    public List<CompanyVoiceRoboticCallBlacklist> selectCompanyVoiceRoboticCallBlacklistList(CompanyVoiceRoboticCallBlacklist companyCallBlacklist)
    {
        return companyVoiceRoboticCallBlacklistMapper.selectCompanyVoiceBlacklistList(companyCallBlacklist);
    }

    /**
     * 新增黑名单
     * 
     * @param companyCallBlacklist 黑名单
     * @return 结果
     */
    @Override
    public int insertCompanyVoiceRoboticCallBlacklist(CompanyVoiceRoboticCallBlacklist companyCallBlacklist)
    {
        companyCallBlacklist.setCreateTime(DateUtils.getNowDate());
        return companyVoiceRoboticCallBlacklistMapper.insertCompanyVoiceBlacklist(companyCallBlacklist);
    }

    /**
     * 修改黑名单
     * 
     * @param companyCallBlacklist 黑名单
     * @return 结果
     */
    @Override
    public int updateCompanyVoiceRoboticCallBlacklist(CompanyVoiceRoboticCallBlacklist companyCallBlacklist)
    {
        companyCallBlacklist.setUpdateTime(DateUtils.getNowDate());
        return companyVoiceRoboticCallBlacklistMapper.updateCompanyVoiceBlacklist(companyCallBlacklist);
    }

    /**
     * 批量删除黑名单
     * 
     * @param blacklistIds 需要删除的黑名单ID
     * @return 结果
     */
    @Override
    public int deleteCompanyVoiceRoboticCallBlacklistByIds(Long[] blacklistIds)
    {
        return companyVoiceRoboticCallBlacklistMapper.deleteCompanyVoiceBlacklistByIds(blacklistIds);
    }

    /**
     * 删除黑名单信息
     * 
     * @param blacklistId 黑名单ID
     * @return 结果
     */
    @Override
    public int deleteCompanyVoiceRoboticCallBlacklistById(Long blacklistId)
    {
        return companyVoiceRoboticCallBlacklistMapper.deleteCompanyVoiceBlacklistById(blacklistId);
    }

    @Override
    public int selectCompanyCallBlacklistByMobile(String mobile) {
        return companyVoiceRoboticCallBlacklistMapper.selectCompanyVoiceBlacklistByMobile(mobile);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int changeStatus(CompanyVoiceRoboticCallBlacklist companyVoiceRoboticCallBlacklist) {
        CompanyVoiceRoboticCallBlacklist db = companyVoiceRoboticCallBlacklistMapper.selectCompanyVoiceBlacklistById(companyVoiceRoboticCallBlacklist.getId());
        if (db == null) {
            throw new RuntimeException("黑名单记录不存在");
        }

        CompanyVoiceRoboticCallBlacklist entity = new CompanyVoiceRoboticCallBlacklist();
        entity.setId(companyVoiceRoboticCallBlacklist.getId());
        entity.setStatus(companyVoiceRoboticCallBlacklist.getStatus());
        return companyVoiceRoboticCallBlacklistMapper.updateStatusById(entity);
    }

    /**
     * 校验黑名单
     */
    @Override
    public CompanyVoiceRoboticCallBlacklistCheckVO checkBlacklist(CompanyVoiceRoboticCallBlacklistCheckParam param) {
        if (param == null || param.getCompanyId() == null
                || !StringUtils.hasText(param.getBusinessType())
                || !StringUtils.hasText(param.getTargetValue())) {
            return CompanyVoiceRoboticCallBlacklistCheckVO.paramNull("校验参数不能为空");
        }

        String targetValue = normalizeTargetValue(param.getTargetType(), param.getTargetValue());

        // 1. 有 companyId，先查公司级黑名单
        if (param.getCompanyId() != null) {
            CompanyVoiceRoboticCallBlacklist companyHit =
                    companyVoiceRoboticCallBlacklistMapper.selectActiveByTarget(
                            param.getCompanyId(),
                            param.getBusinessType(),
                            targetValue,param.getTargetType()
                    );

            if (companyHit != null) {
                return CompanyVoiceRoboticCallBlacklistCheckVO.reject(
                        companyHit.getId(),
                        companyHit.getBusinessType(),
                        companyHit.getTargetType(),
                        companyHit.getTargetValue(),
                        buildReason(companyHit, "公司级黑名单")
                );
            }
        }

//        CompanyVoiceRoboticCallBlacklist hit = companyVoiceRoboticCallBlacklistMapper.selectActiveByTarget(
//                param.getCompanyId(),
//                param.getBusinessType(),
//                targetValue
//        );

//        if (hit == null) {
//            return CompanyVoiceRoboticCallBlacklistCheckVO.pass(param.getBusinessType());
//        }
//
//        return CompanyVoiceRoboticCallBlacklistCheckVO.reject(
//                hit.getId(),
//                hit.getBusinessType(),
//                hit.getTargetType(),
//                hit.getTargetValue(),
//                buildReason(hit)
//        );
        // 2. 查全局黑名单
        CompanyVoiceRoboticCallBlacklist globalHit = companyVoiceRoboticCallBlacklistMapper.selectGlobalActiveByTarget(param.getBusinessType(),targetValue,param.getTargetType());

        if (globalHit != null) {
            return CompanyVoiceRoboticCallBlacklistCheckVO.reject(
                    globalHit.getId(),
                    globalHit.getBusinessType(),
                    globalHit.getTargetType(),
                    globalHit.getTargetValue(),
                    buildReason(globalHit, "全局黑名单")
            );
        }
        return CompanyVoiceRoboticCallBlacklistCheckVO.pass(param.getBusinessType());

    }

    /**
     * 归一化对象值
     */
    private String normalizeTargetValue(Integer targetType, String targetValue) {
        if (!StringUtils.hasText(targetValue)) {
            return targetValue;
        }
        String value = targetValue.trim();

        // 手机号归一化
        if (Integer.valueOf(1).equals(targetType)) {
            value = value.replace(" ", "")
                    .replace("-", "")
                    .replace("+86", "")
                    .replace("(", "")
                    .replace(")", "");
        }
        return value;
    }

    private String buildReason(CompanyVoiceRoboticCallBlacklist hit,String scopeDesc) {
        String prefix;
        if (Integer.valueOf(1).equals(hit.getTargetType())) {
            prefix = "手机号命中" + scopeDesc;
        } else if (Integer.valueOf(2).equals(hit.getTargetType())) {
            prefix = "客户ID命中" + scopeDesc;
        } else if (Integer.valueOf(3).equals(hit.getTargetType())) {
            prefix = "企微客户ID命中" + scopeDesc;
        } else {
            prefix = "命中" + scopeDesc;
        }

        if (StringUtils.hasText(hit.getReason())) {
            return prefix + "：" + hit.getReason();
        }
        return prefix;
    }


}
