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