@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 int getCoin(long userId, long walletId) throws Exception { UserWallet userWallet = userWalletDAOImpl.fetchByUserIdAndWalletId(userId, walletId); if (userWallet != null) { int coin = userWallet.getValue(); return coin; } return 0; }
@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(); } } } }