Subversion Repositories SmartDukaan

Rev

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

Rev 30005 Rev 30121
Line 7... Line 7...
7
import com.spice.profitmandi.common.model.SchemeModel;
7
import com.spice.profitmandi.common.model.SchemeModel;
8
import com.spice.profitmandi.common.model.SendNotificationModel;
8
import com.spice.profitmandi.common.model.SendNotificationModel;
9
import com.spice.profitmandi.common.services.ReporticoService;
9
import com.spice.profitmandi.common.services.ReporticoService;
10
import com.spice.profitmandi.common.util.FormattingUtils;
10
import com.spice.profitmandi.common.util.FormattingUtils;
11
import com.spice.profitmandi.common.util.StringUtils;
11
import com.spice.profitmandi.common.util.StringUtils;
-
 
12
import com.spice.profitmandi.common.util.Utils;
12
import com.spice.profitmandi.dao.entity.catalog.Item;
13
import com.spice.profitmandi.dao.entity.catalog.Item;
13
import com.spice.profitmandi.dao.entity.catalog.Scheme;
14
import com.spice.profitmandi.dao.entity.catalog.Scheme;
14
import com.spice.profitmandi.dao.entity.fofo.*;
15
import com.spice.profitmandi.dao.entity.fofo.*;
15
import com.spice.profitmandi.dao.entity.transaction.PriceDrop;
16
import com.spice.profitmandi.dao.entity.transaction.PriceDrop;
16
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
17
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
Line 72... Line 73...
72
	private ScanRecordRepository scanRecordRepository;
73
	private ScanRecordRepository scanRecordRepository;
73
 
74
 
74
	@Autowired
75
	@Autowired
75
	private SessionFactory sessionFactory;
76
	private SessionFactory sessionFactory;
76
 
77
 
77
	private Set<Integer> tagIds = new HashSet<Integer>(Arrays.asList(4));
78
	private static final Set<Integer> tagIds = new HashSet<Integer>(Arrays.asList(4));
78
 
79
 
79
	@Autowired
80
	@Autowired
80
	private SchemeRepository schemeRepository;
81
	private SchemeRepository schemeRepository;
81
 
82
 
82
	@Autowired
83
	@Autowired
Line 107... Line 108...
107
 
108
 
108
	@Autowired
109
	@Autowired
109
	private WalletService walletService;
110
	private WalletService walletService;
110
 
111
 
111
	@Autowired
112
	@Autowired
112
	private FofoOrderItemRepository fofoOrderItemRepository;
-
 
113
 
-
 
114
	@Autowired
-
 
115
	private FofoLineItemRepository fofoLineItemRepository;
-
 
116
 
-
 
117
	@Autowired
-
 
118
	private PurchaseRepository purchaseRepository;
113
	private PurchaseRepository purchaseRepository;
119
 
114
 
120
	@Autowired
115
	@Autowired
121
	private FofoOrderRepository fofoOrderRepository;
116
	private FofoOrderRepository fofoOrderRepository;
122
 
117
 
Line 133... Line 128...
133
					scheme.getStartDateTime() + ", " + scheme.getEndDateTime(), "SCHM_VE_1005");
128
					scheme.getStartDateTime() + ", " + scheme.getEndDateTime(), "SCHM_VE_1005");
134
		}
129
		}
135
 
130
 
136
		// this.validateItemIds(createSchemeRequest);
131
		// this.validateItemIds(createSchemeRequest);
137
		schemeRepository.persist(scheme);
132
		schemeRepository.persist(scheme);
138
		for (int itemId : createSchemeRequest.getItemIds()) {
133
		for (int catalogId : createSchemeRequest.getCatalogIds()) {
139
			SchemeItem schemeItem = new SchemeItem();
134
			SchemeItem schemeItem = new SchemeItem();
140
			schemeItem.setSchemeId(scheme.getId());
135
			schemeItem.setSchemeId(scheme.getId());
141
			schemeItem.setItemId(itemId);
136
			schemeItem.setCatalogId(catalogId);
142
			schemeItemRepository.persist(schemeItem);
137
			schemeItemRepository.persist(schemeItem);
143
		}
138
		}
