| 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;
|
| 22550 |
ashik.ali |
9 |
|
| 24509 |
amit.gupta |
10 |
import org.apache.logging.log4j.LogManager;
|
| 23568 |
govind |
11 |
import org.apache.logging.log4j.Logger;
|
| 22550 |
ashik.ali |
12 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
13 |
import org.springframework.stereotype.Component;
|
|
|
14 |
|
|
|
15 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 24739 |
tejbeer |
16 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 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,
|
|
|
55 |
String description, float amount) throws ProfitMandiBusinessException {
|
|
|
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);
|
| 23456 |
amit.gupta |
63 |
this.createUserWalletHistory(Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
|
| 22857 |
ashik.ali |
64 |
}
|
| 24739 |
tejbeer |
65 |
|
| 22857 |
ashik.ali |
66 |
@Override
|
|
|
67 |
public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
|
|
|
68 |
String description, float amount) 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);
|
| 23509 |
amit.gupta |
83 |
this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
|
| 22857 |
ashik.ali |
84 |
}
|
| 23509 |
amit.gupta |
85 |
|
|
|
86 |
@Override
|
| 24739 |
tejbeer |
87 |
public void rollbackAmountFromWallet(int retailerId, float amountToRollback, int rollbackReference,
|
| 25395 |
tejbeer |
88 |
WalletReferenceType walletReferenceType, String rollbackReason) throws ProfitMandiBusinessException {
|
|
|
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);
|
| 25395 |
tejbeer |
94 |
// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
|
| 25547 |
amit.gupta |
95 |
userWallet.setAmount(walletAmount - Math.round(amountToRollback));
|
| 23509 |
amit.gupta |
96 |
userWalletRepository.persist(userWallet);
|
| 24739 |
tejbeer |
97 |
this.createUserWalletHistory(-Math.round(amountToRollback), userWallet.getId(), rollbackReference,
|
|
|
98 |
walletReferenceType, rollbackReason);
|
|
|
99 |
|
| 23509 |
amit.gupta |
100 |
}
|
| 24739 |
tejbeer |
101 |
|
|
|
102 |
private void createUserWalletHistory(float amount, int walletId, int referenceId, WalletReferenceType referenceType,
|
|
|
103 |
String description) {
|
|
|
104 |
if (amount == 0)
|
|
|
105 |
return;
|
| 22857 |
ashik.ali |
106 |
UserWalletHistory userWalletHistory = new UserWalletHistory();
|
|
|
107 |
userWalletHistory.setWalletId(walletId);
|
| 23444 |
amit.gupta |
108 |
userWalletHistory.setAmount(Math.round(amount));
|
| 22857 |
ashik.ali |
109 |
userWalletHistory.setReference(referenceId);
|
|
|
110 |
userWalletHistory.setReferenceType(referenceType);
|
|
|
111 |
userWalletHistory.setTimestamp(LocalDateTime.now());
|
|
|
112 |
userWalletHistory.setDescription(description);
|
| 24990 |
amit.gupta |
113 |
userWalletHistoryRepository.persist(userWalletHistory);
|
| 22857 |
ashik.ali |
114 |
}
|
| 24739 |
tejbeer |
115 |
|
| 22857 |
ashik.ali |
116 |
@Override
|
| 22550 |
ashik.ali |
117 |
public UserWallet getUserWalletByUserId(int userId) throws ProfitMandiBusinessException {
|
| 25542 |
amit.gupta |
118 |
UserAccount userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.saholic);
|
|
|
119 |
|
|
|
120 |
if (underMaintainance || !isActive(userAccount.getAccountKey())) {
|
| 25247 |
amit.gupta |
121 |
throw pbse;
|
|
|
122 |
}
|
| 23269 |
ashik.ali |
123 |
return userWalletRepository.selectByRetailerId(Integer.valueOf(userAccount.getAccountKey()));
|
| 22550 |
ashik.ali |
124 |
}
|
|
|
125 |
|
|
|
126 |
@Override
|
|
|
127 |
public List<UserWalletHistory> getUserWalletHistoryByUserId(int userId) throws ProfitMandiBusinessException {
|
| 25542 |
amit.gupta |
128 |
if (underMaintainance || !isActive(userId)) {
|
| 25247 |
amit.gupta |
129 |
throw pbse;
|
|
|
130 |
}
|
| 22550 |
ashik.ali |
131 |
UserWallet userWallet = this.getUserWalletByUserId(userId);
|
|
|
132 |
List<UserWalletHistory> userWalletHistories = userWalletHistoryRepository.selectByWalletId(userWallet.getId());
|
|
|
133 |
return userWalletHistories;
|
|
|
134 |
}
|
| 24739 |
tejbeer |
135 |
|
| 25542 |
amit.gupta |
136 |
private boolean isActive(int userId) {
|
|
|
137 |
boolean active = true;
|
|
|
138 |
try {
|
|
|
139 |
FofoStore fs = fofoStoreRepository.selectByRetailerId(userId);
|
|
|
140 |
active = fs.isActive();
|
|
|
141 |
} catch (Exception e) {
|
|
|
142 |
|
|
|
143 |
}
|
|
|
144 |
return active;
|
|
|
145 |
}
|
|
|
146 |
|
| 22550 |
ashik.ali |
147 |
@Override
|
|
|
148 |
public List<UserWalletHistory> getUserWalletHistoryByRetailerId(int retailerId)
|
|
|
149 |
throws ProfitMandiBusinessException {
|
| 25542 |
amit.gupta |
150 |
if (underMaintainance || !isActive(retailerId)) {
|
| 25247 |
amit.gupta |
151 |
throw pbse;
|
|
|
152 |
}
|
| 22550 |
ashik.ali |
153 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
154 |
return userWalletHistoryRepository.selectByWalletId(userWallet.getId());
|
|
|
155 |
}
|
| 24739 |
tejbeer |
156 |
|
| 22550 |
ashik.ali |
157 |
@Override
|
| 24739 |
tejbeer |
158 |
public long getSizeByRetailerId(int retailerId, LocalDateTime startDateTime, LocalDateTime endDateTime)
|
|
|
159 |
throws ProfitMandiBusinessException {
|
| 25395 |
tejbeer |
160 |
if (underMaintainance) {
|
| 25247 |
amit.gupta |
161 |
throw pbse;
|
|
|
162 |
}
|
| 22550 |
ashik.ali |
163 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
164 |
return userWalletHistoryRepository.selectCountByWalletId(userWallet.getId(), startDateTime, endDateTime);
|
|
|
165 |
}
|
| 24739 |
tejbeer |
166 |
|
| 22550 |
ashik.ali |
167 |
@Override
|
|
|
168 |
public List<UserWalletHistory> getPaginatedUserWalletHistoryByRetailerId(int retailerId,
|
|
|
169 |
LocalDateTime startDateTime, LocalDateTime endDateTime, int offset, int limit)
|
|
|
170 |
throws ProfitMandiBusinessException {
|
| 25902 |
amit.gupta |
171 |
if (underMaintainance) {
|
| 25247 |
amit.gupta |
172 |
throw pbse;
|
|
|
173 |
}
|
| 22550 |
ashik.ali |
174 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
| 24739 |
tejbeer |
175 |
return userWalletHistoryRepository.selectPaginatedByWalletId(userWallet.getId(), startDateTime, endDateTime,
|
|
|
176 |
offset, limit);
|
| 22550 |
ashik.ali |
177 |
}
|
| 24739 |
tejbeer |
178 |
|
| 23110 |
ashik.ali |
179 |
@Override
|
|
|
180 |
public Map<Integer, UserWallet> getRetailerIdUserWalletMap(Set<Integer> retailerIds) {
|
|
|
181 |
List<UserWallet> userWallets = userWalletRepository.selectByRetailerIds(retailerIds);
|
|
|
182 |
Map<Integer, UserWallet> retailerIdUserWalletMap = new HashMap<>();
|
| 24739 |
tejbeer |
183 |
for (UserWallet userWallet : userWallets) {
|
| 23110 |
ashik.ali |
184 |
retailerIdUserWalletMap.put(userWallet.getUserId(), userWallet);
|
|
|
185 |
}
|
|
|
186 |
return retailerIdUserWalletMap;
|
|
|
187 |
}
|
| 24739 |
tejbeer |
188 |
|
| 23504 |
ashik.ali |
189 |
@Override
|
| 24739 |
tejbeer |
190 |
public boolean isExistWalletHistory(int retailerId, int referenceId, WalletReferenceType referenceType)
|
|
|
191 |
throws ProfitMandiBusinessException {
|
| 25542 |
amit.gupta |
192 |
if (underMaintainance || !isActive(retailerId)) {
|
| 25247 |
amit.gupta |
193 |
throw pbse;
|
|
|
194 |
}
|
| 23504 |
ashik.ali |
195 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
196 |
return userWalletHistoryRepository.isExist(userWallet.getId(), referenceType, referenceId);
|
|
|
197 |
}
|
| 22550 |
ashik.ali |
198 |
|
| 23509 |
amit.gupta |
199 |
@Override
|
| 25249 |
amit.gupta |
200 |
public UserWallet getUserWallet(int retailerId) throws ProfitMandiBusinessException {
|
| 25395 |
tejbeer |
201 |
if (underMaintainance) {
|
| 25249 |
amit.gupta |
202 |
throw pbse;
|
|
|
203 |
}
|
| 23509 |
amit.gupta |
204 |
try {
|
|
|
205 |
return userWalletRepository.selectByRetailerId(retailerId);
|
| 24739 |
tejbeer |
206 |
} catch (Exception e) {
|
| 23509 |
amit.gupta |
207 |
UserWallet uw = new UserWallet();
|
|
|
208 |
uw.setAmount(0);
|
|
|
209 |
uw.setRefundableAmount(0);
|
|
|
210 |
uw.setUserId(retailerId);
|
|
|
211 |
userWalletRepository.persist(uw);
|
|
|
212 |
return uw;
|
|
|
213 |
}
|
|
|
214 |
}
|
|
|
215 |
|
| 24509 |
amit.gupta |
216 |
@Override
|
|
|
217 |
public float getOpeningTill(int fofoId, LocalDateTime date) throws ProfitMandiBusinessException {
|
|
|
218 |
UserWallet wallet = userWalletRepository.selectByRetailerId(fofoId);
|
|
|
219 |
return userWalletHistoryRepository.getSumTillDate(wallet.getId(), date);
|
|
|
220 |
}
|
| 24739 |
tejbeer |
221 |
|
|
|
222 |
@Override
|
|
|
223 |
public void refundToWallet(Integer retailerId, Float amountToRefund, Integer transactionId,
|
|
|
224 |
WalletReferenceType walletReferenceType, String description) throws ProfitMandiBusinessException {
|
|
|
225 |
|
|
|
226 |
List<UserWalletHistory> all_entries = userWalletHistoryRepository
|
|
|
227 |
.selectAllByreferenceIdandreferenceType(transactionId, WalletReferenceType.PURCHASE);
|
|
|
228 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
| 25547 |
amit.gupta |
229 |
int walletAmount = walletService.getWalletAmount(retailerId);
|
| 24739 |
tejbeer |
230 |
LOGGER.info("userWallet" + userWallet);
|
|
|
231 |
int creditable_amount_for_reference = 0;
|
|
|
232 |
|
|
|
233 |
for (UserWalletHistory history : all_entries) {
|
|
|
234 |
creditable_amount_for_reference -= history.getAmount();
|
|
|
235 |
|
|
|
236 |
LOGGER.info("creditable_amount_for_reference" + creditable_amount_for_reference);
|
|
|
237 |
|
|
|
238 |
LOGGER.info("walletAmount" + amountToRefund);
|
|
|
239 |
|
| 25395 |
tejbeer |
240 |
}
|
|
|
241 |
if (creditable_amount_for_reference < amountToRefund) {
|
| 24739 |
tejbeer |
242 |
|
| 25395 |
tejbeer |
243 |
LOGGER.info("order" + "Cant be credited back to wallet as most of it has been already credited");
|
|
|
244 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.USER_ID, userWallet.getUserId(),
|
|
|
245 |
"Cant be credited back to wallet as most of it has been already credited");
|
|
|
246 |
}
|
| 24739 |
tejbeer |
247 |
|
| 25547 |
amit.gupta |
248 |
userWallet.setAmount(walletAmount + Math.round(amountToRefund));
|
| 24739 |
tejbeer |
249 |
|
| 25395 |
tejbeer |
250 |
LOGGER.info("userWallet" + userWallet);
|
|
|
251 |
|
| 24739 |
tejbeer |
252 |
UserWalletHistory userWalletHistory = new UserWalletHistory();
|
|
|
253 |
userWalletHistory.setAmount(Math.round(amountToRefund));
|
|
|
254 |
userWalletHistory.setReference(transactionId);
|
|
|
255 |
userWalletHistory.setReferenceType(WalletReferenceType.PURCHASE);
|
|
|
256 |
userWalletHistory.setWalletId(userWallet.getId());
|
|
|
257 |
userWalletHistory.setTimestamp(LocalDateTime.now());
|
|
|
258 |
userWalletHistory.setDescription(description);
|
|
|
259 |
|
|
|
260 |
LOGGER.info("all_entries" + userWalletHistory);
|
| 24990 |
amit.gupta |
261 |
userWalletHistoryRepository.persist(userWalletHistory);
|
| 24739 |
tejbeer |
262 |
|
|
|
263 |
}
|
|
|
264 |
|
| 25547 |
amit.gupta |
265 |
@Override
|
|
|
266 |
public int getWalletAmount(int retailerId) throws ProfitMandiBusinessException {
|
| 25559 |
amit.gupta |
267 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
| 25609 |
amit.gupta |
268 |
if (userWallet == null) {
|
| 25552 |
amit.gupta |
269 |
return 0;
|
|
|
270 |
}
|
| 25609 |
amit.gupta |
271 |
return (int) userWalletHistoryRepository.selectSumByWallet(userWallet.getId());
|
| 25547 |
amit.gupta |
272 |
}
|
|
|
273 |
|
| 25609 |
amit.gupta |
274 |
@Override
|
|
|
275 |
public List<UserWalletHistory> getAllByReference(int fofoId, int reference, WalletReferenceType walletReferenceType)
|
|
|
276 |
throws ProfitMandiBusinessException {
|
|
|
277 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(fofoId);
|
|
|
278 |
if (userWallet == null) {
|
|
|
279 |
return new ArrayList<UserWalletHistory>();
|
|
|
280 |
}
|
|
|
281 |
return userWalletHistoryRepository.selectAllByreferenceIdandreferenceType(userWallet.getId(), reference,
|
|
|
282 |
walletReferenceType);
|
|
|
283 |
}
|
|
|
284 |
|
| 22550 |
ashik.ali |
285 |
}
|