Subversion Repositories SmartDukaan

Rev

Rev 27572 | Rev 27577 | 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;
5
import java.util.List;
6
import java.util.Map;
7
import java.util.stream.Collectors;
8
 
27571 amit.gupta 9
import org.apache.logging.log4j.LogManager;
10
import org.apache.logging.log4j.Logger;
27561 amit.gupta 11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.stereotype.Component;
13
import org.springframework.transaction.annotation.Transactional;
14
 
15
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
16
import com.spice.profitmandi.dao.entity.fofo.PartnerDailyInvestment;
17
import com.spice.profitmandi.dao.entity.fofo.SchemeInOut;
18
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
19
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
20
import com.spice.profitmandi.dao.repository.fofo.PartnerDailyInvestmentRepository;
21
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
22
import com.spice.profitmandi.service.user.RetailerService;;
23
 
24
@Component
25
@Transactional(rollbackFor = Throwable.class)
26
public class InvestmentRelatedTasks {
27
	@Autowired
28
	RetailerService retailerService;
29
	@Autowired
30
	SchemeInOutRepository schemeInOutRepository;
27571 amit.gupta 31
 
32
	private static final Logger LOGGER = LogManager.getLogger(InvestmentRelatedTasks.class);
27561 amit.gupta 33
 
34
	@Autowired
35
	InventoryItemRepository inventoryItemRepository;
36
 
37
	@Autowired
38
	PartnerDailyInvestmentRepository partnerDailyInvestmentRepository;
39
 
27565 amit.gupta 40
	public void payMonthlyInvestment() {
27561 amit.gupta 41
		LocalDate firstDateOfCurrentMonth = LocalDateTime.now().withDayOfMonth(1).toLocalDate();
42
		LocalDate startOfPreviousMonth = firstDateOfCurrentMonth.minusMonths(1);
43
		LocalDate lastOfPreviousMonth = firstDateOfCurrentMonth.minusDays(1);
44
		List<PartnerDailyInvestment> partnerDailyInvestments = partnerDailyInvestmentRepository.selectAll(startOfPreviousMonth, lastOfPreviousMonth);
27568 amit.gupta 45
		Map<Integer, Long> investmentMaintainedDaysMap = partnerDailyInvestments.stream().filter(x->x.getShortPercentage() <= 10).collect(Collectors.groupingBy(x->x.getFofoId(), Collectors.counting()));
27571 amit.gupta 46
		LOGGER.info("investmentMaintainedDaysMap {}", investmentMaintainedDaysMap);
27561 amit.gupta 47
		List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectAllPending(SchemeType.INVESTMENT, startOfPreviousMonth.atStartOfDay(), firstDateOfCurrentMonth.atStartOfDay());
48
		Map<Integer, List<SchemeInOut>> inventoryItemIdSchemeMap = schemeInOuts.stream().collect(Collectors.groupingBy(x->x.getInventoryItemId()));
49
		List<InventoryItem> inventoryItems = inventoryItemRepository.selectByIds(inventoryItemIdSchemeMap.keySet());
50
		Map<Integer, List<Integer>> retailerInventoryItemIdMap = inventoryItems.stream()
51
				.collect(Collectors.groupingBy(x->x.getFofoId(), Collectors.mapping(x->x.getId(), Collectors.toList())));
27570 amit.gupta 52
		System.out.println("Fofo Id\tInvestment Short Days\tEligible payout");
27567 amit.gupta 53
		for(Map.Entry<Integer, 	List<Integer>> retailerEntry : retailerInventoryItemIdMap.entrySet()) {
27561 amit.gupta 54
			int fofoId = retailerEntry.getKey();
55
			List<SchemeInOut> schemeInouts = retailerEntry.getValue().stream().map(x->inventoryItemIdSchemeMap.get(x)).flatMap(List::stream).collect(Collectors.toList());
27573 amit.gupta 56
			double totalAmount = schemeInouts.stream().filter(x->x.getRolledBackTimestamp()==null).collect(Collectors.summingDouble(x->x.getAmount()));
27572 amit.gupta 57
			long investmentMaintainedDays = investmentMaintainedDaysMap.get(fofoId) == null ? 0 : investmentMaintainedDaysMap.get(fofoId);
27568 amit.gupta 58
			if(investmentMaintainedDays < 12) {
59
				totalAmount = totalAmount/2;
60
			} else if(investmentMaintainedDays < 8) {
27567 amit.gupta 61
				totalAmount = 0;
27561 amit.gupta 62
			}
27568 amit.gupta 63
			System.out.printf("%d\t%d\t%f%n", fofoId, investmentMaintainedDays, totalAmount);
27561 amit.gupta 64
		}
65
	}
66
}