package com.sckw.message.service;

import com.alibaba.fastjson2.JSON;
import com.sckw.core.common.enums.EnvConstant;
import com.sckw.core.common.enums.enums.DictTypeEnum;
import com.sckw.core.exception.BusinessException;
import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.utils.NumberUtils;
import com.sckw.core.utils.StringUtils;
import com.sckw.core.web.context.LoginUserHolder;
import com.sckw.message.model.vo.req.GetSmsVerifyCoderReqVO;
import com.sckw.message.model.vo.req.SendSmsVerifyCoderReqVO;
import com.sckw.redis.constant.RedisConstant;
import com.sckw.redis.utils.RedissonUtils;
import com.sckw.stream.enums.SmsCodeEnum;
import com.sckw.stream.model.SckwSms;
import com.sckw.system.api.RemoteSystemService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author: yzc
 * @date: 2023-06-08 11:08
 * @description:
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class SmsService {

    private final StreamBridge streamBridge;

    @DubboReference(version = "1.0.0", group = "design", check = false)
    private RemoteSystemService remoteSystemService;

    @Value("${spring.profiles.active}")
    private String env;


    /**
     * @param param
     * @return com.sckw.core.web.response.HttpResult
     * @desc: 发送验证码
     * @author: yzc
     * @date: 2023-06-13 13:43
     */
    public void sendVerifyCode(SendSmsVerifyCoderReqVO param) {
        Map<String, Map<String, String>> dict = remoteSystemService.queryDictByType(Collections.singletonList(DictTypeEnum.SEND_SMS_TYPE.getType()));
        Map<String, String> map;
        if (CollectionUtils.isNotEmpty(dict)) {
            map = dict.get(DictTypeEnum.SEND_SMS_TYPE.getType());
        } else {
            map = new HashMap<>();
        }
        String type = param.getType();
        if (Objects.isNull(map.get(type))) {
            throw new BusinessException("非法短信类型！");
        }
        String phone = param.getPhone();
        String key = StringUtils.format(RedisConstant.MESSAGE_SMS_VERIFY_CODE_REQUEST_KEY, type, phone);
        if (Boolean.TRUE.equals(RedissonUtils.exists(key))) {
            throw new BusinessException("请勿频繁获取短信验证码！");
        }
        Map<String, Object> params = new HashMap<>(Global.NUMERICAL_SIXTEEN);
        //生产环境才发短信
        String code = EnvConstant.PROD.equals(env) ? NumberUtils.createRandomVcode() : "123456";
        params.put("code", code);
        SckwSms sckwSms = new SckwSms();
        sckwSms.setPhone(param.getPhone()).setType(type).setTemplateCode(SmsCodeEnum.VERIFICATION_CODE)
                .setParams(params).setCreateBy(LoginUserHolder.getUserId());
        streamBridge.send("sckw-sms", JSON.toJSONString(sckwSms));
    }

    public String getVerifyCode(GetSmsVerifyCoderReqVO param) {
        String key = String.format(RedisConstant.MESSAGE_SMS_VERIFY_CODE_VALUE_KEY, param.getType(), param.getPhone());
        return RedissonUtils.getString(key);
    }
}
