yujian
2020-05-11 712fb305d6f79961e879a2f543107e19d7f9e8a2
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
package com.yeshi.fanli.util.rocketmq.consumer.user;
 
import java.util.ArrayList;
import java.util.Date;
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.user.UserTopicTagEnum;
import com.yeshi.fanli.dto.mq.user.body.UserActiveMQMsg;
import com.yeshi.fanli.entity.bus.user.ThreeSale;
import com.yeshi.fanli.entity.bus.user.UserActiveLog;
import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
import com.yeshi.fanli.entity.bus.user.UserInfoRegister;
import com.yeshi.fanli.entity.bus.user.vip.UserVIPPreInfo;
import com.yeshi.fanli.exception.user.vip.UserVIPPreInfoException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.user.UserActiveLogService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.service.inter.user.UserInfoModifyRecordService;
import com.yeshi.fanli.service.inter.user.UserInfoRegisterService;
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.user.vip.TeamUserLevelStatisticService;
import com.yeshi.fanli.service.inter.user.vip.UserVIPPreInfoService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.util.VersionUtil;
import com.yeshi.fanli.util.rocketmq.MQTopicName;
 
/**
 * 同步消息
 * 
 * @author Administrator
 *
 */
@Component
public class SyncBeforeInfoMessageListener implements MessageListener {
 
    @Resource
    private UserInfoModifyRecordService userInfoModifyRecordService;
 
    @Resource
    private TeamUserLevelStatisticService teamUserLevelStatisticService;
 
    @Resource
    private UserActiveLogService userActiveLogService;
 
    @Resource
    private UserInfoRegisterService userInfoRegisterService;
 
    @Resource
    private UserVIPPreInfoService userVIPPreInfoService;
 
    @Resource
    private UserInfoExtraService userInfoExtraService;
 
    @Resource
    private ThreeSaleSerivce threeSaleSerivce;
 
    @Override
    public Action consume(Message message, ConsumeContext context) {
 
        LogHelper.mqInfo("consumer-SyncBeforeInfoMessageListener", message.getMsgID(), message.getTopic(),
                message.getTag(), new String(message.getBody()));
        String tag = message.getTag();
        if (tag == null)
            tag = "";
 
        // 邀请相关
        if (MQTopicName.TOPIC_USER.name().equalsIgnoreCase(message.getTopic())) {
            // 用户活跃
            if (tag.equalsIgnoreCase(UserTopicTagEnum.userActve.name())) {
                UserActiveMQMsg msg = new Gson().fromJson(new String(message.getBody()), UserActiveMQMsg.class);
                Long uid = msg.getUid();
                // 老用户同步绑定信息
 
                userInfoModifyRecordService.syncBeforeInfo(uid);
                // 低版本全部升级普通会员
 
                List<ThreeSale> threeSaleList = threeSaleSerivce.getMyBossDeepList(uid, 2);
                List<Long> uidList = new ArrayList<>();
                uidList.add(uid);
 
                if (threeSaleList != null) {
                    for (ThreeSale ts : threeSaleList) {
                        uidList.add(ts.getBoss().getId());
                    }
                }
                upgradeOldUser(uidList);
            }
        }
        return Action.CommitMessage;
    }
 
    private void upgradeOldUser(List<Long> uidList) {
        for (Long uid : uidList) {
            // 同步老用户等级
            UserActiveLog log = userActiveLogService.getUserLatestActiveInfo(uid);
 
            // 低版本全部升级普通会员
            if (log != null
                    && !VersionUtil.greaterThan_2_1("appstore".equalsIgnoreCase(log.getChannel()) ? "ios" : "android",
                            log.getVersionCode())) {
                UserVIPPreInfo vipInfo = userVIPPreInfoService.getLatestProcessInfo(uid);
                if (vipInfo == null) {
                    vipInfo = userVIPPreInfoService.getLatestProcessInfo(uid);
                    if (vipInfo == null) {// 没有生成高级及以上会员
                        UserInfoExtra extra = userInfoExtraService.getUserInfoExtra(uid);
                        if (extra != null && !StringUtil.isNullOrEmpty(extra.getInviteCode())) {
 
                            List<Long> uids = new ArrayList<>();
                            uids.add(uid);
                            List<UserInfoRegister> list = userInfoRegisterService.listByMultipleUids(uids);
                            if (list != null && list.size() > 0)// 有注册信息
                            {
                                if (list.get(0).getCreateTime().getTime() < Constant.NEW_ORDER_FANLI_RULE_TIME) {// 增加注册时间限制
                                    // 老用户生成普通会员
                                    UserVIPPreInfo vip = new UserVIPPreInfo();
                                    vip.setCreateTime(new Date());
                                    vip.setProcess(UserVIPPreInfo.PROCESS_1);
                                    vip.setUid(uid);
                                    vip.setSourceType(UserVIPPreInfo.SOURCE_TYPE_ORDER);
                                    try {
                                        userVIPPreInfoService.addUserVIPPreInfo(vip);
                                    } catch (UserVIPPreInfoException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    }
                }
            } else if (log != null && VersionUtil.greaterThan_2_1(
                    "appstore".equalsIgnoreCase(log.getChannel()) ? "ios" : "android", log.getVersionCode())) {// 2.1后升级其他等级
                // 线上老用户升级到2.1,自动计算等级
                List<Long> uids = new ArrayList<>();
                uids.add(uid);
                List<UserInfoRegister> list = userInfoRegisterService.listByMultipleUids(uids);
                if (list != null && list.size() > 0)// 有注册信息
                {
                    if (list.get(0).getCreateTime().getTime() < Constant.NEW_ORDER_FANLI_RULE_TIME) {// 老用户
                        // 同步用户等级
                        UserVIPPreInfo vipInfo = userVIPPreInfoService.getLatestProcessInfo(uid);
                        if (vipInfo == null) {
                            // 计算等级
                            userVIPPreInfoService.upgradeVipByMyOrder(uid);
                            userVIPPreInfoService.upgradeVipByTeamNum(uid);
                            vipInfo = userVIPPreInfoService.getLatestProcessInfo(uid);
                            if (vipInfo == null) {// 没有生成高级及以上会员
                                UserInfoExtra extra = userInfoExtraService.getUserInfoExtra(uid);
                                if (extra != null && !StringUtil.isNullOrEmpty(extra.getInviteCode())) {
                                    // 老用户生成普通会员
                                    UserVIPPreInfo vip = new UserVIPPreInfo();
                                    vip.setCreateTime(new Date());
                                    vip.setProcess(UserVIPPreInfo.PROCESS_1);
                                    vip.setUid(uid);
                                    vip.setSourceType(UserVIPPreInfo.SOURCE_TYPE_ORDER);
                                    try {
                                        userVIPPreInfoService.addUserVIPPreInfo(vip);
                                    } catch (UserVIPPreInfoException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        } else {// 之前老版本处理为普通会员的用户升级新版本
                            if (vipInfo.getProcess() == UserVIPPreInfo.PROCESS_1
                                    && vipInfo.getCreateTime().getTime() < TimeUtil
                                            .convertToTimeTemp("2020-04-30 14:10:00", "yyyy-MM-dd HH:mm:ss")) {
                                // 计算等级
                                userVIPPreInfoService.upgradeVipByMyOrder(uid);
                                userVIPPreInfoService.upgradeVipByTeamNum(uid);
                            }
                        }
                    }
                }
            }
        }
    }
}