Subversion Repositories SmartDukaan

Rev

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

Rev 35924 Rev 36049
Line 79... Line 79...
79
    // Dependencies - Core
79
    // Dependencies - Core
80
    @Autowired
80
    @Autowired
81
    private Gson gson;
81
    private Gson gson;
82
 
82
 
83
    @Autowired
83
    @Autowired
-
 
84
    private OfferService offerServiceProxy; // self-injection for Spring cache proxy
-
 
85
 
-
 
86
    @Autowired
84
    private CacheManager oneDayCacheManager;
87
    private CacheManager redisCacheManager;
-
 
88
 
-
 
89
    @Autowired
-
 
90
    private CacheManager redisShortCacheManager;
85
 
91
 
86
    // Dependencies - Repositories
92
    // Dependencies - Repositories
87
    @Autowired
93
    @Autowired
88
    private GenericRepository genericRepository;
94
    private GenericRepository genericRepository;
89
 
95
 
Line 138... Line 144...
138
 
144
 
139
    @Autowired
145
    @Autowired
140
    private WalletService walletService;
146
    private WalletService walletService;
141
 
147
 
142
    @Override
148
    @Override
-
 
149
    public void evictOfferCaches(int offerId) {
-
 
150
        Offer offer = offerRepository.selectById(offerId);
-
 
151
        YearMonth ym = YearMonth.from(offer.getStartDate());
-
 
152
        redisCacheManager.getCache("offer.definition").evict(offerId);
-
 
153
        redisCacheManager.getCache("allOffers").evict(ym);
-
 
154
        redisCacheManager.getCache("catalog.published_yearmonth").evict(ym);
-
 
155
        redisCacheManager.getCache("offer.slabpayout").evict(offerId);
-
 
156
        redisCacheManager.getCache("offer.achievement").clear();
-
 
157
        redisShortCacheManager.getCache("todayOffers").clear();
-
 
158
    }
-
 
159
 
-
 
160
    @Override
143
    public void addOfferService(CreateOfferRequest createOfferRequest) throws ProfitMandiBusinessException {
161
    public void addOfferService(CreateOfferRequest createOfferRequest) throws ProfitMandiBusinessException {
144
        LOGGER.info("createOfferRequest  -- {}", createOfferRequest);
162
        LOGGER.info("createOfferRequest  -- {}", createOfferRequest);
145
        this.createOffer(createOfferRequest);
163
        this.createOffer(createOfferRequest);
146
        this.createTargetSlabs(createOfferRequest);
164
        this.createTargetSlabs(createOfferRequest);
147
    }
165
    }
Line 167... Line 185...
167
        offerRepository.persist(offer);
185
        offerRepository.persist(offer);
168
        createOfferRequest.setId(offer.getId());
186
        createOfferRequest.setId(offer.getId());
169
    }
187
    }
170
 
188
 
171
    @Override
189
    @Override
172
    @Cacheable(value = "publishedOffersWithAchievement", cacheManager = "oneDayCacheManager")
-
 
173
    public List<CreateOfferRequest> getPublishedOffers(int fofoId, YearMonth yearMonth) throws ProfitMandiBusinessException {
190
    public List<CreateOfferRequest> getPublishedOffers(int fofoId, YearMonth yearMonth) throws ProfitMandiBusinessException {
-
 
191
        // Past months (>1 month ago): achievement is frozen, cache the full result
-
 
192
        if (yearMonth.isBefore(YearMonth.now().minusMonths(1))) {
-
 
193
            return offerServiceProxy.getPublishedOffersCached(fofoId, yearMonth);
-
 
194
        }
-
 
195
        // Current/recent month: assemble live from cached building blocks
-
 
196
        return assemblePublishedOffers(fofoId, yearMonth);
-
 
197
    }
-
 
198
 
-
 
199
    @Cacheable(value = "publishedOffersWithAchievement.past", cacheManager = "redisCacheManager")
-
 
200
    public List<CreateOfferRequest> getPublishedOffersCached(int fofoId, YearMonth yearMonth) throws ProfitMandiBusinessException {
-
 
201
        return assemblePublishedOffers(fofoId, yearMonth);
-
 
202
    }
-
 
203
 
-
 
204
    private List<CreateOfferRequest> assemblePublishedOffers(int fofoId, YearMonth yearMonth) throws ProfitMandiBusinessException {
174
        Map<Integer, List<Offer>> publishedMapByPartner = offerRepository.selectAllPublishedMapByPartner(yearMonth);
205
        Map<Integer, List<Offer>> publishedMapByPartner = offerRepository.selectAllPublishedMapByPartner(yearMonth);
175
        List<Offer> publishedOffers = publishedMapByPartner != null ? publishedMapByPartner.get(fofoId) : null;
206
        List<Offer> publishedOffers = publishedMapByPartner != null ? publishedMapByPartner.get(fofoId) : null;
176
        List<CreateOfferRequest> createOfferRequests = new ArrayList<>();
207
        List<CreateOfferRequest> createOfferRequests = new ArrayList<>();
177
        if (publishedOffers != null) {
208
        if (publishedOffers != null) {
178
            for (Offer offer : publishedOffers) {
209
            for (Offer offer : publishedOffers) {
179
                CreateOfferRequest createOfferRequest = this.getCreateOfferRequest(offer);
210
                CreateOfferRequest copy = deepCopy(this.getCreateOfferRequest(offer));
180
                createOfferRequests.add(createOfferRequest);
211
                setCriteriaPayoutAchieved(fofoId, copy);
181
                setCriteriaPayoutAchieved(fofoId, createOfferRequest);
212
                createOfferRequests.add(copy);
182
 
-
 
183
            }
213
            }
184
        }
214
        }
185
        return createOfferRequests;
215
        return createOfferRequests;
186
    }
216
    }
