Kaynağa Gözat

修复佣金统计丢失问题

dos 1 ay önce
ebeveyn
işleme
9fa7ba13ca

+ 5 - 0
game-business/src/main/java/com/game/business/domain/AppUserCount.java

@@ -111,6 +111,11 @@ public class AppUserCount {
     @ApiModelProperty(value = "最后一次更新时间(用于直播、充值、提现增量统计)")
     private Date countTime;
 
+    @Excel(name = "游戏最后一次更新时间")
+    @TableField(value = "game_count_time")
+    @ApiModelProperty(value = "最后一次更新时间(用于游戏返佣)")
+    private Date gameCountTime;
+
     //是否全量更新
     @TableField(exist = false)
     private boolean isUpdate = false;

+ 8 - 0
game-business/src/main/java/com/game/business/service/IAppUserCountService.java

@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
 import com.game.business.domain.AppGameBetting;
 import com.game.business.domain.AppGameCommission;
 import com.game.business.domain.AppUserCount;
+import com.game.business.domain.AppUserGameRecordCount;
 
 import java.util.List;
 import java.util.Date;
@@ -30,10 +31,17 @@ public interface IAppUserCountService extends IService<AppUserCount> {
 
     public Date selectLastDate();
 
+    public Date selectGameLastDate(Long userId);
+
     public void staticsUserBetting(AppGameBetting appGameBetting,List<AppGameCommission> gameRateList);
 
     /**
      * 查询用户代理统计
      * */
     public List<AppUserCount> getListByUserId(long userId,String beginTime,String endTIme);
+
+    /**
+     * 记录当天已经统计过的用户游戏返佣
+     * */
+    public void setGameCache(List<AppUserGameRecordCount> gameRecordCountList);
 }

+ 82 - 30
game-business/src/main/java/com/game/business/service/impl/AppUserCountServiceImpl.java

@@ -7,8 +7,10 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.game.business.domain.*;
 import com.game.business.mapper.AppUserCountMapper;
 import com.game.business.service.*;
+import com.game.common.constant.CacheConstants;
 import com.game.common.constant.finance.FinTranType1;
 import com.game.common.constant.finance.FinTranType3;
+import com.game.common.core.redis.RedisCache;
 import com.game.common.entity.KeyValue;
 import com.game.common.utils.DateUtils;
 import com.game.common.utils.StringUtils;
@@ -55,6 +57,9 @@ public class AppUserCountServiceImpl extends ServiceImpl<AppUserCountMapper, App
     @Autowired
     private IAppUserAgentService appUserAgentService;
 
+    @Autowired
+    private RedisCache redisCache;
+
     @Override
     public List<AppUserCount> getAppUserCount(long userId,String beginTime,String endTIme) {
         LambdaQueryWrapper<AppUserCount> queryWrapper = new LambdaQueryWrapper<AppUserCount>();
@@ -184,7 +189,7 @@ public class AppUserCountServiceImpl extends ServiceImpl<AppUserCountMapper, App
         if(null == appUserCount || null == appUserCount.getUpdateTime()) {
             return DateUtil.beginOfDay(DateUtils.getNowDate());
         }
-        return DateUtil.offsetMillisecond(appUserCount.getUpdateTime(), 1);  //往前加1秒用于增量更新
+        return DateUtil.offsetSecond(appUserCount.getUpdateTime(), 1);  //往前加1秒用于增量更新
     }
 
     /**
@@ -193,45 +198,80 @@ public class AppUserCountServiceImpl extends ServiceImpl<AppUserCountMapper, App
     @Override
     public Date selectLastDate() {
        LambdaQueryWrapper<AppUserCount> queryWrapper = new LambdaQueryWrapper<>();
-       queryWrapper.orderByDesc(AppUserCount::getUpdateTime);
+       queryWrapper.orderByDesc(AppUserCount::getCountTime);
        queryWrapper.last("limit 1");
        AppUserCount appUserCount = appUserCountMapper.selectOne(queryWrapper);
        if(null == appUserCount || null == appUserCount.getCountTime()) {
            return DateUtil.beginOfDay(DateUtils.getNowDate());
        }
-       return DateUtil.offsetMillisecond(appUserCount.getCountTime(), 1);  //往前加1毫秒用于增量更新
+       return DateUtil.offsetSecond(appUserCount.getCountTime(), 1);  //往前加1秒用于增量更新
+    }
+
+    /**
+     * 查询最后更新时间
+     * */
+    @Override
+    public Date selectGameLastDate(Long userId) {
+       LambdaQueryWrapper<AppUserCount> queryWrapper = new LambdaQueryWrapper<>();
+       queryWrapper.orderByDesc(AppUserCount::getGameCountTime);
+       queryWrapper.eq(AppUserCount::getUserId,userId);
+       queryWrapper.last("limit 1");
+       AppUserCount appUserCount = appUserCountMapper.selectOne(queryWrapper);
+       if(null == appUserCount || null == appUserCount.getGameCountTime()) {
+           return DateUtil.beginOfDay(DateUtils.getNowDate());
+       }
+       return appUserCount.getGameCountTime();  //往前加1秒用于增量更新
     }
 
     @Override
     public void staticsUserBetting(AppGameBetting appGameBetting,List<AppGameCommission> gameRateList) {
-        Date curDate = DateUtils.getNowDate();
-        String dateTime = DateUtils.parseDateToStr("yyyy-MM-dd", curDate);
+        synchronized (CacheConstants.USER_GAME_RECORD_COUNT.concat(String.valueOf(appGameBetting.getGameId())).concat(appGameBetting.getGameDate())) {
+            Date curDate = DateUtils.getNowDate();
+            String dateTime = DateUtils.parseDateToStr("yyyy-MM-dd", curDate);
+    /*List<Long> userIds = gameRateList.stream().map(AppGameCommission::getUserId).collect(Collectors.toList());
+                    userIds.add(appGameBetting.getUserId());
+                    userIds = userIds.stream().distinct().collect(Collectors.toList());*/
+            //        List<AppUserGameRecordCount> gameRecordCountList = appUserGameRecordCountService.selectByDate(null, null, null,appGameBetting.getGameId(),appGameBetting.getGameDate());
 
-        List<AppUserGameRecordCount> gameRecordCountList = appUserGameRecordCountService.selectByDate(null, null, null,appGameBetting.getGameId(),appGameBetting.getGameDate());
-        if (null != gameRateList && gameRateList.size() > 0) {
-            List<Long> userIds = gameRateList.stream().map(AppGameCommission::getUserId).collect(Collectors.toList());
-            userIds.add(appGameBetting.getUserId());
-            userIds = userIds.stream().distinct().collect(Collectors.toList());
-            String finalDateTime = dateTime;
-            Date finalCurDate = curDate;
-            userIds.forEach(userId -> {
-                AppUserCount appUserCount = new AppUserCount();
-                appUserCount.setUserId(userId);
-                appUserCount.setStrDate(finalDateTime);
-                appUserCount.setAgentUserId(gameRateList.stream().filter(e->e.getUserId().equals(userId)).map(AppGameCommission::getPid).collect(Collectors.toList()).get(0));
-                appUserCount.setUpdateTime(finalCurDate);
-                if(null != gameRecordCountList && gameRecordCountList.size() > 0) {
-                    //游戏佣金 更新所有用户
-                    appUserCount.setGameCommission(gameRecordCountList.stream().filter(e -> e.getUserId().equals(userId)).mapToDouble(e -> e.getCommission()).sum());
-                }
-                //游戏下注金额 更新当前下注用户 (非金币)
-                if(userId.equals(appGameBetting.getUserId()) && appGameBetting.getBettingType().intValue() == 0) {
-                    appUserCount.setGameBetting(appGameBetting.getBettingAmount());
-                }
-                this.createAppUserCount(appUserCount);
-            });
-            //更新周盈亏数据
-            appUserCountDividendService.reloadCache(null,userIds);
+            if (null != gameRateList && gameRateList.size() > 0) {
+
+                String finalDateTime = dateTime;
+                Date finalCurDate = curDate;
+                List<Long> userIds = new ArrayList<>();
+                gameRateList.forEach(gameRate -> {
+                    Long userId = gameRate.getUserId();
+                    //用户最后一次更新时间
+                    Date updateTime = this.selectGameLastDate(userId);
+                    if (!DateUtil.format(updateTime, "yyyy-MM-dd").equals(DateUtil.format(finalCurDate, "yyyy-MM-dd"))) {
+                        //跨天直接从当日零点开始统计
+                        updateTime = DateUtil.beginOfDay(finalCurDate);
+                    }
+                    //查询用户返佣
+                    List<AppUserGameRecordCount> gameRecordCountList = appUserGameRecordCountService.selectByDate(userId, DateUtil.format(updateTime, "yyyy-MM-dd HH:mm:ss"), DateUtil.format(finalCurDate, "yyyy-MM-dd HH:mm:ss"), gameRate.getGameId(), appGameBetting.getGameDate());
+                    userIds.add(userId);
+                    AppUserCount appUserCount = new AppUserCount();
+                    appUserCount.setUserId(userId);
+                    appUserCount.setStrDate(finalDateTime);
+                    appUserCount.setAgentUserId(gameRateList.stream().filter(e -> e.getUserId().equals(userId)).map(AppGameCommission::getPid).collect(Collectors.toList()).get(0));
+                    appUserCount.setUpdateTime(finalCurDate);
+                    appUserCount.setGameCountTime(finalCurDate);
+                    if (null != gameRecordCountList && gameRecordCountList.size() > 0) {
+                        //游戏佣金
+                        Map<String, Double> map = redisCache.getCacheMap(CacheConstants.USER_GAME_RECORD_COUNT.concat(String.valueOf(userId)));
+                        appUserCount.setGameCommission(gameRecordCountList.stream().filter(e -> {
+                            return null == map || null == map.get(String.valueOf(e.getId())); //过滤已统计过的id
+                        }).mapToDouble(e -> e.getCommission()).sum());
+                        setGameCache(gameRecordCountList);
+                    }
+                    //游戏下注金额 更新当前下注用户 (非金币)
+                    if (userId.equals(appGameBetting.getUserId()) && appGameBetting.getBettingType().intValue() == 0) {
+                        appUserCount.setGameBetting(appGameBetting.getBettingAmount());
+                    }
+                    this.createAppUserCount(appUserCount);
+                });
+                //更新周盈亏数据
+                appUserCountDividendService.reloadCache(null, userIds);
+            }
         }
     }
 
@@ -248,6 +288,16 @@ public class AppUserCountServiceImpl extends ServiceImpl<AppUserCountMapper, App
         return appUserCountMapper.selectList(queryWrapper);
     }
 
+    @Override
+    public void setGameCache(List<AppUserGameRecordCount> gameRecordCountList) {
+        if(null != gameRecordCountList && gameRecordCountList.size() > 0) {
+            gameRecordCountList.forEach(e->{
+                //凌晨定时任务清除前一天的数据
+                redisCache.setCacheMapValue(CacheConstants.USER_GAME_RECORD_COUNT.concat(String.valueOf(e.getUserId())),String.valueOf(e.getId()),e.getCommission());
+            });
+        }
+    }
+
     /**
      * 金额为负数则转为正数
      * */
@@ -255,4 +305,6 @@ public class AppUserCountServiceImpl extends ServiceImpl<AppUserCountMapper, App
         return money<0?money*-1:money;
     }
 
