| 22550 |
ashik.ali |
1 |
package com.spice.profitmandi.service.wallet;
|
|
|
2 |
|
|
|
3 |
import java.time.LocalDateTime;
|
|
|
4 |
import java.util.List;
|
|
|
5 |
|
| 22857 |
ashik.ali |
6 |
import org.slf4j.Logger;
|
|
|
7 |
import org.slf4j.LoggerFactory;
|
| 22550 |
ashik.ali |
8 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
9 |
import org.springframework.stereotype.Component;
|
|
|
10 |
|
|
|
11 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
12 |
import com.spice.profitmandi.dao.entity.dtr.UserAccounts;
|
|
|
13 |
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
|
|
|
14 |
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
|
|
|
15 |
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
|
|
|
16 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
|
|
17 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
|
|
|
18 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
|
|
|
19 |
|
| 22857 |
ashik.ali |
20 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
|
|
21 |
|
| 22550 |
ashik.ali |
22 |
@Component
|
|
|
23 |
public class WalletServiceImpl implements WalletService {
|
|
|
24 |
|
| 22857 |
ashik.ali |
25 |
private static final Logger LOGGER = LoggerFactory.getLogger(WalletServiceImpl.class);
|
|
|
26 |
|
| 22550 |
ashik.ali |
27 |
@Autowired
|
|
|
28 |
UserAccountRepository userAccountRepository;
|
|
|
29 |
|
|
|
30 |
@Autowired
|
|
|
31 |
UserWalletRepository userWalletRepository;
|
|
|
32 |
|
|
|
33 |
@Autowired
|
|
|
34 |
UserWalletHistoryRepository userWalletHistoryRepository;
|
|
|
35 |
|
|
|
36 |
@Override
|
| 22857 |
ashik.ali |
37 |
public void addAmountToWallet(int retailerId, int referenceId, WalletReferenceType referenceType, String description,
|
|
|
38 |
float amount) throws ProfitMandiBusinessException{
|
|
|
39 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
40 |
userWallet.setAmount(userWallet.getAmount() + Float.valueOf(amount).intValue());
|
|
|
41 |
userWalletRepository.persist(userWallet);
|
|
|
42 |
this.createUserWalletHistory(amount, userWallet.getId(), referenceId, referenceType, description);
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
@Override
|
|
|
46 |
public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
|
|
|
47 |
String description, float amount) throws ProfitMandiBusinessException {
|
|
|
48 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
49 |
if(amount > userWallet.getAmount()){
|
|
|
50 |
LOGGER.error("Wallet Balance is insufficient!");
|
|
|
51 |
throw new ProfitMandiBusinessException("balance", userWallet.getAmount(), "WLT_1000");
|
|
|
52 |
}
|
|
|
53 |
userWallet.setAmount(userWallet.getAmount() - Float.valueOf(amount).intValue());
|
|
|
54 |
userWalletRepository.persist(userWallet);
|
|
|
55 |
this.createUserWalletHistory(amount, userWallet.getId(), referenceId, referenceType, description);
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
private void createUserWalletHistory(float amount, int walletId, int referenceId, WalletReferenceType referenceType, String description){
|
|
|
59 |
UserWalletHistory userWalletHistory = new UserWalletHistory();
|
|
|
60 |
userWalletHistory.setWalletId(walletId);
|
|
|
61 |
userWalletHistory.setAmount(Float.valueOf(amount).intValue());
|
|
|
62 |
userWalletHistory.setReference(referenceId);
|
|
|
63 |
userWalletHistory.setReferenceType(referenceType);
|
|
|
64 |
userWalletHistory.setTimestamp(LocalDateTime.now());
|
|
|
65 |
userWalletHistory.setDescription(description);
|
|
|
66 |
userWalletHistoryRepository.persit(userWalletHistory);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
|
|
|
70 |
@Override
|
| 22550 |
ashik.ali |
71 |
public UserWallet getUserWalletByUserId(int userId) throws ProfitMandiBusinessException {
|
|
|
72 |
UserAccounts userAccount = userAccountRepository.getUserAccountByType(userId, AccountType.saholic);
|
| 22857 |
ashik.ali |
73 |
return userWalletRepository.selectByRetailerId(Integer.valueOf(userAccount.getAccount_key()));
|
| 22550 |
ashik.ali |
74 |
}
|
|
|
75 |
|
|
|
76 |
@Override
|
|
|
77 |
public List<UserWalletHistory> getUserWalletHistoryByUserId(int userId) throws ProfitMandiBusinessException {
|
|
|
78 |
UserWallet userWallet = this.getUserWalletByUserId(userId);
|
|
|
79 |
List<UserWalletHistory> userWalletHistories = userWalletHistoryRepository.selectByWalletId(userWallet.getId());
|
|
|
80 |
return userWalletHistories;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
@Override
|
|
|
84 |
public List<UserWalletHistory> getUserWalletHistoryByRetailerId(int retailerId)
|
|
|
85 |
throws ProfitMandiBusinessException {
|
|
|
86 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
87 |
return userWalletHistoryRepository.selectByWalletId(userWallet.getId());
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
@Override
|
|
|
91 |
public long getSizeByRetailerId(int retailerId, LocalDateTime startDateTime, LocalDateTime endDateTime) throws ProfitMandiBusinessException{
|
|
|
92 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
93 |
return userWalletHistoryRepository.selectCountByWalletId(userWallet.getId(), startDateTime, endDateTime);
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
@Override
|
|
|
97 |
public List<UserWalletHistory> getPaginatedUserWalletHistoryByRetailerId(int retailerId,
|
|
|
98 |
LocalDateTime startDateTime, LocalDateTime endDateTime, int offset, int limit)
|
|
|
99 |
throws ProfitMandiBusinessException {
|
|
|
100 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
101 |
return userWalletHistoryRepository.selectPaginatedByWalletId(userWallet.getId(), startDateTime, endDateTime, offset, limit);
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
}
|