<%_ if (position === 'import') { _%>
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.dromara.sms4j.aliyun.config.AlibabaConfig;
import org.dromara.sms4j.comm.enumerate.SupplierType;
import org.dromara.sms4j.core.config.SupplierFactory;
import org.dromara.sms4j.core.factory.SmsFactory;
import org.dromara.sms4j.yunpian.config.YunpianConfig;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
<%_ } _%>
<%_ if (position === 'classExtend') { _%>
<%_ } _%>
<%_ if (position === 'classImplements') { _%>
<%_ } _%>
<%_ if (position === 'methodInterface') { _%>
<%_ } _%>
<%_ if (position === 'method') { _%>
    public void initService() {
    <%_ if (ormTool === 'mybatis') { _%>
        LambdaQueryWrapper<SmsSupplier> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmsSupplier::getEnabled, true);
        List<SmsSupplier> smsSuppliers = smsSupplierRepository.selectList(queryWrapper);
    <%_ } else { _%>
        List<SmsSupplier> smsSuppliers = smsSupplierRepository
            .findAll(Example.of(new SmsSupplier().enabled(true), ExampleMatcher.matching().withIgnoreNullValues()));
    <%_ } _%>
        ObjectMapper objectMapper = new ObjectMapper();
        if (CollectionUtils.isNotEmpty(smsSuppliers)) {
            smsSuppliers.forEach(smsSupplier -> {
                switch (smsSupplier.getProvider()) {
                    case ALIBABA:
                        try {
                            Map<String, String> configMap = objectMapper.readValue(smsSupplier.getConfigData(), new TypeReference<Map<String, String>>() {
                            });
                            AlibabaConfig alibabaConfig = SupplierFactory.getAlibabaConfig();
                            alibabaConfig.setAccessKeyId(configMap.get("accessKeyId"));
                            alibabaConfig.setAccessKeySecret(configMap.get("accessKeySecret"));
                            alibabaConfig.setSignature(configMap.get("signature"));
                            alibabaConfig.setRequestUrl(configMap.get("requestUrl"));
                            alibabaConfig.setAction(StringUtils.defaultIfBlank(configMap.get("action"), "SendSms"));
                            alibabaConfig.setVersion(StringUtils.defaultIfBlank(configMap.get("version"), "2017-05-25"));
                            alibabaConfig.setRegionId(StringUtils.defaultIfBlank(configMap.get("regionId"), "cn-hangzhou"));
                            SmsFactory.refresh(SupplierType.ALIBABA);
                        } catch (JsonProcessingException e) {
                            throw new RuntimeException(e);
                        }
                        break;
                    case YUNPIAN:
                        try {
                            Map<String, String> configMap = objectMapper.readValue(smsSupplier.getConfigData(), new TypeReference<Map<String, String>>() {
                            });
                            YunpianConfig yunpianConfig = SupplierFactory.getYunpianConfig();
                            yunpianConfig.setApikey(configMap.get("apikey"));
                            yunpianConfig.setCallbackUrl(configMap.get("callbackUrl"));
                            SmsFactory.refresh(SupplierType.YUNPIAN);
                        } catch (JsonProcessingException e) {
                            throw new RuntimeException(e);
                        }
                        break;
                    default:
                        break;
                }
            });
        }
    }
<%_ } _%>
