Blame | Last modification | View Log | RSS feed
package com.spice.profitmandi.service.catalog;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.web.client.RestClient;import com.spice.profitmandi.dao.entity.catalog.*;import com.spice.profitmandi.dao.entity.fofo.FofoStore;import com.spice.profitmandi.dao.model.UserCart;import com.spice.profitmandi.dao.repository.catalog.*;import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;import com.spice.profitmandi.dao.repository.user.CartLineRepository;import com.spice.profitmandi.dao.repository.user.UserRepository;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import java.util.*;import java.util.stream.Collectors;@Componentpublic class ComboServiceImpl implements ComboService {@AutowiredCartLineRepository cartLineRepository;@AutowiredItemRepository itemRepository;@AutowiredCatalogRepository catalogRepository;@AutowiredUserRepository userRepository;@AutowiredFofoStoreRepository fofoStoreRepository;@AutowiredComboModelRepository comboModelRepository;@AutowiredComboOptionRepository comboOptionRepository;@AutowiredComboMappedModelRepository comboMappedModelRepository;private static final Logger LOGGER = LogManager.getLogger(ComboServiceImpl.class);@Overridepublic boolean validateCombo(UserCart userCart) throws ProfitMandiBusinessException {Map<Integer, Integer> itemQtyMap = cartLineRepository.selectAllByCart(userCart.getCartId()).stream().collect(Collectors.toMap(x -> x.getItemId(), x -> x.getQuantity()));LOGGER.info("itemQtyMap - {}", itemQtyMap);List<Item> items = itemRepository.selectByIds(itemQtyMap.keySet());Set<Integer> focModelSet = items.stream().filter(x -> x.getBrand().equals("FOC")).map(x -> x.getCatalogItemId()).collect(Collectors.toSet());Map<Integer, Integer> catalogIdCartQtyMap = items.stream().collect(Collectors.groupingBy(x -> x.getCatalogItemId(), Collectors.summingInt(x -> itemQtyMap.get(x.getId()))));FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(userCart.getUserId());List<ComboModel> comboModelList = comboModelRepository.selectByCatalogIdsAndWarehouseId(new ArrayList<>(catalogIdCartQtyMap.keySet()), fofoStore.getWarehouseId());if(comboModelList.size() == 0) {return false;}Map<Integer, List<ComboModel>> comboMap = comboModelList.stream().collect(Collectors.groupingBy(x -> x.getCatalogId()));for (Map.Entry<Integer, List<ComboModel>> comboEntry : comboMap.entrySet()) {int catalogId = comboEntry.getKey();List<ComboModel> comboModels = comboEntry.getValue();ComboModel filteredModel = comboModels.stream().filter(x -> x.getQty() == catalogIdCartQtyMap.get(catalogId)).findAny().orElse(null);if (filteredModel == null) {throw new ProfitMandiBusinessException("Combo Quantity mismatched", "Combo Quantity mismatched", "Combo Quantity mismatched");}List<ComboOption> comboOptions = comboOptionRepository.selectByComboId(filteredModel.getId());List<ComboMappedModel> comboMappedModels = comboMappedModelRepository.selectByComboOptionId(comboOptions.get(0).getId());for (ComboMappedModel comboMappedModel : comboMappedModels) {int secondaryCatalogId = comboMappedModel.getComboCatalogId();int requiredQty = comboMappedModel.getComboQty();LOGGER.info("catalogIdCartQtyMap.get(secondaryCatalogId) - {}", catalogIdCartQtyMap.get(secondaryCatalogId));if (!catalogIdCartQtyMap.containsKey(secondaryCatalogId) || requiredQty > catalogIdCartQtyMap.get(secondaryCatalogId)) {Catalog catalog = catalogRepository.selectCatalogById(catalogId);throw new ProfitMandiBusinessException("Missing required Qty for Combo", "", "Missing required qty for Combo - " + catalog.getDescription());} else {catalogIdCartQtyMap.put(secondaryCatalogId, catalogIdCartQtyMap.get(secondaryCatalogId) - requiredQty);}}}for (int focModel : focModelSet) {int qtyLeft = catalogIdCartQtyMap.get(focModel);if (qtyLeft != 0) {throw new ProfitMandiBusinessException("FOC items cant be billed above Combo Qty", "FOC", "FOC items cant be billed above Combo Qty");}}return true;}}