Subversion Repositories SmartDukaan

Rev

Rev 27577 | Rev 28534 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 27577 Rev 28531
Line 1... Line 1...
1
package com.smartdukaan.cron.scheduled;
1
package com.smartdukaan.cron.scheduled;
2
 
2
 
3
import java.time.LocalDate;
3
import java.time.LocalDate;
4
import java.time.LocalDateTime;
4
import java.time.LocalDateTime;
-
 
5
import java.time.LocalTime;
5
import java.util.List;
6
import java.util.List;
6
import java.util.Map;
7
import java.util.Map;
7
import java.util.stream.Collectors;
8
import java.util.stream.Collectors;
8
 
9
 
9
import org.apache.logging.log4j.LogManager;
10
import org.apache.logging.log4j.LogManager;
10
import org.apache.logging.log4j.Logger;
11
import org.apache.logging.log4j.Logger;
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.stereotype.Component;
13
import org.springframework.stereotype.Component;
13
import org.springframework.transaction.annotation.Transactional;
14
import org.springframework.transaction.annotation.Transactional;
14
 
15
 
-
 
16
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
-
 
17
import com.spice.profitmandi.common.util.FormattingUtils;
15
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
18
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
16
import com.spice.profitmandi.dao.entity.fofo.PartnerDailyInvestment;
19
import com.spice.profitmandi.dao.entity.fofo.PartnerDailyInvestment;
17
import com.spice.profitmandi.dao.entity.fofo.SchemeInOut;
20
import com.spice.profitmandi.dao.entity.fofo.SchemeInOut;
18
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
21
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
-
 
22
import com.spice.profitmandi.dao.enumuration.transaction.SchemePayoutStatus;
19
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
23
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
20
import com.spice.profitmandi.dao.repository.fofo.PartnerDailyInvestmentRepository;
24
import com.spice.profitmandi.dao.repository.fofo.PartnerDailyInvestmentRepository;
21
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
25
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
22
import com.spice.profitmandi.service.user.RetailerService;;
26
import com.spice.profitmandi.service.user.RetailerService;
-
 
27
import com.spice.profitmandi.service.wallet.WalletService;
-
 
28
 
-
 
29
import in.shop2020.model.v1.order.WalletReferenceType;;
23
 
30
 
