@Override public void addCoin(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) { if (coin >= 0) { userWallet = new UserWallet(); userWallet.setUserWalletId(null); userWallet.setUserId(userId); userWallet.setWalletId(GameConstants.WALLET_TYPE_COIN); userWallet.setValue(coin); 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_COIN); userWalletTransaction.setTotalSpend(coin); userWalletTransaction.setSubject(subject); userWalletTransaction.setIp(IP); userWalletTransaction.setCreateDate(new Date()); userWalletTransactionDAOImpl.save(userWalletTransaction); // userWalletTransactionDAOImpl.flush(); } } else { int value = userWallet.getValue(); int currentValue = value + coin; if (currentValue >= 0) { userWallet.setValue(value + coin); 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_COIN); userWalletTransaction.setTotalSpend(coin); userWalletTransaction.setSubject(subject); userWalletTransaction.setIp(IP); userWalletTransaction.setCreateDate(new Date()); userWalletTransactionDAOImpl.save(userWalletTransaction); // UserWalletTransactionDAOImpl.flush(); } } } }