Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
33916 amit.gupta 1
package com.spice.profitmandi.service.catalog;
2
 
3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
4
import com.spice.profitmandi.common.web.client.RestClient;
5
import com.spice.profitmandi.dao.entity.catalog.*;
6
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
7
import com.spice.profitmandi.dao.model.UserCart;
8
import com.spice.profitmandi.dao.repository.catalog.*;
9
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
10
import com.spice.profitmandi.dao.repository.user.CartLineRepository;
11
import com.spice.profitmandi.dao.repository.user.UserRepository;
12
import org.apache.logging.log4j.LogManager;
13
import org.apache.logging.log4j.Logger;
14
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.stereotype.Component;
16
 
17
import java.util.*;
18
import java.util.stream.Collectors;
19
 
20
@Component
21
public class ComboServiceImpl implements ComboService {
22
    @Autowired
23
    CartLineRepository cartLineRepository;
24
    @Autowired
25
    ItemRepository itemRepository;
26
    @Autowired
27
    CatalogRepository catalogRepository;
28
 
29
    @Autowired
30
    UserRepository userRepository;
31
 
32
    @Autowired
33
    FofoStoreRepository fofoStoreRepository;
34
 
35
    @Autowired
36
    ComboModelRepository comboModelRepository;
37
    @Autowired
38
    ComboOptionRepository comboOptionRepository;
39
    @Autowired
40
    ComboMappedModelRepository comboMappedModelRepository;
41
 
42
    private static final Logger LOGGER = LogManager.getLogger(ComboServiceImpl.class);
43
 
44
    @Override
45
    public boolean validateCombo(UserCart userCart) throws ProfitMandiBusinessException {
46
        Map<Integer, Integer> itemQtyMap = cartLineRepository.selectAllByCart(userCart.getCartId()).stream().collect(Collectors.toMap(x -> x.getItemId(), x -> x.getQuantity()));
47
        LOGGER.info("itemQtyMap - {}", itemQtyMap);
48
        List<Item> items = itemRepository.selectByIds(itemQtyMap.keySet());
49
        Set<Integer> focModelSet = items.stream().filter(x -> x.getBrand().equals("FOC")).map(x -> x.getCatalogItemId()).collect(Collectors.toSet());
50
        Map<Integer, Integer> catalogIdCartQtyMap = items.stream().collect(Collectors.groupingBy(x -> x.getCatalogItemId(), Collectors.summingInt(x -> itemQtyMap.get(x.getId()))));
51
 
52
 
53
        FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(userCart.getUserId());
54
 
55
        List<ComboModel> comboModelList = comboModelRepository.selectByCatalogIdsAndWarehouseId(new ArrayList<>(catalogIdCartQtyMap.keySet()), fofoStore.getWarehouseId());
56
        if(comboModelList.size() == 0) {
57
            return false;
58
        }
59
        Map<Integer, List<ComboModel>> comboMap = comboModelList.stream().collect(Collectors.groupingBy(x -> x.getCatalogId()));
60
 
61
        for (Map.Entry<Integer, List<ComboModel>> comboEntry : comboMap.entrySet()) {
62
            int catalogId = comboEntry.getKey();
63
            List<ComboModel> comboModels = comboEntry.getValue();
64
            ComboModel filteredModel = comboModels.stream().filter(x -> x.getQty() == catalogIdCartQtyMap.get(catalogId)).findAny().orElse(null);
65
            if (filteredModel == null) {
66
                throw new ProfitMandiBusinessException("Combo Quantity mismatched", "Combo Quantity mismatched", "Combo Quantity mismatched");
67
            }
68
            List<ComboOption> comboOptions = comboOptionRepository.selectByComboId(filteredModel.getId());
69
            List<ComboMappedModel> comboMappedModels = comboMappedModelRepository.selectByComboOptionId(comboOptions.get(0).getId());
70
            for (ComboMappedModel comboMappedModel : comboMappedModels) {
71
                int secondaryCatalogId = comboMappedModel.getComboCatalogId();
72
                int requiredQty = comboMappedModel.getComboQty();
73
                LOGGER.info("catalogIdCartQtyMap.get(secondaryCatalogId) - {}", catalogIdCartQtyMap.get(secondaryCatalogId));
74
                if (!catalogIdCartQtyMap.containsKey(secondaryCatalogId) || requiredQty > catalogIdCartQtyMap.get(secondaryCatalogId)) {
75
                    Catalog catalog = catalogRepository.selectCatalogById(catalogId);
76
                    throw new ProfitMandiBusinessException("Missing required Qty for Combo", "", "Missing required qty for Combo - " + catalog.getDescription());
77
                } else {
78
                    catalogIdCartQtyMap.put(secondaryCatalogId, catalogIdCartQtyMap.get(secondaryCatalogId) - requiredQty);
79
                }
80
            }
81
        }
82
        for (int focModel : focModelSet) {
83
            int qtyLeft = catalogIdCartQtyMap.get(focModel);
84
            if (qtyLeft != 0) {
85
                throw new ProfitMandiBusinessException("FOC items cant be billed above Combo Qty", "FOC", "FOC items cant be billed above Combo Qty");
86
            }
87
        }
88
        return true;
89
    }
90
}