Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
23506 amit.gupta 1
package com.spice.profitmandi.web.controller;
2
 
23662 amit.gupta 3
import java.time.LocalDate;
23506 amit.gupta 4
import java.time.LocalDateTime;
23618 amit.gupta 5
import java.time.temporal.ChronoUnit;
23621 amit.gupta 6
import java.util.ArrayList;
23618 amit.gupta 7
import java.util.Arrays;
8
import java.util.HashMap;
23506 amit.gupta 9
import java.util.List;
23618 amit.gupta 10
import java.util.Map;
23662 amit.gupta 11
import java.util.stream.Collectors;
23506 amit.gupta 12
 
13
import javax.servlet.http.HttpServletRequest;
23612 amit.gupta 14
 
23568 govind 15
import org.apache.logging.log4j.LogManager;
16
import org.apache.logging.log4j.Logger;
23506 amit.gupta 17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.beans.factory.annotation.Value;
19
import org.springframework.scheduling.annotation.Scheduled;
20
import org.springframework.stereotype.Controller;
21
import org.springframework.transaction.annotation.Transactional;
22
import org.springframework.ui.Model;
23
import org.springframework.web.bind.annotation.RequestMapping;
24
import org.springframework.web.bind.annotation.RequestMethod;
25
 
23618 amit.gupta 26
import com.spice.profitmandi.common.enumuration.RechargeStatus;
23506 amit.gupta 27
import com.spice.profitmandi.common.enumuration.SchemeType;
23618 amit.gupta 28
import com.spice.profitmandi.common.model.RechargeCredential;
23506 amit.gupta 29
import com.spice.profitmandi.common.web.client.RestClient;
23662 amit.gupta 30
import com.spice.profitmandi.dao.entity.dtr.DailyRecharge;
31
import com.spice.profitmandi.dao.entity.dtr.RechargeProvider;
32
import com.spice.profitmandi.dao.entity.dtr.RechargeProviderCreditWalletHistory;
23618 amit.gupta 33
import com.spice.profitmandi.dao.entity.dtr.RechargeTransaction;
23511 amit.gupta 34
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
23506 amit.gupta 35
import com.spice.profitmandi.dao.entity.fofo.Purchase;
23662 amit.gupta 36
import com.spice.profitmandi.dao.repository.dtr.DailyRechargeRepository;
37
import com.spice.profitmandi.dao.repository.dtr.RechargeProviderCreditWalletHistoryRepository;
38
import com.spice.profitmandi.dao.repository.dtr.RechargeProviderRepository;
23612 amit.gupta 39
import com.spice.profitmandi.dao.repository.dtr.RechargeTransactionRepository;
23511 amit.gupta 40
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
23506 amit.gupta 41
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
23612 amit.gupta 42
import com.spice.profitmandi.service.recharge.provider.OxigenRechargeProviderService;
43
import com.spice.profitmandi.service.recharge.provider.ThinkWalnutDigitalRechargeProviderService;
23506 amit.gupta 44
import com.spice.profitmandi.service.scheme.SchemeService;
45
import com.spice.profitmandi.web.util.MVCResponseSender;
46
 
