Subversion Repositories SmartDukaan

Rev

Rev 23456 | Rev 23504 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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