Subversion Repositories SmartDukaan

Rev

Rev 33127 | Rev 34513 | Go to most recent revision | 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.util.Utils;
import com.spice.profitmandi.dao.entity.catalog.BrandCatalog;
import com.spice.profitmandi.dao.entity.catalog.BrandCategory;
import com.spice.profitmandi.dao.repository.catalog.BrandCategoryRepository;
import com.spice.profitmandi.dao.repository.catalog.BrandsRepository;
import com.spice.profitmandi.dao.repository.dtr.RetailerBlockBrandsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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;

        @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> blockedBrands = retailerBlockBrandsRepository.selectAllByRetailer(fofoId).stream()
                                .map(x -> x.getBlockBrands()).collect(Collectors.toList());
                brandsDisplay = brandsDisplay.stream().filter(x -> !blockedBrands.contains(x.getName()))
                                .collect(Collectors.toList());

                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;
        }

}