Subversion Repositories SmartDukaan

Rev

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