+
+
 }

+ 1 - 0
game-business/src/main/java/com/game/business/task/AppAgentGameBettingTask.java

@@ -67,6 +67,7 @@ public class AppAgentGameBettingTask {
             AppGameCommission appGameCommission = new AppGameCommission();
             appGameCommission.setUserId(appGameBetting.getUserId());
             appGameCommission.setPid(userAgent.getPid());
+            appGameCommission.setGameRate(0L);
             gameRateList.add(appGameCommission);
             appUserCountService.staticsUserBetting(appGameBetting,gameRateList);
             return;

+ 14 - 0
game-business/src/main/java/com/game/business/task/AppUserCountTask.java

@@ -6,6 +6,7 @@ import com.baomidou.dynamic.datasource.annotation.DSTransactional;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.game.business.domain.*;
 import com.game.business.service.*;
+import com.game.common.constant.CacheConstants;
 import com.game.common.constant.finance.FinTranType1;
 import com.game.common.constant.finance.FinTranType3;
 import com.game.common.core.redis.RedisCache;
@@ -72,6 +73,8 @@ public class AppUserCountTask {
             if (StringUtils.isNotBlank(dateTime)) {
                 curDate = DateUtil.parse(dateTime, "yyyy-MM-dd");
             } else {
+                //清除前一天游戏返佣缓存
+
                 dateTime = DateUtils.parseDateToStr("yyyy-MM-dd", curDate);
             }
             AppUser queryUser = new AppUser();
@@ -83,6 +86,7 @@ public class AppUserCountTask {
             List<AppUserLiveDividedRecord> liveDividedRecordList = appUserLiveDividedRecordService.selectByDate(null, dateTime.concat(" 00:00:00"), dateTime.concat(" 23:59:59"));
             List<AppUserGameRecordCount> gameRecordCountList = appUserGameRecordCountService.selectByDate(null, dateTime.concat(" 00:00:00"), dateTime.concat(" 23:59:59"),null,null);
             List<AppGameBetting> gameBettingList = appGameBettingService.selectListByDate(null, dateTime.concat(" 00:00:00"), dateTime.concat(" 23:59:59"),null,null);
+            Date nowDate = new Date();
             if (null != list && list.size() > 0) {
                 List<AppUserCount> appUserCountList = new ArrayList<>();
                 String finalDateTime = dateTime;
@@ -92,6 +96,9 @@ public class AppUserCountTask {
                     appUserCount.setStrDate(finalDateTime);
                     appUserCount.setAgentUserId(appUser.getPid());
                     appUserCount.setUpdate(true);//全量更新
+                    appUserCount.setCountTime(nowDate);
+                    appUserCount.setGameCountTime(nowDate);
+                    appUserCount.setUpdateTime(nowDate);
                     //充值总额
                     appUserCount.setRechargeAmount(appUsersChargeList.stream().filter(e -> e.getUid().equals(appUser.getUserid())).mapToDouble(e -> e.getCoin().doubleValue()).sum());
                     //提现金额
@@ -138,6 +145,8 @@ public class AppUserCountTask {
                     appUserCountService.createAppUserCount(appUserCount);
                 });
             }
+            //统计游戏返佣缓存
+            appUserCountService.setGameCache(gameRecordCountList);
 
             //统计当日用户游戏输赢
             if (null != gameBettingList && gameBettingList.size() > 0) {
@@ -436,6 +445,11 @@ public class AppUserCountTask {
         appUserCountDividendService.reloadCache(dateTime,StringUtils.isBlank(topUserId)?null:Arrays.asList(Long.valueOf(topUserId)));
     }
 
+    //凌晨清除缓存
+    public void cleanCache(){
+        redisCache.deleteObject(CacheConstants.USER_GAME_RECORD_COUNT.concat("*"));
+    }
+
     /**
      * 金额为负数则转为正数
      * */

+ 5 - 0
game-common/src/main/java/com/game/common/constant/CacheConstants.java

@@ -47,4 +47,9 @@ public class CacheConstants
      */
     public static final String USER_AGENT_TEAM_PHASE = "user_agent_team_phase";
 
+    /**
+     * 用户已经统计过的游戏返佣
+     */
+    public static final String USER_GAME_RECORD_COUNT = "user_game_record_count:";
+
 }