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