Sfoglia il codice sorgente

扣减用户余额

dos 1 mese fa
parent
commit
d247e12a3b

+ 5 - 0
game-business/src/main/java/com/game/business/mapper/AppUserMapper.java

@@ -64,4 +64,9 @@ public interface AppUserMapper extends BaseMapper<AppUser> {
 
     @Select("select * from app_user where pid = 0 and agent_flag = 1")
     public List<AppUser> selectTeamUserList();
+
+    /**
+     * 更新用户钱包
+     * */
+    public int updateUserMoney(AppUser appUser);
 }

+ 6 - 0
game-business/src/main/java/com/game/business/service/IAppUserService.java

@@ -3,6 +3,7 @@ package com.game.business.service;
 import java.util.List;
 import com.game.business.domain.AppUser;
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.game.business.domain.FinTranRecord;
 
 /**
  * app用户Service接口
@@ -77,4 +78,9 @@ public interface IAppUserService extends IService<AppUser> {
     public List<AppUser> selectListByIds(List<Long> ids);
 
     Long nextId();
+
+    /**
+     * 更新用户余额、插入流水
+     * */
+    int updateUserAmount(FinTranRecord finTranRecord)  throws Exception;
 }

+ 51 - 49
game-business/src/main/java/com/game/business/service/impl/AppUserCountServiceImpl.java