144
 
139
 
145
	}
140
	}
146
 
141
 
Line 186... Line 181...
186
		return scheme;
181
		return scheme;
187
	}
182
	}
188
 
183
 
189
 
184
 
190
	private void validateItemIds(CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
185
	private void validateItemIds(CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
191
		if (createSchemeRequest.getItemIds() == null || createSchemeRequest.getItemIds().isEmpty()) {
186
		if (createSchemeRequest.getCatalogIds() == null || createSchemeRequest.getCatalogIds().isEmpty()) {
192
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, createSchemeRequest.getItemIds(),
187
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, createSchemeRequest.getCatalogIds(),
193
					"SCHM_1003");
188
					"SCHM_1003");
194
		}
189
		}
195
		List<Integer> foundItemIds = itemRepository.selectIdsByIdsAndType(createSchemeRequest.getItemIds(),
190
		List<Integer> foundItemIds = itemRepository.selectIdsByIdsAndType(createSchemeRequest.getCatalogIds(),
196
				ItemType.SERIALIZED);
191
				ItemType.SERIALIZED);
197
		if (foundItemIds.size() != createSchemeRequest.getItemIds().size()) {
192
		if (foundItemIds.size() != createSchemeRequest.getCatalogIds().size()) {
198
			createSchemeRequest.getItemIds().removeAll(foundItemIds);
193
			createSchemeRequest.getCatalogIds().removeAll(foundItemIds);
199
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, createSchemeRequest.getItemIds(),
194
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, createSchemeRequest.getCatalogIds(),
200
					"SCHM_1004");
195
					"SCHM_1004");
201
		}
196
		}
202
	}
197
	}
203
 
198
 
204
	@Override
199
	@Override
205
	public Scheme getSchemeById(int schemeId) throws ProfitMandiBusinessException {
200
	public Scheme getSchemeById(int schemeId) throws ProfitMandiBusinessException {
206
		Scheme scheme = schemeRepository.selectById(schemeId);
201
		Scheme scheme = schemeRepository.selectById(schemeId);
207
		List<Integer> itemIds = schemeItemRepository.selectItemIdsBySchemeId(scheme.getId());
202
		List<Integer> catalogIds = schemeItemRepository.selectCatalogIdsBySchemeId(scheme.getId());
208
		if (itemIds.size() > 0) {
203
		if (catalogIds.size() > 0) {
209
			List<Item> items = itemRepository.selectByIds(new HashSet<>(itemIds));
204
			List<Item> items = itemRepository.selectAllByCatalogIds(new HashSet<>(catalogIds));
210
			scheme.setItemStringMap(this.toItemStringMap(items));
205
			scheme.setCatalogStringMap(this.toCatalogStringMap(items));
211
		}
206
		}
212
		return scheme;
207
		return scheme;
213
	}
208
	}
214
 
209
 
215
	public Map<Integer, String> toItemStringMap(List<Item> items) {
210
	public Map<Integer, String> toCatalogStringMap(List<Item> items) {
216
		Map<Integer, String> itemMap = new HashMap<>();
211
		Map<Integer, String> catalogMap = new HashMap<>();
217
		for (Item item : items) {
212
		for (Item item : items) {
218
			itemMap.put(item.getId(), this.getItemString(item));
-
 
219
		}
-
 
220
		return itemMap;
-
 
221
	}
-
 
222
 
-
 
223
	public String getItemString(Item item) {
-
 
224
		StringBuilder itemString = new StringBuilder();
-
 
225
		if (item.getBrand() != null && !item.getBrand().isEmpty()) {
213
			if (!catalogMap.containsKey(item.getCatalogItemId())) {
226
			itemString.append(item.getBrand().trim());
-
 
227
		}
-
 
228
		itemString.append(" ");
-
 
229
		if (item.getModelName() != null && !item.getModelName().isEmpty()) {
-
 
230
			itemString.append(item.getModelName().trim());
-
 
231
		}
-
 
232
		itemString.append(" ");
-
 
233
		if (item.getModelNumber() != null && !item.getModelNumber().isEmpty()) {
-
 
234
			itemString.append(item.getModelNumber().trim());
214
				catalogMap.put(item.getCatalogItemId(), item.getItemDescriptionNoColor());
235
		}
215
			}
236
		itemString.append(" ");
-
 
237
		if (item.getColor() != null && !item.getColor().isEmpty()) {
-
 
238
			itemString.append(item.getColor().trim());
-
 
239
		}
216
		}
240
		return itemString.toString();
217
		return catalogMap;
241
	}
218
	}
