| 34306 |
ranu |
1 |
package com.smartdukaan.cron.scheduled;
|
|
|
2 |
|
|
|
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
4 |
import com.spice.profitmandi.dao.entity.transaction.Loan;
|
|
|
5 |
import com.spice.profitmandi.dao.entity.transaction.Transaction;
|
|
|
6 |
import com.spice.profitmandi.dao.repository.transaction.LoanRepository;
|
|
|
7 |
import com.spice.profitmandi.dao.repository.transaction.TransactionRepository;
|
| 34308 |
ranu |
8 |
import com.spice.profitmandi.service.transaction.SDCreditService;
|
| 34306 |
ranu |
9 |
import org.apache.logging.log4j.LogManager;
|
|
|
10 |
import org.apache.logging.log4j.Logger;
|
|
|
11 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
12 |
import org.springframework.beans.factory.annotation.Value;
|
|
|
13 |
import org.springframework.stereotype.Component;
|
|
|
14 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
15 |
|
|
|
16 |
import java.time.LocalDateTime;
|
|
|
17 |
import java.time.temporal.ChronoUnit;
|
|
|
18 |
import java.util.*;
|
|
|
19 |
|
|
|
20 |
@Component
|
|
|
21 |
@Transactional(rollbackFor = {Throwable.class, ProfitMandiBusinessException.class})
|
|
|
22 |
public class ScheduledTasksTest {
|
|
|
23 |
|
|
|
24 |
private static final Logger LOGGER = LogManager.getLogger(ScheduledTasksTest.class);
|
|
|
25 |
|
|
|
26 |
@Autowired
|
|
|
27 |
TransactionRepository transactionRepository;
|
|
|
28 |
|
|
|
29 |
@Autowired
|
|
|
30 |
LoanRepository loanRepository;
|
|
|
31 |
|
| 34308 |
ranu |
32 |
@Autowired
|
|
|
33 |
SDCreditService sdCreditService;
|
|
|
34 |
|
| 34307 |
ranu |
35 |
public void test(List<Integer> loanIds) throws Exception {
|
| 34306 |
ranu |
36 |
System.out.println("test start");
|
| 34307 |
ranu |
37 |
this.findLoanTransactionMapingAccordingLoan(loanIds);
|
| 34306 |
ranu |
38 |
System.out.println("test end");
|
|
|
39 |
|
|
|
40 |
}
|
|
|
41 |
|
| 34308 |
ranu |
42 |
public void createLoanForBillingByTransactionIdAndInvoiceNumber(int transactionId, double invoiceAmount, String invoiceNumber) throws Exception {
|
|
|
43 |
sdCreditService.createLoanForBilling(transactionId, invoiceAmount, invoiceNumber);
|
| 34306 |
ranu |
44 |
|
| 34308 |
ranu |
45 |
}
|
| 34306 |
ranu |
46 |
|
| 34308 |
ranu |
47 |
|
|
|
48 |
|
| 34307 |
ranu |
49 |
public Map<Integer,Integer> findLoanTransactionMapingAccordingLoan(List<Integer> loanIds) throws ProfitMandiBusinessException {
|
| 34306 |
ranu |
50 |
|
|
|
51 |
Map<Integer, Integer> transactionLoanMap = new HashMap<>();
|
|
|
52 |
|
|
|
53 |
for(int loanId : loanIds){
|
|
|
54 |
Transaction transaction = null;
|
|
|
55 |
Loan loan = loanRepository.selectByLoanId(loanId);
|
|
|
56 |
List<Transaction> transactions = transactionRepository.selectByRetailerId(loan.getFofoId());
|
|
|
57 |
|
|
|
58 |
LocalDateTime nearestDateTime = transactions.stream().map(x -> x.getCreateTimestamp())
|
|
|
59 |
.min(Comparator.comparingLong(x -> Math.abs(ChronoUnit.MILLIS.between(x, loan.getCreatedOn()))))
|
|
|
60 |
.orElse(null);
|
|
|
61 |
|
|
|
62 |
if (nearestDateTime != null && loan.getCreatedOn().plusMinutes(2).isAfter(nearestDateTime) &&
|
|
|
63 |
loan.getCreatedOn().minusMinutes(1).isBefore(nearestDateTime)) {
|
|
|
64 |
// Here transaction is still null
|
|
|
65 |
transaction = transactions.stream()
|
|
|
66 |
.filter(x -> x.getCreateTimestamp().equals(nearestDateTime))
|
|
|
67 |
.findFirst().get();
|
|
|
68 |
transactionLoanMap.put(transaction.getId(), loanId);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
}
|
|
|
72 |
LOGGER.info("transactionLoanMap {}",transactionLoanMap);
|
|
|
73 |
return transactionLoanMap;
|
|
|
74 |
}
|
|
|
75 |
}
|