admin
2019-11-23 486e22c57ca8a1e2dc6877b539e2f3add67250f1
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
package com.yeshi.fanli.util.rocketmq.consumer.redpack;
 
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.aliyun.openservices.ons.api.Producer;
import com.google.gson.Gson;
import com.yeshi.fanli.dto.mq.order.OrderTopicTagEnum;
import com.yeshi.fanli.dto.mq.order.body.OrderMQMsg;
import com.yeshi.fanli.dto.mq.order.body.OrderMoneyRecievedMQMsg;
import com.yeshi.fanli.dto.mq.user.UserTopicTagEnum;
import com.yeshi.fanli.dto.mq.user.body.UserAccountBindingMQMsg;
import com.yeshi.fanli.dto.mq.user.body.UserInviteMQMsg;
import com.yeshi.fanli.dto.mq.user.body.UserRedPackGiftMQMsg;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.redpack.RedPackGiveRecordService;
import com.yeshi.fanli.service.inter.redpack.RedPackWinInviteService;
import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.rocketmq.MQTopicName;
 
/**
 * 红包消费
 * 
 * @author Administrator
 *
 */
@Component
public class RedPackMessageListener implements MessageListener {
 
    @Resource
    private UserSystemCouponService userSystemCouponService;
 
    @Resource
    private RedPackWinInviteService redPackWinInviteService;
 
    @Resource
    private RedPackGiveRecordService redPackGiveRecordService;
 
    @Resource(name = "producer")
    private Producer producer;
 
    @Override
    public Action consume(Message message, ConsumeContext context) {
        LogHelper.mqInfo("consumer-RedPackMessageListener", message.getMsgID(), message.getTopic(), message.getTag(),
                new String(message.getBody()));
        String tag = message.getTag();
        if (tag == null)
            tag = "";
 
        if (MQTopicName.TOPIC_ORDER.name().equalsIgnoreCase(message.getTopic())) {
            if (tag.equalsIgnoreCase(OrderTopicTagEnum.orderStatistic.name())) {// 订单统计
                OrderMQMsg orderMQMsg = new Gson().fromJson(new String(message.getBody()), OrderMQMsg.class);
                // 发送24小时延时消息
                // 测试时发送
                int hour = 24;
                if (Constant.IS_TEST) {
                    hour = 1;
                }
                orderMQMsg.setDelayHour(hour);
                Message msg = new Message(message.getTopic(), OrderTopicTagEnum.orderStatisticDelay.name(),
                        new Gson().toJson(orderMQMsg).getBytes());
                if (orderMQMsg.getStaticticDate().getTime() + 1000 * 60 * 60L * hour > System.currentTimeMillis())// 时间是否已经过了
                    msg.setStartDeliverTime(orderMQMsg.getStaticticDate().getTime() + 1000 * 60 * 60L * hour);
                else
                    msg.setStartDeliverTime(System.currentTimeMillis() + 1000 * 60);
                producer.send(msg);
                return Action.CommitMessage;
            }
        } else if (tag.equalsIgnoreCase(OrderTopicTagEnum.orderStatisticDelay.name())) {// 订单统计
            OrderMQMsg orderMQMsg = new Gson().fromJson(new String(message.getBody()), OrderMQMsg.class);
            int hour = 24;
            if (Constant.IS_TEST) {
                hour = 1;
            }
 
            if (orderMQMsg.getDelayHour() == hour) {
                try {// 邀请奖励
                    redPackWinInviteService.inviteSucceedReward(orderMQMsg.getUid());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return Action.CommitMessage;
        } else if (tag.equalsIgnoreCase(OrderTopicTagEnum.orderFanLiSeparateByOrderNo.name())) {// 订单到账(按订单号处理)
            OrderMoneyRecievedMQMsg orderMoneyRecievedMQMsg = new Gson().fromJson(new String(message.getBody()),
                    OrderMoneyRecievedMQMsg.class);
            if (orderMoneyRecievedMQMsg.getType() == OrderMoneyRecievedMQMsg.TYPE_ZIGOU
                    || orderMoneyRecievedMQMsg.getType() == OrderMoneyRecievedMQMsg.TYPE_SHARE) {
                if (!StringUtil.isNullOrEmpty(orderMoneyRecievedMQMsg.getOrderId())
                        && orderMoneyRecievedMQMsg.getUid() != null) {
                     try {
                         redPackWinInviteService.orderArriveReward(orderMoneyRecievedMQMsg.getUid(), orderMoneyRecievedMQMsg.getSourceType(), orderMoneyRecievedMQMsg.getOrderId());
                     } catch (Exception e) {
                         e.printStackTrace();
                     }
                }
            }
            return Action.CommitMessage;
        } else if (MQTopicName.TOPIC_USER.name().equalsIgnoreCase(message.getTopic())) {
            boolean inviteSucceedReward = false;
            Long uid = null;
            if (tag.equalsIgnoreCase(UserTopicTagEnum.userAccountBinding.name())) {// 账号绑定
                UserAccountBindingMQMsg userAccountBindingMQMsg = new Gson().fromJson(new String(message.getBody()),
                        UserAccountBindingMQMsg.class);
                Integer type = userAccountBindingMQMsg.getType();
                if (type == UserAccountBindingMQMsg.TYPE_PHONE || type == UserAccountBindingMQMsg.TYPE_WX
                        || type == UserAccountBindingMQMsg.TYPE_TAOBAO) {
                    inviteSucceedReward = true;
                    uid = userAccountBindingMQMsg.getUid();
                }
            } else if (tag.equalsIgnoreCase(UserTopicTagEnum.inviteSuccess.name())) {// 邀请成功
                UserInviteMQMsg userInviteMQMsg = new Gson().fromJson(new String(message.getBody()),
                        UserInviteMQMsg.class);
                inviteSucceedReward = true;
                uid = userInviteMQMsg.getWorkerId();
            } else if (tag.equalsIgnoreCase(UserTopicTagEnum.redPackGiftDrawback.name())) {// 红包赠送
                UserRedPackGiftMQMsg userRedPackGiftMQMsg = new Gson().fromJson(new String(message.getBody()),
                        UserRedPackGiftMQMsg.class);
                try { // 红包赠送到期未领取
                    redPackGiveRecordService.overdueByPrimaryKey(userRedPackGiftMQMsg.getId());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return Action.CommitMessage;
            }
 
            if (inviteSucceedReward) {
                try {// 邀请奖励
                    redPackWinInviteService.inviteSucceedReward(uid);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return Action.CommitMessage;
    }
}