@@ -77,58 +77,60 @@ public class AppUserCountServiceImpl extends ServiceImpl<AppUserCountMapper, App
 //    @Async
     @Transactional
     public void createAppUserCount(AppUserCount appUserCount) {
-        //查询当前用户当日是否已经生成记录
-        LambdaQueryWrapper<AppUserCount> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(AppUserCount::getStrDate,appUserCount.getStrDate());
-        queryWrapper.eq(AppUserCount::getUserId,appUserCount.getUserId());
-        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()) {
+        synchronized ("app_user_count_".concat(String.valueOf(appUserCount.getUserId()))) {
+            //查询当前用户当日是否已经生成记录
+            LambdaQueryWrapper<AppUserCount> queryWrapper = new LambdaQueryWrapper<>();
+            queryWrapper.eq(AppUserCount::getStrDate, appUserCount.getStrDate());
+            queryWrapper.eq(AppUserCount::getUserId, appUserCount.getUserId());
+            AppUserCount userCount = appUserCountMapper.selectOne(queryWrapper);
+            int res = 0;
+            if (userCount == null) {
+                appUserCount.setCreateTime(new Date());
                 appUserCount.setUpdateTime(new Date());
-            }*/
-            if(!appUserCount.isUpdate()) {
-                //非全量更新
-                if (appUserCount.getRechargeAmount() != null) {
-                    appUserCount.setRechargeAmount(userCount.getRechargeAmount() + appUserCount.getRechargeAmount());
-                }
-                if (appUserCount.getRechargeCommission() != null) {
-                    appUserCount.setRechargeCommission(userCount.getRechargeCommission() + appUserCount.getRechargeCommission());
-                }
-                if (appUserCount.getRechargeGive() != null) {
-                    appUserCount.setRechargeGive(userCount.getRechargeGive() + appUserCount.getRechargeGive());
-                }
-                if (appUserCount.getWithdrawalCommission() != null) {
-                    appUserCount.setWithdrawalAmount(userCount.getWithdrawalAmount() + appUserCount.getWithdrawalAmount());
-                }
-                if (appUserCount.getWithdrawalCommission() != null) {
-                    appUserCount.setWithdrawalCommission(userCount.getWithdrawalCommission() + appUserCount.getWithdrawalCommission());
-                }
-                if (appUserCount.getLiveUseAmount() != null) {
-                    appUserCount.setLiveUseAmount(userCount.getLiveUseAmount() + appUserCount.getLiveUseAmount());
-                }
-                if (appUserCount.getLiveCommission() != null) {
-                    appUserCount.setLiveCommission(userCount.getLiveCommission() + appUserCount.getLiveCommission());
-                }
-                if (appUserCount.getGameWinAmount() != null) {
-                    appUserCount.setGameWinAmount(userCount.getGameWinAmount() + appUserCount.getGameWinAmount());
-                }
-                if (appUserCount.getGameLoseAmount() != null) {
-                    appUserCount.setGameLoseAmount(userCount.getGameLoseAmount() + appUserCount.getGameLoseAmount());
-                }
-                if (appUserCount.getGameCommission() != null) {
-                    appUserCount.setGameCommission(userCount.getGameCommission() + appUserCount.getGameCommission());
-                }
-                if (appUserCount.getGameBetting() != null) {
-                    appUserCount.setGameBetting(userCount.getGameBetting() + appUserCount.getGameBetting());
+                res = appUserCountMapper.insert(appUserCount);
+            } else {
+                appUserCount.setId(userCount.getId());
+                /*if(null == appUserCount.getUpdateTime()) {
+                    appUserCount.setUpdateTime(new Date());
+                }*/
+                if (!appUserCount.isUpdate()) {
+                    //非全量更新
+                    if (appUserCount.getRechargeAmount() != null) {
+                        appUserCount.setRechargeAmount(userCount.getRechargeAmount() + appUserCount.getRechargeAmount());
+                    }
+                    if (appUserCount.getRechargeCommission() != null) {
+                        appUserCount.setRechargeCommission(userCount.getRechargeCommission() + appUserCount.getRechargeCommission());
+                    }
+                    if (appUserCount.getRechargeGive() != null) {
+                        appUserCount.setRechargeGive(userCount.getRechargeGive() + appUserCount.getRechargeGive());
+                    }
+                    if (appUserCount.getWithdrawalCommission() != null) {
+                        appUserCount.setWithdrawalAmount(userCount.getWithdrawalAmount() + appUserCount.getWithdrawalAmount());
+                    }
+                    if (appUserCount.getWithdrawalCommission() != null) {
+                        appUserCount.setWithdrawalCommission(userCount.getWithdrawalCommission() + appUserCount.getWithdrawalCommission());
+                    }
+                    if (appUserCount.getLiveUseAmount() != null) {
+                        appUserCount.setLiveUseAmount(userCount.getLiveUseAmount() + appUserCount.getLiveUseAmount());
+                    }
+                    if (appUserCount.getLiveCommission() != null) {
+                        appUserCount.setLiveCommission(userCount.getLiveCommission() + appUserCount.getLiveCommission());
+                    }
+                    if (appUserCount.getGameWinAmount() != null) {
+                        appUserCount.setGameWinAmount(userCount.getGameWinAmount() + appUserCount.getGameWinAmount());
+                    }
+                    if (appUserCount.getGameLoseAmount() != null) {
+                        appUserCount.setGameLoseAmount(userCount.getGameLoseAmount() + appUserCount.getGameLoseAmount());
+                    }
+                    if (appUserCount.getGameCommission() != null) {
+                        appUserCount.setGameCommission(userCount.getGameCommission() + appUserCount.getGameCommission());
+                    }
+                    if (appUserCount.getGameBetting() != null) {
+                        appUserCount.setGameBetting(userCount.getGameBetting() + appUserCount.getGameBetting());
+                    }
                 }
+                res = appUserCountMapper.updateById(appUserCount);
             }
-            res = appUserCountMapper.updateById(appUserCount);
         }
     }
 

+ 62 - 0
game-business/src/main/java/com/game/business/service/impl/AppUserServiceImpl.java

@@ -3,14 +3,22 @@ package com.game.business.service.impl;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 
+import java.rmi.ServerException;
 import java.util.Collections;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
+import com.game.business.domain.FinTranRecord;
+import com.game.business.service.IFinTranRecordService;
 import com.game.common.annotation.DataSource;
 import com.game.common.enums.DataSourceType;
 import com.game.common.utils.DateUtils;
+import com.game.common.utils.MoneyUtils;
 import com.game.common.utils.StringUtils;
+import lombok.extern.log4j.Log4j2;
 import lombok.val;
+import org.redisson.api.RLock;
+import org.redisson.api.RedissonClient;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import com.game.business.mapper.AppUserMapper;
@@ -24,10 +32,17 @@ import com.game.business.service.IAppUserService;
  * @date 2024-06-18
  */
 @Service
+@Log4j2
 public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> implements IAppUserService {
     @Autowired
     private AppUserMapper appUserMapper;
 
+    @Autowired
+    private RedissonClient redisson;
+
+    @Autowired
+    private IFinTranRecordService finTranRecordService;
+
     /**
      * 查询app用户
      *
@@ -147,4 +162,51 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> impl
         Long id = appUserMapper.nextId();
         return  id < 10000 ? 10000 : id;
     }
+
+    @Override
+    @DataSource(DataSourceType.SLAVE)
+    public int updateUserAmount(FinTranRecord finTranRecord) throws Exception{
+        int res = 0;
+        RLock rLock = redisson.getLock("app_user_amount_".concat(String.valueOf(finTranRecord.getUid())));
+        boolean tryLock = false;
+        try {
+            tryLock = rLock.tryLock(10L,10L, TimeUnit.SECONDS); //等待10秒 最多持有锁10秒
+            if(tryLock){
+                AppUser appUser = appUserMapper.selectAppUserByUserid(finTranRecord.getUid());
+                if(null != appUser){
+                    AppUser updateAppUser = new AppUser();
+                    updateAppUser.setUserid(appUser.getUserid());
+                    if(2 == finTranRecord.getCurrencyType().intValue()){
+                        //金币
+                        updateAppUser.setCoin(finTranRecord.getCoinChange());
+                        updateAppUser.setCoinCash(MoneyUtils.consumeCash(updateAppUser.getCoin(),updateAppUser.getCoinCash(),finTranRecord.getCoinChange()));
+                    }else if(4 == finTranRecord.getCurrencyType().intValue()){
+                        //余额
+                        updateAppUser.setDiamondCoin(finTranRecord.getDiamondCoinChange());
+                        updateAppUser.setDiamondCoinTotal(finTranRecord.getDiamondCoinChange());
+                        updateAppUser.setDiamondCoinCash(MoneyUtils.consumeCash(updateAppUser.getDiamondCoin(),updateAppUser.getDiamondCoinCash(),finTranRecord.getDiamondCoinChange()));
+                    }
+                    res = appUserMapper.updateUserMoney(updateAppUser);
+                    if(res > 0) {
+                        AppUser afterUser = appUserMapper.selectAppUserByUserid(finTranRecord.getUid());
+                        if (2 == finTranRecord.getCurrencyType().intValue()) {
+                            finTranRecord.setAfterCoin(afterUser.getCoin() + finTranRecord.getCoinChange());
+                        } else if (4 == finTranRecord.getCurrencyType().intValue()) {
+                            finTranRecord.setAfterDiamondCoin(afterUser.getDiamondCoin() + finTranRecord.getDiamondCoinChange());
+                        }
+                        res = finTranRecordService.insertFinTranRecord(finTranRecord);
+                    }
+                }
+            }else{
+                log.info("用户{}钱包更新失败:{}",finTranRecord.getUid(),tryLock);
+            }
+        } catch (InterruptedException e) {
+            log.info("用户{}钱包更新失败:{}",finTranRecord.getUid(),e);
+        } finally {
+            if(res < 1){
+                throw new Exception("更新失败");
+            }
+        }
+        return res;
+    }
 }

+ 14 - 0
game-business/src/main/resources/mapper/business/AppUserMapper.xml

@@ -832,4 +832,18 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     <select id="nextId" resultType="java.lang.Long">
         select max(userid) + 1 from app_user
     </select>
+
+    <update id="updateUserMoney" parameterType="com.game.business.domain.AppUser">
+        update app_user
+        <trim prefix="SET" suffixOverrides=",">
+            <if test="coin != null">
+                coin = coin + #{coin}
+            </if>
+            <if test="coinCash != null">coin_cash = coin_cash + #{coinCash},</if>
+            <if test="diamondCoin != null">diamond_coin = diamond_coin + #{diamondCoin},</if>
+            <if test="diamondCoinCash != null">diamond_coin_cash = diamond_coin_cash + #{diamondCoinCash},</if>
+            <if test="diamondCoinTotal != null">diamond_coin_total = diamond_coin_total + #{diamondCoinTotal}</if>
+        </trim>
+        where userid = #{userid}
+    </update>
 </mapper>