admin
2020-05-20 98b1a0affd69bbe63223c21fdd2c404e8bedfccb
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
package com.yeshi.fanli.service.impl.user;
 
import java.util.Date;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
 
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.yeshi.fanli.dao.mybatis.user.UserActiveLogMapper;
import com.yeshi.fanli.dto.mq.user.UserTopicTagEnum;
import com.yeshi.fanli.dto.mq.user.body.UserActiveMQMsg;
import com.yeshi.fanli.entity.bus.user.UserActiveLog;
import com.yeshi.fanli.entity.bus.user.UserInfoRegister;
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.UserInfoRegisterService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.rocketmq.MQMsgBodyFactory;
import com.yeshi.fanli.util.rocketmq.MQTopicName;
 
@Service
public class UserActiveLogServiceImpl implements UserActiveLogService {
 
    @Resource
    private UserActiveLogMapper userActiveLogMapper;
 
    @Resource
    private UserInfoExtraService userInfoExtraService;
 
    @Resource
    private UserInfoRegisterService userInfoRegisterService;
 
    @Resource(name = "producer")
    private Producer producer;
 
    @Override
    public void addUserActiveLog(UserActiveLog userActiveLog) {
        if (userActiveLog == null)
            return;
        if (userActiveLog.getUid() == null || userActiveLog.getUid() == 0)
            return;
 
        UserActiveLog latestLog = getUserLatestActiveInfo(userActiveLog.getUid());
        if (latestLog == null) {
            try { // 保存注册信息
                UserInfoRegister register = new UserInfoRegister();
                register.setId(userActiveLog.getUid());
                register.setIp(userActiveLog.getIp());
                register.setChannel(userActiveLog.getChannel());
                register.setDevice(userActiveLog.getDevice());
                userInfoRegisterService.addRegisterInfo(register);
            } catch (Exception e) {
                LogHelper.errorDetailInfo(e);
            }
        }
 
        // 间隔5分钟以上再记录
        if (latestLog == null || System.currentTimeMillis() - latestLog.getCreateTime().getTime() > 1000 * 60 * 5L) {
            userActiveLog.setCreateTime(new Date());
            userActiveLog.setUpdateTime(new Date());
            userActiveLogMapper.insertSelective(userActiveLog);
            // 更新最新活跃时间
            userInfoExtraService.updateActiveTime(userActiveLog.getUid(), new Date());
            addMQMsg(userActiveLog.getUid());
        } else if (latestLog != null) {
            // 如果设备 ,版本,渠道有变化则需要更改
            String oldIdentify = latestLog.getDevice() + "#" + latestLog.getVersionCode() + "#"
                    + latestLog.getChannel();
            String newIdentify = userActiveLog.getDevice() + "#" + userActiveLog.getVersionCode() + "#"
                    + userActiveLog.getChannel();
            if (!oldIdentify.equalsIgnoreCase(newIdentify)) {// 设备信息变化要记录信息
                userActiveLog.setCreateTime(new Date());
                userActiveLog.setUpdateTime(new Date());
                userActiveLogMapper.insertSelective(userActiveLog);
                // 更新最新活跃时间
                userInfoExtraService.updateActiveTime(userActiveLog.getUid(), new Date());
                addMQMsg(userActiveLog.getUid());
            }
        }
    }
 
    //添加活跃消息
    private void addMQMsg(Long uid) {
        if (Constant.IS_TEST) {
            return;
        }
        
        UserActiveMQMsg msg = new UserActiveMQMsg(uid, new Date());
        Message message = MQMsgBodyFactory.create(MQTopicName.TOPIC_USER, UserTopicTagEnum.userActve, msg);
        message.setStartDeliverTime(System.currentTimeMillis() + 1000 * 5L);// 5s后发送活跃消息
        producer.send(message);
    }
 
    @Override
    public UserActiveLog getUserLatestActiveInfo(Long uid) {
        return userActiveLogMapper.selectLatestByUid(uid);
    }
 
    @Override
    public UserActiveLog getFirstActiveInfo(Long uid) {
        return userActiveLogMapper.selectFirstActiveInfo(uid);
    }
 
    @Override
    public long count90DaysLaterActiveNum(String preDay) {
        Long count = userActiveLogMapper.countActiveNumByDate(preDay);
        if (count == null) {
            count = 0L;
        }
        return count;
    }
 
}