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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.yeshi.fanli.util.mq.rabbit.consumer;
 
import com.google.gson.Gson;
import com.rabbitmq.client.Channel;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.bus.user.Order;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.order.CommonOrder;
import com.yeshi.fanli.entity.order.HongBaoOrder;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.order.CommonOrderService;
import com.yeshi.fanli.service.inter.order.HongBaoOrderService;
import com.yeshi.fanli.service.inter.order.HongBaoV2Service;
import com.yeshi.fanli.service.inter.user.integral.IntegralTaskRecordService;
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleSerivce;
import com.yeshi.fanli.util.mq.cmq.order.PlaceOrderCMQManager;
import com.yeshi.fanli.util.mq.rabbit.RabbitmqMsgConsumeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AcknowledgeMode;
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;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
/**
 * @author hxh
 * @title: QueueHelloWorldListener
 * @description:
 * @date 2024/9/26 13:47
 */
@Component
public class TopicPlaceOrderIntegralListener {
    @Resource
    private RabbitTemplate rabbitTemplate;
 
    @Resource
    private CommonOrderService commonOrderService;
 
    @Resource
    private HongBaoOrderService hongBaoOrderService;
 
    @Resource
    private HongBaoV2Service hongBaoV2Service;
 
    @Resource
    private IntegralTaskRecordService integralTaskRecordService;
 
    @Resource
    private ThreeSaleSerivce threeSaleSerivce;
 
 
    private boolean isFirstValidOrder(String orderId, int sourceType, Long uid) {
        CommonOrder commonOrder = commonOrderService.selectLatestValidByUid(uid);
        if (commonOrder != null && commonOrder.getOrderNo().equalsIgnoreCase(orderId)
                && sourceType == commonOrder.getSourceType())
            return true;
        else
            return false;
    }
 
    @RabbitListener(queues = "topic_place_order_integral", 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);
            Order order = new Gson().fromJson(result, Order.class);
            try {
                // 是否是首单
                if (isFirstValidOrder(order.getOrderId(), order.getOrderType(),
                        order.getUserInfo().getId())) {
                    // 统计订单下的用户所获得的返利金额
                    List<CommonOrder> list = commonOrderService
                            .listBySourceTypeAndOrderId(order.getOrderType(), order.getOrderId());
                    Map<Long, BigDecimal> hongBaoMoney = new HashMap<>();
                    if (list != null) {
                        for (CommonOrder commonOrder : list) {
                            HongBaoOrder hongBaoOrder = hongBaoOrderService
                                    .selectDetailByCommonOrderId(commonOrder.getId());
                            if (hongBaoOrder != null && hongBaoOrder.getHongBaoV2() != null) {
                                Long mainUid = hongBaoOrder.getHongBaoV2().getUserInfo().getId();
                                if (hongBaoMoney.get(mainUid) == null)
                                    hongBaoMoney.put(mainUid, new BigDecimal(0));
                                hongBaoMoney.put(mainUid, hongBaoMoney.get(mainUid)
                                        .add(hongBaoOrder.getHongBaoV2().getMoney()));
                                // 查询上级
                                List<HongBaoV2> children = hongBaoV2Service
                                        .listChildrenById(hongBaoOrder.getHongBaoV2().getId());
                                if (children != null) {
                                    for (HongBaoV2 hongBao : children) {
                                        Long uid = hongBao.getUserInfo().getId();
                                        if (hongBaoMoney.get(uid) == null)
                                            hongBaoMoney.put(uid, new BigDecimal(0));
                                        hongBaoMoney.put(uid,
                                                hongBaoMoney.get(uid).add(hongBao.getMoney()));
                                    }
                                }
                            }
                        }
                    }
 
                    if (isFirstValidOrder(order.getOrderId(), order.getOrderType(),
                            order.getUserInfo().getId())) {// 有效的首单
                        if (hongBaoMoney.get(order.getUserInfo().getId()) == null || hongBaoMoney
                                .get(order.getUserInfo().getId()).compareTo(new BigDecimal("0")) <= 0) {
                            // 分享奖金是0
                            integralTaskRecordService.firstShareOrderReward(order.getUserInfo().getId(),
                                    null);
                        }
 
                        // 获取上两级数据
                        UserInfo boss = threeSaleSerivce.getBoss(order.getUserInfo().getId());
                        if (boss != null) {// 判断上级的红包
                            if (hongBaoMoney.get(boss.getId()) == null || hongBaoMoney.get(boss.getId())
                                    .compareTo(new BigDecimal("0")) <= 0) {
                                // 补偿金币
                                integralTaskRecordService.firstSharerOrderRewardBoss(boss.getId(),
                                        order.getUserInfo().getId(), null);
                            }
                        }
 
                    } else {// 自购订单
                        UserInfo boss = threeSaleSerivce.getBoss(order.getUserInfo().getId());
                        if (boss != null) {// 判断上级的红包
                            if (hongBaoMoney.get(boss.getId()) == null || hongBaoMoney.get(boss.getId())
                                    .compareTo(new BigDecimal("0")) <= 0) {
                                // 补偿金币1级
                                integralTaskRecordService.firstRebateOrderRewardBoss(boss.getId(),
                                        order.getUserInfo().getId(), null);
                            }
 
                            boss = threeSaleSerivce.getBoss(boss.getId());
                            if (boss != null) {// 判断上级的红包
                                if (hongBaoMoney.get(boss.getId()) == null || hongBaoMoney
                                        .get(boss.getId()).compareTo(new BigDecimal("0")) <= 0) {
                                    // 补偿金币2级
                                    integralTaskRecordService.firstRebateOrderRewardBossSuper(
                                            boss.getId(), order.getUserInfo().getId(), null);
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                LogHelper.errorDetailInfo(e);
                throw e;
            }
        });
    }
}