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