@Override public void deductCoin(long userId, String subject, int coin) throws Exception { User user = userDAOImpl.fetchByUserId(userId); if (user != null) { String IP = user.getLastLoginIP(); UserWallet userWallet = userWalletDAOImpl.fetchByUserIdAndWalletId(userId, GameConstants.WALLET_TYPE_COIN); if (userWallet != null) { int value = userWallet.getValue(); int currentValue = value - coin; if (currentValue >= 0) { userWallet.setValue(currentValue); userWalletDAOImpl.save(userWallet); UserWalletTransaction userWalletTransaction = new UserWalletTransaction(); userWalletTransaction.setTransactionId(null); userWalletTransaction.setUserWalletId(userWallet.getUserWalletId()); userWalletTransaction.setUserId(userId); userWalletTransaction.setWalletId(GameConstants.WALLET_TYPE_COIN); userWalletTransaction.setTotalSpend(-coin); userWalletTransaction.setSubject(subject); userWalletTransaction.setIp(IP); userWalletTransaction.setCreateDate(new Date()); userWalletTransactionDAOImpl.save(userWalletTransaction); } } } }
@Override public void addCash(long userId, String subject, int cash) throws Exception { User user = userDAOImpl.fetchByUserId(userId); if (user != null) { String IP = user.getLastLoginIP(); UserWallet userWallet = userWalletDAOImpl.fetchByUserIdAndWalletId(userId, GameConstants.WALLET_TYPE_CASH); if (userWallet == null) { if (cash >= 0) { userWallet = new UserWallet(); userWallet.setUserWalletId(null); userWallet.setUserId(userId); userWallet.setWalletId(GameConstants.WALLET_TYPE_CASH); userWallet.setValue(cash); userWallet.setCreateDate(new Date()); userWalletDAOImpl.save(userWallet); // userWalletDAOImpl.flush(); UserWalletTransaction userWalletTransaction = new UserWalletTransaction(); userWalletTransaction.setTransactionId(null); userWalletTransaction.setUserWalletId(userWallet.getUserWalletId()); userWalletTransaction.setUserId(userId); userWalletTransaction.setWalletId(GameConstants.WALLET_TYPE_CASH); userWalletTransaction.setTotalSpend(cash); userWalletTransaction.setSubject(subject); userWalletTransaction.setIp(IP); userWalletTransaction.setCreateDate(new Date()); userWalletTransactionDAOImpl.save(userWalletTransaction); // userWalletTransactionDAOImpl.flush(); } } else { int value = userWallet.getValue(); int currentValue = value + cash; if (currentValue >= 0) { userWallet.setValue(value + cash); userWalletDAOImpl.save(userWallet); // UserWalletDAOImpl.getDAO().flush(); UserWalletTransaction userWalletTransaction = new UserWalletTransaction(); userWalletTransaction.setTransactionId(null); userWalletTransaction.setUserWalletId(userWallet.getUserWalletId()); userWalletTransaction.setUserId(userId); userWalletTransaction.setWalletId(GameConstants.WALLET_TYPE_CASH); userWalletTransaction.setTotalSpend(cash); userWalletTransaction.setSubject(subject); userWalletTransaction.setIp(IP); userWalletTransaction.setCreateDate(new Date()); userWalletTransactionDAOImpl.save(userWalletTransaction); // UserWalletTransactionDAOImpl.getDAO().flush(); } } } }
@Override public WalletVo updateWallet(long userId, int coin, int cash) throws Exception { // assertUserExist(userId); User userProfile = userDAOImpl.fetchByUserId(userId); // UserDAOImpl.getDAO().save(userProfile); long accountId = userProfile.getAccountId(); long cashs = getCash(accountId, GameConstants.WALLET_TYPE_CASH); long coins = getCoin(userId, GameConstants.WALLET_TYPE_COIN); WalletVo walletVo = new WalletVo(); if (coin == 0 && cash == 0) { walletVo.setCash(cashs); walletVo.setCoin(coins); walletVo.setUserId(userId); return walletVo; } if (coin > 0) { addCoin(userId, "Add Coin", coin); } else if (coin < 0) { deductCoin(userId, "Deduct Coin", -coin); } else { // do nothing } if (cash > 0) { addCash(accountId, "Add Cash", cash); } else if (cash < 0) { deductCash(accountId, "Deduct Cash", -cash); } else { // do nothing } long coinNow = coins + coin; long cashNow = cashs + cash; walletVo.setCash(cashNow > 0 ? cashNow : 0); walletVo.setCoin(coinNow > 0 ? coinNow : 0); walletVo.setUserId(userId); return walletVo; }