yujian
2020-04-18 db751b1fda37f52134cdc1730a4037138b7f3550
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
package com.yeshi.fanli.service.impl.user.invite;
 
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.DateUtil;
 
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.yeshi.fanli.dao.mybatis.user.UserInviteSeparateMapper;
import com.yeshi.fanli.dto.mq.user.UserTopicTagEnum;
import com.yeshi.fanli.dto.mq.user.body.ThreeSaleSeparateMQMsg;
import com.yeshi.fanli.entity.bus.user.ThreeSale;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.bus.user.UserInviteSeparate;
import com.yeshi.fanli.entity.bus.user.vip.UserVIPPreInfo;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.service.inter.user.UserInviteSeparateService;
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.user.invite.UserInviteMsgNotificationService;
import com.yeshi.fanli.service.inter.user.invite.UserInviteValidNumService;
import com.yeshi.fanli.service.inter.user.invite.UserInviteValidRecordService;
import com.yeshi.fanli.service.inter.user.vip.UserVIPPreInfoService;
import com.yeshi.fanli.service.inter.user.vip.UserVipConfigService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.rocketmq.MQMsgBodyFactory;
import com.yeshi.fanli.util.rocketmq.MQTopicName;
 
@Service
public class UserInviteSeparateServiceImpl implements UserInviteSeparateService {
 
    @Resource
    private UserInviteSeparateMapper userInviteSeparateMapper;
 
    @Resource
    private UserVipConfigService userVipConfigService;
 
    @Lazy
    @Resource
    private ThreeSaleSerivce threeSaleSerivce;
 
    @Lazy
    @Resource
    private UserVIPPreInfoService userVIPPreInfoService;
 
    @Lazy
    @Resource
    private UserInfoService userInfoService;
 
    @Lazy
    @Resource
    private UserInviteMsgNotificationService userInviteMsgNotificationService;
 
    @Lazy
    @Resource
    private UserInviteValidRecordService userInviteValidRecordService;
 
    @Lazy
    @Resource
    private UserInviteValidNumService userInviteValidNumService;
    
    @Resource(name = "producer")
    private Producer producer;
    
    
 
    @Override
    public void insertSelective(UserInviteSeparate record) {
        userInviteSeparateMapper.insertSelective(record);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addPreSeparateRecord(Long workerId, Long bossId) {
        // 更新之前状态失效
        userInviteSeparateMapper.updateStateByWorkerIdAndBossId(workerId, bossId, UserInviteSeparate.STATE_INVALID);
 
        // 限制天数
        int limitDays = Integer.parseInt(userVipConfigService.getValueByKey("invite_separate_limit_days"));
 
        // 保存记录
        UserInviteSeparate inviteSeparate = new UserInviteSeparate();
        inviteSeparate.setBossId(bossId);
        inviteSeparate.setWorkerId(workerId);
        inviteSeparate.setState(UserInviteSeparate.STATE_INIT);
        inviteSeparate.setEndTime(DateUtil.plusDayDate(limitDays, new Date()));
        inviteSeparate.setCreateTime(new Date());
        inviteSeparate.setUpdateTime(new Date());
        userInviteSeparateMapper.insertSelective(inviteSeparate);
    }
 
    @Override
    public void updateInvalidByBossId(Long uid) {
        userInviteSeparateMapper.updateInvalidByBossId(uid);
    }
 
    @Override
    public void updateStateByWorkerIdAndBossId(Long workerId, Long bossId, int state) {
        userInviteSeparateMapper.updateStateByWorkerIdAndBossId(workerId, bossId, state);
    }
 
    @Override
    public UserInviteSeparate selectByWorkerIdAndBossId(Long workerId, Long bossId) {
        return userInviteSeparateMapper.selectByWorkerIdAndBossId(workerId, bossId);
    }
 
    @Override
    public List<UserInviteSeparate> getHandleOverdue(int start, int count) {
        return userInviteSeparateMapper.getHandleOverdue(start, count);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void inviteSeparate(UserInviteSeparate record) {
        if (record == null)
            return;
 
        Long workerId = record.getWorkerId();
        Long bossId = record.getBossId();
        ThreeSale threeSale = threeSaleSerivce.getMyBoss(workerId);
        if (threeSale == null) {
            // 已经成功脱离 不发消息
            userInviteSeparateMapper.updateStateByWorkerIdAndBossId(workerId, bossId, UserInviteSeparate.STATE_SUCCESS);
            return;
        }
 
        boolean separate = false;
        UserVIPPreInfo info = userVIPPreInfoService.getLatestProcessInfo(bossId);
        if (info == null) {
            separate = true;
        } else if (info.getProcess() == UserVIPPreInfo.PROCESS_1) {
            if (info.getCreateTime().getTime() < record.getEndTime().getTime()) {
                separate = true; // 成为会员 晚于 脱离时间
            }
        } else if (info.getProcess() == UserVIPPreInfo.PROCESS_2) {
            Date upTime = info.getCreateTime();
            UserVIPPreInfo info1 = userVIPPreInfoService.getVipByProcess(bossId, UserVIPPreInfo.PROCESS_1);
            if (info1 != null) {
                upTime = info1.getCreateTime();
            }
 
            if (upTime.getTime() < record.getEndTime().getTime()) {
                separate = true; // 成为会员 晚于 脱离时间
            }
        }
 
        // 未脱离
        if (!separate) {
            userInviteSeparateMapper.updateStateByWorkerIdAndBossId(workerId, bossId, UserInviteSeparate.STATE_INVALID);
            return;
        }
 
        // 脱离关系
        userInviteSeparateMapper.updateStateByWorkerIdAndBossId(workerId, bossId, UserInviteSeparate.STATE_SUCCESS);
        // 脱离邀请关系
        threeSaleSerivce.inviteSeparate(workerId, bossId);
        
        if (!Constant.IS_TEST) {
            ThreeSaleSeparateMQMsg msg = new ThreeSaleSeparateMQMsg(bossId, workerId , new Date());
            Message message = MQMsgBodyFactory.create(MQTopicName.TOPIC_USER, UserTopicTagEnum.threeSaleSeparate, msg);
            producer.send(message);
        }
        
        // 直接有效粉丝脱离-1
        userInviteValidNumService.reduceValidNumFirst(bossId, workerId);
        // 间接有效粉丝脱离-1
        ThreeSale threeSaleSuper = threeSaleSerivce.getMyBoss(bossId);
        if (threeSaleSuper != null) {
            Long bossIdSuper = threeSaleSuper.getBoss().getId();
            userInviteValidNumService.reduceValidNumSecond(bossIdSuper, workerId);
        }
        
        // 通知上级消息
        try {
            // 提醒上级脱离
            Date date = new Date();
            UserInfo worker = userInfoService.selectByPKey(workerId);
            String nickName = worker.getNickName();
            userInviteMsgNotificationService.fansDivorced(bossId, nickName, date);
 
            // 提醒上上级脱离
            if (threeSaleSuper != null) {
                Long bossIdSuper = threeSaleSuper.getBoss().getId();
                UserInfo boss = userInfoService.selectByPKey(workerId);
                userInviteMsgNotificationService.fansDivorcedIndirect(bossIdSuper, nickName, boss.getNickName(), date);
            }
        } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
        }
    }
 
}