Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
26460 amit.gupta 1
package com.spice.profitmandi.service;
2
 
32429 amit.gupta 3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
32421 amit.gupta 4
import com.spice.profitmandi.common.model.ProfitMandiConstants;
27548 tejbeer 5
import com.spice.profitmandi.dao.entity.auth.AuthUser;
26479 amit.gupta 6
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
26460 amit.gupta 7
import com.spice.profitmandi.dao.entity.fofo.PartnerDailyInvestment;
27548 tejbeer 8
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
27545 tejbeer 9
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
27548 tejbeer 10
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
26460 amit.gupta 11
import com.spice.profitmandi.dao.model.PartnerDetailModel;
32429 amit.gupta 12
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
26460 amit.gupta 13
import com.spice.profitmandi.dao.repository.cs.CsService;
32430 amit.gupta 14
import com.spice.profitmandi.dao.repository.cs.PositionRepository;
26460 amit.gupta 15
import com.spice.profitmandi.dao.repository.cs.TicketRepository;
26479 amit.gupta 16
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
26460 amit.gupta 17
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
18
import com.spice.profitmandi.dao.repository.fofo.HygieneDataRepository;
27903 tejbeer 19
import com.spice.profitmandi.dao.repository.fofo.PartnerDailyInvestmentRepository;
27548 tejbeer 20
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
27893 tejbeer 21
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
27545 tejbeer 22
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
26460 amit.gupta 23
import com.spice.profitmandi.service.user.RetailerService;
32429 amit.gupta 24
import org.apache.logging.log4j.LogManager;
25
import org.apache.logging.log4j.Logger;
26
import org.springframework.beans.factory.annotation.Autowired;
27
import org.springframework.stereotype.Component;
26460 amit.gupta 28
 
32429 amit.gupta 29
import java.time.LocalDate;
30
import java.time.LocalDateTime;
31
import java.time.LocalTime;
32
import java.util.*;
33
import java.util.stream.Collectors;
34
 
