package com.schisandra.auth.application.handler.sms;

import com.schisandra.auth.application.convert.SchisandraSmsConfigDTOConvert;
import com.schisandra.auth.application.dto.SchisandraSmsConfigDTO;
import com.schisandra.auth.common.enums.SmsSupplierType;
import com.schisandra.auth.domain.bo.SchisandraSmsConfigBO;
import com.schisandra.auth.domain.service.SchisandraSmsConfigDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.dromara.sms4j.provider.config.BaseConfig;
import org.dromara.sms4j.tencent.config.TencentConfig;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Classname TencentSmsHandler
 * @BelongsProject: schisandra-cloud-storage
 * @BelongsPackage: com.schisandra.auth.application.handler
 * @Author: landaiqing
 * @CreateTime: 2024-05-12  13:07
 * @Description: TODO
 * @Version: 1.0
 */
@Component
@Slf4j
public class TencentSmsHandler implements SchisandraSmsTypeHandler{
    @Resource
    SchisandraSmsConfigDomainService schisandraSmsConfigDomainService;
    @Override
    public SmsSupplierType getHandlerType() {
        return SmsSupplierType.TENCENT;
    }

    @Override
    public BaseConfig addConfig(String configId) {
        SchisandraSmsConfigBO schisandraSmsConfigBO = schisandraSmsConfigDomainService.queryByConfigId(configId);
        SchisandraSmsConfigDTO schisandraSmsConfigDTO = SchisandraSmsConfigDTOConvert.INSTANCE.convertBOToDTO(schisandraSmsConfigBO);
        if (ObjectUtils.isEmpty(schisandraSmsConfigDTO)){
            log.error("腾讯短信配置信息获取失败");
            return null;
        }
        TencentConfig tencentConfig=new TencentConfig();
        tencentConfig.setAccessKeyId(schisandraSmsConfigDTO.getAccessKeyId());
        tencentConfig.setAccessKeySecret(schisandraSmsConfigDTO.getAccessKeySecret());
        tencentConfig.setConfigId(schisandraSmsConfigDTO.getConfigId());
        tencentConfig.setAction(schisandraSmsConfigDTO.getAction());
        tencentConfig.setService(schisandraSmsConfigDTO.getService());
        tencentConfig.setRequestUrl(schisandraSmsConfigDTO.getRequestUrl());
        tencentConfig.setConnTimeout(schisandraSmsConfigDTO.getConnTimeout());
        tencentConfig.setTerritory(schisandraSmsConfigDTO.getRegion());
        tencentConfig.setVersion(schisandraSmsConfigDTO.getVersion());
        tencentConfig.setTemplateId(schisandraSmsConfigDTO.getTemplateId());
        tencentConfig.setSignature(schisandraSmsConfigDTO.getSignature());
        tencentConfig.setSdkAppId(schisandraSmsConfigDTO.getSdkAppId());
        tencentConfig.setMaxRetries(schisandraSmsConfigDTO.getMaxRetries());
        tencentConfig.setMaximum(Math.toIntExact(schisandraSmsConfigDTO.getMaximum()));
        tencentConfig.setWeight(schisandraSmsConfigDTO.getWeight());
        tencentConfig.setMaximum(Math.toIntExact(schisandraSmsConfigDTO.getMaximum()));
        tencentConfig.setMaxRetries(Math.toIntExact(schisandraSmsConfigDTO.getMaxRetries()));
        return tencentConfig;
    }
}
