package com.fzm.mall.server.admin.mq.consumer.message_center;

import com.alibaba.fastjson.JSONObject;
import com.fzm.mall.server.admin.message.enums.status.SmsTemplateStatusEnum;
import com.fzm.mall.server.admin.message.service.ISmsTemplateService;
import com.fzm.mall.server.admin.util.AliCloudSmsUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author wangp
 * @date 2021/5/6 16:52
 * @description 短信模板
 * @since JDK 1.8
 */
@Component
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SmsTemplateConsumer {

    private final ISmsTemplateService smsTemplateService;

    //发行
    @RabbitListener(queues = "ly.query.sms.template.check.queue")
    public void querySmsTemplateCheck(JSONObject obj) {
        String templateCode = obj.getString("templateCode");
        Integer status = 0;
        String reason = "";

        //TODO check 循环调用直到请求成功
        AliCloudSmsUtil.querySmsTemplate(templateCode);

        if (false) {
            status = SmsTemplateStatusEnum.CHECK_FAIL.getStatus();
        } else {
            status = SmsTemplateStatusEnum.CHECK_PASS.getStatus();
        }
        reason = "";

        smsTemplateService.updateCheckResult(templateCode, status, reason);
    }
}