47
@Controller
23662 amit.gupta 48
@Transactional(rollbackFor = Throwable.class)
23506 amit.gupta 49
public class CronController {
23662 amit.gupta 50
 
23637 amit.gupta 51
	@Value("${oxigen.recharge.transaction.url}")
52
	private String oxigenRechargeTransactionUrl;
23662 amit.gupta 53
 
23618 amit.gupta 54
	@Value("${oxigen.recharge.enquiry.url}")
55
	private String oxigenRechargeEnquiryUrl;
23662 amit.gupta 56
 
23618 amit.gupta 57
	@Value("${oxigen.recharge.auth.key}")
58
	private String oxigenRechargeAuthKey;
23662 amit.gupta 59
 
23618 amit.gupta 60
	@Value("${oxigen.recharge.validation.url}")
61
	private String oxigenRechargeValidationUrl;
23662 amit.gupta 62
 
23618 amit.gupta 63
	@Value("${oxigen.recharge.validation.auth.key}")
64
	private String oxigenRechargeValidationAuthKey;
23662 amit.gupta 65
 
23618 amit.gupta 66
	@Value("${think.walnut.digital.recharge.transaction.mobile.url}")
67
	private String thinkWalnutDigitalRechargeTransactionMobileUrl;
23662 amit.gupta 68
 
23618 amit.gupta 69
	@Value("${think.walnut.digital.recharge.transaction.dth.url}")
70
	private String thinkWalnutDigitalRechargeTransactionDthUrl;
23662 amit.gupta 71
 
23618 amit.gupta 72
	@Value("${think.walnut.digital.recharge.enquiry.url}")
73
	private String thinkWalnutDigitalRechargeEnquiryUrl;
23662 amit.gupta 74
 
23637 amit.gupta 75
	@Value("${think.walnut.digital.recharge.balance.url}")
76
	private String thinkWalnutDigitalRechargeBalanceUrl;
23662 amit.gupta 77
 
23637 amit.gupta 78
	@Value("${think.walnut.digital.recharge.username}")
79
	private String thinkWalnutDigitalRechargeUserName;
23662 amit.gupta 80
 
23637 amit.gupta 81
	@Value("${think.walnut.digital.recharge.password}")
82
	private String thinkWalnutDigitalRechargePassword;
23662 amit.gupta 83
 
23618 amit.gupta 84
	@Value("${think.walnut.digital.recharge.auth.key}")
85
	private String thinkWalnutDigitalRechargeAuthKey;
23506 amit.gupta 86
 
87
	@Autowired
23662 amit.gupta 88
	private PurchaseRepository purchaseRepository;
23506 amit.gupta 89
 
90
	@Autowired
23662 amit.gupta 91
	private SchemeService schemeService;
23506 amit.gupta 92
 
93
	@Autowired
23662 amit.gupta 94
	private RechargeTransactionRepository rechargeTransactionRepository;
95
 
23612 amit.gupta 96
	@Autowired
23662 amit.gupta 97
	private RechargeProviderCreditWalletHistoryRepository rechargeProviderCreditWalletHistoryRepository;
98
 
23530 ashik.ali 99
	@Autowired
23662 amit.gupta 100
	private FofoOrderRepository fofoOrderRepository;
23612 amit.gupta 101
 
102
	@Autowired
23662 amit.gupta 103
	private ThinkWalnutDigitalRechargeProviderService thinkWalnutDigitalRechargeProviderService;
104
 
23612 amit.gupta 105
	@Autowired
23662 amit.gupta 106
	private OxigenRechargeProviderService oxigenRechargeProviderService;
107
 
108
	@Autowired
109
	private RechargeProviderRepository rechargeProviderRepository;
110
 
111
	@Autowired
112
	private DailyRechargeRepository dailyRechargeRepository;
113
 
114
	@Autowired
23530 ashik.ali 115
	private RestClient restClient;
23511 amit.gupta 116
 
117
	@Autowired
23506 amit.gupta 118
	private MVCResponseSender mvcResponseSender;
23662 amit.gupta 119
 
23506 amit.gupta 120
	@Value("${prod}")
121
	private boolean prod;
122
 
23612 amit.gupta 123
	private static final Logger LOGGER = LogManager.getLogger(CronController.class);
23506 amit.gupta 124
 
125
	@Scheduled(cron = "0 45 6 * * *")
126
	public void executeJob() throws Exception {
23662 amit.gupta 127
		if (prod) {
23506 amit.gupta 128
			String uri = "/cron/process-schemes";
23530 ashik.ali 129
			restClient.get(SchemeType.HTTP, "localhost", 8080, uri, null);
23506 amit.gupta 130
		}
131
	}
23662 amit.gupta 132
 
133
	@RequestMapping(value = "/cron/daily-generation", method = RequestMethod.GET)
134
	public void executeDailRechargeGeneration() {
135
		List<RechargeProviderCreditWalletHistory> allCreditHistory = rechargeProviderCreditWalletHistoryRepository
136
				.selectAll(0, 2000);
137
		List<RechargeProvider> rechargeProviders = rechargeProviderRepository.selectAll();
138
		rechargeProviders.stream().forEach(x -> x.setAmount(0));
139
 
140
 
141
		rechargeProviders.stream().forEach(x -> {
23677 amit.gupta 142
			Map<LocalDate, List<RechargeProviderCreditWalletHistory>> dateWiseProviderCreditsMap = allCreditHistory.stream()
23679 amit.gupta 143
					.filter(z->z.getProviderId()==x.getId()).collect(Collectors.groupingBy(x1 -> x1.getReceiveTimestamp().toLocalDate()));
23678 amit.gupta 144
 
145
			LOGGER.info("dateWiseProviderCreditsMap -- {}",dateWiseProviderCreditsMap);
23667 amit.gupta 146
			LocalDate endDate = LocalDate.now().plusDays(1);
147
			float previousDayClosing = 0;
23662 amit.gupta 148
			LocalDate date = LocalDate.of(2018, 4, 6);
149
			while (date.isBefore(endDate)) {
150
				List<RechargeTransaction> dateWiseRechargeTransactions = rechargeTransactionRepository
151
						.selectAllBetweenTimestamp(Arrays.asList(RechargeStatus.values()), date.atStartOfDay(),
152
								date.plusDays(1).atStartOfDay());
153
 
154
				List<RechargeTransaction> successfulTransactions = dateWiseRechargeTransactions.stream()
155
						.filter(y -> y.getStatus().equals(RechargeStatus.SUCCESS)).collect(Collectors.toList());
156
 
157
				float dailyAmount = 0;
158
				float totalCommission = 0;
159
				for (RechargeTransaction rechargeTransaction : successfulTransactions) {
160
					if (rechargeTransaction.getProviderId() == x.getId()) {
161
						dailyAmount += rechargeTransaction.getAmount();
162
						totalCommission += rechargeTransaction.getCommission();
163
					}
164
				}
165
 
166
				List<RechargeProviderCreditWalletHistory> rechargeHistoryList = dateWiseProviderCreditsMap.get(date);
167
				float dailyWalletRecharge = 0;
23664 amit.gupta 168
				if(rechargeHistoryList!=null) {
169
					for (RechargeProviderCreditWalletHistory rechargeProviderCreditWalletHistory : rechargeHistoryList) {
170
						if (rechargeProviderCreditWalletHistory.getProviderId() == x.getId()) {
171
							dailyWalletRecharge += rechargeProviderCreditWalletHistory.getAmount();
172
						}
23662 amit.gupta 173
					}
174
				}
175
				if (dailyAmount > 0 || dailyWalletRecharge > 0) {
23677 amit.gupta 176
					DailyRecharge dailyRecharge = null;
177
					try {
178
						dailyRecharge = dailyRechargeRepository.selectByProviderIdAndCreateDate(x.getId(), date);
179
					} catch(Exception e) {
180
						LOGGER.info("Could not find Recharge entry");
181
					}
182
					if (dailyRecharge == null) {
183
						dailyRecharge = new DailyRecharge();
184
						dailyRecharge.setCreateDate(date);
185
					}
23662 amit.gupta 186
					dailyRecharge.setOpeningBalance(previousDayClosing);
187
					dailyRecharge.setProviderId(x.getId());
188
					dailyRecharge.setWalletRechargeAmount(dailyWalletRecharge);
23675 amit.gupta 189
					dailyRecharge.setTotalAmount(dailyAmount);
23662 amit.gupta 190
					dailyRecharge.setTotalCommission(totalCommission);
191
					float closingBalance = dailyRecharge.getOpeningBalance() + dailyWalletRecharge - dailyAmount;
192
					dailyRecharge.setClosingBalance(closingBalance);
193
					dailyRechargeRepository.persist(dailyRecharge);
23665 amit.gupta 194
					x.setAmount(x.getAmount() + dailyRecharge.getClosingBalance() - dailyRecharge.getOpeningBalance());
23676 amit.gupta 195
					previousDayClosing = dailyRecharge.getClosingBalance();
23662 amit.gupta 196
				}
197
				date = date.plusDays(1);
198
			}
23665 amit.gupta 199
			rechargeProviderRepository.persist(x);
23662 amit.gupta 200
		});
201
	}
202
 
23618 amit.gupta 203
	@RequestMapping(value = "/cron/reconcile-recharge", method = RequestMethod.GET)
204
	public String reconcileRecharge(Model model) throws Exception {
205
		LocalDateTime fromDate = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS).minusDays(30);
206
		LocalDateTime toDate = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS);
