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