242
 
219
 
243
	private Set<Integer> schemeItemsToItemIds(List<SchemeItem> schemeItems) {
220
	private Set<Integer> schemeItemsToCatalogIds(List<SchemeItem> schemeItems) {
244
		Set<Integer> itemIds = new HashSet<>();
221
		Set<Integer> catalogId = new HashSet<>();
245
		for (SchemeItem schemeItem : schemeItems) {
222
		for (SchemeItem schemeItem : schemeItems) {
246
			itemIds.add(schemeItem.getItemId());
223
			catalogId.add(schemeItem.getCatalogId());
247
		}
224
		}
248
		return itemIds;
225
		return catalogId;
249
	}
226
	}
250
 
227
 
251
	@Override
228
	@Override
252
	public List<SchemeModel> getAllSchemeModels(LocalDateTime startDateTime, LocalDateTime endDateTime) {
229
	public List<SchemeModel> getAllSchemeModels(LocalDateTime startDateTime, LocalDateTime endDateTime) {
253
		List<Scheme> schemes = schemeRepository.selectAllBetweenCreateTimestamp(startDateTime, endDateTime);
230
		List<Scheme> schemes = schemeRepository.selectAllBetweenCreateTimestamp(startDateTime, endDateTime);
254
		Map<Integer, Scheme> schemeIdSchemeMap = this.toSchemeIdSchemeMap(schemes);
231
		Map<Integer, Scheme> schemeIdSchemeMap = schemes.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
255
		List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(schemeIdSchemeMap.keySet());
232
		List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(schemeIdSchemeMap.keySet());
256
		Set<Integer> itemIds = this.schemeItemsToItemIds(schemeItems);
233
		Set<Integer> catalogIds = schemeItems.stream().map(x -> x.getCatalogId()).collect(Collectors.toSet());
257
		List<Item> items = itemRepository.selectByIds(itemIds);
234
		List<Item> items = itemRepository.selectAllByCatalogIds(catalogIds);
258
		Map<Integer, String> itemStringMap = this.toItemStringMap(items);
235
		Map<Integer, String> catalogStringMap = this.toCatalogStringMap(items);
259
		this.addItemIdsToSchemes(schemeItems, schemeIdSchemeMap, itemStringMap);
236
		this.addCatalogIdsToSchemes(schemeItems, schemeIdSchemeMap, catalogStringMap);
260
		return this.toSchemeModels(schemeIdSchemeMap);
237
		return this.toSchemeModels(schemeIdSchemeMap);
261
	}
238
	}
262
 
239
 
263
	@Autowired
240
	@Autowired
264
	NotificationService notificationService;
241
	NotificationService notificationService;
Line 283... Line 260...
283
		schemeModel.setEndDateTime(StringUtils.toString(scheme.getEndDateTime()));
260
		schemeModel.setEndDateTime(StringUtils.toString(scheme.getEndDateTime()));
284
		schemeModel.setCreateTimestamp(StringUtils.toString(scheme.getCreateTimestamp()));
261
		schemeModel.setCreateTimestamp(StringUtils.toString(scheme.getCreateTimestamp()));
285
		schemeModel.setActiveTimestamp(StringUtils.toString(scheme.getActiveTimestamp()));
262
		schemeModel.setActiveTimestamp(StringUtils.toString(scheme.getActiveTimestamp()));
286
		schemeModel.setExpireTimestamp(StringUtils.toString(scheme.getExpireTimestamp()));
263
		schemeModel.setExpireTimestamp(StringUtils.toString(scheme.getExpireTimestamp()));
287
		schemeModel.setCreatedBy(scheme.getCreatedBy());
264
		schemeModel.setCreatedBy(scheme.getCreatedBy());
