admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.yeshi.fanli.util.mq.rabbit.consumer;
 
import com.google.gson.Gson;
import com.rabbitmq.client.Channel;
import com.yeshi.fanli.dto.money.UserMoneyChangeDTO;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.money.UserMoneyDebtService;
import com.yeshi.fanli.util.mq.cmq.UserMoneyChangeCMQManager;
import com.yeshi.fanli.util.mq.rabbit.RabbitmqMsgConsumeUtil;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
 
/**
 * @author hxh
 * @title: QueueHelloWorldListener
 * @description:
 * @date 2024/9/26 13:47
 */
@Component
public class TopicUserMoneyDebtListener {
    @Resource
    private RabbitTemplate rabbitTemplate;
 
    @Resource
    private UserMoneyDebtService userMoneyDebtService;
 
    @RabbitListener(queues = "topic_user_money_debt", ackMode = "MANUAL")
    public void onMessage(Message message, Channel channel) throws Exception {
        RabbitmqMsgConsumeUtil.processMessage(message, channel, rabbitTemplate, () -> {
            String result = new String(message.getBody(), StandardCharsets.UTF_8);
            UserMoneyChangeDTO dto = new Gson().fromJson(result, UserMoneyChangeDTO.class);
            try {
                if (dto != null && dto.getChangeMoney().compareTo(new BigDecimal(0)) > 0) {
                    userMoneyDebtService.repayDebt(dto.getUid());
                }
            } catch (Exception e) {
                try {
                    LogHelper.errorDetailInfo(e);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
                throw e;
            }
 
        });
    }
}