package com.css.simulation.resource.sms.consumer;


import api.common.util.JsonUtil;
import com.css.simulation.resource.sms.pojo.dto.SmsDTO;
import com.css.simulation.resource.sms.util.TencentSMSUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SmsConsumer {

    /**
     * 新增用户时发送短信
     *
     * @param msg 消息
     */
    @KafkaListener(groupId = "simulation-resource-sms", topics = "${sms.topic}")
    @SneakyThrows
    public void sendShortMessage(ConsumerRecord<String, String> msg) {
        String smsDTOJson = msg.value();
        SmsDTO smsDTO = JsonUtil.jsonToBean(smsDTOJson, SmsDTO.class);
        log.info("准备发送短信：" + smsDTOJson);
        TencentSMSUtil.send(
                smsDTO.getSecretId(),
                smsDTO.getSecretKey(),
                smsDTO.getSdkAppId(),
                smsDTO.getSignName(),
                smsDTO.getTemplateId(),
                new String[]{},
                new String[]{smsDTO.getPhone()}
        );
    }


}