187
 
217
 
188
    private void setCriteriaPayoutAchieved(int fofoId, CreateOfferRequest createOfferRequest) {
218
    private void setCriteriaPayoutAchieved(int fofoId, CreateOfferRequest createOfferRequest) {
189
        Map<Integer, Long> criteriaTransactionMap = null;
219
        Map<Integer, Long> criteriaTransactionMap;
190
        if (createOfferRequest.getSchemeType().equals(OfferSchemeType.SELLIN)) {
220
        if (createOfferRequest.getSchemeType().equals(OfferSchemeType.SELLIN)) {
191
            Map<Integer, Map<Integer, Long>> partnerWiseMap = offerRepository.getPartnerWisePurchaseSum(createOfferRequest);
221
            criteriaTransactionMap = offerRepository.getPurchaseSumForPartner(
192
            criteriaTransactionMap = partnerWiseMap != null ? partnerWiseMap.get(fofoId) : null;
222
                    createOfferRequest.getId(), fofoId, createOfferRequest);
193
        } else {
223
        } else {
194
            Map<Integer, Map<Integer, Long>> partnerWiseMap = offerRepository.getPartnerWiseSalesSum(createOfferRequest);
224
            criteriaTransactionMap = offerRepository.getSalesSumForPartner(
195
            criteriaTransactionMap = partnerWiseMap != null ? partnerWiseMap.get(fofoId) : null;
225
                    createOfferRequest.getId(), fofoId, createOfferRequest);
196
        }
226
        }
197
        LOGGER.info("I am here ------- {}", createOfferRequest.getId());
-
 
198
        List<com.spice.profitmandi.dao.model.ItemCriteriaPayout> itemCriteriaPayouts = createOfferRequest
227
        List<com.spice.profitmandi.dao.model.ItemCriteriaPayout> itemCriteriaPayouts = createOfferRequest
199
                .getTargetSlabs().get(0).getItemCriteriaPayouts();
228
                .getTargetSlabs().get(0).getItemCriteriaPayouts();
200
 
229
 
201
        for (com.spice.profitmandi.dao.model.ItemCriteriaPayout itemCriteriaPayout : itemCriteriaPayouts) {
230
        for (com.spice.profitmandi.dao.model.ItemCriteriaPayout itemCriteriaPayout : itemCriteriaPayouts) {
202
            itemCriteriaPayout.setNextSlab(null);
231
            itemCriteriaPayout.setNextSlab(null);
203
            LOGGER.info("I am here --- {}", itemCriteriaPayout.getPayoutSlabs());
-
 
204
            long saleAmount = 0;
232
            long saleAmount = 0;
205
            if (criteriaTransactionMap != null
233
            if (criteriaTransactionMap != null
206
                    && criteriaTransactionMap.containsKey(itemCriteriaPayout.getItemCriteria().getId())) {
234
                    && criteriaTransactionMap.containsKey(itemCriteriaPayout.getItemCriteria().getId())) {
207
                saleAmount = criteriaTransactionMap.containsKey(0) ? criteriaTransactionMap.get(0) : criteriaTransactionMap.get(itemCriteriaPayout.getItemCriteria().getId());
235
                saleAmount = criteriaTransactionMap.containsKey(0) ? criteriaTransactionMap.get(0) : criteriaTransactionMap.get(itemCriteriaPayout.getItemCriteria().getId());
208
            }
236
            }
Line 215... Line 243...
215
                    endSlab = payoutSlabs.get(i + 1);
243
                    endSlab = payoutSlabs.get(i + 1);
216
                }
244
                }
217
                if (beginSlab.getOnwardsAmount() > saleAmount) {
245
                if (beginSlab.getOnwardsAmount() > saleAmount) {
218
                    itemCriteriaPayout.setShortValue(beginSlab.getOnwardsAmount() - saleAmount);
246
                    itemCriteriaPayout.setShortValue(beginSlab.getOnwardsAmount() - saleAmount);
219
                    itemCriteriaPayout.setNextSlab(beginSlab);
247
                    itemCriteriaPayout.setNextSlab(beginSlab);
220
                    LOGGER.info("I am here");
-
 
221
                    break;
248
                    break;
222
                }
249
                }
223
                if (endSlab != null) {
250
                if (endSlab != null) {
224
                    if (beginSlab.getOnwardsAmount() <= saleAmount && endSlab.getOnwardsAmount() > saleAmount) {
251
                    if (beginSlab.getOnwardsAmount() <= saleAmount && endSlab.getOnwardsAmount() > saleAmount) {
225
                        itemCriteriaPayout.setCurrentSlab(beginSlab);
252
                        itemCriteriaPayout.setCurrentSlab(beginSlab);
226
                        beginSlab.setSelected(true);
253
                        beginSlab.setSelected(true);
227
                        itemCriteriaPayout.setNextSlab(endSlab);
254
                        itemCriteriaPayout.setNextSlab(endSlab);
228
                        itemCriteriaPayout.setShortValue(endSlab.getOnwardsAmount() - saleAmount);
255
                        itemCriteriaPayout.setShortValue(endSlab.getOnwardsAmount() - saleAmount);
229
                        LOGGER.info("I am here");
-
 
230
                        break;
256
                        break;
231
                    }
257
                    }
232
                    // Last loop
-
 
233
                } else {
258
                } else {
234
                    beginSlab.setSelected(true);
259
                    beginSlab.setSelected(true);
235
                    itemCriteriaPayout.setCurrentSlab(beginSlab);
260
                    itemCriteriaPayout.setCurrentSlab(beginSlab);
236
                    LOGGER.info("I am here");
-
 
237
                }
261
                }
238
            }
262
            }
