package com.bootdo.datas.service;

import cn.com.scca.api.DefaultSccaClient;
import cn.com.scca.api.SccaClient;
import cn.com.scca.api.data.request.message.SendSmsByTemplateRequest;
import cn.com.scca.api.data.response.message.SendSmsByTemplateResponse;
import cn.com.scca.api.error.ApiException;
import cn.com.scca.api.result.ResponseResult;
import com.bootdo.common.utils.BDException;
import com.bootdo.datas.controller.GyDataImportController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhouhj
 */
@Service
public class MessageService {
    private static Logger log = LoggerFactory.getLogger(MessageService.class);

    @Value("${message_gateway.prefix:#{null}}")
    private String gateWayPrefix;

    @Value("${message_gateway.appId:#{null}}")
    private String gatewayAppId;

    @Value("${message_gateway.appKey:#{null}}")
    private String gatewayAppKey;

    @Value("${message_gateway.catalog_record.templateId:#{null}}")
    private String catalogTemplateId;

    @Value("${message_gateway.risk_assessment.templateId:#{null}}")
    private String riskTemplateId;

    /**
     * 重要和核心数据目录审核驳回通知
     *
     * @param phoneNumberList 手机集合
     * @param templateParams  风险名称
     * @return
     */
    public Integer importance(String phoneNumberList, String templateParams) throws BDException {
        log.error("========短信的参数phoneNumberList：" + phoneNumberList);
        log.error("========短信的参数templateParams：" + templateParams);
        SccaClient sccaClient = new DefaultSccaClient(
                this.gateWayPrefix,
                this.gatewayAppId,
                gatewayAppKey);
        SendSmsByTemplateRequest request = new SendSmsByTemplateRequest();
        request.setTempleteId(this.catalogTemplateId);
        request.setTempleteParams(templateParams);
        String result = this.removeEmptyValues(phoneNumberList);
        request.setPhoneNumberList(result);
        ResponseResult<SendSmsByTemplateResponse> execute = null;
        try {
            execute = sccaClient.execute(request);
        } catch (ApiException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
        System.out.println("====短信发送反馈状态码:" + execute.getResult_code() + "====反馈信息：" + execute.getResult_msg());
        return execute.getResult_code();
    }

    /**
     * 安全风险评估审核驳回通知
     *
     * @param phoneNumberList 手机集合
     * @param templateParams  风险名称
     * @return
     */
    public Integer securityRisk(String phoneNumberList, String templateParams) throws BDException {
        SccaClient sccaClient = new DefaultSccaClient(
                gateWayPrefix,
                this.gatewayAppId,
                this.gatewayAppKey);
        SendSmsByTemplateRequest request = new SendSmsByTemplateRequest();
        request.setTempleteId(this.riskTemplateId);
        request.setTempleteParams(templateParams);
        String result = this.removeEmptyValues(phoneNumberList);
        request.setPhoneNumberList(result);
        ResponseResult<SendSmsByTemplateResponse> execute = null;
        try {
            execute = sccaClient.execute(request);
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
        System.out.println("====短信发送反馈状态码:" + execute.getResult_code() + "====反馈信息：" + execute.getResult_msg());
        return execute.getResult_code();
    }

    private String removeEmptyValues(String phoneNumberList) throws BDException {
        if (Objects.isNull(phoneNumberList)) {
            throw new BDException("电话号码列表不得为空！");
        } else {
            String result = Arrays.stream(phoneNumberList.split(","))
                    .filter(s -> s != null && !s.trim().isEmpty() && !s.equals("null"))
                    .collect(Collectors.joining(","));
            System.out.println("=========去除空值后的电话号码列表：" + result);
            if (Objects.isNull(result)) {
                throw new BDException("电话号码列表不得为空！");
            }
            return result;
        }
    }
}
