| 22550 |
ashik.ali |
1 |
package com.spice.profitmandi.service.wallet;
|
|
|
2 |
|
|
|
3 |
import java.time.LocalDateTime;
|
| 23110 |
ashik.ali |
4 |
import java.util.HashMap;
|
| 22550 |
ashik.ali |
5 |
import java.util.List;
|
| 23110 |
ashik.ali |
6 |
import java.util.Map;
|
|
|
7 |
import java.util.Set;
|
| 22550 |
ashik.ali |
8 |
|
| 23568 |
govind |
9 |
import org.apache.logging.log4j.Logger;
|
|
|
10 |
import org.apache.logging.log4j.LogManager;
|
| 22550 |
ashik.ali |
11 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
12 |
import org.springframework.stereotype.Component;
|
|
|
13 |
|
|
|
14 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 23269 |
ashik.ali |
15 |
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
|
| 22550 |
ashik.ali |
16 |
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
|
|
|
17 |
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
|
|
|
18 |
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
|
|
|
19 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
|
|
20 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
|
|
|
21 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
|
|
|
22 |
|
| 22857 |
ashik.ali |
23 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
|
|
24 |
|
| 22550 |
ashik.ali |
25 |
@Component
|
|
|
26 |
public class WalletServiceImpl implements WalletService {
|
|
|
27 |
|
| 23568 |
govind |
28 |
private static final Logger LOGGER = LogManager.getLogger(WalletServiceImpl.class);
|
| 22857 |
ashik.ali |
29 |
|
| 22550 |
ashik.ali |
30 |
@Autowired
|
| 22925 |
ashik.ali |
31 |
private UserAccountRepository userAccountRepository;
|
| 22550 |
ashik.ali |
32 |
|
|
|
33 |
@Autowired
|
| 22925 |
ashik.ali |
34 |
private UserWalletRepository userWalletRepository;
|
| 22550 |
ashik.ali |
35 |
|
|
|
36 |
@Autowired
|
| 22925 |
ashik.ali |
37 |
private UserWalletHistoryRepository userWalletHistoryRepository;
|
| 22550 |
ashik.ali |
38 |
|
|
|
39 |
@Override
|
| 22857 |
ashik.ali |
40 |
public void addAmountToWallet(int retailerId, int referenceId, WalletReferenceType referenceType, String description,
|
|
|
41 |
float amount) throws ProfitMandiBusinessException{
|
| 24269 |
amit.gupta |
42 |
if(amount == 0) return;
|
| 22857 |
ashik.ali |
43 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
| 23456 |
amit.gupta |
44 |
userWallet.setAmount(userWallet.getAmount() + Math.round(amount));
|
| 22857 |
ashik.ali |
45 |
userWalletRepository.persist(userWallet);
|
| 23456 |
amit.gupta |
46 |
this.createUserWalletHistory(Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
|
| 22857 |
ashik.ali |
47 |
}
|
|
|
48 |
|
|
|
49 |
@Override
|
|
|
50 |
public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
|
|
|
51 |
String description, float amount) throws ProfitMandiBusinessException {
|
| 24269 |
amit.gupta |
52 |
if(amount == 0) return;
|
| 22857 |
ashik.ali |
53 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
54 |
if(amount > userWallet.getAmount()){
|
|
|
55 |
LOGGER.error("Wallet Balance is insufficient!");
|
|
|
56 |
throw new ProfitMandiBusinessException("balance", userWallet.getAmount(), "WLT_1000");
|
|
|
57 |
}
|
| 23509 |
amit.gupta |
58 |
userWallet.setAmount(userWallet.getAmount() - Math.round(amount));
|
| 22857 |
ashik.ali |
59 |
userWalletRepository.persist(userWallet);
|
| 23509 |
amit.gupta |
60 |
this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
|
| 22857 |
ashik.ali |
61 |
}
|
|
|
62 |
|
| 23509 |
amit.gupta |
63 |
|
|
|
64 |
@Override
|
|
|
65 |
public void rollbackAmountFromWallet(int retailerId, float amountToRollback, int rollbackReference, WalletReferenceType walletReferenceType,
|
|
|
66 |
String rollbackReason) {
|
| 24269 |
amit.gupta |
67 |
if(amountToRollback == 0) return;
|
| 23509 |
amit.gupta |
68 |
UserWallet userWallet = this.getUserWallet(retailerId);
|
|
|
69 |
userWallet.setAmount(userWallet.getAmount() - Math.round(amountToRollback));
|
|
|
70 |
userWalletRepository.persist(userWallet);
|
|
|
71 |
this.createUserWalletHistory(-Math.round(amountToRollback), userWallet.getId(), rollbackReference, walletReferenceType, rollbackReason);
|
|
|
72 |
|
|
|
73 |
}
|
|
|
74 |
|
| 22857 |
ashik.ali |
75 |
private void createUserWalletHistory(float amount, int walletId, int referenceId, WalletReferenceType referenceType, String description){
|
| 24269 |
amit.gupta |
76 |
if(amount==0) return;
|
| 22857 |
ashik.ali |
77 |
UserWalletHistory userWalletHistory = new UserWalletHistory();
|
|
|
78 |
userWalletHistory.setWalletId(walletId);
|
| 23444 |
amit.gupta |
79 |
userWalletHistory.setAmount(Math.round(amount));
|
| 22857 |
ashik.ali |
80 |
userWalletHistory.setReference(referenceId);
|
|
|
81 |
userWalletHistory.setReferenceType(referenceType);
|
|
|
82 |
userWalletHistory.setTimestamp(LocalDateTime.now());
|
|
|
83 |
userWalletHistory.setDescription(description);
|
|
|
84 |
userWalletHistoryRepository.persit(userWalletHistory);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
@Override
|
| 22550 |
ashik.ali |
89 |
public UserWallet getUserWalletByUserId(int userId) throws ProfitMandiBusinessException {
|
| 23269 |
ashik.ali |
90 |
UserAccount userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.saholic);
|
|
|
91 |
return userWalletRepository.selectByRetailerId(Integer.valueOf(userAccount.getAccountKey()));
|
| 22550 |
ashik.ali |
92 |
}
|
|
|
93 |
|
|
|
94 |
@Override
|
|
|
95 |
public List<UserWalletHistory> getUserWalletHistoryByUserId(int userId) throws ProfitMandiBusinessException {
|
|
|
96 |
UserWallet userWallet = this.getUserWalletByUserId(userId);
|
|
|
97 |
List<UserWalletHistory> userWalletHistories = userWalletHistoryRepository.selectByWalletId(userWallet.getId());
|
|
|
98 |
return userWalletHistories;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
@Override
|
|
|
102 |
public List<UserWalletHistory> getUserWalletHistoryByRetailerId(int retailerId)
|
|
|
103 |
throws ProfitMandiBusinessException {
|
|
|
104 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
105 |
return userWalletHistoryRepository.selectByWalletId(userWallet.getId());
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
@Override
|
|
|
109 |
public long getSizeByRetailerId(int retailerId, LocalDateTime startDateTime, LocalDateTime endDateTime) throws ProfitMandiBusinessException{
|
|
|
110 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
111 |
return userWalletHistoryRepository.selectCountByWalletId(userWallet.getId(), startDateTime, endDateTime);
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
@Override
|
|
|
115 |
public List<UserWalletHistory> getPaginatedUserWalletHistoryByRetailerId(int retailerId,
|
|
|
116 |
LocalDateTime startDateTime, LocalDateTime endDateTime, int offset, int limit)
|
|
|
117 |
throws ProfitMandiBusinessException {
|
|
|
118 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
119 |
return userWalletHistoryRepository.selectPaginatedByWalletId(userWallet.getId(), startDateTime, endDateTime, offset, limit);
|
|
|
120 |
}
|
| 23110 |
ashik.ali |
121 |
|
|
|
122 |
@Override
|
|
|
123 |
public Map<Integer, UserWallet> getRetailerIdUserWalletMap(Set<Integer> retailerIds) {
|
|
|
124 |
List<UserWallet> userWallets = userWalletRepository.selectByRetailerIds(retailerIds);
|
|
|
125 |
Map<Integer, UserWallet> retailerIdUserWalletMap = new HashMap<>();
|
|
|
126 |
for(UserWallet userWallet : userWallets){
|
|
|
127 |
retailerIdUserWalletMap.put(userWallet.getUserId(), userWallet);
|
|
|
128 |
}
|
|
|
129 |
return retailerIdUserWalletMap;
|
|
|
130 |
}
|
| 23504 |
ashik.ali |
131 |
|
|
|
132 |
@Override
|
|
|
133 |
public boolean isExistWalletHistory(int retailerId, int referenceId, WalletReferenceType referenceType) throws ProfitMandiBusinessException{
|
|
|
134 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
135 |
return userWalletHistoryRepository.isExist(userWallet.getId(), referenceType, referenceId);
|
|
|
136 |
}
|
| 22550 |
ashik.ali |
137 |
|
| 23509 |
amit.gupta |
138 |
@Override
|
|
|
139 |
public UserWallet getUserWallet(int retailerId) {
|
|
|
140 |
try {
|
|
|
141 |
return userWalletRepository.selectByRetailerId(retailerId);
|
|
|
142 |
} catch(Exception e) {
|
|
|
143 |
UserWallet uw = new UserWallet();
|
|
|
144 |
uw.setAmount(0);
|
|
|
145 |
uw.setRefundableAmount(0);
|
|
|
146 |
uw.setUserId(retailerId);
|
|
|
147 |
userWalletRepository.persist(uw);
|
|
|
148 |
return uw;
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
|
| 22550 |
ashik.ali |
152 |
}
|