26461 amit.gupta 35
@Component
26460 amit.gupta 36
public class PartnerStatsServiceImpl implements PartnerStatsService {
37
 
32435 amit.gupta 38
    private static final Logger LOGGER = LogManager.getLogger(PartnerStatsServiceImpl.class);
27545 tejbeer 39
 
32435 amit.gupta 40
    @Autowired
41
    RetailerService retailerService;
26460 amit.gupta 42
 
32435 amit.gupta 43
    @Autowired
44
    FofoStoreRepository fofoStoreRepository;
26479 amit.gupta 45
 
32435 amit.gupta 46
    @Autowired
47
    FofoOrderItemRepository fofoOrderItemRepository;
26460 amit.gupta 48
 
32435 amit.gupta 49
    @Autowired
50
    CsService csService;
26460 amit.gupta 51
 
32435 amit.gupta 52
    @Autowired
53
    TicketRepository ticketRepository;
26460 amit.gupta 54
 
32435 amit.gupta 55
    @Autowired
56
    PartnerInvestmentService partnerInvestmentService;
26460 amit.gupta 57
 
32435 amit.gupta 58
    @Autowired
59
    HygieneDataRepository hygieneDataRepository;
26460 amit.gupta 60
 
32435 amit.gupta 61
    @Autowired
62
    UserWalletRepository userWalletRepository;
27545 tejbeer 63
 
32435 amit.gupta 64
    @Autowired
65
    PartnerTypeChangeService partnerTypeChangeService;
27548 tejbeer 66
 
32435 amit.gupta 67
    @Autowired
68
    OrderRepository orderRepository;
27893 tejbeer 69
 
32435 amit.gupta 70
    @Autowired
71
    AuthRepository authRepository;
32421 amit.gupta 72
 
32435 amit.gupta 73
    @Autowired
74
    PartnerDailyInvestmentRepository partnerDailyInvestmentRepository;
27903 tejbeer 75
 
32435 amit.gupta 76
    @Autowired
77
    PositionRepository positionRepository;
32430 amit.gupta 78
 
32435 amit.gupta 79
    @Override
80
    // @Cacheable(value = "partnerStats", cacheManager =
81
    // "thirtyMinsTimeOutCacheManager")
82
    public Map<Integer, PartnerDetailModel> getAllPartnerStats() throws ProfitMandiBusinessException {
83
        LocalDateTime curDate = LocalDate.now().atStartOfDay();
84
        Map<Integer, Double> lmtdSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(
85
                curDate.withDayOfMonth(1).minusMonths(1), curDate.with(LocalTime.MAX).minusMonths(1), 0, false);
28617 tejbeer 86
 
32435 amit.gupta 87
        Map<Integer, Double> mtdSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(curDate.withDayOfMonth(1),
88
                curDate.with(LocalTime.MAX), 0, false);
26460 amit.gupta 89
 
32435 amit.gupta 90
        Map<Integer, Double> lmsSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(
91
                curDate.withDayOfMonth(1).minusMonths(1), curDate.withDayOfMonth(1), 0, false);
27660 tejbeer 92
 
32435 amit.gupta 93
        Map<Integer, Double> todaytertiary = fofoOrderItemRepository.selectSumMopGroupByRetailer(curDate,
94
                curDate.with(LocalTime.MAX), 0, false);
27545 tejbeer 95
 
32435 amit.gupta 96
        Map<Integer, Long> last3daystertiary = fofoOrderItemRepository.selectQtyGroupByRetailer(curDate.minusDays(4),
97
                curDate.minusDays(1).with(LocalTime.MAX), 0, false);
27545 tejbeer 98
 
32435 amit.gupta 99
        Map<Integer, Long> ticketMap = ticketRepository.selectAllOpenTicketsGroupByRetailer();
26460 amit.gupta 100
 
32435 amit.gupta 101
        Map<Integer, Double> secondaryMtd = orderRepository
102
                .selectBillingDatesBetweenSumGroupByRetailerId(curDate.withDayOfMonth(1), curDate.with(LocalTime.MAX));
103
        Map<Integer, Double> secondarylmtd = orderRepository.selectBillingDatesBetweenSumGroupByRetailerId(
104
                curDate.withDayOfMonth(1).minusMonths(1), curDate.with(LocalTime.MAX).minusMonths(1));
105
        Map<Integer, Double> secondarylms = orderRepository.selectBillingDatesBetweenSumGroupByRetailerId(
106
                curDate.withDayOfMonth(1).minusMonths(1), curDate.withDayOfMonth(1));
27893 tejbeer 107
 
32435 amit.gupta 108
        LOGGER.info("secondarylmtd" + secondarylmtd);
109
        LOGGER.info("secondarylms" + secondarylms);
110
        List<FofoStore> fofoStores = fofoStoreRepository.selectActiveStores();
111
        Set<Integer> fofoIds = fofoStores.stream().filter(x -> !x.isInternal()).map(x -> x.getId()).collect(Collectors.toSet());
112
        Map<Integer, UserWallet> userWallet = userWalletRepository.selectByRetailerIds(fofoIds).stream()
113
                .collect(Collectors.toMap(x -> x.getUserId(), x -> x));
27545 tejbeer 114
 
32435 amit.gupta 115
        Map<Integer, PartnerDetailModel> allPartnerStats = new HashMap<>();
26460 amit.gupta 116
 
32435 amit.gupta 117
        List<PartnerDailyInvestment> partnerDailyInvestments = partnerDailyInvestmentRepository
118
                .selectAll(curDate.withDayOfMonth(1).toLocalDate(), curDate.toLocalDate());
119
        Map<Integer, Long> investmentMaintainedDaysMap = partnerDailyInvestments.stream()
120
                .filter(x -> x.getShortPercentage() <= 10)
121
                .collect(Collectors.groupingBy(x -> x.getFofoId(), Collectors.counting()));
27903 tejbeer 122
 
32435 amit.gupta 123
        LOGGER.info("investmentMaintainedDaysMap" + investmentMaintainedDaysMap);
27903 tejbeer 124
 
32435 amit.gupta 125
        Map<Integer, PartnerDailyInvestment> investmentMap = fofoStores.stream().map(x -> {
126
            try {
127
                return partnerInvestmentService.getInvestment(x.getId(), 0);
128
            } catch (Exception e) {
129
                LOGGER.info("Could not get invetment summary for {}", x);
130
                return new PartnerDailyInvestment();
131
            }
132
        }).collect(Collectors.toMap(x -> x.getFofoId(), x -> x));
26460 amit.gupta 133
 
32435 amit.gupta 134
        Map<String, Set<Integer>> storeGuyMap = csService.getAuthUserPartnerIdMappingByCategoryIds(Arrays.asList(ProfitMandiConstants.TICKET_CATEGORY_RBM), true);
135
        Map<String, AuthUser> authUserMap = authRepository.selectAll().stream().filter(x -> storeGuyMap.keySet().contains(x.getEmailId())).collect(Collectors.toMap(x -> x.getEmailId(), x -> x));
136
        Map<Integer, Set<AuthUser>> partnerRbmsMap = new HashMap<>();
137
        for (Map.Entry<String, Set<Integer>> storeGuySetEntry : storeGuyMap.entrySet()) {
138
            //store
139
            for (Integer storeId : storeGuySetEntry.getValue()) {
140
                if (!partnerRbmsMap.containsKey(storeId)) {
141
                    partnerRbmsMap.put(storeId, new HashSet<>());
142
                }
143
                partnerRbmsMap.get(storeId).add(authUserMap.get(storeGuySetEntry.getKey()));
144
            }
145
        }
32421 amit.gupta 146
 
32435 amit.gupta 147
        Set<Integer> l1Rbms = positionRepository.selectPositionbyCategoryIdAndEscalationType(
148
                ProfitMandiConstants.TICKET_CATEGORY_RBM, EscalationType.L1).stream().map(x -> x.getAuthUserId()).collect(Collectors.toSet());
149
        Set<Integer> l2Rbms = positionRepository.selectPositionbyCategoryIdAndEscalationType(
150
                ProfitMandiConstants.TICKET_CATEGORY_RBM, EscalationType.L2).stream().map(x -> x.getAuthUserId()).collect(Collectors.toSet());
32430 amit.gupta 151
 
32435 amit.gupta 152
        for (FofoStore store : fofoStores) {
153
            int fofoId = store.getId();
32421 amit.gupta 154
 
32435 amit.gupta 155
            int hygieneCount = (int) hygieneDataRepository.selectHygieneCount(fofoId, true,
156
                    curDate.withDayOfMonth(1).minusMonths(1), curDate.plusMonths(1).withDayOfMonth(1));
157
            int invalidHygieneCount = (int) hygieneDataRepository.selectHygieneCount(fofoId, true,
158
                    curDate.withDayOfMonth(1).minusMonths(1), curDate.plusMonths(1).withDayOfMonth(1));
159
            int totalHygieneCount = hygieneCount + invalidHygieneCount;
160
            PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(fofoId, LocalDate.now());
27548 tejbeer 161
 
32435 amit.gupta 162
            Map<EscalationType, AuthUser> authuserEsclationTypeMap = csService
163
                    .getAuthUserAndEsclationByPartnerId(fofoId);
164
            PartnerDetailModel pm = new PartnerDetailModel();
165
            pm.setFofoId(fofoId);
166
            pm.setLmtd(lmtdSale.get(fofoId) == null ? 0 : lmtdSale.get(fofoId).intValue());
167
            pm.setMtd(mtdSale.get(fofoId) == null ? 0 : mtdSale.get(fofoId).intValue());
168
            pm.setLms(lmsSale.get(fofoId) == null ? 0 : lmsSale.get(fofoId).intValue());
169
            pm.setSecondarymtd(secondaryMtd.get(fofoId) == null ? 0 : secondaryMtd.get(fofoId).intValue());
170
            pm.setSecondarylmtd(secondarylmtd.get(fofoId) == null ? 0 : secondarylmtd.get(fofoId).intValue());
171
            pm.setSecondarylms(secondarylms.get(fofoId) == null ? 0 : secondarylms.get(fofoId).intValue());
172
            pm.setTodayTertiary(todaytertiary.get(fofoId) == null ? 0 : todaytertiary.get(fofoId).intValue());
173
            pm.setLastThreeDaytertiary(last3daystertiary.get(fofoId) == null ? 0 : last3daystertiary.get(fofoId));
174
            pm.setWalletAmount(userWallet.get(fofoId) == null ? 0 : userWallet.get(fofoId).getAmount());
175
            pm.setInvestment(investmentMap.get(fofoId));
176
            pm.setTicket(ticketMap.get(fofoId) == null ? 0 : ticketMap.get(fofoId).intValue());
177
            pm.setHygiene(hygieneCount);
178
            pm.setInvestment_ok(
179
                    investmentMaintainedDaysMap.get(fofoId) == null ? 0 : investmentMaintainedDaysMap.get(fofoId));
180
            pm.setPartnerType(partnerType);
181
            if (authuserEsclationTypeMap.get(EscalationType.L1) != null) {
182
                pm.setAuthUser(authuserEsclationTypeMap.get(EscalationType.L1).getName());
183
            } else if (authuserEsclationTypeMap.get(EscalationType.L2) != null) {
184
                pm.setAuthUser(authuserEsclationTypeMap.get(EscalationType.L2).getName());
185
            } else if (authuserEsclationTypeMap.get(EscalationType.L3) != null) {
186
                pm.setAuthUser(authuserEsclationTypeMap.get(EscalationType.L3).getName());
187
            } else if (authuserEsclationTypeMap.get(EscalationType.L4) != null) {
188
                pm.setAuthUser(authuserEsclationTypeMap.get(EscalationType.L4).getName());
189
            } else {
190
                pm.setAuthUser(" - ");
191
            }
192
            pm.setTotalHygiene(totalHygieneCount);
26460 amit.gupta 193
 
32435 amit.gupta 194
            pm.setTicket(ticketMap.get(fofoId) == null ? 0 : ticketMap.get(fofoId).intValue());
195
            Set<AuthUser> rbmAuths = partnerRbmsMap.get(fofoId);
196
            if (rbmAuths == null) {
197
                pm.setRbms("-");
198
            } else {
199
                pm.setRbms(rbmAuths.stream().filter(x -> l1Rbms.contains(x.getId())).map(x -> x.getFullName()).collect(Collectors.joining(",")));
200
                if (pm.getRbms().equals("")) {
201
                    pm.setRbms(rbmAuths.stream().filter(x -> l2Rbms.contains(x.getId())).map(x -> x.getFullName()).collect(Collectors.joining(",")));
202
                }
203
                if (pm.getRbms().equals("")) {
204
                    pm.setRbms("-");
205
                }
206
            }
207
            allPartnerStats.put(fofoId, pm);
208
            LOGGER.info("pm {}", pm);
27545 tejbeer 209
 
32435 amit.gupta 210
        }
211
        return allPartnerStats;
212
    }
26460 amit.gupta 213
 
32435 amit.gupta 214
    @Override
215
    // @Cacheable(value = "partnerAggregateStats", cacheManager =
216
    // "oneDayCacheManager")
217
    public PartnerDetailModel getAggregateStats(List<PartnerDetailModel> partnerDetailModels)
218
            throws ProfitMandiBusinessException {
219
        PartnerDetailModel pdm = new PartnerDetailModel();
220
        PartnerDailyInvestment aggregateInvestment = new PartnerDailyInvestment();
221
        pdm.setInvestment(aggregateInvestment);
222
        double totallmsAmount = 0;
223
        double totallmtdAmount = 0;
224
        double totalmtdAmount = 0;
225
        double totalTodayTertiary = 0;
226
        int totalTicketCount = 0;
26460 amit.gupta 227
 
32435 amit.gupta 228
        int currentHygieneCount = 0;
229
        int currentTotalHygieneCount = 0;
230
        if (partnerDetailModels != null && !partnerDetailModels.isEmpty()) {
231
            for (PartnerDetailModel partnerDetailModel : partnerDetailModels) {
232
                if (partnerDetailModel != null) {
233
                    PartnerDailyInvestment pdi = partnerDetailModel.getInvestment();
234
                    totallmsAmount += partnerDetailModel.getLms();
235
                    totallmtdAmount += partnerDetailModel.getLmtd();
236
                    totalmtdAmount += partnerDetailModel.getMtd();
237
                    totalTicketCount += partnerDetailModel.getTicket();
238
                    totalTodayTertiary += partnerDetailModel.getTodayTertiary();
239
                    currentHygieneCount += partnerDetailModel.getHygiene();
240
                    currentTotalHygieneCount += partnerDetailModel.getTotalHygiene();
241
                    if (pdi != null) {
242
                        aggregateInvestment.setActivatedStockAmount(
243
                                aggregateInvestment.getActivatedStockAmount() + pdi.getActivatedStockAmount());
244
                        aggregateInvestment.setGrnPendingAmount(
245
                                aggregateInvestment.getGrnPendingAmount() + pdi.getGrnPendingAmount());
246
                        aggregateInvestment
247
                                .setInStockAmount(aggregateInvestment.getInStockAmount() + pdi.getInStockAmount());
248
                        aggregateInvestment.setReturnInTransitAmount(
249
                                aggregateInvestment.getReturnInTransitAmount() + pdi.getReturnInTransitAmount());
250
                        aggregateInvestment.setSalesAmount(aggregateInvestment.getSalesAmount() + pdi.getSalesAmount());
251
                        aggregateInvestment
252
                                .setUnbilledAmount(aggregateInvestment.getUnbilledAmount() + pdi.getUnbilledAmount());
253
                        aggregateInvestment
254
                                .setWalletAmount(aggregateInvestment.getWalletAmount() + pdi.getWalletAmount());
255
                    }
256
                }
27635 tejbeer 257
 
32435 amit.gupta 258
                pdm.setHygiene(currentHygieneCount);
259
                pdm.setTotalHygiene(currentTotalHygieneCount);
260
                pdm.setLms((int) totallmsAmount);
261
                pdm.setLmtd((int) totallmtdAmount);
262
                pdm.setMtd((int) totalmtdAmount);
263
                pdm.setTicket((int) totalTicketCount);
264
                pdm.setTodayTertiary((int) totalTodayTertiary);
265
                pdm.setCount(partnerDetailModels.size());
266
            }
267
        }
268
        return pdm;
269
    }
26460 amit.gupta 270
 
271
}