Subversion Repositories SmartDukaan

Rev

Rev 23723 | Rev 23738 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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