package com.njcn.msgpush.module.push.sms;

import com.njcn.msgpush.module.push.client.factory.MessageProviderFactory;
import com.njcn.msgpush.module.push.client.sender.SmsSender;
import com.njcn.msgpush.module.push.client.sender.impl.AliyunSmsSender;
import com.njcn.msgpush.module.push.constant.MsgPushConstant;
import com.njcn.msgpush.module.push.dal.dataobject.channel.ChannelProviderConfigDO;
import com.njcn.msgpush.module.push.dal.dataobject.message.MessageRecordDO;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author caozehui
 * @data 2026-02-06
 */
@SpringBootTest
public class MsgPushClientTest {

    @Autowired
    @Qualifier("messageProviderFactoryMap")
    private Map<String, MessageProviderFactory> messageProviderFactoryMap;

    @Test
    public void testSendSms() throws Exception {

        ChannelProviderConfigDO channelProviderConfigDO = new ChannelProviderConfigDO();
        channelProviderConfigDO.setAppKey("LTAI4FxsR76x2dq3w9c5puUe");
        channelProviderConfigDO.setAppSecret("GxkTR8fsrvHtixTlD9UPmOGli35tZs");
        SmsSender smsSender = messageProviderFactoryMap.get(MsgPushConstant.PROVIDER_TYPE_ALI_YUN).createSmsSender(channelProviderConfigDO);

        MessageRecordDO message = new MessageRecordDO();
        message.setMessageId("1c2w1e2a3c456");
        message.setChannel(MsgPushConstant.CHANNEL_SMS);
        message.setTitle("灿能云");
        message.setReceiver("18839431215");
        message.setTemplateCode("SMS_481710295");
        message.setTemplateParams("{\"code\":\"123456\"}");
        message.setProviderType(MsgPushConstant.PROVIDER_TYPE_ALI_YUN);
        boolean b = smsSender.sendSms(message);
        System.out.println(System.currentTimeMillis() + " " + b);
    }

    @Test
    public void testSendBatchSms() throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put(AliyunSmsSender.SIGN_NAME, List.of("灿能云"));
        params.put(AliyunSmsSender.TEMPLATE_CODE, "SMS_481710295");
        params.put(AliyunSmsSender.TEMPLATE_PARAM, List.of("{\"code\":\"123456\"}"));

        List<MessageRecordDO> messageIdList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            MessageRecordDO message = new MessageRecordDO();
            message.setMessageId(i + "c518e2a3c");
            message.setChannel(MsgPushConstant.CHANNEL_SMS);
            message.setTitle("灿能云");
            message.setReceiver("18839431215");
            message.setTemplateCode("SMS_481710295");
            message.setTemplateParams("{\"code\":\"65432" + i + "\"}");
            message.setProviderType(MsgPushConstant.PROVIDER_TYPE_ALI_YUN);
            messageIdList.add(message);
        }

        ChannelProviderConfigDO channelProviderConfigDO = new ChannelProviderConfigDO();
        channelProviderConfigDO.setAppKey("LTAI4FxsR76x2dq3w9c5puUe");
        channelProviderConfigDO.setAppSecret("GxkTR8fsrvHtixTlD9UPmOGli35tZs");
        SmsSender smsSender = messageProviderFactoryMap.get(MsgPushConstant.PROVIDER_TYPE_ALI_YUN).createSmsSender(channelProviderConfigDO);

        boolean b = smsSender.sendBatchSms(messageIdList);
        System.out.println(System.currentTimeMillis() + " " + b);
    }
}
