Explorar el Código

改为增量更新用户充值、提现、直播数据

dos hace 2 meses
padre
commit
9e17bd8a6e

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

@@ -8,6 +8,7 @@ import com.game.common.annotation.Excel;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
+import java.util.Date;
 
 
 @Data
@@ -84,4 +85,23 @@ public class AppUserCount {
     @TableField(value = "game_commission")
     @ApiModelProperty(value = "游戏佣金")
     private double gameCommission;
+
+    @Excel(name = "创建时间")
+    @TableField(value = "create_time")
+    @ApiModelProperty(value = "创建时间")
+    private Date createTime;
+
+    @Excel(name = "更新时间")
+    @TableField(value = "update_time")
+    @ApiModelProperty(value = "更新时间 (用于游戏增量统计)")
+    private Date updateTime;
+
+    @Excel(name = "最后一次更新时间")
+    @TableField(value = "count_time")
+    @ApiModelProperty(value = "最后一次更新时间(用于直播、充值、提现增量统计)")
+    private Date countTime;
+
+    //是否全量更新
+    @TableField(exist = false)
+    private boolean isUpdate = false;
 }

+ 5 - 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.AppUserCount;
 
 import java.util.List;
+import java.util.Date;
 
 public interface IAppUserCountService extends IService<AppUserCount> {
 
@@ -14,4 +15,8 @@ public interface IAppUserCountService extends IService<AppUserCount> {
 
     public void staticsUserCount(Long gameId,String dateTime);
 
+    public Date selectLastUpdateDate();
+
+    public Date selectLastDate();
+
 }

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

@@ -2,6 +2,7 @@ package com.game.business.service.impl;
 
 import cn.hutool.core.date.DateUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.game.business.domain.*;
 import com.game.business.mapper.AppUserCountMapper;
@@ -18,6 +19,7 @@ import org.springframework.stereotype.Service;
 import java.util.*;
 import java.util.function.Function;
 import java.util.stream.Collectors;
+import java.util.Date;
 
 @Service
 public class AppUserCountServiceImpl extends ServiceImpl<AppUserCountMapper, AppUserCount> implements IAppUserCountService {
@@ -64,9 +66,47 @@ public class AppUserCountServiceImpl extends ServiceImpl<AppUserCountMapper, App
         AppUserCount userCount = appUserCountMapper.selectOne(queryWrapper);
         int res = 0;
         if(userCount==null) {
+            appUserCount.setCreateTime(new Date());
+            appUserCount.setUpdateTime(new Date());
             res = appUserCountMapper.insert(appUserCount);
         }else{
             appUserCount.setId(userCount.getId());
+            if(null == appUserCount.getUpdateTime()) {
+                appUserCount.setUpdateTime(new Date());
+            }
+            if(!appUserCount.isUpdate()) {
+                //非全量更新
+                if (appUserCount.getRechargeAmount() > 0) {
+                    appUserCount.setRechargeAmount(userCount.getRechargeAmount() + appUserCount.getRechargeAmount());
+                }
+                if (appUserCount.getRechargeCommission() > 0) {
+                    appUserCount.setRechargeCommission(userCount.getRechargeCommission() + appUserCount.getRechargeCommission());
+                }
+                if (appUserCount.getRechargeGive() > 0) {
+                    appUserCount.setRechargeGive(userCount.getRechargeGive() + appUserCount.getRechargeGive());
+                }
+                if (appUserCount.getWithdrawalCommission() > 0) {
+                    appUserCount.setWithdrawalAmount(userCount.getWithdrawalAmount() + appUserCount.getWithdrawalAmount());
+                }
+                if (appUserCount.getWithdrawalCommission() > 0) {
+                    appUserCount.setWithdrawalCommission(userCount.getWithdrawalCommission() + appUserCount.getWithdrawalCommission());
+                }
+                if (appUserCount.getLiveUseAmount() > 0) {
+                    appUserCount.setLiveUseAmount(userCount.getLiveUseAmount() + appUserCount.getLiveUseAmount());
+                }
+                if (appUserCount.getLiveCommission() > 0) {
+                    appUserCount.setLiveCommission(userCount.getLiveCommission() + appUserCount.getLiveCommission());
+                }
+                if (appUserCount.getGameWinAmount() > 0) {
+                    appUserCount.setGameWinAmount(userCount.getGameWinAmount() + appUserCount.getGameWinAmount());
+                }
+                if (appUserCount.getGameLoseAmount() > 0) {
+                    appUserCount.setGameLoseAmount(userCount.getGameLoseAmount() + appUserCount.getGameLoseAmount());
+                }
+                if (appUserCount.getGameCommission() > 0) {
+                    appUserCount.setGameCommission(userCount.getGameCommission() + appUserCount.getGameCommission());
+                }
+            }
             res = appUserCountMapper.updateById(appUserCount);
         }
     }
@@ -79,46 +119,27 @@ public class AppUserCountServiceImpl extends ServiceImpl<AppUserCountMapper, App
         } else {
             dateTime = DateUtils.parseDateToStr("yyyy-MM-dd", curDate);
         }
