package com.lz.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lz.modules.app.dto.SmsResult;
import com.lz.modules.app.entity.SmsSendLogEntity;
import com.lz.modules.app.enums.SmsChannelEnum;
import com.lz.modules.app.service.SmsSendLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Component("dhgjSmsUtil")
public class DhgjSmsUtil {

    protected static Logger logger = LoggerFactory.getLogger("LT_THIRD");
    private final static String URL = "http://www.dh3t.com/json/sms/Submit";
    private final static String ACCOUNT = "dh80914";
    private final static String ACCOUNT_PASSWORD = "wfE0y58Z";
    private final static String SIGN = "【霖梓控股】";

    @Resource
    private SmsSendLogService smsSendLogService;
    @Value(value = "${spring.console.env}")
    private String environment;

    /**
     * 发送监控短信
     *
     * @param mobile
     * @param content
     * @return
     */
    public boolean sendMonitorSms(String mobile, String content, Long templateId) {
        SmsResult smsResult = sendSlsSmsToDhst(mobile, content, SIGN, templateId);
        return smsResult.isSucc();
    }

    /**
     * 发送短信
     *
     * @param content 短信内容
     * @param sign    签名
     * @param mobiles 手机号
     * @param params  替换参数
     * @return
     */
    public SmsResult sendMessage(String content, String sign, String mobiles, Map<String, String> params, Long templateId) {
        if (params != null && !params.isEmpty()) {
            for (String key : params.keySet()) {
                content = content.replace("${" + key + "}", params.get(key));
            }
        }
        SmsResult smsResult = sendSlsSmsToDhst(mobiles, content, sign, templateId);
        return smsResult;
    }

    /**
     * 对单个手机号发送短消息，这里不验证手机号码有效性
     *
     * @param mobiles 手机号
     * @param content 内容
     * @param sign    签名
     */
    private SmsResult sendSlsSmsToDhst(String mobiles, String content, String sign, Long templateId) {
        SmsResult result = new SmsResult();
        Map<String, String> paramsMap = new HashMap<String, String>();
        paramsMap.put("account", ACCOUNT);
        paramsMap.put("password", DigestUtil.MD5(ACCOUNT_PASSWORD).toLowerCase());
        paramsMap.put("phones", mobiles);
        paramsMap.put("content", content);
        paramsMap.put("sign", sign);

        SmsSendLogEntity smsSendLogDo = smsSendLogService.createAndInsert(content, mobiles, SmsChannelEnum.SMS_CHANNEL_MONITOR.getCode(), templateId);

        if (StringUtil.equals(environment, Constant.INVELOMENT_TYPE_TEST)) {
            result.setSucc(true);
            result.setResultStr("dev");
            logger.info("当前环境是：" + environment + " 不允许发送短信。");
            return result;
        }

        String reqResult = HttpUtil.doHttpPost(URL, JSONObject.toJSONString(paramsMap));

        logger.info(StringUtil.appendStrs("sendSms params=|", mobiles, "|", content, "|", reqResult));

        JSONObject json = JSON.parseObject(reqResult);
        if (json.getInteger("result") == 0) {
            result.setSucc(true);
            result.setResultStr(json.getString("desc"));
        } else {
            result.setSucc(false);
            result.setResultStr(json.getString("desc"));
        }
        smsSendLogDo.setStatus(json.getInteger("result"));
        smsSendLogService.updateById(smsSendLogDo);
        return result;
    }


}
