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;
|
}
|
});
|
}
|
}
|