23621 amit.gupta 207
		List<RechargeStatus> nonSuccessRechargeStatuses = new ArrayList<>(Arrays.asList(RechargeStatus.values()));
23620 amit.gupta 208
		LOGGER.info("nonSuccessRechargeStatuses {} ", nonSuccessRechargeStatuses);
23618 amit.gupta 209
		nonSuccessRechargeStatuses.remove(RechargeStatus.SUCCESS);
210
		nonSuccessRechargeStatuses.remove(RechargeStatus.FAILED);
211
		RechargeCredential thinkWalnutDigitalRechargeEnquiryCredential = new RechargeCredential();
212
		thinkWalnutDigitalRechargeEnquiryCredential.setRechargeUrl(thinkWalnutDigitalRechargeEnquiryUrl);
23637 amit.gupta 213
		thinkWalnutDigitalRechargeEnquiryCredential.setRechargeUserName(thinkWalnutDigitalRechargeUserName);
214
		thinkWalnutDigitalRechargeEnquiryCredential.setRechargePassword(thinkWalnutDigitalRechargePassword);
23618 amit.gupta 215
		thinkWalnutDigitalRechargeEnquiryCredential.setRechargeAuthKey(thinkWalnutDigitalRechargeAuthKey);
216
		Map<String, RechargeStatus> requestRechargeStatusChanged = new HashMap<>();
