Subversion Repositories SmartDukaan

Rev

Rev 24269 | Rev 24739 | 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
 
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;
23269 ashik.ali 16
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
22550 ashik.ali 17
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
18
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
19
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
20
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
21
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
22
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
23
 
22857 ashik.ali 24
import in.shop2020.model.v1.order.WalletReferenceType;
25
 
22550 ashik.ali 26
@Component
27
public class WalletServiceImpl implements WalletService {
28
 
23568 govind 29
	private static final Logger LOGGER = LogManager.getLogger(WalletServiceImpl.class);
22857 ashik.ali 30
 
22550 ashik.ali 31
	@Autowired
22925 ashik.ali 32
	private UserAccountRepository userAccountRepository;
22550 ashik.ali 33
 
34
	@Autowired
22925 ashik.ali 35
	private UserWalletRepository userWalletRepository;
22550 ashik.ali 36
 
37
	@Autowired
22925 ashik.ali 38
	private UserWalletHistoryRepository userWalletHistoryRepository;
22550 ashik.ali 39
 
40
	@Override
22857 ashik.ali 41
	public void addAmountToWallet(int retailerId, int referenceId, WalletReferenceType referenceType, String description,
42
			float amount) throws ProfitMandiBusinessException{
24269 amit.gupta 43
		if(amount == 0) return;
22857 ashik.ali 44
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
23456 amit.gupta 45
		userWallet.setAmount(userWallet.getAmount() + Math.round(amount));
22857 ashik.ali 46
		userWalletRepository.persist(userWallet);
23456 amit.gupta 47
		this.createUserWalletHistory(Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
22857 ashik.ali 48
	}
49
 
50
	@Override
51
	public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
52
			String description, float amount) throws ProfitMandiBusinessException {
24269 amit.gupta 53
		if(amount == 0) return;
22857 ashik.ali 54
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
55
		if(amount > userWallet.getAmount()){
56
			LOGGER.error("Wallet Balance is insufficient!");
57
			throw new ProfitMandiBusinessException("balance", userWallet.getAmount(), "WLT_1000");
58
		}
23509 amit.gupta 59
		userWallet.setAmount(userWallet.getAmount() - Math.round(amount));
22857 ashik.ali 60
		userWalletRepository.persist(userWallet);
23509 amit.gupta 61
		this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
22857 ashik.ali 62
	}
63
 
23509 amit.gupta 64
 
65
	@Override
66
	public void rollbackAmountFromWallet(int retailerId, float amountToRollback, int rollbackReference, WalletReferenceType walletReferenceType,
67
			String rollbackReason) {
24269 amit.gupta 68
		if(amountToRollback == 0) return;
23509 amit.gupta 69
		UserWallet userWallet = this.getUserWallet(retailerId);
70
		userWallet.setAmount(userWallet.getAmount() - Math.round(amountToRollback));
71
		userWalletRepository.persist(userWallet);
72
		this.createUserWalletHistory(-Math.round(amountToRollback), userWallet.getId(), rollbackReference, walletReferenceType, rollbackReason);
73
 
74
	}
75
 
22857 ashik.ali 76
	private void createUserWalletHistory(float amount, int walletId, int referenceId, WalletReferenceType referenceType, String description){
24269 amit.gupta 77
		if(amount==0) return;
22857 ashik.ali 78
		UserWalletHistory userWalletHistory = new UserWalletHistory();
79
		userWalletHistory.setWalletId(walletId);
23444 amit.gupta 80
		userWalletHistory.setAmount(Math.round(amount));
22857 ashik.ali 81
		userWalletHistory.setReference(referenceId);
82
		userWalletHistory.setReferenceType(referenceType);
83
		userWalletHistory.setTimestamp(LocalDateTime.now());
84
		userWalletHistory.setDescription(description);
85
		userWalletHistoryRepository.persit(userWalletHistory);
86
	}
87
 
88
 
89
	@Override
22550 ashik.ali 90
	public UserWallet getUserWalletByUserId(int userId) throws ProfitMandiBusinessException {
23269 ashik.ali 91
		UserAccount userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.saholic);
92
		return userWalletRepository.selectByRetailerId(Integer.valueOf(userAccount.getAccountKey()));
22550 ashik.ali 93
	}
94
 
95
	@Override
96
	public List<UserWalletHistory> getUserWalletHistoryByUserId(int userId) throws ProfitMandiBusinessException {
97
		UserWallet userWallet = this.getUserWalletByUserId(userId);
98
		List<UserWalletHistory> userWalletHistories = userWalletHistoryRepository.selectByWalletId(userWallet.getId());
99
		return userWalletHistories;
100
	}
101
 
102
	@Override
103
	public List<UserWalletHistory> getUserWalletHistoryByRetailerId(int retailerId)
104
			throws ProfitMandiBusinessException {
105
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
106
		return userWalletHistoryRepository.selectByWalletId(userWallet.getId());
107
	}
108
 
109
	@Override
110
	public long getSizeByRetailerId(int retailerId, LocalDateTime startDateTime, LocalDateTime endDateTime) throws ProfitMandiBusinessException{
111
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
112
		return userWalletHistoryRepository.selectCountByWalletId(userWallet.getId(), startDateTime, endDateTime);
113
	}
114
 
115
	@Override
116
	public List<UserWalletHistory> getPaginatedUserWalletHistoryByRetailerId(int retailerId,
117
			LocalDateTime startDateTime, LocalDateTime endDateTime, int offset, int limit)
118
			throws ProfitMandiBusinessException {
119
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
120
		return userWalletHistoryRepository.selectPaginatedByWalletId(userWallet.getId(), startDateTime, endDateTime, offset, limit);
121
	}
23110 ashik.ali 122
 
123
	@Override
124
	public Map<Integer, UserWallet> getRetailerIdUserWalletMap(Set<Integer> retailerIds) {
125
		List<UserWallet> userWallets = userWalletRepository.selectByRetailerIds(retailerIds);
126
		Map<Integer, UserWallet> retailerIdUserWalletMap = new HashMap<>();
127
		for(UserWallet userWallet : userWallets){
128
			retailerIdUserWalletMap.put(userWallet.getUserId(), userWallet);
129
		}
130
		return retailerIdUserWalletMap;
131
	}
23504 ashik.ali 132
 
133
	@Override
134
	public boolean isExistWalletHistory(int retailerId, int referenceId, WalletReferenceType referenceType) throws ProfitMandiBusinessException{
135
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
136
		return userWalletHistoryRepository.isExist(userWallet.getId(), referenceType, referenceId);
137
	}
22550 ashik.ali 138
 
23509 amit.gupta 139
	@Override
140
	public UserWallet getUserWallet(int retailerId) {
141
		try {
142
			return userWalletRepository.selectByRetailerId(retailerId);
143
		} catch(Exception e) {
144
			UserWallet uw = new UserWallet();
145
			uw.setAmount(0);
146
			uw.setRefundableAmount(0);
147
			uw.setUserId(retailerId);
148
			userWalletRepository.persist(uw);
149
			return uw;
150
		}
151
	}
152
 
24509 amit.gupta 153
	@Override
154
	public float getOpeningTill(int fofoId, LocalDateTime date) throws ProfitMandiBusinessException {
155
		UserWallet wallet = userWalletRepository.selectByRetailerId(fofoId);
156
		return userWalletHistoryRepository.getSumTillDate(wallet.getId(), date);
157
	}
158
 
22550 ashik.ali 159
}