yujian
2020-04-27 d5c2554ebae75afbff31c3c2966cc3a747ba941c
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
package com.yeshi.fanli.util.rocketmq.consumer.order;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Component;
 
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.google.gson.Gson;
import com.yeshi.fanli.dto.mq.order.OrderTopicTagEnum;
import com.yeshi.fanli.dto.mq.order.body.OrderConfirmMQMsg;
import com.yeshi.fanli.dto.mq.order.body.OrderMQMsg;
import com.yeshi.fanli.dto.mq.order.body.OrderWeiQuanMQMsg;
import com.yeshi.fanli.dto.mq.user.UserTopicTagEnum;
import com.yeshi.fanli.dto.mq.user.body.UserForbiddenMQMsg;
import com.yeshi.fanli.dto.order.UserTeamLevel;
import com.yeshi.fanli.entity.bus.user.ThreeSale;
import com.yeshi.fanli.entity.bus.user.vip.UserLevelEnum;
import com.yeshi.fanli.entity.order.CommonOrder;
import com.yeshi.fanli.exception.ParamsException;
import com.yeshi.fanli.exception.money.TeamDividentsDebtException;
import com.yeshi.fanli.exception.order.dividents.TeamDividentsSourceOrderException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.order.CommonOrderService;
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleSerivce;
import com.yeshi.fanli.service.manger.order.TeamDividentsManager;
import com.yeshi.fanli.service.manger.order.TeamDividentsSourceManager;
import com.yeshi.fanli.service.manger.user.UserLevelManager;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.order.CommonOrderUtil;
import com.yeshi.fanli.util.rocketmq.MQTopicName;
 
/**
 * 订单分红消息
 * @author Administrator
 *
 */
@Component
public class OrderDividentsMessageListener implements MessageListener {
 
    @Resource
    private CommonOrderService commonOrderService;
 
    @Resource
    private TeamDividentsManager teamDividentsManager;
 
    @Resource
    private TeamDividentsSourceManager teamDividentsSourceManager;
 
    @Resource
    private ThreeSaleSerivce threeSaleSerivce;
 
    @Resource
    private UserLevelManager userLevelManager;
 
    public OrderDividentsMessageListener() {
 
    }
 
    @Override
    public Action consume(Message message, ConsumeContext context) {
        LogHelper.mqInfo("consumer:OrderDividentsMessageListener", message.getMsgID(), message.getTopic(),
                message.getTag(), new String(message.getBody()));
        String tag = message.getTag();
 
        if (MQTopicName.TOPIC_USER.name().equalsIgnoreCase(message.getTopic())) {
            if (tag.equalsIgnoreCase(UserTopicTagEnum.forbiddenUser.name())) {
                // 查询上级红包
                UserForbiddenMQMsg dto = new Gson().fromJson(new String(message.getBody()), UserForbiddenMQMsg.class);
                if (dto != null) {
                    teamDividentsSourceManager.invalidBySourceUid(dto.getUid(), "用户被封禁");
                    return Action.CommitMessage;
                }
            }
        } else if (MQTopicName.TOPIC_ORDER.name().equalsIgnoreCase(message.getTopic())) {
            if (tag.equalsIgnoreCase(OrderTopicTagEnum.orderStatistic.name())) {
 
                OrderMQMsg dto = new Gson().fromJson(new String(message.getBody()), OrderMQMsg.class);
                if (dto == null || dto.getStaticticDate().getTime() < Constant.NEW_ORDER_FANLI_RULE_TIME)
                    return Action.CommitMessage;
 
                if (dto.isMiandan())// 免单的商品不参与分红
                    return Action.CommitMessage;
 
                if (dto.getHandleType() == OrderMQMsg.HANDLE_TYPE_ADD) {
                    List<CommonOrder> commonOrderList = commonOrderService.listBySourceTypeAndOrderId(dto.getType(),
                            dto.getOrderId());
 
                    int state = CommonOrderUtil.getState(commonOrderList);
                    if (commonOrderList.size() > 0 && state != CommonOrder.STATE_SX) {
                        List<UserTeamLevel> bossList = new ArrayList<>();
                        List<ThreeSale> threeSalesList = threeSaleSerivce.getMyBossDeepList(dto.getUid(), 100);
                        if (threeSalesList != null)
                            for (ThreeSale ts : threeSalesList) {
                                UserLevelEnum level = userLevelManager.getUserLevel(ts.getBoss().getId());
                                bossList.add(new UserTeamLevel(ts.getBoss().getId(), level));
                            }
                        try {
                            teamDividentsSourceManager.addSource(commonOrderList, bossList);
                            return Action.CommitMessage;
                        } catch (TeamDividentsSourceOrderException e) {
                            if (e.getCode() == TeamDividentsSourceOrderException.CODE_EXIST) {
                                return Action.CommitMessage;
                            }
                        } catch (ParamsException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    teamDividentsSourceManager.updateOrderMoney(dto.getOrderId(), dto.getType());
                }
 
            } else if (tag.equalsIgnoreCase(OrderTopicTagEnum.orderConfirm.name())) {// 订单确认收货
                OrderConfirmMQMsg dto = new Gson().fromJson(new String(message.getBody()), OrderConfirmMQMsg.class);
                teamDividentsSourceManager.orderSettle(dto.getOrderNo(), dto.getSourceType());
            } else if (tag.equalsIgnoreCase(OrderTopicTagEnum.taoBaoOrderWeiQuan.name())) {
                OrderWeiQuanMQMsg dto = new Gson().fromJson(new String(message.getBody()), OrderWeiQuanMQMsg.class);
                try {
                    teamDividentsManager.weiQuan(dto.getOrderNo(), dto.getSourceType());
                    return Action.CommitMessage;
                } catch (TeamDividentsDebtException e) {
                    e.printStackTrace();
                }
            }
        }
        return Action.CommitMessage;
    }
}