package com.mcwl.memberCenter.consumer;

import com.mcwl.common.constant.QueueConstants;
import com.mcwl.common.core.domain.entity.SysUser;
import com.mcwl.memberCenter.domain.Member;
import com.mcwl.resource.domain.SysAdvice;
import com.mcwl.resource.service.ISysAdviceService;
import com.mcwl.system.domain.enums.AdviceEnum;
import com.mcwl.system.service.ISysUserService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 * 积分消费者
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class EmptyPointsRemindConsumer {

    private final ISysAdviceService sysAdviceService;

    private final ISysUserService sysUserService;

    /**
     * 积分重置提醒
     */
    @RabbitListener(queues = QueueConstants.EMPTY_POINTS_REMIND_QUEUE, ackMode = "MANUAL")
    public void emptyPointsRemind(Member member, Channel channel, Message message) {
        try {
            SysUser sysUser = sysUserService.selectUserByUserName("admin");
            SysAdvice sysAdvice = new SysAdvice();
            sysAdvice.setSenderId(sysUser.getUserId());
            sysAdvice.setReceiverId(member.getUserId());
            sysAdvice.setType(AdviceEnum.SYSTEM_NOTICE);
            sysAdvice.setTitle("积分重置提醒");
            sysAdvice.setContent("您的积分月底将重置，请及时消费");
            sysAdviceService.save(sysAdvice);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            log.error("处理积分重置提醒消息时出错: {}", e.getMessage(), e);
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            } catch (IOException ex) {
                log.error("消息确认失败: {}", ex.getMessage(), ex);
            }
        }
    }


}
