| 23506 |
amit.gupta |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
|
|
3 |
import java.time.LocalDateTime;
|
|
|
4 |
import java.util.List;
|
|
|
5 |
|
|
|
6 |
import javax.servlet.http.HttpServletRequest;
|
| 23568 |
govind |
7 |
import org.apache.logging.log4j.LogManager;
|
|
|
8 |
import org.apache.logging.log4j.Logger;
|
| 23506 |
amit.gupta |
9 |
|
| 23568 |
govind |
10 |
|
| 23506 |
amit.gupta |
11 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
12 |
import org.springframework.beans.factory.annotation.Value;
|
|
|
13 |
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
14 |
import org.springframework.stereotype.Controller;
|
|
|
15 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
16 |
import org.springframework.ui.Model;
|
|
|
17 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
18 |
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
19 |
|
|
|
20 |
import com.spice.profitmandi.common.enumuration.SchemeType;
|
|
|
21 |
import com.spice.profitmandi.common.web.client.RestClient;
|
| 23511 |
amit.gupta |
22 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
|
| 23506 |
amit.gupta |
23 |
import com.spice.profitmandi.dao.entity.fofo.Purchase;
|
| 23511 |
amit.gupta |
24 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
|
| 23506 |
amit.gupta |
25 |
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
|
|
|
26 |
import com.spice.profitmandi.service.scheme.SchemeService;
|
|
|
27 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
|
|
28 |
|
|
|
29 |
@Controller
|
| 23511 |
amit.gupta |
30 |
@Transactional(rollbackFor=Throwable.class)
|
| 23506 |
amit.gupta |
31 |
public class CronController {
|
|
|
32 |
|
|
|
33 |
@Autowired
|
|
|
34 |
PurchaseRepository purchaseRepository;
|
|
|
35 |
|
|
|
36 |
@Autowired
|
|
|
37 |
SchemeService schemeService;
|
|
|
38 |
|
|
|
39 |
@Autowired
|
| 23511 |
amit.gupta |
40 |
FofoOrderRepository fofoOrderRepository;
|
| 23530 |
ashik.ali |
41 |
|
|
|
42 |
@Autowired
|
|
|
43 |
private RestClient restClient;
|
| 23511 |
amit.gupta |
44 |
|
|
|
45 |
@Autowired
|
| 23506 |
amit.gupta |
46 |
private MVCResponseSender mvcResponseSender;
|
|
|
47 |
|
|
|
48 |
@Value("${prod}")
|
|
|
49 |
private boolean prod;
|
|
|
50 |
|
|
|
51 |
|
| 23568 |
govind |
52 |
private static final Logger LOGGER =LogManager.getLogger(CronController.class);
|
| 23506 |
amit.gupta |
53 |
|
|
|
54 |
@Scheduled(cron = "0 45 6 * * *")
|
|
|
55 |
public void executeJob() throws Exception {
|
|
|
56 |
if(prod) {
|
|
|
57 |
String uri = "/cron/process-schemes";
|
| 23530 |
ashik.ali |
58 |
restClient.get(SchemeType.HTTP, "localhost", 8080, uri, null);
|
| 23506 |
amit.gupta |
59 |
}
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
@RequestMapping(value = "/cron/process-schemes", method = RequestMethod.GET)
|
| 23558 |
amit.gupta |
63 |
public String createScheme(HttpServletRequest request, Model model) throws Exception {
|
| 23564 |
amit.gupta |
64 |
LocalDateTime fromDate = LocalDateTime.now().minusDays(15);
|
| 23506 |
amit.gupta |
65 |
LOGGER.info("Started execution at {}", LocalDateTime.now());
|
|
|
66 |
List<Purchase> purchases = purchaseRepository.selectFromPurchaseCompleteDate(fromDate);
|
|
|
67 |
for (Purchase purchase : purchases) {
|
|
|
68 |
try {
|
|
|
69 |
schemeService.processSchemeIn(purchase.getId(), purchase.getFofoId());
|
|
|
70 |
} catch (Exception e) {
|
|
|
71 |
LOGGER.error("Error while processing purchase {} for scheme In ", purchase.getId());
|
|
|
72 |
e.printStackTrace();
|
|
|
73 |
|
|
|
74 |
}
|
|
|
75 |
}
|
| 23511 |
amit.gupta |
76 |
List<FofoOrder> fofoOrders = fofoOrderRepository.selectFromSaleDate(fromDate);
|
|
|
77 |
for (FofoOrder fofoOrder: fofoOrders) {
|
|
|
78 |
try {
|
|
|
79 |
schemeService.processSchemeOut(fofoOrder.getId(), fofoOrder.getFofoId());
|
|
|
80 |
} catch (Exception e) {
|
|
|
81 |
LOGGER.error("Error while processing sale order {} for scheme Out", fofoOrder.getId());
|
|
|
82 |
}
|
|
|
83 |
}
|
| 23506 |
amit.gupta |
84 |
model.addAttribute("response", mvcResponseSender.createResponseString(true));
|
|
|
85 |
return "response";
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
}
|