288
		schemeModel.setItemStringMap(scheme.getItemStringMap());
265
		schemeModel.setCatalogStringMap(scheme.getCatalogStringMap());
289
		schemeModel.setRetailerIds(scheme.getRetailerIds());
266
		schemeModel.setRetailerIds(scheme.getRetailerIds());
290
		return schemeModel;
267
		return schemeModel;
291
	}
268
	}
292
 
269
 
293
	private void addItemIdsToSchemes(List<SchemeItem> schemeItems, Map<Integer, Scheme> schemeIdSchemeMap,
270
	private void addCatalogIdsToSchemes(List<SchemeItem> schemeItems, Map<Integer, Scheme> schemeIdSchemeMap,
294
									 Map<Integer, String> itemStringMap) {
271
										Map<Integer, String> catalogStringMap) {
295
		for (SchemeItem schemeItem : schemeItems) {
272
		for (SchemeItem schemeItem : schemeItems) {
296
			schemeIdSchemeMap.get(schemeItem.getSchemeId()).getItemStringMap().put(schemeItem.getItemId(),
273
			Scheme scheme = schemeIdSchemeMap.get(schemeItem.getSchemeId());
297
					itemStringMap.get(schemeItem.getItemId()));
274
			scheme.getCatalogStringMap().put(schemeItem.getCatalogId(), catalogStringMap.get(schemeItem.getCatalogId()));
298
		}
275
		}
299
	}
276
	}
300
 
277
 
301
	@Override
278
	@Override