239
            LOGGER.info("I am here --- {}", itemCriteriaPayout);
-
 
240
        }
263
        }
241
 
-
 
242
    }
264
    }
243
 
265
 
244
    @Override
266
    @Override
245
    @Cacheable(value = "allOffers", cacheManager = "oneDayCacheManager")
267
    @Cacheable(value = "allOffers", cacheManager = "redisCacheManager")
246
    public Map<Integer, CreateOfferRequest> getAllOffers(YearMonth yearMonth) throws ProfitMandiBusinessException {
268
    public Map<Integer, CreateOfferRequest> getAllOffers(YearMonth yearMonth) throws ProfitMandiBusinessException {
247
        List<Offer> allOffers = offerRepository.selectAll(yearMonth, false);
269
        List<Offer> allOffers = offerRepository.selectAll(yearMonth, false);
248
        List<CreateOfferRequest> createOfferRequests = new ArrayList<>();
270
        List<CreateOfferRequest> createOfferRequests = new ArrayList<>();
249
        for (Offer offer : allOffers) {
271
        for (Offer offer : allOffers) {
250
            CreateOfferRequest createOfferRequest = this.getCreateOfferRequest(offer);
272
            CreateOfferRequest createOfferRequest = this.getCreateOfferRequest(offer);
251
            createOfferRequests.add(createOfferRequest);
273
            createOfferRequests.add(createOfferRequest);
252
        }
274
        }
253
        return createOfferRequests.stream().collect(Collectors.toMap(CreateOfferRequest::getId, x -> x));
275
        return createOfferRequests.stream().collect(Collectors.toMap(CreateOfferRequest::getId, x -> x));
254
    }
276
    }
255
 
277
 
-
 
278
    private CreateOfferRequest deepCopy(CreateOfferRequest source) {
-
 
279
        return gson.fromJson(gson.toJson(source), CreateOfferRequest.class);
-
 
280
    }
-
 
281
 
256
    @Override
282
    @Override
257
    @Cacheable(value = "partnerOffers", cacheManager = "thirtyMinsTimeOutCacheManager")
