admin
2024-10-14 b9b263043cbb2e028017b9a5626c664b54513749
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package com.yeshi.fanli.util.mq.rabbit.consumer;
 
import com.rabbitmq.client.Channel;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.bus.user.ThreeSale;
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.order.tb.TaoBaoOrderService;
import com.yeshi.fanli.service.inter.user.integral.IntegralGetService;
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.HongBaoRecieveCMQManager;
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 org.yeshi.utils.TimeUtil;
 
import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
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 TopicHongbaoIntegralListener {
    @Resource
    private RabbitTemplate rabbitTemplate;
 
    @Resource
    private HongBaoV2Service hongBaoV2Service;
 
    @Resource
    private CommonOrderService commonOrderService;
 
    @Resource
    private HongBaoOrderService hongBaoOrderService;
 
    @Resource
    private ThreeSaleSerivce threeSaleSerivce;
 
    @Resource
    private IntegralGetService integralGetService;
 
    @Resource
    private IntegralTaskRecordService integralTaskRecordService;
 
    private void fanliGetIntegral(HongBaoV2 hongBaoV2) {
 
        if (hongBaoV2 != null && hongBaoV2.getState() == HongBaoV2.STATE_YILINGQU
                && hongBaoV2.getMoney().compareTo(new BigDecimal(0)) > 0) {
            // 查询对应的订单号
            Long mainHongBaoId = hongBaoV2.getId();
            if (hongBaoV2.getParent() != null && hongBaoV2.getParent().getId() != null)
                mainHongBaoId = hongBaoV2.getParent().getId();
 
            HongBaoOrder hongbaoOrder = hongBaoOrderService.selectDetailByHongBaoId(mainHongBaoId);
            if (hongbaoOrder == null || hongbaoOrder.getCommonOrder() == null)
                return;
            CommonOrder commonOrder = hongbaoOrder.getCommonOrder();
 
            if (hongBaoV2.getType() == HongBaoV2.TYPE_ZIGOU) {// 自购订单
                integralGetService.addRebateOrder(hongBaoV2.getUserInfo().getId(), commonOrder.getOrderNo(),
                        commonOrder.getSourceType());
            } else if (hongBaoV2.getType() == HongBaoV2.TYPE_SHARE_GOODS) {// 分享订单
                integralGetService.addShareOrder(hongBaoV2.getUserInfo().getId(), commonOrder.getOrderNo(),
                        commonOrder.getSourceType());
            } else if (hongBaoV2.getType() == HongBaoV2.TYPE_YIJI || hongBaoV2.getType() == HongBaoV2.TYPE_SHARE_YIJI) {// 一级邀请
                integralGetService.addInviteOrderLevelOne(hongBaoV2.getUserInfo().getId(), commonOrder.getOrderNo(),
                        commonOrder.getSourceType());
            } else if (hongBaoV2.getType() == HongBaoV2.TYPE_ERJI || hongBaoV2.getType() == HongBaoV2.TYPE_SHARE_ERJI) {// 二级邀请
                integralGetService.addInviteOrderLevelTwo(hongBaoV2.getUserInfo().getId(), commonOrder.getOrderNo(),
                        commonOrder.getSourceType());
            }
        }
    }
 
 
    @RabbitListener(queues = "topic_hongbao_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);
 
 
            Long hongBaoId = Long.parseLong(result);
            try {
                HongBaoV2 hongBaoV2 = hongBaoV2Service.selectByPrimaryKey(hongBaoId);
                if (hongBaoV2 != null && hongBaoV2.getState() == HongBaoV2.STATE_YILINGQU
                        && hongBaoV2.getMoney().compareTo(new BigDecimal(0)) > 0) {
                    // 到账送金币
                    fanliGetIntegral(hongBaoV2);
                    // 返利不足0.01元送金币
                    if (hongBaoV2.getType() == HongBaoV2.TYPE_ZIGOU) {// 自购
                        // 判断是否为该用户的首笔红包
                        List<Integer> typeList = new ArrayList<>();
                        typeList.add(HongBaoV2.TYPE_ZIGOU);
                        HongBaoV2 firstHongBao = hongBaoV2Service.getFirstValidHongBaoByTypeAndUid(
                                typeList, hongBaoV2.getUserInfo().getId());
                        if (firstHongBao != null
                                && firstHongBao.getId().longValue() == hongBaoV2.getId()) {// 自购首单到账
                            // 查询下级红包
                            BigDecimal firstLevelMoney = new BigDecimal(0);
                            BigDecimal secondLevelMoney = new BigDecimal(0);
                            // 获取改单下面的红包
                            List<HongBaoV2> hongBaoList = new ArrayList<>();
                            HongBaoOrder hongBaoOrder = hongBaoOrderService
                                    .selectDetailByHongBaoId(hongBaoV2.getId());
                            if (hongBaoOrder != null && hongBaoOrder.getCommonOrder() != null) {// 计算改订单号下面的所有返利金额
                                CommonOrder commonOrder = hongBaoOrder.getCommonOrder();
                                List<CommonOrder> list = commonOrderService.listBySourceTypeAndOrderId(
                                        commonOrder.getSourceType(), commonOrder.getOrderNo());
                                for (CommonOrder co : list) {
                                    HongBaoOrder hbo = hongBaoOrderService
                                            .selectDetailByCommonOrderId(co.getId());
                                    if (hbo != null && hbo.getHongBaoV2() != null)
                                        hongBaoList.add(hbo.getHongBaoV2());
                                }
                            }
 
                            for (HongBaoV2 hb : hongBaoList) {
                                List<HongBaoV2> children = hongBaoV2Service
                                        .listChildrenById(hb.getId());
                                if (children != null)
                                    for (HongBaoV2 child : children)
                                        if (child.getType() == HongBaoV2.TYPE_YIJI) {
                                            firstLevelMoney = firstLevelMoney.add(child.getMoney());
                                        } else if (child.getType() == HongBaoV2.TYPE_ERJI) {
                                            secondLevelMoney = secondLevelMoney.add(child.getMoney());
                                        }
                            }
 
                            // 查询下级的生效时间
                            ThreeSale threeSale = threeSaleSerivce.selectLatestByWorkerIdAndState(
                                    hongBaoV2.getUserInfo().getId(), ThreeSale.STATE_SUCCESS);
                            if (threeSale != null && threeSale.getSucceedTime() != null && threeSale
                                    .getSucceedTime() < hongBaoV2.getCreateTime().getTime()) {// 邀请成功的时间是否在下单的时间之前
                                // 成功时间要大于20190910日才开始送金币
                                if (threeSale.getSucceedTime() > TimeUtil.convertToTimeTemp("20190901",
                                        "yyyyMMdd")) {
                                    if (firstLevelMoney.compareTo(new BigDecimal("0.01")) < 0) {// 一级返利小于0.01
                                        integralTaskRecordService.firstRebateOrderRewardBoss(
                                                threeSale.getBoss().getId(),
                                                hongBaoV2.getUserInfo().getId(), null);
                                        LogHelper.test("自购-一级用户-" + threeSale.getBoss().getId()
                                                + "-hongBaoId:" + hongBaoV2.getId());
                                    }
                                }
                            }
                            // 查询下下级生效时间
                            if (threeSale != null) {
                                threeSale = threeSaleSerivce.selectLatestByWorkerIdAndState(
                                        threeSale.getBoss().getId(), ThreeSale.STATE_SUCCESS);
                                if (threeSale != null && threeSale.getSucceedTime() != null && threeSale
                                        .getSucceedTime() < hongBaoV2.getCreateTime().getTime()) {// 邀请成功的时间是否在下单的时间之前
                                    // 成功时间要大于20190910日才开始送金币
                                    if (threeSale.getSucceedTime() > TimeUtil
                                            .convertToTimeTemp("20190901", "yyyyMMdd")) {
                                        if (secondLevelMoney.compareTo(new BigDecimal("0.01")) < 0) {// 二级返利小于0.01
                                            integralTaskRecordService.firstRebateOrderRewardBossSuper(
                                                    threeSale.getBoss().getId(),
                                                    hongBaoV2.getUserInfo().getId(), null);
                                            LogHelper.test("自购-二级用户-" + threeSale.getBoss().getId()
                                                    + "-hongBaoId:" + hongBaoV2.getId());
                                        }
                                    }
                                }
                            }
                        }
                    } else if (hongBaoV2.getType() == HongBaoV2.TYPE_SHARE_GOODS) {// 分享赚
                        // 判断是否为该用户的首笔红包
                        List<Integer> typeList = new ArrayList<>();
                        typeList.add(HongBaoV2.TYPE_SHARE_GOODS);
                        HongBaoV2 firstHongBao = hongBaoV2Service.getFirstValidHongBaoByTypeAndUid(
                                typeList, hongBaoV2.getUserInfo().getId());
                        if (firstHongBao != null
                                && firstHongBao.getId().longValue() == hongBaoV2.getId()) // 分享首笔到账
                        {
 
                            // 查询下级红包
                            BigDecimal firstLevelMoney = new BigDecimal(0);
                            // 获取改单下面的红包
                            List<HongBaoV2> hongBaoList = new ArrayList<>();
                            HongBaoOrder hongBaoOrder = hongBaoOrderService
                                    .selectDetailByHongBaoId(hongBaoV2.getId());
                            if (hongBaoOrder != null && hongBaoOrder.getCommonOrder() != null) {// 计算改订单号下面的所有返利金额
                                CommonOrder commonOrder = hongBaoOrder.getCommonOrder();
                                List<CommonOrder> list = commonOrderService.listBySourceTypeAndOrderId(
                                        commonOrder.getSourceType(), commonOrder.getOrderNo());
                                for (CommonOrder co : list) {
                                    HongBaoOrder hbo = hongBaoOrderService
                                            .selectDetailByCommonOrderId(co.getId());
                                    if (hbo != null && hbo.getHongBaoV2() != null)
                                        hongBaoList.add(hbo.getHongBaoV2());
                                }
                            }
 
                            for (HongBaoV2 hb : hongBaoList) {
                                List<HongBaoV2> children = hongBaoV2Service
                                        .listChildrenById(hb.getId());
                                if (children != null)
                                    for (HongBaoV2 child : children)
                                        if (child.getType() == HongBaoV2.TYPE_SHARE_YIJI) {
                                            firstLevelMoney = firstLevelMoney.add(child.getMoney());
                                        }
                            }
 
                            ThreeSale threeSale = threeSaleSerivce.selectLatestByWorkerIdAndState(
                                    hongBaoV2.getUserInfo().getId(), ThreeSale.STATE_SUCCESS);
                            if (threeSale != null && threeSale.getSucceedTime() != null && threeSale
                                    .getSucceedTime() < hongBaoV2.getCreateTime().getTime()) {// 邀请成功的时间是否在下单的时间之前
                                // 成功时间要大于20190910日才开始送金币
                                if (threeSale.getSucceedTime() > TimeUtil.convertToTimeTemp("20190901",
                                        "yyyyMMdd")) {
                                    if (firstLevelMoney.compareTo(new BigDecimal("0.01")) < 0) {// 一级返利小于0.01
                                        integralTaskRecordService.firstSharerOrderRewardBoss(
                                                threeSale.getBoss().getId(),
                                                hongBaoV2.getUserInfo().getId(), null);
                                        LogHelper.test("分享-一级用户-" + threeSale.getBoss().getId()
                                                + "-hongBaoId:" + hongBaoV2.getId());
                                    }
                                }
                            }
                        }
                    }
                }
 
            } catch (Exception e) {
                try {
                    LogHelper.errorDetailInfo(e);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
                // 金币不重要无需重复处理
            }
        });
    }
}