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 projectRecord 项目启动消息
     */
    @KafkaListener(groupId = "simulation-resource-sms", topics = "${sms.topic}")
    @SneakyThrows
    public void sendShortMessage(ConsumerRecord<String, String> projectRecord) {
        String smsDTOJson = projectRecord.value();
        SmsDTO smsDTO = JsonUtil.jsonToBean(smsDTOJson, SmsDTO.class);
        log.info("SmsConsumer.sendShortMessage() 准备发送短信：" + smsDTOJson);
        TencentSMSUtil.send(
                smsDTO.getSecretId(),
                smsDTO.getSecretKey(),
                smsDTO.getSdkAppId(),
                smsDTO.getSignName(),
                smsDTO.getTemplateId(),
                new String[]{},
                new String[]{smsDTO.getPhone()}
        );
    }


}
