package com.bonus.canteen.core.customer.mq;

import com.bonus.canteen.core.common.constant.LeMqConstant;
import com.bonus.canteen.core.common.redis.RedisUtil;
import com.bonus.canteen.core.customer.constants.CustCacheKey;
import com.bonus.canteen.core.order.mq.MqUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class DelayedMessageSender {
   private static final Logger log = LoggerFactory.getLogger(DelayedMessageSender.class);
   private static final Long DELAY = 10L;
   private static final String formatter = "yyyy-MM-dd HH:mm:ss.SSS";

   public void asyncDelaySend(CustomerMessageDTO customerMessageDTO) {
      log.info("[用户模块]数据变动发送消息类型：{}", customerMessageDTO.getMessageType().getDesc());
      String delayTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
      customerMessageDTO.setDelayTime(delayTime);
      if (customerMessageDTO.isDelay()) {
         MqUtil.sendDelay(customerMessageDTO, LeMqConstant.Topic.CUSTOMER_CHANGE_DELAY, DELAY.intValue() * 1000);
      } else {
         MqUtil.sendDelay(customerMessageDTO, LeMqConstant.Topic.CUSTOMER_CHANGE_DELAY, 0);
      }

      RedisUtil.setString(CustCacheKey.getTenantKey(customerMessageDTO.getMessageType().name()), delayTime, 7200L);
   }

   public void asyncDelaySendToDevice(DelayedMessageTypeEnum messageType) {
      CustomerMessageDTO messageDTO = new CustomerMessageDTO();
      messageDTO.setMessageType(messageType);
      messageDTO.setDelay(true);
      this.asyncDelaySend(messageDTO);
   }
}
