| 22550 |
ashik.ali |
1 |
package com.spice.profitmandi.service.wallet;
|
|
|
2 |
|
| 24509 |
amit.gupta |
3 |
import java.time.LocalDate;
|
| 22550 |
ashik.ali |
4 |
import java.time.LocalDateTime;
|
| 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;
|
| 22550 |
ashik.ali |
18 |
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
|
|
|
19 |
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
|
|
|
20 |
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
|
|
|
21 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
|
|
22 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
|
|
|
23 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
|
|
|
24 |
|
| 22857 |
ashik.ali |
25 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
|
|
26 |
|
| 22550 |
ashik.ali |
27 |
@Component
|
|
|
28 |
public class WalletServiceImpl implements WalletService {
|
|
|
29 |
|
| 23568 |
govind |
30 |
private static final Logger LOGGER = LogManager.getLogger(WalletServiceImpl.class);
|
| 24739 |
tejbeer |
31 |
|
| 22550 |
ashik.ali |
32 |
@Autowired
|
| 22925 |
ashik.ali |
33 |
private UserAccountRepository userAccountRepository;
|
| 24739 |
tejbeer |
34 |
|
| 22550 |
ashik.ali |
35 |
@Autowired
|
| 22925 |
ashik.ali |
36 |
private UserWalletRepository userWalletRepository;
|
| 24739 |
tejbeer |
37 |
|
| 22550 |
ashik.ali |
38 |
@Autowired
|
| 22925 |
ashik.ali |
39 |
private UserWalletHistoryRepository userWalletHistoryRepository;
|
| 24739 |
tejbeer |
40 |
|
| 22550 |
ashik.ali |
41 |
@Override
|
| 24739 |
tejbeer |
42 |
public void addAmountToWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
|
|
|
43 |
String description, float amount) throws ProfitMandiBusinessException {
|
|
|
44 |
if (amount == 0)
|
|
|
45 |
return;
|
| 22857 |
ashik.ali |
46 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
| 23456 |
amit.gupta |
47 |
userWallet.setAmount(userWallet.getAmount() + Math.round(amount));
|
| 22857 |
ashik.ali |
48 |
userWalletRepository.persist(userWallet);
|
| 23456 |
amit.gupta |
49 |
this.createUserWalletHistory(Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
|
| 22857 |
ashik.ali |
50 |
}
|
| 24739 |
tejbeer |
51 |
|
| 22857 |
ashik.ali |
52 |
@Override
|
|
|
53 |
public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
|
|
|
54 |
String description, float amount) throws ProfitMandiBusinessException {
|
| 24739 |
tejbeer |
55 |
if (amount == 0)
|
|
|
56 |
return;
|
| 22857 |
ashik.ali |
57 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
| 24739 |
tejbeer |
58 |
if (amount > userWallet.getAmount()) {
|
| 22857 |
ashik.ali |
59 |
LOGGER.error("Wallet Balance is insufficient!");
|
|
|
60 |
throw new ProfitMandiBusinessException("balance", userWallet.getAmount(), "WLT_1000");
|
|
|
61 |
}
|
| 23509 |
amit.gupta |
62 |
userWallet.setAmount(userWallet.getAmount() - Math.round(amount));
|
| 22857 |
ashik.ali |
63 |
userWalletRepository.persist(userWallet);
|
| 23509 |
amit.gupta |
64 |
this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
|
| 22857 |
ashik.ali |
65 |
}
|
| 23509 |
amit.gupta |
66 |
|
|
|
67 |
@Override
|
| 24739 |
tejbeer |
68 |
public void rollbackAmountFromWallet(int retailerId, float amountToRollback, int rollbackReference,
|
|
|
69 |
WalletReferenceType walletReferenceType, String rollbackReason) {
|
|
|
70 |
if (amountToRollback == 0)
|
|
|
71 |
return;
|
| 23509 |
amit.gupta |
72 |
UserWallet userWallet = this.getUserWallet(retailerId);
|
|
|
73 |
userWallet.setAmount(userWallet.getAmount() - Math.round(amountToRollback));
|
|
|
74 |
userWalletRepository.persist(userWallet);
|
| 24739 |
tejbeer |
75 |
this.createUserWalletHistory(-Math.round(amountToRollback), userWallet.getId(), rollbackReference,
|
|
|
76 |
walletReferenceType, rollbackReason);
|
|
|
77 |
|
| 23509 |
amit.gupta |
78 |
}
|
| 24739 |
tejbeer |
79 |
|
|
|
80 |
private void createUserWalletHistory(float amount, int walletId, int referenceId, WalletReferenceType referenceType,
|
|
|
81 |
String description) {
|
|
|
82 |
if (amount == 0)
|
|
|
83 |
return;
|
| 22857 |
ashik.ali |
84 |
UserWalletHistory userWalletHistory = new UserWalletHistory();
|
|
|
85 |
userWalletHistory.setWalletId(walletId);
|
| 23444 |
amit.gupta |
86 |
userWalletHistory.setAmount(Math.round(amount));
|
| 22857 |
ashik.ali |
87 |
userWalletHistory.setReference(referenceId);
|
|
|
88 |
userWalletHistory.setReferenceType(referenceType);
|
|
|
89 |
userWalletHistory.setTimestamp(LocalDateTime.now());
|
|
|
90 |
userWalletHistory.setDescription(description);
|
|
|
91 |
userWalletHistoryRepository.persit(userWalletHistory);
|
|
|
92 |
}
|
| 24739 |
tejbeer |
93 |
|
| 22857 |
ashik.ali |
94 |
@Override
|
| 22550 |
ashik.ali |
95 |
public UserWallet getUserWalletByUserId(int userId) throws ProfitMandiBusinessException {
|
| 23269 |
ashik.ali |
96 |
UserAccount userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.saholic);
|
|
|
97 |
return userWalletRepository.selectByRetailerId(Integer.valueOf(userAccount.getAccountKey()));
|
| 22550 |
ashik.ali |
98 |
}
|
|
|
99 |
|
|
|
100 |
@Override
|
|
|
101 |
public List<UserWalletHistory> getUserWalletHistoryByUserId(int userId) throws ProfitMandiBusinessException {
|
|
|
102 |
UserWallet userWallet = this.getUserWalletByUserId(userId);
|
|
|
103 |
List<UserWalletHistory> userWalletHistories = userWalletHistoryRepository.selectByWalletId(userWallet.getId());
|
|
|
104 |
return userWalletHistories;
|
|
|
105 |
}
|
| 24739 |
tejbeer |
106 |
|
| 22550 |
ashik.ali |
107 |
@Override
|
|
|
108 |
public List<UserWalletHistory> getUserWalletHistoryByRetailerId(int retailerId)
|
|
|
109 |
throws ProfitMandiBusinessException {
|
|
|
110 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
111 |
return userWalletHistoryRepository.selectByWalletId(userWallet.getId());
|
|
|
112 |
}
|
| 24739 |
tejbeer |
113 |
|
| 22550 |
ashik.ali |
114 |
@Override
|
| 24739 |
tejbeer |
115 |
public long getSizeByRetailerId(int retailerId, LocalDateTime startDateTime, LocalDateTime endDateTime)
|
|
|
116 |
throws ProfitMandiBusinessException {
|
| 22550 |
ashik.ali |
117 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
118 |
return userWalletHistoryRepository.selectCountByWalletId(userWallet.getId(), startDateTime, endDateTime);
|
|
|
119 |
}
|
| 24739 |
tejbeer |
120 |
|
| 22550 |
ashik.ali |
121 |
@Override
|
|
|
122 |
public List<UserWalletHistory> getPaginatedUserWalletHistoryByRetailerId(int retailerId,
|
|
|
123 |
LocalDateTime startDateTime, LocalDateTime endDateTime, int offset, int limit)
|
|
|
124 |
throws ProfitMandiBusinessException {
|
|
|
125 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
| 24739 |
tejbeer |
126 |
return userWalletHistoryRepository.selectPaginatedByWalletId(userWallet.getId(), startDateTime, endDateTime,
|
|
|
127 |
offset, limit);
|
| 22550 |
ashik.ali |
128 |
}
|
| 24739 |
tejbeer |
129 |
|
| 23110 |
ashik.ali |
130 |
@Override
|
|
|
131 |
public Map<Integer, UserWallet> getRetailerIdUserWalletMap(Set<Integer> retailerIds) {
|
|
|
132 |
List<UserWallet> userWallets = userWalletRepository.selectByRetailerIds(retailerIds);
|
|
|
133 |
Map<Integer, UserWallet> retailerIdUserWalletMap = new HashMap<>();
|
| 24739 |
tejbeer |
134 |
for (UserWallet userWallet : userWallets) {
|
| 23110 |
ashik.ali |
135 |
retailerIdUserWalletMap.put(userWallet.getUserId(), userWallet);
|
|
|
136 |
}
|
|
|
137 |
return retailerIdUserWalletMap;
|
|
|
138 |
}
|
| 24739 |
tejbeer |
139 |
|
| 23504 |
ashik.ali |
140 |
@Override
|
| 24739 |
tejbeer |
141 |
public boolean isExistWalletHistory(int retailerId, int referenceId, WalletReferenceType referenceType)
|
|
|
142 |
throws ProfitMandiBusinessException {
|
| 23504 |
ashik.ali |
143 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
144 |
return userWalletHistoryRepository.isExist(userWallet.getId(), referenceType, referenceId);
|
|
|
145 |
}
|
| 22550 |
ashik.ali |
146 |
|
| 23509 |
amit.gupta |
147 |
@Override
|
|
|
148 |
public UserWallet getUserWallet(int retailerId) {
|
|
|
149 |
try {
|
|
|
150 |
return userWalletRepository.selectByRetailerId(retailerId);
|
| 24739 |
tejbeer |
151 |
} catch (Exception e) {
|
| 23509 |
amit.gupta |
152 |
UserWallet uw = new UserWallet();
|
|
|
153 |
uw.setAmount(0);
|
|
|
154 |
uw.setRefundableAmount(0);
|
|
|
155 |
uw.setUserId(retailerId);
|
|
|
156 |
userWalletRepository.persist(uw);
|
|
|
157 |
return uw;
|
|
|
158 |
}
|
|
|
159 |
}
|
|
|
160 |
|
| 24509 |
amit.gupta |
161 |
@Override
|
|
|
162 |
public float getOpeningTill(int fofoId, LocalDateTime date) throws ProfitMandiBusinessException {
|
|
|
163 |
UserWallet wallet = userWalletRepository.selectByRetailerId(fofoId);
|
|
|
164 |
return userWalletHistoryRepository.getSumTillDate(wallet.getId(), date);
|
|
|
165 |
}
|
| 24739 |
tejbeer |
166 |
|
|
|
167 |
@Override
|
|
|
168 |
public void refundToWallet(Integer retailerId, Float amountToRefund, Integer transactionId,
|
|
|
169 |
WalletReferenceType walletReferenceType, String description) throws ProfitMandiBusinessException {
|
|
|
170 |
|
|
|
171 |
List<UserWalletHistory> all_entries = userWalletHistoryRepository
|
|
|
172 |
.selectAllByreferenceIdandreferenceType(transactionId, WalletReferenceType.PURCHASE);
|
|
|
173 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
174 |
|
|
|
175 |
LOGGER.info("userWallet" + userWallet);
|
|
|
176 |
int creditable_amount_for_reference = 0;
|
|
|
177 |
|
|
|
178 |
for (UserWalletHistory history : all_entries) {
|
|
|
179 |
creditable_amount_for_reference -= history.getAmount();
|
|
|
180 |
|
|
|
181 |
LOGGER.info("creditable_amount_for_reference" + creditable_amount_for_reference);
|
|
|
182 |
|
|
|
183 |
LOGGER.info("walletAmount" + amountToRefund);
|
|
|
184 |
if (creditable_amount_for_reference < amountToRefund) {
|
|
|
185 |
|
|
|
186 |
LOGGER.info("order" + "Cant be credited back to wallet as most of it has been already credited");
|
|
|
187 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.USER_ID,userWallet.getUserId(), "Cant be credited back to wallet as most of it has been already credited");
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
userWallet.setAmount(userWallet.getAmount() + Math.round(amountToRefund));
|
|
|
191 |
|
|
|
192 |
LOGGER.info("userWallet" + userWallet);
|
|
|
193 |
|
|
|
194 |
}
|
|
|
195 |
UserWalletHistory userWalletHistory = new UserWalletHistory();
|
|
|
196 |
userWalletHistory.setAmount(Math.round(amountToRefund));
|
|
|
197 |
userWalletHistory.setReference(transactionId);
|
|
|
198 |
userWalletHistory.setReferenceType(WalletReferenceType.PURCHASE);
|
|
|
199 |
userWalletHistory.setWalletId(userWallet.getId());
|
|
|
200 |
userWalletHistory.setTimestamp(LocalDateTime.now());
|
|
|
201 |
userWalletHistory.setDescription(description);
|
|
|
202 |
|
|
|
203 |
LOGGER.info("all_entries" + userWalletHistory);
|
|
|
204 |
userWalletHistoryRepository.persit(userWalletHistory);
|
|
|
205 |
|
|
|
206 |
|
|
|
207 |
}
|
|
|
208 |
|
| 22550 |
ashik.ali |
209 |
}
|