package com.zsc.edu.gateway.framework.message.sms;


import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestClient;

import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author pegnzheng
 */
@AllArgsConstructor
@Component
public class SmsSender {

    private final SmsProperties properties;
    @Resource
    private final RestClient restClient;

    @Async
    public void send(String mobile, String text) {
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("apikey", properties.apiKey);
        params.add("mobile", mobile);
        params.add("text", text);
        restClient.post().uri(properties.singleSendUrl).body(params);
    }

    @Async
    public void send(Set<String> mobiles, String text) {
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("apikey", properties.apiKey);
        params.add("mobile", mobiles.stream().filter(StringUtils::hasText).collect(Collectors.joining(",")));
        params.add("text", text);
        restClient.post().uri(properties.batchSendUrl).body(params);
    }

}