24
@Component
31
@Component
25
@Transactional(rollbackFor = Throwable.class)
32
@Transactional(rollbackFor = Throwable.class)
26
public class InvestmentRelatedTasks {
33
public class InvestmentRelatedTasks {
27
	@Autowired
34
	@Autowired
28
	RetailerService retailerService;
35
	RetailerService retailerService;
29
	@Autowired
36
	@Autowired
30
	SchemeInOutRepository schemeInOutRepository;
37
	SchemeInOutRepository schemeInOutRepository;
31
	
38
 
32
	private static final Logger LOGGER = LogManager.getLogger(InvestmentRelatedTasks.class);
39
	private static final Logger LOGGER = LogManager.getLogger(InvestmentRelatedTasks.class);
33
 
40
 
34
	@Autowired
41
	@Autowired
35
	InventoryItemRepository inventoryItemRepository;
42
	InventoryItemRepository inventoryItemRepository;
36
	
43
 
37
	@Autowired
44
	@Autowired
38
	PartnerDailyInvestmentRepository partnerDailyInvestmentRepository;
45
	PartnerDailyInvestmentRepository partnerDailyInvestmentRepository;
39
	
46
	
-
 
47
	@Autowired
-
 
48
	WalletService walletService;
-
 
49
 
-
 
50
	public void payMonthlyInvestment() throws ProfitMandiBusinessException {
-
 
51
		LocalDate firstDateOfCurrentMonth = LocalDateTime.now().withDayOfMonth(1).toLocalDate();
-
 
52
		LocalDate startOfPreviousMonth = firstDateOfCurrentMonth.minusMonths(1);
-
 
53
		int referenceId = Integer.parseInt(FormattingUtils.getYearMonth(startOfPreviousMonth.atStartOfDay()));
-
 
54
		LocalDate lastOfPreviousMonth = firstDateOfCurrentMonth.minusDays(1);
-
 
55
		List<PartnerDailyInvestment> partnerDailyInvestments = partnerDailyInvestmentRepository
-
 
56
				.selectAll(startOfPreviousMonth, lastOfPreviousMonth);
-
 
57
		Map<Integer, Long> investmentMaintainedDaysMap = partnerDailyInvestments.stream()
-
 
58
				.filter(x -> x.getShortPercentage() <= 10)
-
 
59
				.collect(Collectors.groupingBy(x -> x.getFofoId(), Collectors.counting()));
-
 
60
		LOGGER.info("investmentMaintainedDaysMap {}", investmentMaintainedDaysMap);
-
 
61
		List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectAllPending(SchemeType.INVESTMENT,
-
 
62
				startOfPreviousMonth.atStartOfDay(), firstDateOfCurrentMonth.atStartOfDay());
-
 
63
		Map<Integer, List<SchemeInOut>> inventoryItemIdSchemeMap = schemeInOuts.stream()
-
 
64
				.collect(Collectors.groupingBy(x -> x.getInventoryItemId()));
-
 
65
		List<InventoryItem> inventoryItems = inventoryItemRepository.selectByIds(inventoryItemIdSchemeMap.keySet());
-
 
66
		Map<Integer, List<Integer>> retailerInventoryItemIdMap = inventoryItems.stream().collect(
-
 
67
				Collectors.groupingBy(x -> x.getFofoId(), Collectors.mapping(x -> x.getId(), Collectors.toList())));
-
 
68
		System.out.println("Fofo Id\tInvestment Short Days\tEligible payout");
-
 
69
		for (Map.Entry<Integer, List<Integer>> retailerEntry : retailerInventoryItemIdMap.entrySet()) {
-
 
70
			int fofoId = retailerEntry.getKey();
-
 
71
			long investmentMaintainedDays = investmentMaintainedDaysMap.get(fofoId) == null ? 0
-
 
72
					: investmentMaintainedDaysMap.get(fofoId);
-
 
73
 
-
 
74
			List<SchemeInOut> schemeInouts = retailerEntry.getValue().stream().map(x -> inventoryItemIdSchemeMap.get(x))
-
 
75
					.flatMap(List::stream).filter(x -> x.getRolledBackTimestamp() == null).collect(Collectors.toList());
-
 
76
			float totalAmount = 0;
-
 
77
			for(SchemeInOut sio : schemeInouts) {
-
 
78
				
-
 
79
				if (investmentMaintainedDays < 8) {
-
 
80
					sio.setStatus(SchemePayoutStatus.REJECTED);
-
 
81
					sio.setRolledBackTimestamp(LocalDateTime.now());
-
 
82
					sio.setStatusDescription("Investment maintained for " + investmentMaintainedDays + "(< 8 days)");
-
 
83
				} else if (investmentMaintainedDays < 12) {
-
 
84
					sio.setStatus(SchemePayoutStatus.CREDITED);
-
 
85
					sio.setAmount(sio.getAmount()/2);
-
 
86
					sio.setCreditTimestamp(LocalDateTime.now());
-
 
87
					sio.setStatusDescription("Investment maintained for " + investmentMaintainedDays + "(< 12 days)");
-
 
88
					totalAmount += sio.getAmount();
-
 
89
				} else {
-
 
90
					sio.setStatus(SchemePayoutStatus.CREDITED);
-
 
91
					sio.setCreditTimestamp(LocalDateTime.now());
-
 
92
					totalAmount += sio.getAmount();
-
 
93
				}
-
 
94
			}
-
 
95
			if(totalAmount > 0) {
-
 
96
				String description = "Margin rolled out for " + FormattingUtils.formatYearMonth(startOfPreviousMonth.atStartOfDay());
-
 
97
				if(investmentMaintainedDays < 12) {
-
 
98
					description += ", as maintained for < 12 days";
-
 
99
				}
-
 
100
				walletService.addAmountToWallet(fofoId,referenceId, WalletReferenceType.INVESTMENT_PAYOUT,  description, totalAmount, lastOfPreviousMonth.atTime(LocalTime.MAX));
-
 
101
			}
-
 
102
			System.out.printf("%d\t%d\t%f%n", fofoId, investmentMaintainedDays, totalAmount);
-
 
103
		}
-
 
104
	}
-
 
105
 
40
	public void payMonthlyInvestment() {
106
	public void evaluateActualInvestmentPayout() {
41
		LocalDate firstDateOfCurrentMonth = LocalDateTime.now().withDayOfMonth(1).toLocalDate();
107
		LocalDate firstDateOfCurrentMonth = LocalDateTime.now().withDayOfMonth(1).toLocalDate();
42
		LocalDate startOfPreviousMonth = firstDateOfCurrentMonth.minusMonths(1);
108
		LocalDate startOfPreviousMonth = firstDateOfCurrentMonth.minusMonths(1);
43
		LocalDate lastOfPreviousMonth = firstDateOfCurrentMonth.minusDays(1);
109
		LocalDate lastOfPreviousMonth = firstDateOfCurrentMonth.minusDays(1);
44
		List<PartnerDailyInvestment> partnerDailyInvestments = partnerDailyInvestmentRepository.selectAll(startOfPreviousMonth, lastOfPreviousMonth);
110
		List<PartnerDailyInvestment> partnerDailyInvestments = partnerDailyInvestmentRepository
-
 
111
				.selectAll(startOfPreviousMonth, lastOfPreviousMonth);
45
		Map<Integer, Long> investmentMaintainedDaysMap = partnerDailyInvestments.stream().filter(x->x.getShortPercentage() <= 10).collect(Collectors.groupingBy(x->x.getFofoId(), Collectors.counting()));
112
		Map<Integer, Long> investmentMaintainedDaysMap = partnerDailyInvestments.stream()
-
 
113
				.filter(x -> x.getShortPercentage() <= 10)
-
 
114
				.collect(Collectors.groupingBy(x -> x.getFofoId(), Collectors.counting()));
46
		LOGGER.info("investmentMaintainedDaysMap {}", investmentMaintainedDaysMap);
115
		LOGGER.info("investmentMaintainedDaysMap {}", investmentMaintainedDaysMap);
47
		List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectAllPending(SchemeType.INVESTMENT, startOfPreviousMonth.atStartOfDay(), firstDateOfCurrentMonth.atStartOfDay());
116
		List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectAllPending(SchemeType.INVESTMENT,
-
 
117
				startOfPreviousMonth.atStartOfDay(), firstDateOfCurrentMonth.atStartOfDay());
48
		Map<Integer, List<SchemeInOut>> inventoryItemIdSchemeMap = schemeInOuts.stream().collect(Collectors.groupingBy(x->x.getInventoryItemId()));
118
		Map<Integer, List<SchemeInOut>> inventoryItemIdSchemeMap = schemeInOuts.stream()
-
 
119
				.collect(Collectors.groupingBy(x -> x.getInventoryItemId()));
49
		List<InventoryItem> inventoryItems = inventoryItemRepository.selectByIds(inventoryItemIdSchemeMap.keySet());
120
		List<InventoryItem> inventoryItems = inventoryItemRepository.selectByIds(inventoryItemIdSchemeMap.keySet());
50
		Map<Integer, List<Integer>> retailerInventoryItemIdMap = inventoryItems.stream()
121
		Map<Integer, List<Integer>> retailerInventoryItemIdMap = inventoryItems.stream().collect(
51
				.collect(Collectors.groupingBy(x->x.getFofoId(), Collectors.mapping(x->x.getId(), Collectors.toList())));
122
				Collectors.groupingBy(x -> x.getFofoId(), Collectors.mapping(x -> x.getId(), Collectors.toList())));
52
		System.out.println("Fofo Id\tInvestment Short Days\tEligible payout");
123
		System.out.println("Fofo Id\tInvestment Short Days\tEligible payout");
53
		for(Map.Entry<Integer, 	List<Integer>> retailerEntry : retailerInventoryItemIdMap.entrySet()) {
124
		for (Map.Entry<Integer, List<Integer>> retailerEntry : retailerInventoryItemIdMap.entrySet()) {
54
			int fofoId = retailerEntry.getKey();
125
			int fofoId = retailerEntry.getKey();
55
			List<SchemeInOut> schemeInouts = retailerEntry.getValue().stream().map(x->inventoryItemIdSchemeMap.get(x)).flatMap(List::stream).collect(Collectors.toList());
126
			List<SchemeInOut> schemeInouts = retailerEntry.getValue().stream().map(x -> inventoryItemIdSchemeMap.get(x))
-
 
127
					.flatMap(List::stream).collect(Collectors.toList());
56
			double totalAmount = schemeInouts.stream().filter(x->x.getRolledBackTimestamp()==null).collect(Collectors.summingDouble(x->x.getAmount()));
128
			double totalAmount = schemeInouts.stream().filter(x -> x.getRolledBackTimestamp() == null)
-
 
129
					.collect(Collectors.summingDouble(x -> x.getAmount()));
57
			long investmentMaintainedDays = investmentMaintainedDaysMap.get(fofoId) == null ? 0 : investmentMaintainedDaysMap.get(fofoId);
130
			long investmentMaintainedDays = investmentMaintainedDaysMap.get(fofoId) == null ? 0
-
 
131
					: investmentMaintainedDaysMap.get(fofoId);
58
			if(investmentMaintainedDays < 8) {
132
			if (investmentMaintainedDays < 8) {
59
				totalAmount = 0;
133
				totalAmount = 0;
60
			} else if(investmentMaintainedDays < 12) {
134
			} else if (investmentMaintainedDays < 12) {
61
				totalAmount = totalAmount/2;
135
				totalAmount = totalAmount / 2;
62
			}
136
			}
63
			System.out.printf("%d\t%d\t%f%n", fofoId, investmentMaintainedDays, totalAmount);
137
			System.out.printf("%d\t%d\t%f%n", fofoId, investmentMaintainedDays, totalAmount);
64
		}
138
		}
65
	}
139
	}
66
}
140
}
67
141