| 22550 |
ashik.ali |
1 |
package com.spice.profitmandi.service.wallet;
|
|
|
2 |
|
|
|
3 |
import java.time.LocalDateTime;
|
| 25609 |
amit.gupta |
4 |
import java.util.ArrayList;
|
| 23110 |
ashik.ali |
5 |
import java.util.HashMap;
|
| 22550 |
ashik.ali |
6 |
import java.util.List;
|
| 23110 |
ashik.ali |
7 |
import java.util.Map;
|
|
|
8 |
import java.util.Set;
|
| 26498 |
amit.gupta |
9 |
import java.util.stream.Collectors;
|
| 22550 |
ashik.ali |
10 |
|
| 24509 |
amit.gupta |
11 |
import org.apache.logging.log4j.LogManager;
|
| 23568 |
govind |
12 |
import org.apache.logging.log4j.Logger;
|
| 22550 |
ashik.ali |
13 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
14 |
import org.springframework.stereotype.Component;
|
|
|
15 |
|
|
|
16 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 23269 |
ashik.ali |
17 |
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
|
| 25542 |
amit.gupta |
18 |
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
|
| 22550 |
ashik.ali |
19 |
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
|
|
|
20 |
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
|
|
|
21 |
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
|
| 25542 |
amit.gupta |
22 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
| 22550 |
ashik.ali |
23 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
|
|
24 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
|
|
|
25 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
|
|
|
26 |
|
| 22857 |
ashik.ali |
27 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
|
|
28 |
|
| 22550 |
ashik.ali |
29 |
@Component
|
|
|
30 |
public class WalletServiceImpl implements WalletService {
|
|
|
31 |
|
| 23568 |
govind |
32 |
private static final Logger LOGGER = LogManager.getLogger(WalletServiceImpl.class);
|
| 25395 |
tejbeer |
33 |
|
| 25557 |
amit.gupta |
34 |
private boolean underMaintainance = false;
|
| 25395 |
tejbeer |
35 |
ProfitMandiBusinessException pbse = new ProfitMandiBusinessException("Wallet", "Wallet",
|
|
|
36 |
"Wallet is under maintainance, please try after some time");
|
| 24739 |
tejbeer |
37 |
|
| 22550 |
ashik.ali |
38 |
@Autowired
|
| 22925 |
ashik.ali |
39 |
private UserAccountRepository userAccountRepository;
|
| 25609 |
amit.gupta |
40 |
|
| 25547 |
amit.gupta |
41 |
@Autowired
|
|
|
42 |
private WalletService walletService;
|
| 24739 |
tejbeer |
43 |
|
| 22550 |
ashik.ali |
44 |
@Autowired
|
| 22925 |
ashik.ali |
45 |
private UserWalletRepository userWalletRepository;
|
| 24739 |
tejbeer |
46 |
|
| 22550 |
ashik.ali |
47 |
@Autowired
|
| 25542 |
amit.gupta |
48 |
private FofoStoreRepository fofoStoreRepository;
|
|
|
49 |
|
|
|
50 |
@Autowired
|
| 22925 |
ashik.ali |
51 |
private UserWalletHistoryRepository userWalletHistoryRepository;
|
| 24739 |
tejbeer |
52 |
|
| 22550 |
ashik.ali |
53 |
@Override
|
| 24739 |
tejbeer |
54 |
public void addAmountToWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
|
| 26498 |
amit.gupta |
55 |
String description, float amount, LocalDateTime businessTime) throws ProfitMandiBusinessException {
|
| 24739 |
tejbeer |
56 |
if (amount == 0)
|
|
|
57 |
return;
|
| 25265 |
amit.gupta |
58 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
| 25547 |
amit.gupta |
59 |
int walletAmount = walletService.getWalletAmount(retailerId);
|
| 25395 |
tejbeer |
60 |
// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
|
| 25547 |
amit.gupta |
61 |
userWallet.setAmount(walletAmount + Math.round(amount));
|
| 22857 |
ashik.ali |
62 |
userWalletRepository.persist(userWallet);
|
| 26498 |
amit.gupta |
63 |
this.createUserWalletHistory(Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime);
|
| 22857 |
ashik.ali |
64 |
}
|
| 24739 |
tejbeer |
65 |
|
| 22857 |
ashik.ali |
66 |
@Override
|
|
|
67 |
public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
|
| 26498 |
amit.gupta |
68 |
String description, float amount, LocalDateTime businessTime) throws ProfitMandiBusinessException {
|
| 25542 |
amit.gupta |
69 |
if ((underMaintainance && referenceType.equals(WalletReferenceType.PURCHASE) || !isActive(retailerId))) {
|
| 25247 |
amit.gupta |
70 |
throw pbse;
|
|
|
71 |
}
|
| 24739 |
tejbeer |
72 |
if (amount == 0)
|
|
|
73 |
return;
|
| 22857 |
ashik.ali |
74 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
| 25547 |
amit.gupta |
75 |
int walletAmount = walletService.getWalletAmount(retailerId);
|
| 25395 |
tejbeer |
76 |
// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
|
| 25547 |
amit.gupta |
77 |
if (amount > walletAmount) {
|
| 22857 |
ashik.ali |
78 |
LOGGER.error("Wallet Balance is insufficient!");
|
| 25547 |
amit.gupta |
79 |
throw new ProfitMandiBusinessException("balance", walletAmount, "WLT_1000");
|
| 22857 |
ashik.ali |
80 |
}
|
| 25547 |
amit.gupta |
81 |
userWallet.setAmount(walletAmount - Math.round(amount));
|
| 22857 |
ashik.ali |
82 |
userWalletRepository.persist(userWallet);
|
| 26498 |
amit.gupta |
83 |
this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime);
|
| 22857 |
ashik.ali |
84 |
}
|
| 23509 |
amit.gupta |
85 |
|
|
|
86 |
@Override
|
| 24739 |
tejbeer |
87 |
public void rollbackAmountFromWallet(int retailerId, float amountToRollback, int rollbackReference,
|
| 26498 |
amit.gupta |
88 |
WalletReferenceType walletReferenceType, String rollbackReason, LocalDateTime businessTime) throws ProfitMandiBusinessException {
|
| 25395 |
tejbeer |
89 |
|
| 24739 |
tejbeer |
90 |
if (amountToRollback == 0)
|
|
|
91 |
return;
|
| 25547 |
amit.gupta |
92 |
int walletAmount = walletService.getWalletAmount(retailerId);
|
| 25265 |
amit.gupta |
93 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
| 26498 |
amit.gupta |
94 |
List<UserWalletHistory> uwh = userWalletHistoryRepository.selectAllByreferenceIdandreferenceType(rollbackReference, walletReferenceType)
|
|
|
95 |
.stream().filter(x->x.getWalletId()==userWallet.getId()).collect(Collectors.toList());
|
|
|
96 |
if(uwh.size() == 0) {
|
| 26715 |
amit.gupta |
97 |
LOGGER.info("Retailer with id {} dont have valid reference {} and reference type {}",
|
| 26498 |
amit.gupta |
98 |
retailerId, rollbackReference, walletReferenceType);
|
|
|
99 |
throw new ProfitMandiBusinessException("Retailer specific wallet entries doesn't exist", retailerId, "Nothing to rollback");
|
|
|
100 |
}
|
| 25395 |
tejbeer |
101 |
// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
|
| 25547 |
amit.gupta |
102 |
userWallet.setAmount(walletAmount - Math.round(amountToRollback));
|
| 26498 |
amit.gupta |
103 |
this.createUserWalletHistory(-Math.round(amountToRollback), userWallet.getId(), rollbackReference,
|
|
|
104 |
walletReferenceType, rollbackReason, businessTime);
|
| 23509 |
amit.gupta |
105 |
userWalletRepository.persist(userWallet);
|
| 24739 |
tejbeer |
106 |
|
| 23509 |
amit.gupta |
107 |
}
|
| 24739 |
tejbeer |
108 |
|
|
|
109 |
private void createUserWalletHistory(float amount, int walletId, int referenceId, WalletReferenceType referenceType,
|
| 26498 |
amit.gupta |
110 |
String description, LocalDateTime businessTimestamp) {
|
| 24739 |
tejbeer |
111 |
if (amount == 0)
|
|
|
112 |
return;
|
| 22857 |
ashik.ali |
113 |
UserWalletHistory userWalletHistory = new UserWalletHistory();
|
|
|
114 |
userWalletHistory.setWalletId(walletId);
|
| 23444 |
amit.gupta |
115 |
userWalletHistory.setAmount(Math.round(amount));
|
| 22857 |
ashik.ali |
116 |
userWalletHistory.setReference(referenceId);
|
|
|
117 |
userWalletHistory.setReferenceType(referenceType);
|
|
|
118 |
userWalletHistory.setTimestamp(LocalDateTime.now());
|
|
|
119 |
userWalletHistory.setDescription(description);
|
| 26498 |
amit.gupta |
120 |
userWalletHistory.setBusinessTimestamp(businessTimestamp);
|
| 24990 |
amit.gupta |
121 |
userWalletHistoryRepository.persist(userWalletHistory);
|
| 22857 |
ashik.ali |
122 |
}
|
| 24739 |
tejbeer |
123 |
|
| 22857 |
ashik.ali |
124 |
@Override
|
| 22550 |
ashik.ali |
125 |
public UserWallet getUserWalletByUserId(int userId) throws ProfitMandiBusinessException {
|
| 25542 |
amit.gupta |
126 |
UserAccount userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.saholic);
|
|
|
127 |
|
|
|
128 |
if (underMaintainance || !isActive(userAccount.getAccountKey())) {
|
| 25247 |
amit.gupta |
129 |
throw pbse;
|
|
|
130 |
}
|
| 23269 |
ashik.ali |
131 |
return userWalletRepository.selectByRetailerId(Integer.valueOf(userAccount.getAccountKey()));
|
| 22550 |
ashik.ali |
132 |
}
|
|
|
133 |
|
|
|
134 |
@Override
|
|
|
135 |
public List<UserWalletHistory> getUserWalletHistoryByUserId(int userId) throws ProfitMandiBusinessException {
|
| 25542 |
amit.gupta |
136 |
if (underMaintainance || !isActive(userId)) {
|
| 25247 |
amit.gupta |
137 |
throw pbse;
|
|
|
138 |
}
|
| 22550 |
ashik.ali |
139 |
UserWallet userWallet = this.getUserWalletByUserId(userId);
|
|
|
140 |
List<UserWalletHistory> userWalletHistories = userWalletHistoryRepository.selectByWalletId(userWallet.getId());
|
|
|
141 |
return userWalletHistories;
|
|
|
142 |
}
|
| 24739 |
tejbeer |
143 |
|
| 25542 |
amit.gupta |
144 |
private boolean isActive(int userId) {
|
|
|
145 |
boolean active = true;
|
|
|
146 |
try {
|
|
|
147 |
FofoStore fs = fofoStoreRepository.selectByRetailerId(userId);
|
|
|
148 |
active = fs.isActive();
|
|
|
149 |
} catch (Exception e) {
|
|
|
150 |
|
|
|
151 |
}
|
|
|
152 |
return active;
|
|
|
153 |
}
|
|
|
154 |
|
| 22550 |
ashik.ali |
155 |
@Override
|
|
|
156 |
public List<UserWalletHistory> getUserWalletHistoryByRetailerId(int retailerId)
|
|
|
157 |
throws ProfitMandiBusinessException {
|
| 25542 |
amit.gupta |
158 |
if (underMaintainance || !isActive(retailerId)) {
|
| 25247 |
amit.gupta |
159 |
throw pbse;
|
|
|
160 |
}
|
| 22550 |
ashik.ali |
161 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
162 |
return userWalletHistoryRepository.selectByWalletId(userWallet.getId());
|
|
|
163 |
}
|
| 24739 |
tejbeer |
164 |
|
| 22550 |
ashik.ali |
165 |
@Override
|
| 24739 |
tejbeer |
166 |
public long getSizeByRetailerId(int retailerId, LocalDateTime startDateTime, LocalDateTime endDateTime)
|
|
|
167 |
throws ProfitMandiBusinessException {
|
| 25395 |
tejbeer |
168 |
if (underMaintainance) {
|
| 25247 |
amit.gupta |
169 |
throw pbse;
|
|
|
170 |
}
|
| 22550 |
ashik.ali |
171 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
172 |
return userWalletHistoryRepository.selectCountByWalletId(userWallet.getId(), startDateTime, endDateTime);
|
|
|
173 |
}
|
| 24739 |
tejbeer |
174 |
|
| 22550 |
ashik.ali |
175 |
@Override
|
|
|
176 |
public List<UserWalletHistory> getPaginatedUserWalletHistoryByRetailerId(int retailerId,
|
|
|
177 |
LocalDateTime startDateTime, LocalDateTime endDateTime, int offset, int limit)
|
|
|
178 |
throws ProfitMandiBusinessException {
|
| 25902 |
amit.gupta |
179 |
if (underMaintainance) {
|
| 25247 |
amit.gupta |
180 |
throw pbse;
|
|
|
181 |
}
|
| 22550 |
ashik.ali |
182 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
| 24739 |
tejbeer |
183 |
return userWalletHistoryRepository.selectPaginatedByWalletId(userWallet.getId(), startDateTime, endDateTime,
|
|
|
184 |
offset, limit);
|
| 22550 |
ashik.ali |
185 |
}
|
| 24739 |
tejbeer |
186 |
|
| 23110 |
ashik.ali |
187 |
@Override
|
|
|
188 |
public Map<Integer, UserWallet> getRetailerIdUserWalletMap(Set<Integer> retailerIds) {
|
|
|
189 |
List<UserWallet> userWallets = userWalletRepository.selectByRetailerIds(retailerIds);
|
|
|
190 |
Map<Integer, UserWallet> retailerIdUserWalletMap = new HashMap<>();
|
| 24739 |
tejbeer |
191 |
for (UserWallet userWallet : userWallets) {
|
| 23110 |
ashik.ali |
192 |
retailerIdUserWalletMap.put(userWallet.getUserId(), userWallet);
|
|
|
193 |
}
|
|
|
194 |
return retailerIdUserWalletMap;
|
|
|
195 |
}
|
| 28596 |
amit.gupta |
196 |
|
|
|
197 |
@Override
|
|
|
198 |
public Map<Integer, Integer> getWaleltRetailerMap(Set<Integer> walletIds) {
|
|
|
199 |
List<UserWallet> userWallets = userWalletRepository.selectAllById(walletIds);
|
|
|
200 |
Map<Integer, Integer> walletRetailerMap = new HashMap<>();
|
|
|
201 |
for (UserWallet userWallet : userWallets) {
|
|
|
202 |
walletRetailerMap.put(userWallet.getId(), userWallet.getUserId());
|
|
|
203 |
}
|
|
|
204 |
return walletRetailerMap;
|
|
|
205 |
}
|
| 24739 |
tejbeer |
206 |
|
| 23504 |
ashik.ali |
207 |
@Override
|
| 24739 |
tejbeer |
208 |
public boolean isExistWalletHistory(int retailerId, int referenceId, WalletReferenceType referenceType)
|
|
|
209 |
throws ProfitMandiBusinessException {
|
| 25542 |
amit.gupta |
210 |
if (underMaintainance || !isActive(retailerId)) {
|
| 25247 |
amit.gupta |
211 |
throw pbse;
|
|
|
212 |
}
|
| 23504 |
ashik.ali |
213 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
214 |
return userWalletHistoryRepository.isExist(userWallet.getId(), referenceType, referenceId);
|
|
|
215 |
}
|
| 22550 |
ashik.ali |
216 |
|
| 23509 |
amit.gupta |
217 |
@Override
|
| 25249 |
amit.gupta |
218 |
public UserWallet getUserWallet(int retailerId) throws ProfitMandiBusinessException {
|
| 25395 |
tejbeer |
219 |
if (underMaintainance) {
|
| 25249 |
amit.gupta |
220 |
throw pbse;
|
|
|
221 |
}
|
| 23509 |
amit.gupta |
222 |
try {
|
|
|
223 |
return userWalletRepository.selectByRetailerId(retailerId);
|
| 24739 |
tejbeer |
224 |
} catch (Exception e) {
|
| 23509 |
amit.gupta |
225 |
UserWallet uw = new UserWallet();
|
|
|
226 |
uw.setAmount(0);
|
|
|
227 |
uw.setRefundableAmount(0);
|
|
|
228 |
uw.setUserId(retailerId);
|
|
|
229 |
userWalletRepository.persist(uw);
|
|
|
230 |
return uw;
|
|
|
231 |
}
|
|
|
232 |
}
|
|
|
233 |
|
| 24509 |
amit.gupta |
234 |
@Override
|
|
|
235 |
public float getOpeningTill(int fofoId, LocalDateTime date) throws ProfitMandiBusinessException {
|
|
|
236 |
UserWallet wallet = userWalletRepository.selectByRetailerId(fofoId);
|
|
|
237 |
return userWalletHistoryRepository.getSumTillDate(wallet.getId(), date);
|
|
|
238 |
}
|
| 28518 |
amit.gupta |
239 |
|
|
|
240 |
@Override
|
|
|
241 |
public float getOpeningTillExcludingPurchase(int fofoId, LocalDateTime date) throws ProfitMandiBusinessException {
|
|
|
242 |
UserWallet wallet = userWalletRepository.selectByRetailerId(fofoId);
|
|
|
243 |
return userWalletHistoryRepository.getSumTillDate(wallet.getId(), date) -userWalletHistoryRepository.getSumTillDate(wallet.getId(), date, WalletReferenceType.PURCHASE);
|
|
|
244 |
}
|
| 28596 |
amit.gupta |
245 |
|
| 24739 |
tejbeer |
246 |
@Override
|
| 28241 |
amit.gupta |
247 |
public boolean refundToWallet(int retailerId, float amountToRefund, int transactionId,
|
| 24739 |
tejbeer |
248 |
WalletReferenceType walletReferenceType, String description) throws ProfitMandiBusinessException {
|
|
|
249 |
|
|
|
250 |
List<UserWalletHistory> all_entries = userWalletHistoryRepository
|
| 26037 |
amit.gupta |
251 |
.selectAllByreferenceIdandreferenceType(transactionId, walletReferenceType);
|
| 26254 |
amit.gupta |
252 |
|
| 24739 |
tejbeer |
253 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
| 25547 |
amit.gupta |
254 |
int walletAmount = walletService.getWalletAmount(retailerId);
|
| 24739 |
tejbeer |
255 |
LOGGER.info("userWallet" + userWallet);
|
| 26089 |
amit.gupta |
256 |
int max_eligible_credit_amount = 0;
|
| 24739 |
tejbeer |
257 |
|
| 26254 |
amit.gupta |
258 |
LOGGER.info("all_entries {}",all_entries);
|
| 24739 |
tejbeer |
259 |
for (UserWalletHistory history : all_entries) {
|
| 26089 |
amit.gupta |
260 |
max_eligible_credit_amount -= history.getAmount();
|
| 25395 |
tejbeer |
261 |
}
|
| 26089 |
amit.gupta |
262 |
if (max_eligible_credit_amount < amountToRefund) {
|
| 26035 |
amit.gupta |
263 |
LOGGER.info("Cant be credited back to wallet as most of it has been already credited");
|
| 26256 |
amit.gupta |
264 |
return false;
|
| 25395 |
tejbeer |
265 |
}
|
| 26089 |
amit.gupta |
266 |
|
| 25547 |
amit.gupta |
267 |
userWallet.setAmount(walletAmount + Math.round(amountToRefund));
|
| 24739 |
tejbeer |
268 |
|
| 25395 |
tejbeer |
269 |
LOGGER.info("userWallet" + userWallet);
|
|
|
270 |
|
| 24739 |
tejbeer |
271 |
UserWalletHistory userWalletHistory = new UserWalletHistory();
|
|
|
272 |
userWalletHistory.setAmount(Math.round(amountToRefund));
|
|
|
273 |
userWalletHistory.setReference(transactionId);
|
| 26037 |
amit.gupta |
274 |
userWalletHistory.setReferenceType(walletReferenceType);
|
| 24739 |
tejbeer |
275 |
userWalletHistory.setWalletId(userWallet.getId());
|
|
|
276 |
userWalletHistory.setTimestamp(LocalDateTime.now());
|
|
|
277 |
userWalletHistory.setDescription(description);
|
| 26498 |
amit.gupta |
278 |
userWalletHistory.setBusinessTimestamp(all_entries.get(0).getBusinessTimestamp());
|
| 24739 |
tejbeer |
279 |
|
| 24990 |
amit.gupta |
280 |
userWalletHistoryRepository.persist(userWalletHistory);
|
| 26256 |
amit.gupta |
281 |
return true;
|
| 24739 |
tejbeer |
282 |
|
|
|
283 |
}
|
|
|
284 |
|
| 25547 |
amit.gupta |
285 |
@Override
|
|
|
286 |
public int getWalletAmount(int retailerId) throws ProfitMandiBusinessException {
|
| 25559 |
amit.gupta |
287 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
| 25609 |
amit.gupta |
288 |
if (userWallet == null) {
|
| 25552 |
amit.gupta |
289 |
return 0;
|
|
|
290 |
}
|
| 25609 |
amit.gupta |
291 |
return (int) userWalletHistoryRepository.selectSumByWallet(userWallet.getId());
|
| 25547 |
amit.gupta |
292 |
}
|
|
|
293 |
|
| 25609 |
amit.gupta |
294 |
@Override
|
|
|
295 |
public List<UserWalletHistory> getAllByReference(int fofoId, int reference, WalletReferenceType walletReferenceType)
|
|
|
296 |
throws ProfitMandiBusinessException {
|
|
|
297 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(fofoId);
|
|
|
298 |
if (userWallet == null) {
|
|
|
299 |
return new ArrayList<UserWalletHistory>();
|
|
|
300 |
}
|
|
|
301 |
return userWalletHistoryRepository.selectAllByreferenceIdandreferenceType(userWallet.getId(), reference,
|
|
|
302 |
walletReferenceType);
|
|
|
303 |
}
|
| 22550 |
ashik.ali |
304 |
}
|