| Line 1... |
Line 1... |
| 1 |
package com.spice.profitmandi.service.wallet;
|
1 |
package com.spice.profitmandi.service.wallet;
|
| 2 |
|
2 |
|
| 3 |
import java.time.LocalDateTime;
|
3 |
import java.time.LocalDateTime;
|
| 4 |
import java.util.List;
|
4 |
import java.util.List;
|
| 5 |
|
5 |
|
| - |
|
6 |
import org.slf4j.Logger;
|
| - |
|
7 |
import org.slf4j.LoggerFactory;
|
| 6 |
import org.springframework.beans.factory.annotation.Autowired;
|
8 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 7 |
import org.springframework.stereotype.Component;
|
9 |
import org.springframework.stereotype.Component;
|
| 8 |
|
10 |
|
| 9 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
11 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 10 |
import com.spice.profitmandi.dao.entity.dtr.UserAccounts;
|
12 |
import com.spice.profitmandi.dao.entity.dtr.UserAccounts;
|
| Line 13... |
Line 15... |
| 13 |
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
|
15 |
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
|
| 14 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
16 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
| 15 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
|
17 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
|
| 16 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
|
18 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
|
| 17 |
|
19 |
|
| - |
|
20 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
| - |
|
21 |
|
| 18 |
@Component
|
22 |
@Component
|
| 19 |
public class WalletServiceImpl implements WalletService {
|
23 |
public class WalletServiceImpl implements WalletService {
|
| 20 |
|
24 |
|
| - |
|
25 |
private static final Logger LOGGER = LoggerFactory.getLogger(WalletServiceImpl.class);
|
| - |
|
26 |
|
| 21 |
@Autowired
|
27 |
@Autowired
|
| 22 |
UserAccountRepository userAccountRepository;
|
28 |
UserAccountRepository userAccountRepository;
|
| 23 |
|
29 |
|
| 24 |
@Autowired
|
30 |
@Autowired
|
| 25 |
UserWalletRepository userWalletRepository;
|
31 |
UserWalletRepository userWalletRepository;
|
| 26 |
|
32 |
|
| 27 |
@Autowired
|
33 |
@Autowired
|
| 28 |
UserWalletHistoryRepository userWalletHistoryRepository;
|
34 |
UserWalletHistoryRepository userWalletHistoryRepository;
|
| 29 |
|
35 |
|
| 30 |
@Override
|
36 |
@Override
|
| - |
|
37 |
public void addAmountToWallet(int retailerId, int referenceId, WalletReferenceType referenceType, String description,
|
| - |
|
38 |
float amount) throws ProfitMandiBusinessException{
|
| - |
|
39 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
| - |
|
40 |
userWallet.setAmount(userWallet.getAmount() + Float.valueOf(amount).intValue());
|
| - |
|
41 |
userWalletRepository.persist(userWallet);
|
| - |
|
42 |
this.createUserWalletHistory(amount, userWallet.getId(), referenceId, referenceType, description);
|
| - |
|
43 |
}
|
| - |
|
44 |
|
| - |
|
45 |
@Override
|
| - |
|
46 |
public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
|
| - |
|
47 |
String description, float amount) throws ProfitMandiBusinessException {
|
| - |
|
48 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
| - |
|
49 |
if(amount > userWallet.getAmount()){
|
| - |
|
50 |
LOGGER.error("Wallet Balance is insufficient!");
|
| - |
|
51 |
throw new ProfitMandiBusinessException("balance", userWallet.getAmount(), "WLT_1000");
|
| - |
|
52 |
}
|
| - |
|
53 |
userWallet.setAmount(userWallet.getAmount() - Float.valueOf(amount).intValue());
|
| - |
|
54 |
userWalletRepository.persist(userWallet);
|
| - |
|
55 |
this.createUserWalletHistory(amount, userWallet.getId(), referenceId, referenceType, description);
|
| - |
|
56 |
}
|
| - |
|
57 |
|
| - |
|
58 |
private void createUserWalletHistory(float amount, int walletId, int referenceId, WalletReferenceType referenceType, String description){
|
| - |
|
59 |
UserWalletHistory userWalletHistory = new UserWalletHistory();
|
| - |
|
60 |
userWalletHistory.setWalletId(walletId);
|
| - |
|
61 |
userWalletHistory.setAmount(Float.valueOf(amount).intValue());
|
| - |
|
62 |
userWalletHistory.setReference(referenceId);
|
| - |
|
63 |
userWalletHistory.setReferenceType(referenceType);
|
| - |
|
64 |
userWalletHistory.setTimestamp(LocalDateTime.now());
|
| - |
|
65 |
userWalletHistory.setDescription(description);
|
| - |
|
66 |
userWalletHistoryRepository.persit(userWalletHistory);
|
| - |
|
67 |
}
|
| - |
|
68 |
|
| - |
|
69 |
|
| - |
|
70 |
@Override
|
| 31 |
public UserWallet getUserWalletByUserId(int userId) throws ProfitMandiBusinessException {
|
71 |
public UserWallet getUserWalletByUserId(int userId) throws ProfitMandiBusinessException {
|
| 32 |
UserAccounts userAccount = userAccountRepository.getUserAccountByType(userId, AccountType.saholic);
|
72 |
UserAccounts userAccount = userAccountRepository.getUserAccountByType(userId, AccountType.saholic);
|
| 33 |
Integer retailerId = Integer.valueOf(userAccount.getAccount_key());
|
73 |
return userWalletRepository.selectByRetailerId(Integer.valueOf(userAccount.getAccount_key()));
|
| 34 |
try {
|
- |
|
| 35 |
return userWalletRepository.selectByRetailerId(retailerId);
|
- |
|
| 36 |
} catch (ProfitMandiBusinessException pbe) {
|
- |
|
| 37 |
UserWallet uw = new UserWallet();
|
- |
|
| 38 |
uw.setAmount(0);
|
- |
|
| 39 |
uw.setRefundableAmount(0);
|
- |
|
| 40 |
uw.setUserId(retailerId);
|
- |
|
| 41 |
return uw;
|
- |
|
| 42 |
}
|
- |
|
| 43 |
}
|
74 |
}
|
| 44 |
|
75 |
|
| 45 |
@Override
|
76 |
@Override
|
| 46 |
public List<UserWalletHistory> getUserWalletHistoryByUserId(int userId) throws ProfitMandiBusinessException {
|
77 |
public List<UserWalletHistory> getUserWalletHistoryByUserId(int userId) throws ProfitMandiBusinessException {
|
| 47 |
UserWallet userWallet = this.getUserWalletByUserId(userId);
|
78 |
UserWallet userWallet = this.getUserWalletByUserId(userId);
|