23662 amit.gupta 217
		List<RechargeTransaction> rechargeTransactions = rechargeTransactionRepository
218
				.selectAllBetweenTimestamp(nonSuccessRechargeStatuses, fromDate, toDate);
219
		for (RechargeTransaction rechargeTransaction : rechargeTransactions) {
23626 amit.gupta 220
			try {
221
				int providerId = rechargeTransaction.getProviderId();
23662 amit.gupta 222
				if (providerId == 1) {
223
					oxigenRechargeProviderService.doCheckStatusRequest(oxigenRechargeEnquiryUrl, oxigenRechargeAuthKey,
224
							rechargeTransaction);
225
				} else if (providerId == 2) {
226
					thinkWalnutDigitalRechargeProviderService
227
							.doCheckStatusRequest(thinkWalnutDigitalRechargeEnquiryCredential, rechargeTransaction);
23626 amit.gupta 228
				}
23662 amit.gupta 229
				if (rechargeTransaction.getStatus().equals(RechargeStatus.SUCCESS)
230
						|| rechargeTransaction.getStatus().equals(RechargeStatus.FAILED)) {
231
					requestRechargeStatusChanged.put(rechargeTransaction.getRequestId(),
232
							rechargeTransaction.getStatus());
23626 amit.gupta 233
				}
234
			} catch (Exception e) {
235
				LOGGER.info("Could not check status for Request {}", rechargeTransaction.getRequestId());
23612 amit.gupta 236
			}
237
		}
23618 amit.gupta 238
		model.addAttribute("response", mvcResponseSender.createResponseString(requestRechargeStatusChanged));
239
		return "response";
240
	}
23662 amit.gupta 241
 
23506 amit.gupta 242
	@RequestMapping(value = "/cron/process-schemes", method = RequestMethod.GET)
23558 amit.gupta 243
	public String createScheme(HttpServletRequest request, Model model) throws Exception {
23564 amit.gupta 244
		LocalDateTime fromDate = LocalDateTime.now().minusDays(15);
23506 amit.gupta 245
		LOGGER.info("Started execution at {}", LocalDateTime.now());
246
		List<Purchase> purchases = purchaseRepository.selectFromPurchaseCompleteDate(fromDate);
247
		for (Purchase purchase : purchases) {
248
			try {
249
				schemeService.processSchemeIn(purchase.getId(), purchase.getFofoId());
250
			} catch (Exception e) {
251
				LOGGER.error("Error while processing purchase {} for scheme In ", purchase.getId());
252
				e.printStackTrace();
23662 amit.gupta 253
 
23506 amit.gupta 254
			}
255
		}
23511 amit.gupta 256
		List<FofoOrder> fofoOrders = fofoOrderRepository.selectFromSaleDate(fromDate);
23662 amit.gupta 257
		for (FofoOrder fofoOrder : fofoOrders) {
23511 amit.gupta 258
			try {
259
				schemeService.processSchemeOut(fofoOrder.getId(), fofoOrder.getFofoId());
260
			} catch (Exception e) {
261
				LOGGER.error("Error while processing sale order {} for scheme Out", fofoOrder.getId());
262
			}
263
		}
23506 amit.gupta 264
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
265
		return "response";
266
	}
267
 
268
}