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.aliyun.config.AlibabaConfig;
import org.dromara.sms4j.provider.config.BaseConfig;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Classname AlibabaSmsHandler
 * @BelongsProject: schisandra-cloud-storage
 * @BelongsPackage: com.schisandra.auth.application.handler
 * @Author: schisandra
 * @CreateTime: 2024-05-11  18:20
 * @Description: TODO
 * @Version: 1.0
 */
@Component
@Slf4j
public class AlibabaSmsHandler implements SchisandraSmsTypeHandler{
    @Resource
    SchisandraSmsConfigDomainService schisandraSmsConfigDomainService;
    @Override
    public SmsSupplierType getHandlerType() {
        return SmsSupplierType.ALIBABA;
    }

    @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;
        }
        AlibabaConfig alibabaConfig=new AlibabaConfig();
        alibabaConfig.setConfigId(schisandraSmsConfigDTO.getConfigId());
        alibabaConfig.setRequestUrl(schisandraSmsConfigDTO.getRequestUrl());
        alibabaConfig.setAccessKeyId(schisandraSmsConfigDTO.getAccessKeyId());
        alibabaConfig.setAccessKeySecret(schisandraSmsConfigDTO.getAccessKeySecret());
        alibabaConfig.setSignature(schisandraSmsConfigDTO.getSignature());
        alibabaConfig.setTemplateId(schisandraSmsConfigDTO.getTemplateId());
        alibabaConfig.setTemplateName(schisandraSmsConfigDTO.getTemplateName());
        alibabaConfig.setSdkAppId(schisandraSmsConfigDTO.getSdkAppId());
        alibabaConfig.setMaximum(Math.toIntExact(schisandraSmsConfigDTO.getMaximum()));
        return alibabaConfig;
    }
}
