| 23755 |
amit.gupta |
1 |
package com.smartdukaan.cron.migrations;
|
|
|
2 |
|
| 23824 |
amit.gupta |
3 |
import java.time.LocalDate;
|
| 23755 |
amit.gupta |
4 |
import java.time.LocalDateTime;
|
| 23824 |
amit.gupta |
5 |
import java.time.LocalTime;
|
| 23898 |
amit.gupta |
6 |
import java.time.Month;
|
| 23755 |
amit.gupta |
7 |
import java.util.Arrays;
|
| 23827 |
amit.gupta |
8 |
import java.util.Collections;
|
| 23755 |
amit.gupta |
9 |
import java.util.List;
|
|
|
10 |
|
|
|
11 |
import org.apache.commons.lang.StringUtils;
|
|
|
12 |
import org.apache.logging.log4j.LogManager;
|
|
|
13 |
import org.apache.logging.log4j.Logger;
|
|
|
14 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
15 |
import org.springframework.stereotype.Component;
|
|
|
16 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
17 |
|
| 23898 |
amit.gupta |
18 |
import com.spice.profitmandi.dao.entity.fofo.Purchase;
|
| 23824 |
amit.gupta |
19 |
import com.spice.profitmandi.dao.entity.transaction.LineItem;
|
| 23755 |
amit.gupta |
20 |
import com.spice.profitmandi.dao.entity.transaction.LineItemImei;
|
|
|
21 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
| 23898 |
amit.gupta |
22 |
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
|
| 23755 |
amit.gupta |
23 |
import com.spice.profitmandi.dao.repository.transaction.LineItemImeisRepository;
|
| 23824 |
amit.gupta |
24 |
import com.spice.profitmandi.dao.repository.transaction.LineItemRepository;
|
| 23755 |
amit.gupta |
25 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
|
|
26 |
|
|
|
27 |
@Component
|
|
|
28 |
@Transactional(rollbackFor = Throwable.class)
|
|
|
29 |
public class RunOnceTasks {
|
|
|
30 |
|
|
|
31 |
private static final Logger LOGGER = LogManager.getLogger(RunOnceTasks.class);
|
|
|
32 |
|
|
|
33 |
@Autowired
|
| 23824 |
amit.gupta |
34 |
private LineItemRepository lineItemRepository;
|
|
|
35 |
|
|
|
36 |
@Autowired
|
| 23755 |
amit.gupta |
37 |
private OrderRepository orderRepository;
|
| 23767 |
amit.gupta |
38 |
|
| 23755 |
amit.gupta |
39 |
@Autowired
|
|
|
40 |
private LineItemImeisRepository lineItemImeisRepository;
|
| 23898 |
amit.gupta |
41 |
|
|
|
42 |
@Autowired
|
|
|
43 |
private PurchaseRepository purchaseRepository;
|
| 23755 |
amit.gupta |
44 |
|
| 23898 |
amit.gupta |
45 |
|
|
|
46 |
|
|
|
47 |
public void populateGrnTimestamp() {
|
|
|
48 |
List<Purchase> allPurchases = purchaseRepository.selectAll();
|
|
|
49 |
for(Purchase p : allPurchases) {
|
|
|
50 |
String invoiceNumber = p.getPurchaseReference();
|
|
|
51 |
if(p.getCompleteTimestamp() == null) {
|
|
|
52 |
LOGGER.info("GRN for invoice {} is delivered but partially Completed.", p.getPurchaseReference());
|
|
|
53 |
} else {
|
|
|
54 |
List<Order> orders = orderRepository.selectByAirwayBillOrInvoiceNumber(invoiceNumber, p.getFofoId());
|
|
|
55 |
for(Order order : orders) {
|
|
|
56 |
if(order.getPartnerPurchaseTimestamp() == null) {
|
|
|
57 |
order.setPartnerPurchaseTimestamp(p.getCompleteTimestamp());
|
|
|
58 |
orderRepository.persist(order);
|
|
|
59 |
}
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
}
|
|
|
65 |
|
| 23755 |
amit.gupta |
66 |
public void migarateLineItemsToNewTable() {
|
|
|
67 |
LOGGER.info("Before Migrated LineItems Successfully");
|
| 23824 |
amit.gupta |
68 |
int lineItemImeiId = 0;
|
|
|
69 |
LocalDateTime startDate = null;
|
|
|
70 |
try {
|
|
|
71 |
lineItemImeiId = lineItemImeisRepository.selectMaxId();
|
|
|
72 |
LineItem lineItem = lineItemRepository.selectById(lineItemImeisRepository.selectById(lineItemImeiId).getLineItemId());
|
|
|
73 |
Order order = orderRepository.selectById(lineItem.getOrderId());
|
|
|
74 |
startDate = order.getBillingTimestamp();
|
|
|
75 |
} catch (Exception e) {
|
|
|
76 |
LOGGER.info("Running before first time");
|
| 23826 |
amit.gupta |
77 |
startDate = LocalDateTime.of(LocalDate.of(2017, 7, 1), LocalTime.MIDNIGHT);
|
| 23824 |
amit.gupta |
78 |
}
|
|
|
79 |
List<Order> orders = orderRepository.selectAllByBillingDatesBetween(startDate, LocalDateTime.now());
|
| 23827 |
amit.gupta |
80 |
Collections.reverse(orders);
|
|
|
81 |
|
| 23755 |
amit.gupta |
82 |
for (Order order : orders) {
|
| 23824 |
amit.gupta |
83 |
try {
|
| 23767 |
amit.gupta |
84 |
String serialNumbers = order.getLineItem().getSerialNumber();
|
|
|
85 |
if (!StringUtils.isEmpty(serialNumbers)) {
|
|
|
86 |
List<String> serialNumberList = Arrays.asList(serialNumbers.split(","));
|
|
|
87 |
for (String serialNumber : serialNumberList) {
|
|
|
88 |
int lineItemId = order.getLineItem().getId();
|
|
|
89 |
LineItemImei lineItemImei = new LineItemImei();
|
|
|
90 |
lineItemImei.setSerialNumber(serialNumber);
|
|
|
91 |
lineItemImei.setLineItemId(lineItemId);
|
|
|
92 |
lineItemImeisRepository.persist(lineItemImei);
|
|
|
93 |
}
|
|
|
94 |
} else {
|
|
|
95 |
LOGGER.info("Serial Numbers dont exist for Order {}", order.getId());
|
| 23755 |
amit.gupta |
96 |
}
|
| 23824 |
amit.gupta |
97 |
} catch (Exception e) {
|
|
|
98 |
LOGGER.info("Error occurred while creating lineitem imei {}, because of {}", order.getId(), e.getMessage());
|
| 23755 |
amit.gupta |
99 |
}
|
|
|
100 |
}
|
|
|
101 |
LOGGER.info("Migrated LineItems Successfully");
|
|
|
102 |
}
|
|
|
103 |
}
|