+
+        Date lastDate = this.selectLastUpdateDate();
+        String lastDateStr = DateUtil.format(lastDate,"yyyy-MM-dd HH:mm:ss");
+        if(!dateTime.equals(DateUtil.format(lastDate,"yyyy-MM-dd"))){
+            lastDateStr = DateUtil.format(curDate, "yyyy-MM-dd").concat(" 00:00:00");
+        }
+        String endTimeStr = DateUtil.format(curDate,"yyyy-MM-dd HH:mm:ss");
         AppUser queryUser = new AppUser();
         queryUser.setAgentFlag(1);
         List<AppUser> list = appUserService.selectAppUserList(queryUser);//查询所有代理用户
-        List<AppUsersCharge> appUsersChargeList = appUsersChargeService.getChargeList(null, dateTime.concat(" 00:00:00"), dateTime.concat(" 23:59:59"));
-        List<AppUsersCashrecord> appUsersCashrecords = appUsersCashrecordService.getUserCashList(null, dateTime.concat(" 00:00:00"), dateTime.concat(" 23:59:59"));
-        List<FinTranRecord> finTranRecordList = finTranRecordService.getUserRecordList(null, dateTime.concat(" 00:00:00"), dateTime.concat(" 23:59:59"));
-        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"));
-        List<AppGameBetting> gameBettingList = appGameBettingService.selectListByDate(null, dateTime.concat(" 00:00:00"), dateTime.concat(" 23:59:59"),null);
+        List<AppUserGameRecordCount> gameRecordCountList = appUserGameRecordCountService.selectByDate(null, lastDateStr, endTimeStr);
+        List<AppGameBetting> gameBettingList = appGameBettingService.selectListByDate(null, lastDateStr, endTimeStr,null);
         if (null != list && list.size() > 0) {
             String finalDateTime = dateTime;
+            Date finalCurDate = curDate;
             list.forEach(appUser -> {
                 AppUserCount appUserCount = new AppUserCount();
                 appUserCount.setUserId(appUser.getUserid());
                 appUserCount.setStrDate(finalDateTime);
                 appUserCount.setAgentUserId(appUser.getPid());
-                //充值总额
-                appUserCount.setRechargeAmount(appUsersChargeList.stream().filter(e -> e.getUid().equals(appUser.getUserid())).mapToDouble(e -> e.getMoney().doubleValue()).sum());
-                //提现金额
-                appUserCount.setWithdrawalAmount(appUsersCashrecords.stream().filter(e -> e.getUid().equals(appUser.getUserid())).mapToDouble(e -> e.getMoney().doubleValue()).sum());
-                //充值服务费/手续费
-                appUserCount.setRechargeCommission(appUsersChargeList.stream().filter(e -> e.getUid().equals(appUser.getUserid())).mapToDouble(e -> e.getPlatformService()).sum());
-                //充值赠送余额
-                appUserCount.setRechargeGive(finTranRecordList.stream().filter(e -> {
-                    return e.getUid().equals(appUser.getUserid()) &&
-                            e.getTranType1().intValue() == FinTranType1.U_Income_Coin_Balance.getType() &&
-                            e.getTranType3().intValue() == FinTranType3.CHARGE_IN_REWARD.getType();
-                }).mapToDouble(e->e.getCoinChange()).sum());
-                //提现服务费/手续费
-                appUserCount.setWithdrawalCommission(appUsersCashrecords.stream().filter(e -> e.getUid().equals(appUser.getUserid())).mapToDouble(e -> e.getService().doubleValue()).sum());
-                //直播消费金额
-                Map<String, KeyValue> type1Keys = Arrays.stream(FinTranType1.getKeyValues(1)).collect(Collectors.toMap(e -> e.a, Function.identity(), (k1, k2) -> k2)); //支出交易类型
-                Map<String, KeyValue> type3Keys = Arrays.stream(FinTranType3.getKeyValues(1)).collect(Collectors.toMap(e -> e.a, Function.identity(), (k1, k2) -> k2)); //支出交易类型
-                appUserCount.setLiveUseAmount(finTranRecordList.stream().filter(e -> {
-                    return e.getUid().equals(appUser.getUserid()) &&
-                            null != type1Keys.get(String.valueOf(e.getTranType1().intValue())) &&
-                            null != type3Keys.get(String.valueOf(e.getTranType3().intValue()));
-                }).mapToDouble(e -> dealMoney(e.getCoinChange())).sum());
-                //直播佣金
-                appUserCount.setLiveCommission(liveDividedRecordList.stream().filter(e -> e.getUserId().equals(appUser.getUserid())).mapToDouble(e -> e.getMoney().doubleValue()).sum());
+                appUserCount.setUpdateTime(finalCurDate);
                 //游戏佣金
                 appUserCount.setGameCommission(gameRecordCountList.stream().filter(e -> e.getUserId().equals(appUser.getUserid())).mapToDouble(e -> e.getCommission()).sum());
                 //游戏输
@@ -138,6 +159,33 @@ public class AppUserCountServiceImpl extends ServiceImpl<AppUserCountMapper, App
         }
     }
 
+    @Override
+    public Date selectLastUpdateDate() {
+        LambdaQueryWrapper<AppUserCount> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.orderByDesc(AppUserCount::getUpdateTime);
+        queryWrapper.last("limit 1");
+        AppUserCount appUserCount = appUserCountMapper.selectOne(queryWrapper);
+        if(null == appUserCount || null == appUserCount.getUpdateTime()) {
+            return DateUtil.beginOfDay(DateUtils.getNowDate());
+        }
+        return DateUtil.offsetMillisecond(appUserCount.getUpdateTime(), 1);  //往前加1毫秒用于增量更新
+    }
+
+    /**
+     * 查询最后更新时间
+     * */
+    @Override
+    public Date selectLastDate() {
+       LambdaQueryWrapper<AppUserCount> queryWrapper = new LambdaQueryWrapper<>();
+       queryWrapper.orderByDesc(AppUserCount::getUpdateTime);
+       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毫秒用于增量更新
+    }
+
     /**
      * 金额为负数则转为正数
      * */

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

@@ -85,6 +85,7 @@ public class AppUserCountTask {
                     appUserCount.setUserId(appUser.getUserid());
                     appUserCount.setStrDate(finalDateTime);
                     appUserCount.setAgentUserId(appUser.getPid());
+                    appUserCount.setUpdate(true);//全量更新
                     //充值总额
                     appUserCount.setRechargeAmount(appUsersChargeList.stream().filter(e -> e.getUid().equals(appUser.getUserid())).mapToDouble(e -> e.getMoney().doubleValue()).sum());
                     //提现金额
@@ -162,6 +163,70 @@ public class AppUserCountTask {
 
     }
 
+    /**
+     * 统计用户直播/充值 (增量更新 1分钟)
+     * */
+    public void statisticsLive(){
+        log.info("开始统计用户每日直播、游戏、佣金");
+        try {
+            Date curDate = new Date();
+            String dateTime = DateUtil.format(curDate, "yyyy-MM-dd");
+            Date lastDate = appUserCountService.selectLastDate();
+            String lastDateStr = DateUtil.format(lastDate,"yyyy-MM-dd HH:mm:ss");
+            if(!dateTime.equals(DateUtil.format(lastDate,"yyyy-MM-dd"))){
+                lastDateStr = DateUtil.format(curDate, "yyyy-MM-dd").concat(" 00:00:00");
+            }
+            String endTimeStr = DateUtil.format(curDate,"yyyy-MM-dd HH:mm:ss");
+            AppUser queryUser = new AppUser();
+            queryUser.setAgentFlag(1);
+            List<AppUser> list = appUserService.selectAppUserList(queryUser);//查询所有代理用户
+            List<AppUsersCharge> appUsersChargeList = appUsersChargeService.getChargeList(null, lastDateStr, endTimeStr);
+            List<AppUsersCashrecord> appUsersCashrecords = appUsersCashrecordService.getUserCashList(null, lastDateStr, endTimeStr);
+            List<FinTranRecord> finTranRecordList = finTranRecordService.getUserRecordList(null, lastDateStr, endTimeStr);
+            List<AppUserLiveDividedRecord> liveDividedRecordList = appUserLiveDividedRecordService.selectByDate(null, lastDateStr, endTimeStr);
+            if (null != list && list.size() > 0) {
+                List<AppUserCount> appUserCountList = new ArrayList<>();
+                String finalDateTime = dateTime;
+                list.forEach(appUser -> {
+                    AppUserCount appUserCount = new AppUserCount();
+                    appUserCount.setUserId(appUser.getUserid());
+                    appUserCount.setStrDate(finalDateTime);
+                    appUserCount.setAgentUserId(appUser.getPid());
+                    appUserCount.setCountTime(curDate);
+                    //充值总额
+                    appUserCount.setRechargeAmount(appUsersChargeList.stream().filter(e -> e.getUid().equals(appUser.getUserid())).mapToDouble(e -> e.getMoney().doubleValue()).sum());
+                    //提现金额
+                    appUserCount.setWithdrawalAmount(appUsersCashrecords.stream().filter(e -> e.getUid().equals(appUser.getUserid())).mapToDouble(e -> e.getMoney().doubleValue()).sum());
+                    //充值服务费/手续费
+                    appUserCount.setRechargeCommission(appUsersChargeList.stream().filter(e -> e.getUid().equals(appUser.getUserid())).mapToDouble(e -> e.getPlatformService()).sum());
+                    //充值赠送余额
+                    appUserCount.setRechargeGive(finTranRecordList.stream().filter(e -> {
+                        return e.getUid().equals(appUser.getUserid()) &&
+                                e.getTranType1().intValue() == FinTranType1.U_Income_Coin_Balance.getType() &&
+                                e.getTranType3().intValue() == FinTranType3.CHARGE_IN_REWARD.getType();
+                    }).mapToDouble(e->e.getCoinChange()).sum());
+                    //提现服务费/手续费
+                    appUserCount.setWithdrawalCommission(appUsersCashrecords.stream().filter(e -> e.getUid().equals(appUser.getUserid())).mapToDouble(e -> e.getService().doubleValue()).sum());
+                    //直播消费金额
+                    Map<String, KeyValue> type1Keys = Arrays.stream(FinTranType1.getKeyValues(1)).collect(Collectors.toMap(e -> e.a, Function.identity(), (k1, k2) -> k2)); //支出交易类型
+                    Map<String, KeyValue> type3Keys = Arrays.stream(FinTranType3.getKeyValues(1)).collect(Collectors.toMap(e -> e.a, Function.identity(), (k1, k2) -> k2)); //支出交易类型
+                    appUserCount.setLiveUseAmount(finTranRecordList.stream().filter(e -> {
+                        return e.getUid().equals(appUser.getUserid()) &&
+                                null != type1Keys.get(String.valueOf(e.getTranType1().intValue())) &&
+                                null != type3Keys.get(String.valueOf(e.getTranType3().intValue()));
+                    }).mapToDouble(e -> dealMoney(e.getCoinChange())).sum());
+                    //直播佣金
+                    appUserCount.setLiveCommission(liveDividedRecordList.stream().filter(e -> e.getUserId().equals(appUser.getUserid())).mapToDouble(e -> e.getMoney().doubleValue()).sum());
+                    appUserCountService.createAppUserCount(appUserCount);
+                });
+            }
+        }catch (Exception e){
+            log.info("用户每日直播、佣金统计异常:{}",e.getMessage());
+        }
+        log.info("用户每日直播、佣金统计完成");
+
+    }
+
     /**
      * 计算分红
      * @param dateTime 格式yyyy-MM-dd