Subversion Repositories SmartDukaan

Rev

Rev 28531 | Rev 28738 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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