302
	public void activeSchemeById(int schemeId) throws ProfitMandiBusinessException {
279
	public void activeSchemeById(int schemeId) throws ProfitMandiBusinessException {
Line 323... Line 300...
323
	private void sendSchemeNotification(Scheme scheme) throws ProfitMandiBusinessException {
300
	private void sendSchemeNotification(Scheme scheme) throws ProfitMandiBusinessException {
324
		if (scheme.getType().equals(SchemeType.ACTIVATION)) {
301
		if (scheme.getType().equals(SchemeType.ACTIVATION)) {
325
 
302
 
326
			SendNotificationModel sendNotificationModel = new SendNotificationModel();
303
			SendNotificationModel sendNotificationModel = new SendNotificationModel();
327
			List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(Collections.singleton(scheme.getId()));
304
			List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(Collections.singleton(scheme.getId()));
328
			Set<Integer> itemIds = schemeItems.stream().map(x -> x.getItemId()).collect(Collectors.toSet());
305
			Set<Integer> catalogIds = schemeItems.stream().map(x -> x.getCatalogId()).collect(Collectors.toSet());
329
			List<String> itemDescriptions = itemRepository.selectByIds(itemIds).stream().map(x -> x.getItemDescriptionNoColor()).distinct().collect(Collectors.toList());
306
			List<String> itemDescriptions = itemRepository.selectAllByCatalogIds(catalogIds).stream().filter(Utils.distinctByKey(Item::getCatalogItemId))
-
 
307
					.map(x -> x.getItemDescriptionNoColor()).collect(Collectors.toList());
330
 
308
 
331
 
309
 
332
			String title = "Activation scheme of Rs." + FormattingUtils.formatDecimal(scheme.getAmount()) + " for " + String.join(", ", itemDescriptions);
310
			String title = "Activation scheme of Rs." + FormattingUtils.formatDecimal(scheme.getAmount()) + " for " + String.join(", ", itemDescriptions);
333
			String message = "Duration from - " + FormattingUtils.formatDateMonth(scheme.getStartDateTime()) + " - " + FormattingUtils.formatDateMonth(scheme.getEndDateTime());
311
			String message = "Duration from - " + FormattingUtils.formatDateMonth(scheme.getStartDateTime()) + " - " + FormattingUtils.formatDateMonth(scheme.getEndDateTime());
334
			sendNotificationModel.setCampaignName("activationscheme");
312
			sendNotificationModel.setCampaignName("activationscheme");
Line 376... Line 354...
376
			itemIds.add(inventoryItem.getItemId());
354
			itemIds.add(inventoryItem.getItemId());
377
		}
355
		}
378
		return itemIds;
356
		return itemIds;
379
	}
357
	}
380
 
358
 
381
	private Map<Integer, Set<Scheme>> toItemIdSchemesMap(List<SchemeItem> schemeItems, List<Scheme> schemes) {
359
	private Map<Integer, Set<Scheme>> toCatalogIdSchemesMap(List<SchemeItem> schemeItems, List<Scheme> schemes) {
382
		Map<Integer, Scheme> schemeIdSchemesMap = this.toSchemeIdSchemeMap(schemes);
360
		Map<Integer, Scheme> schemeIdSchemesMap = this.toSchemeIdSchemeMap(schemes);
383
		Map<Integer, Set<Scheme>> itemIdSchemesMap = new HashMap<>();
361
		Map<Integer, Set<Scheme>> catalogIdSchemesMap = new HashMap<>();
384
		for (SchemeItem schemeItem : schemeItems) {
362
		for (SchemeItem schemeItem : schemeItems) {
385
			if (!itemIdSchemesMap.containsKey(schemeItem.getItemId())) {
363
			if (!catalogIdSchemesMap.containsKey(schemeItem.getCatalogId())) {
386
				Set<Scheme> schemesSet = new HashSet<>();
364
				Set<Scheme> schemesSet = new HashSet<>();
387
				schemesSet.add(schemeIdSchemesMap.get(schemeItem.getSchemeId()));
365
				schemesSet.add(schemeIdSchemesMap.get(schemeItem.getSchemeId()));
388
				itemIdSchemesMap.put(schemeItem.getItemId(), schemesSet);
366
				catalogIdSchemesMap.put(schemeItem.getCatalogId(), schemesSet);
389
			} else {
367
			} else {
390
				itemIdSchemesMap.get(schemeItem.getItemId()).add(schemeIdSchemesMap.get(schemeItem.getSchemeId()));
368
				catalogIdSchemesMap.get(schemeItem.getCatalogId()).add(schemeIdSchemesMap.get(schemeItem.getSchemeId()));
391
			}
369
			}
392
		}
370
		}
393
		return itemIdSchemesMap;
371
		return catalogIdSchemesMap;
394
	}
372
	}
395
 
373
 
396
	private Map<InventoryItem, Set<Scheme>> toInventoryItemSchemesMap(List<Scheme> schemes,
374
	private Map<InventoryItem, Set<Scheme>> toInventoryItemSchemesMap(List<Scheme> schemes,
397
																	  List<InventoryItem> inventoryItems) {
375
																	  List<InventoryItem> inventoryItems) {
398
		Set<Integer> schemeIds = new HashSet<>();
376
		Set<Integer> schemeIds = new HashSet<>();
399
		for (Scheme scheme : schemes) {
377
		for (Scheme scheme : schemes) {
400
			schemeIds.add(scheme.getId());
378
			schemeIds.add(scheme.getId());
401
		}
379
		}
402
		Set<Integer> itemIds = this.inventoryItemsToItemIds(inventoryItems);
380
		Set<Integer> itemIds = this.inventoryItemsToItemIds(inventoryItems);
403
		List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIdsAndItemIds(schemeIds, itemIds);
381
		List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIdsAndCatalogIds(schemeIds, itemIds);
404
 
382
 
405
		Map<Integer, Set<Scheme>> itemIdSchemesMap = this.toItemIdSchemesMap(schemeItems, schemes);
383
		Map<Integer, Set<Scheme>> itemIdSchemesMap = this.toCatalogIdSchemesMap(schemeItems, schemes);
406
		Map<InventoryItem, Set<Scheme>> inventoryItemSchemsMap = new HashMap<>();
384
		Map<InventoryItem, Set<Scheme>> inventoryItemSchemsMap = new HashMap<>();
407
		for (InventoryItem inventoryItem : inventoryItems) {
385
		for (InventoryItem inventoryItem : inventoryItems) {
408
			if (itemIdSchemesMap.containsKey(inventoryItem.getItemId())) {
386
			if (itemIdSchemesMap.containsKey(inventoryItem.getItemId())) {
409
				inventoryItemSchemsMap.put(inventoryItem, itemIdSchemesMap.get(inventoryItem.getItemId()));
387
				inventoryItemSchemsMap.put(inventoryItem, itemIdSchemesMap.get(inventoryItem.getItemId()));
410
			}
388
			}
Line 638... Line 616...
638
					fofoOrder.getCreateTimestamp(), false);
616
					fofoOrder.getCreateTimestamp(), false);
639
 
617
 
640
			for (InventoryItem inventoryItem : inventoryItems) {
618
			for (InventoryItem inventoryItem : inventoryItems) {
641
				float itemCashback = 0;
619
				float itemCashback = 0;
642
				Set<Integer> schemeIds = new HashSet<>(
620
				Set<Integer> schemeIds = new HashSet<>(
643
						schemeItemRepository.selectSchemeIdByItemId(inventoryItem.getItemId()));
621
						schemeItemRepository.selectSchemeIdByCatalogId(inventoryItem.getItem().getCatalogItemId()));
644
				List<Scheme> itemActiveSchemes = allActiveSchemes.stream().filter(x -> schemeIds.contains(x.getId()))
622
				List<Scheme> itemActiveSchemes = allActiveSchemes.stream().filter(x -> schemeIds.contains(x.getId()))
645
						.collect(Collectors.toList());
623
						.collect(Collectors.toList());
646
				for (Scheme scheme : itemActiveSchemes) {
624
				for (Scheme scheme : itemActiveSchemes) {
647
					LOGGER.info("Scheme ==== {}", scheme);
625
					LOGGER.info("Scheme ==== {}", scheme);
648
					itemCashback += this.createSchemeInOut(scheme, inventoryItem);
626
					itemCashback += this.createSchemeInOut(scheme, inventoryItem);
Line 867... Line 845...
867
 
845
 
868
	}
846
	}
869
 
847
 
870
	@Override
848
	@Override
871
	@Cacheable(value = "itemSchemeCashback", cacheManager = "timeoutCacheManager")
849
	@Cacheable(value = "itemSchemeCashback", cacheManager = "timeoutCacheManager")
872
	public Map<Integer, Float> getItemSchemeCashBack() {
850
	public Map<Integer, Float> getCatalogSchemeCashBack() {
873
		Map<Integer, Float> itemCashbackMap = new HashMap<>();
851
		Map<Integer, Float> itemCashbackMap = new HashMap<>();
874
		Map<Integer, Scheme> cashbackSchemesMap = schemeRepository
852
		Map<Integer, Scheme> cashbackSchemesMap = schemeRepository
875
				.selectActiveAll(Arrays.asList(SchemeType.ACTIVATION), PartnerType.ALL, LocalDateTime.now(), false)
853
				.selectActiveAll(Arrays.asList(SchemeType.ACTIVATION), PartnerType.ALL, LocalDateTime.now(), false)
876
				.stream().filter(x -> x.getAmountType().equals(AmountType.FIXED))
854
				.stream().filter(x -> x.getAmountType().equals(AmountType.FIXED))
877
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
855
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
878
		if (cashbackSchemesMap.size() > 0) {
856
		if (cashbackSchemesMap.size() > 0) {
879
			List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(cashbackSchemesMap.keySet());
857
			List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(cashbackSchemesMap.keySet());
880
			schemeItems.stream().forEach(x -> {
858
			schemeItems.stream().forEach(x -> {
881
				float cashbackAmount = cashbackSchemesMap.get(x.getSchemeId()).getAmount();
859
				float cashbackAmount = cashbackSchemesMap.get(x.getSchemeId()).getAmount();
882
				if (!itemCashbackMap.containsKey(x.getItemId())) {
860
				if (!itemCashbackMap.containsKey(x.getCatalogId())) {
883
					itemCashbackMap.put(x.getItemId(), cashbackAmount);
861
					itemCashbackMap.put(x.getCatalogId(), cashbackAmount);
884
				} else {
862
				} else {
885
					itemCashbackMap.put(x.getItemId(), itemCashbackMap.get(x.getItemId()) + cashbackAmount);
863
					itemCashbackMap.put(x.getCatalogId(), itemCashbackMap.get(x.getCatalogId()) + cashbackAmount);
886
				}
864
				}
887
			});
865
			});
888
		}
866
		}
889
		// A107FD Model needs to removed
867
		// A107FD Model needs to removed
890
		itemCashbackMap.remove(30211);
868
		itemCashbackMap.remove(30211);
Line 903... Line 881...
903
	 *
881
	 *
904
	 * }
882
	 * }
905
	 */
883
	 */
906
 
884
 
907
	@Override
885
	@Override
908
	public List<Scheme> selectSchemeByPartnerType(PartnerType partnerType, LocalDate onDate, int itemId,
886
	public List<Scheme> selectSchemeByPartnerType(PartnerType partnerType, LocalDate onDate, int catalogId,
909
												  boolean isAdmin, int offset, int limit) throws ProfitMandiBusinessException {
887
												  boolean isAdmin, int offset, int limit) throws ProfitMandiBusinessException {
910
		Session session = sessionFactory.getCurrentSession();
888
		Session session = sessionFactory.getCurrentSession();
911
		CriteriaBuilder cb = session.getCriteriaBuilder();
889
		CriteriaBuilder cb = session.getCriteriaBuilder();
912
		CriteriaQuery<Scheme> query = cb.createQuery(Scheme.class);
890
		CriteriaQuery<Scheme> query = cb.createQuery(Scheme.class);
913
		Root<Scheme> scheme = query.from(Scheme.class);
891
		Root<Scheme> scheme = query.from(Scheme.class);
-
 
892
		Predicate finalPredicate = cb.and();
-
 
893
		if (partnerType.equals(PartnerType.ALL)) {
914
		List<PartnerType> pt = new ArrayList<>();
894
			List<PartnerType> pt = new ArrayList<>();
915
		pt.add(PartnerType.ALL);
895
			pt.add(PartnerType.ALL);
916
		pt.add(partnerType);
896
			pt.add(partnerType);
917
		Predicate p1 = cb.in(scheme.get("partnerType")).value(pt);
897
			Predicate p1 = cb.in(scheme.get("partnerType")).value(pt);
-
 
898
			finalPredicate = cb.and(p1);
-
 
899
		}
918
		Predicate p2 = cb.isNotNull(scheme.get("activeTimestamp"));
900
		Predicate p2 = cb.isNotNull(scheme.get("activeTimestamp"));
919
		cb.desc(cb.isNull(scheme.get("expireTimestamp")));
901
		cb.desc(cb.isNull(scheme.get("expireTimestamp")));
920
 
902
 
921
		Predicate p3 = null;
-
 
922
		Predicate finalPredicate = null;
-
 
923
		if (itemId != 0) {
903
		if (catalogId > 0) {
924
 
904
 
925
			List<Integer> schemeIds = schemeItemRepository.selectSchemeIdByItemId(itemId);
905
			List<Integer> schemeIds = schemeItemRepository.selectSchemeIdByCatalogId(catalogId);
926
			LOGGER.info("schemeId" + schemeIds);
906
			LOGGER.info("schemeId" + schemeIds);
927
			if (schemeIds.isEmpty()) {
907
			if (schemeIds.isEmpty()) {
928
				return new ArrayList<>();
908
				return new ArrayList<>();
929
			}
909
			}
930
			p3 = cb.in(scheme.get("id")).value(schemeIds);
910
			Predicate p3 = cb.in(scheme.get("id")).value(schemeIds);
931
			if (!isAdmin) {
911
			if (onDate != null) {
932
 
-
 
933
				finalPredicate = cb.and(p1, p2, p3, cb.greaterThan(scheme.get("endDateTime"), onDate.atStartOfDay()),
912
				finalPredicate = cb.and(p2, p3, cb.greaterThan(scheme.get("endDateTime"), onDate.atStartOfDay()),
934
						cb.lessThanOrEqualTo(scheme.get("startDateTime"), onDate.atStartOfDay()));
913
						cb.lessThanOrEqualTo(scheme.get("startDateTime"), onDate.atStartOfDay()));
935
				query.where(finalPredicate);
-
 
936
			} else {
914
			} else {
937
				finalPredicate = cb.and(p1, p3);
915
				finalPredicate = cb.and(p3);
938
				query.where(finalPredicate);
-
 
939
			}
916
			}
940
		} else {
917
		} else {
941
 
-
 
942
			if (!isAdmin) {
918
			if (!isAdmin) {
943
 
-
 
944
				finalPredicate = cb.and(p1, p2);
919
				finalPredicate = cb.and(p2);
945
				query.where(finalPredicate);
-
 
946
			} else {
-
 
947
				query.where(p1);
-
 
948
			}
920
			}
949
		}
921
		}
-
 
922
		query.where(finalPredicate);
950
		query.orderBy(cb.desc(cb.function("isnull", Boolean.class, scheme.get("expireTimestamp"))));
923
		query.orderBy(cb.desc(cb.function("isnull", Boolean.class, scheme.get("expireTimestamp"))));
951
 
924
 
952
		return session.createQuery(query).setFirstResult(offset).setMaxResults(limit).getResultList();
925
		return session.createQuery(query).setFirstResult(offset).setMaxResults(limit).getResultList();
953
 
926
 
954
	}
927
	}
955
 
928
 
956
	@Override
929
	@Override
957
	public long selectSchemeCount(PartnerType partnerType, LocalDate onDate, int itemId, boolean isAdmin) {
-
 
958
		Session session = sessionFactory.getCurrentSession();
-
 
959
		CriteriaBuilder cb = session.getCriteriaBuilder();
-
 
960
		CriteriaQuery<Long> query = cb.createQuery(Long.class);
-
 
961
		Root<Scheme> scheme = query.from(Scheme.class);
-
 
962
		List<PartnerType> pt = new ArrayList<>();
-
 
963
		pt.add(PartnerType.ALL);
-
 
964
		pt.add(partnerType);
-
 
965
		Predicate p1 = cb.in(scheme.get("partnerType")).value(pt);
-
 
966
		Predicate p2 = cb.isNotNull(scheme.get("activeTimestamp"));
-
 
967
		Predicate finalPredicate = null;
-
 
968
		if (itemId != 0) {
-
 
969
			List<Integer> schemeIds = schemeItemRepository.selectSchemeIdByItemId(itemId);
-
 
970
			Predicate p3 = cb.in(scheme.get("id")).value(schemeIds);
-
 
971
			if (!isAdmin) {
-
 
972
 
-
 
973
				finalPredicate = cb.and(p1, p2, p3, cb.greaterThan(scheme.get("endDateTime"), onDate.atStartOfDay()),
-
 
974
						cb.lessThanOrEqualTo(scheme.get("startDateTime"), onDate.atStartOfDay()));
-
 
975
				query.select(cb.count(scheme)).where(finalPredicate);
-
 
976
			} else {
-
 
977
				finalPredicate = cb.and(p1, p3);
-
 
978
				query.select(cb.count(scheme)).where(finalPredicate);
-
 
979
			}
-
 
980
		} else {
-
 
981
			if (!isAdmin) {
-
 
982
				finalPredicate = cb.and(p1, p2);
-
 
983
				query.select(cb.count(scheme)).where(finalPredicate);
-
 
984
 
-
 
985
			} else {
-
 
986
				query.select(cb.count(scheme)).where(p1);
-
 
987
 
-
 
988
			}
-
 
989
		}
-
 
990
 
-
 
991
		return session.createQuery(query).getSingleResult();
-
 
992
 
-
 
993
	}
-
 
994
 
-
 
995
	@Override
-
 
996
	public void processActivation() throws ProfitMandiBusinessException {
930
	public void processActivation() throws ProfitMandiBusinessException {
997
		List<SchemeInOut> pendingPayouts = schemeInOutRepository.selectAllPending();
931
		List<SchemeInOut> pendingPayouts = schemeInOutRepository.selectAllPending();
998
		List<Integer> schemeIds = new ArrayList<>();
932
		List<Integer> schemeIds = new ArrayList<>();
999
		Set<Integer> inventoryIds = new HashSet<>();
933
		Set<Integer> inventoryIds = new HashSet<>();
1000
		for (SchemeInOut pendingPayout : pendingPayouts) {
934
		for (SchemeInOut pendingPayout : pendingPayouts) {