283
    @Cacheable(value = "offer.definition", cacheManager = "redisCacheManager", key = "#offerId")
258
    public CreateOfferRequest getOffer(int fofoId, int offerId) throws ProfitMandiBusinessException {
284
    public CreateOfferRequest getOfferDefinition(int offerId) throws ProfitMandiBusinessException {
259
        Offer offer = offerRepository.selectById(offerId);
285
        Offer offer = offerRepository.selectById(offerId);
-
 
286
        return this.getCreateOfferRequest(offer);
-
 
287
    }
-
 
288
 
-
 
289
    @Override
-
 
290
    public CreateOfferRequest getOffer(int fofoId, int offerId) throws ProfitMandiBusinessException {
260
        CreateOfferRequest createOfferRequest = this.getCreateOfferRequest(offer);
291
        CreateOfferRequest definition = offerServiceProxy.getOfferDefinition(offerId);
261
        if (fofoId > 0) {
292
        if (fofoId > 0) {
-
 
293
            CreateOfferRequest copy = deepCopy(definition);
262
            createOfferRequest.setCriteriaQtyAmountModel(this.getCriteriaQtyAmounModelMap(createOfferRequest, fofoId));
294
            copy.setCriteriaQtyAmountModel(this.getCriteriaQtyAmounModelMap(copy, fofoId));
-
 
295
            return copy;
263
        }
296
        }
264
 
-
 
265
        return createOfferRequest;
297
        return definition;
266
    }
298
    }
267
 
299
 
268
    @Override
300
    @Override
269
    public CreateOfferRequest getCreateOfferRequest(Offer fromOffer) throws ProfitMandiBusinessException {
301
    public CreateOfferRequest getCreateOfferRequest(Offer fromOffer) throws ProfitMandiBusinessException {
270
        CreateOfferRequest createOfferRequest = new CreateOfferRequest();
302
        CreateOfferRequest createOfferRequest = new CreateOfferRequest();
Line 448... Line 480...
448
                this.addOfferService(createOfferRequest);
480
                this.addOfferService(createOfferRequest);
449
            }
481
            }
450
            ymListToEvict.add(YearMonth.from(createOfferRequest.getStartDate()));
482
            ymListToEvict.add(YearMonth.from(createOfferRequest.getStartDate()));
451
        }
483
        }
452
        for (YearMonth ymToEvict : ymListToEvict) {
484
        for (YearMonth ymToEvict : ymListToEvict) {
453
            oneDayCacheManager.getCache("allOffers").evict(ymToEvict);
485
            redisCacheManager.getCache("allOffers").evict(ymToEvict);
454
        }
486
        }
455
    }
487
    }
456
 
488
 
457
    private Map<CreateOfferRequest, List<PartnerTargetModel>> parseFromExcel(InputStream inputStream)
489
    private Map<CreateOfferRequest, List<PartnerTargetModel>> parseFromExcel(InputStream inputStream)
458
            throws ProfitMandiBusinessException {
490
            throws ProfitMandiBusinessException {
Line 593... Line 625...
593
        }
625
        }
594
        return catalogSlabPayoutMap;
626
        return catalogSlabPayoutMap;
595
    }
627
    }
596
 
628
 
597
    @Override
629
    @Override
598
    @Cacheable(value = "slabPayoutMap", cacheManager = "oneDayCacheManager")
630
    @Cacheable(value = "offer.slabpayout", cacheManager = "redisCacheManager", key = "#createOfferRequest.id")
599
    public Map<Integer, Map<Integer, AmountModel>> getSlabPayoutMapNew(CreateOfferRequest createOfferRequest) {
631
    public Map<Integer, Map<Integer, AmountModel>> getSlabPayoutMapNew(CreateOfferRequest createOfferRequest) {
600
        Map<Integer, Map<Integer, AmountModel>> catalogSlabPayoutMap = new HashMap<>();
632
        Map<Integer, Map<Integer, AmountModel>> catalogSlabPayoutMap = new HashMap<>();
601
        com.spice.profitmandi.dao.model.TargetSlab targetSlab = createOfferRequest.getTargetSlabs().get(0);
633
        com.spice.profitmandi.dao.model.TargetSlab targetSlab = createOfferRequest.getTargetSlabs().get(0);
602
        List<ItemCriteriaPayout> payouts = targetSlab.getItemCriteriaPayouts();
634
        List<ItemCriteriaPayout> payouts = targetSlab.getItemCriteriaPayouts();
603
 
635