public void saveActiveUsersInPlatform(Long uid, String os, String version, String ip) {
if(uid == null || uid == 0)return;
UsersDTO usersDTO = usersManager.getUser(uid);
if (usersDTO == null) {
return;
}
//在线记录 user_online_record,
//通过用户在线状态,进而更新用户连续每天在线情况
Date date = new Date();
String today = DateFormatUtils.YYYY_MM_DD.date2Str(date);
//昨天
Date day = DateTimeUtils.addDay(date, -1);
String yesterday = DateFormatUtils.YYYY_MM_DD.date2Str(day);
Date seven = DateTimeUtils.addDay(date, -7);
String sevenDay = DateFormatUtils.YYYY_MM_DD.date2Str(seven);
String todayCacheKey = CacheKey.getCacheKey(CacheKey.USER_DAY_ONLINE_RECORD, today);
String uidStr = uid.toString();
//今天登录记录
String onlineFlag = redisManager.hget(todayCacheKey, uidStr);
//记录用户在线记录
redisManager.hset(todayCacheKey, uidStr, "1");
//7天前登录记录
String sevenDayLogin = redisManager.hget(CacheKey.getCacheKey(CacheKey.USER_DAY_ONLINE_RECORD, sevenDay), uidStr);
//昨天前登录记录
String yesterdayLogin = redisManager.hget(CacheKey.getCacheKey(CacheKey.USER_DAY_ONLINE_RECORD, yesterday), uidStr);
if (StringUtils.isNotEmpty(onlineFlag)) {
NewUserListenCountDto countDto = listenCountDao.selectByUid(uid);
if (countDto != null) {
if(org.apache.commons.lang.StringUtils.isEmpty(yesterdayLogin)) {
//更新用户为非连续登录
listenCountDao.updateContinueFlagByUid(uid);
} else {
//在线天数加1
listenCountDao.updateOnLineDayByUid(uid);
}
}
} else{
//今天没有在线记录
UserOnlineRecordDto recordDto = new UserOnlineRecordDto();
recordDto.setUid(uid);
recordDto.setNewFlag(1);
//7天前登录过 表示老用户
if(StringUtils.isNotEmpty(sevenDayLogin)){
recordDto.setNewFlag(2);
}
recordDto.setCreateTime(date);
recordDto.setUpdateTime(date);
recordDto.setOs(os);
recordDto.setIp(ip);
recordDto.setVersion(version);
//记录入库
recordDao.save(recordDto);
}
redisManager.expire(todayCacheKey, 7, TimeUnit.DAYS);
}
1 画出代码逻辑流程图
2 找出代码逻辑缺陷和代码缺陷;
3 设计一个更优方案当前存在的问题和预计后期维护成本;