package com.sqx.common.sms;

import cn.hutool.core.util.ObjectUtil;
import com.sqx.common.exception.SqxException;
import com.sqx.modules.common.service.CommonInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 短信发送配置缓存
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class SmsConfigValueCache {

    private final CommonInfoService commonInfoService;

    /**
     * 腾讯短信配置
     */
    private SmsConfig tencentSmsConfig;

    /**
     * 获取腾讯云sms配置
     * @return
     */
    public SmsConfig getTencentSmsConfig() {
        if (ObjectUtil.isNull(tencentSmsConfig)) {
            try {
                this.tencentSmsConfig = SmsConfig.builder()
                        .clientId(commonInfoService.findOne(31).getValue())
                        .clientSecret(commonInfoService.findOne(32).getValue())
                        .smsSign(commonInfoService.findOne(12).getValue())
                        .build();
            } catch (Exception e) {
                log.error("腾讯短信配置构建失败,【{}】", e.getMessage());
                throw new SqxException("腾讯短信配置构建失败");
            }
        }

        return tencentSmsConfig;
    }
}
