Subversion Repositories SmartDukaan

Rev

Rev 23509 | Rev 24269 | 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
 
23568 govind 9
import org.apache.logging.log4j.Logger;
10
import org.apache.logging.log4j.LogManager;
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
 
23568 govind 28
	private static final Logger LOGGER = LogManager.getLogger(WalletServiceImpl.class);
22857 ashik.ali 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
		}
23509 amit.gupta 56
		userWallet.setAmount(userWallet.getAmount() - Math.round(amount));
22857 ashik.ali 57
		userWalletRepository.persist(userWallet);
23509 amit.gupta 58
		this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
22857 ashik.ali 59
	}
60
 
23509 amit.gupta 61
 
62
	@Override
63
	public void rollbackAmountFromWallet(int retailerId, float amountToRollback, int rollbackReference, WalletReferenceType walletReferenceType,
64
			String rollbackReason) {
65
		UserWallet userWallet = this.getUserWallet(retailerId);
66
		userWallet.setAmount(userWallet.getAmount() - Math.round(amountToRollback));
67
		userWalletRepository.persist(userWallet);
68
		this.createUserWalletHistory(-Math.round(amountToRollback), userWallet.getId(), rollbackReference, walletReferenceType, rollbackReason);
69
 
70
	}
71
 
22857 ashik.ali 72
	private void createUserWalletHistory(float amount, int walletId, int referenceId, WalletReferenceType referenceType, String description){
73
		UserWalletHistory userWalletHistory = new UserWalletHistory();
74
		userWalletHistory.setWalletId(walletId);
23444 amit.gupta 75
		userWalletHistory.setAmount(Math.round(amount));
22857 ashik.ali 76
		userWalletHistory.setReference(referenceId);
77
		userWalletHistory.setReferenceType(referenceType);
78
		userWalletHistory.setTimestamp(LocalDateTime.now());
79
		userWalletHistory.setDescription(description);
80
		userWalletHistoryRepository.persit(userWalletHistory);
81
	}
82
 
83
 
84
	@Override
22550 ashik.ali 85
	public UserWallet getUserWalletByUserId(int userId) throws ProfitMandiBusinessException {
23269 ashik.ali 86
		UserAccount userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.saholic);
87
		return userWalletRepository.selectByRetailerId(Integer.valueOf(userAccount.getAccountKey()));
22550 ashik.ali 88
	}
89
 
90
	@Override
91
	public List<UserWalletHistory> getUserWalletHistoryByUserId(int userId) throws ProfitMandiBusinessException {
92
		UserWallet userWallet = this.getUserWalletByUserId(userId);
93
		List<UserWalletHistory> userWalletHistories = userWalletHistoryRepository.selectByWalletId(userWallet.getId());
94
		return userWalletHistories;
95
	}
96
 
97
	@Override
98
	public List<UserWalletHistory> getUserWalletHistoryByRetailerId(int retailerId)
99
			throws ProfitMandiBusinessException {
100
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
101
		return userWalletHistoryRepository.selectByWalletId(userWallet.getId());
102
	}
103
 
104
	@Override
105
	public long getSizeByRetailerId(int retailerId, LocalDateTime startDateTime, LocalDateTime endDateTime) throws ProfitMandiBusinessException{
106
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
107
		return userWalletHistoryRepository.selectCountByWalletId(userWallet.getId(), startDateTime, endDateTime);
108
	}
109
 
110
	@Override
111
	public List<UserWalletHistory> getPaginatedUserWalletHistoryByRetailerId(int retailerId,
112
			LocalDateTime startDateTime, LocalDateTime endDateTime, int offset, int limit)
113
			throws ProfitMandiBusinessException {
114
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
115
		return userWalletHistoryRepository.selectPaginatedByWalletId(userWallet.getId(), startDateTime, endDateTime, offset, limit);
116
	}
23110 ashik.ali 117
 
118
	@Override
119
	public Map<Integer, UserWallet> getRetailerIdUserWalletMap(Set<Integer> retailerIds) {
120
		List<UserWallet> userWallets = userWalletRepository.selectByRetailerIds(retailerIds);
121
		Map<Integer, UserWallet> retailerIdUserWalletMap = new HashMap<>();
122
		for(UserWallet userWallet : userWallets){
123
			retailerIdUserWalletMap.put(userWallet.getUserId(), userWallet);
124
		}
125
		return retailerIdUserWalletMap;
126
	}
23504 ashik.ali 127
 
128
	@Override
129
	public boolean isExistWalletHistory(int retailerId, int referenceId, WalletReferenceType referenceType) throws ProfitMandiBusinessException{
130
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
131
		return userWalletHistoryRepository.isExist(userWallet.getId(), referenceType, referenceId);
132
	}
22550 ashik.ali 133
 
23509 amit.gupta 134
	@Override
135
	public UserWallet getUserWallet(int retailerId) {
136
		try {
137
			return userWalletRepository.selectByRetailerId(retailerId);
138
		} catch(Exception e) {
139
			UserWallet uw = new UserWallet();
140
			uw.setAmount(0);
141
			uw.setRefundableAmount(0);
142
			uw.setUserId(retailerId);
143
			userWalletRepository.persist(uw);
144
			return uw;
145
		}
146
	}
147
 
22550 ashik.ali 148
}