Subversion Repositories SmartDukaan

Rev

Rev 36209 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.service.catalog;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.BrandAndAddToCartEligibleModel;
import com.spice.profitmandi.common.util.Utils;
import com.spice.profitmandi.dao.entity.catalog.BrandCatalog;
import com.spice.profitmandi.dao.entity.catalog.BrandCategory;
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
import com.spice.profitmandi.dao.entity.fofo.PartnerDealerMapping;
import com.spice.profitmandi.dao.entity.fofo.PartnerOnBoardingPanel;
import com.spice.profitmandi.dao.repository.catalog.BrandCategoryRepository;
import com.spice.profitmandi.dao.repository.catalog.BrandsRepository;
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
import com.spice.profitmandi.dao.repository.dtr.PartnerDealerRepository;
import com.spice.profitmandi.dao.repository.dtr.PartnerOnBoardingPanelRepository;
import com.spice.profitmandi.dao.repository.dtr.RetailerBlockBrandsRepository;
import com.spice.profitmandi.dao.repository.onboarding.BrandCommitRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class BrandsServiceImpl implements BrandsService {

    @Autowired
    private BrandCategoryRepository brandCategoryRepository;

    @Autowired
    private BrandsRepository brandCatalogRepository;

    @Autowired
    private RetailerBlockBrandsRepository retailerBlockBrandsRepository;

    @Autowired
    private FofoStoreRepository fofoStoreRepository;

    @Autowired
    private PartnerOnBoardingPanelRepository partnerOnBoardingPanelRepository;

    @Autowired
    private PartnerDealerRepository partnerDealerRepository;

    @Autowired
    private BrandCommitRepository brandCommitRepository;

    @Override
    public List<BrandCatalog> getBrands(int fofoId, String email, int categoryId) throws ProfitMandiBusinessException {
        List<BrandCatalog> brandsDisplay = this.getBrandsToDisplay(categoryId);
        if (fofoId == Utils.SYSTEM_PARTNER_ID) {
            return brandsDisplay;
        }
        List<String> ineligibleBrands = this.partnerIneligibleBrands(fofoId);
        brandsDisplay.forEach(x -> x.setTrialBlocked(ineligibleBrands.contains(x.getName())));

        return brandsDisplay;
    }

    @Override
    public List<BrandCatalog> getBrandsToDisplay(int categoryId) throws ProfitMandiBusinessException {
        List<BrandCategory> brandCategories = brandCategoryRepository.selectByCategoryId(categoryId).stream()
                .filter(x -> x.isActive()).collect(Collectors.toList());

        Map<Integer, BrandCategory> brandCategoryMap = brandCategories.stream()
                .collect(Collectors.toMap(x -> x.getBrandId(), x -> x));

        List<Integer> brandIds = brandCategories.stream().map(x -> x.getBrandId()).collect(Collectors.toList());

        List<BrandCatalog> brands = brandCatalogRepository.selectByIds(brandIds);

        for (BrandCatalog brand : brands) {
            brand.setBrandCategory(brandCategoryMap.get(brand.getId()));
        }
        return brands;
    }

    @Override
    public List<String> partnerIneligibleBrands(int fofoId) throws ProfitMandiBusinessException {
        BrandAndAddToCartEligibleModel brandAndAddToCartEligibleModel = wodcompletBrands(fofoId);
        List<String> requiredBrand = brandAndAddToCartEligibleModel.getWodRequiredBrands();
        List<String> wodCompleteBrands = brandAndAddToCartEligibleModel.getWodCompleteBrands();
        List<String> ineligibleBrands = new ArrayList<>(requiredBrand);
        ineligibleBrands.removeAll(wodCompleteBrands);
        return ineligibleBrands;
    }

    @Override
    public List<BrandCatalog> getAllActiveBrands() throws ProfitMandiBusinessException {
        List<BrandCategory> brandCategories = brandCategoryRepository.selectAllActive();

        Map<Integer, BrandCategory> brandCategoryMap = brandCategories.stream()
                .collect(Collectors.toMap(BrandCategory::getBrandId, x -> x, (a, b) -> a));

        List<Integer> brandIds = brandCategories.stream()
                .map(BrandCategory::getBrandId)
                .distinct()
                .collect(Collectors.toList());

        if (brandIds.isEmpty()) {
            return new ArrayList<>();
        }

        List<BrandCatalog> brands = brandCatalogRepository.selectByIds(brandIds);
        for (BrandCatalog brand : brands) {
            brand.setBrandCategory(brandCategoryMap.get(brand.getId()));
        }
        return brands;
    }

    @Override
    public BrandAndAddToCartEligibleModel wodcompletBrands(int fofoId) throws ProfitMandiBusinessException {
        FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
        if(!fofoStore.isInternal()){
            PartnerOnBoardingPanel partnerOnBoardingPanel = partnerOnBoardingPanelRepository.selectByCode(fofoStore.getCode());
            List<PartnerDealerMapping> partnerDealerMappingList = new ArrayList<>();
            List<String> wodCompletedBrands = new ArrayList<>();
            if (partnerOnBoardingPanel != null) {
                partnerDealerMappingList = partnerDealerRepository.selectByOnboardingId(partnerOnBoardingPanel.getId());
                if (partnerDealerMappingList != null && partnerDealerMappingList.size() > 0) {
                    //working brands -> Partner only work with brands that having 'Dealer Code'
                    wodCompletedBrands = partnerDealerMappingList.stream().filter(x -> x.getBrandCode() != null && x.getBrandCode().trim().length() > 0).map(x -> x.getBrand()).collect(Collectors.toList());
                }
            }
            BrandAndAddToCartEligibleModel partnerWorkingBrandMappingModel = new BrandAndAddToCartEligibleModel();
            partnerWorkingBrandMappingModel.setWodCompleteBrands(wodCompletedBrands);
            //brandsToBeCheck - only brands where noc_required = true need dealer code verification
            List<String> wodRequiredBrand = brandCommitRepository.selectAllActiveBrand().stream()
                    .filter(x -> x.isNocRequired())
                    .map(x -> x.getBrand()).collect(Collectors.toList());
            partnerWorkingBrandMappingModel.setWodRequiredBrands(wodRequiredBrand);
            return partnerWorkingBrandMappingModel;
        }else {
            BrandAndAddToCartEligibleModel model = new BrandAndAddToCartEligibleModel();
            List<String> wodRequiredBrand = brandCommitRepository.selectAllActiveBrand().stream().map(x -> x.getBrand()).collect(Collectors.toList());
            model.setWodCompleteBrands(wodRequiredBrand);
            model.setWodRequiredBrands(wodRequiredBrand);
            return model;
        }
    }


}