| 23755 |
amit.gupta |
1 |
package com.smartdukaan.cron.migrations;
|
|
|
2 |
|
|
|
3 |
import java.time.LocalDateTime;
|
|
|
4 |
import java.util.Arrays;
|
|
|
5 |
import java.util.List;
|
|
|
6 |
|
|
|
7 |
import org.apache.commons.lang.StringUtils;
|
|
|
8 |
import org.apache.logging.log4j.LogManager;
|
|
|
9 |
import org.apache.logging.log4j.Logger;
|
|
|
10 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
11 |
import org.springframework.stereotype.Component;
|
|
|
12 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
13 |
|
|
|
14 |
import com.spice.profitmandi.dao.entity.transaction.LineItemImei;
|
|
|
15 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
|
|
16 |
import com.spice.profitmandi.dao.repository.transaction.LineItemImeisRepository;
|
|
|
17 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
|
|
18 |
|
|
|
19 |
@Component
|
|
|
20 |
@Transactional(rollbackFor = Throwable.class)
|
|
|
21 |
public class RunOnceTasks {
|
|
|
22 |
|
|
|
23 |
private static final Logger LOGGER = LogManager.getLogger(RunOnceTasks.class);
|
|
|
24 |
|
|
|
25 |
@Autowired
|
|
|
26 |
private OrderRepository orderRepository;
|
| 23767 |
amit.gupta |
27 |
|
| 23755 |
amit.gupta |
28 |
@Autowired
|
|
|
29 |
private LineItemImeisRepository lineItemImeisRepository;
|
|
|
30 |
|
|
|
31 |
public void migarateLineItemsToNewTable() {
|
|
|
32 |
LOGGER.info("Before Migrated LineItems Successfully");
|
| 23767 |
amit.gupta |
33 |
int maxLineItemId = lineItemImeisRepository.selectMaximumLineItemId();
|
|
|
34 |
|
| 23755 |
amit.gupta |
35 |
LocalDateTime endDate = LocalDateTime.now();
|
| 23767 |
amit.gupta |
36 |
LocalDateTime startDate = endDate.plusHours(1);
|
|
|
37 |
List<Order> orders = orderRepository.selectAllByDatesBetween(startDate, endDate);
|
| 23755 |
amit.gupta |
38 |
for (Order order : orders) {
|
| 23767 |
amit.gupta |
39 |
if (order.getLineItem().getId() > maxLineItemId) {
|
|
|
40 |
String serialNumbers = order.getLineItem().getSerialNumber();
|
|
|
41 |
if (!StringUtils.isEmpty(serialNumbers)) {
|
|
|
42 |
List<String> serialNumberList = Arrays.asList(serialNumbers.split(","));
|
|
|
43 |
for (String serialNumber : serialNumberList) {
|
|
|
44 |
int lineItemId = order.getLineItem().getId();
|
|
|
45 |
LineItemImei lineItemImei = new LineItemImei();
|
|
|
46 |
lineItemImei.setSerialNumber(serialNumber);
|
|
|
47 |
lineItemImei.setLineItemId(lineItemId);
|
|
|
48 |
lineItemImeisRepository.persist(lineItemImei);
|
|
|
49 |
}
|
|
|
50 |
} else {
|
|
|
51 |
LOGGER.info("Serial Numbers dont exist for Order {}", order.getId());
|
| 23755 |
amit.gupta |
52 |
}
|
|
|
53 |
}
|
|
|
54 |
}
|
|
|
55 |
LOGGER.info("Migrated LineItems Successfully");
|
|
|
56 |
}
|
|
|
57 |
}
|