Subversion Repositories SmartDukaan

Rev

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

package com.spice.profitmandi.service.tag;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.model.TagListingModel;
import com.spice.profitmandi.dao.entity.catalog.*;
import com.spice.profitmandi.dao.entity.dtr.Retailer;
import com.spice.profitmandi.dao.entity.inventory.VendorItemPricing;
import com.spice.profitmandi.dao.enumuration.catalog.TagType;
import com.spice.profitmandi.dao.repository.catalog.*;
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
import com.spice.profitmandi.dao.repository.inventory.VendorItemPricingRepository;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Component
public class TagServiceImpl implements TagService {

        @Autowired
        private TagRepository tagRepository;
        
        @Autowired
        @Qualifier("catalogItemRepository")
        private ItemRepository itemRepository;
        
        @Autowired
        private TagListingRepository tagListingRepository;
        
        @Autowired
        private RetailerTagRepository retailerTagRepository;
        
        @Autowired
        private VendorItemPricingRepository vendorItemPricingRepository;
        
        @Autowired
        private RetailerRepository retailerRepository;
        
        @Autowired
        private PinCodeTagRepository pinCodeTagRepository;
        
        @Override
        public void createTagListings(List<TagListingModel> tagListingModels) throws ProfitMandiBusinessException {
                if(tagListingModels.isEmpty()){
                        throw new ProfitMandiBusinessException("tagListing", tagListingModels, "TGLSTNG_1001");
                }
                Set<Integer> tagIds = new HashSet<>();
                Set<Integer> itemIds = new HashSet<>();
                for(TagListingModel tagListingModel : tagListingModels){
                        tagIds.add(tagListingModel.getTagId());
                        itemIds.add(tagListingModel.getItemId());
                }
                
                List<Tag> tags = tagRepository.selectByIds(tagIds);
                
                if(tags.size() != tagIds.size()){
                        Set<Integer> foundTagIds = new HashSet<>();
                        for(Tag tag : tags){
                                foundTagIds.add(tag.getId());
                        }
                        Collection<?> invalidTagIds = CollectionUtils.subtract(tagIds, foundTagIds);
                        ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(ProfitMandiConstants.TAG_ID, invalidTagIds, "TG_1001");
                        throw profitMandiBusinessException;
                }
                
                List<Item> items = itemRepository.selectByIds(itemIds);
                
                if(items.size() != itemIds.size()){
                        Set<Integer> foundItemIds = new HashSet<>();
                        for(Item item : items){
                                foundItemIds.add(item.getId());
                        }
                        Collection<?> invalidItemIds = CollectionUtils.subtract(itemIds, foundItemIds);
                        ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, invalidItemIds, "TG_1002");
                        throw profitMandiBusinessException;
                }
                
                
                for(TagListingModel tagListingModel : tagListingModels){
                        this.createTagListing(tagListingModel.getTagId(), tagListingModel.getItemId(), tagListingModel.getSellingPrice(), tagListingModel.getMop(), 
                                        tagListingModel.getSupportPrice(), tagListingModel.getMaxDiscountPrice(), tagListingModel.getStartDate());
                }
        }
        
        @Override
        public void createTagListing(int tagId, int itemId, float sellingPrice, float mop, float priceSupport, float maxDiscount, LocalDateTime startDate) {
                List<TagListing> tatListingList = tagListingRepository.selectByItemIdsAndTagIds(new HashSet<Integer>(Arrays.asList(itemId)), new HashSet<Integer>(Arrays.asList(tagId)));
                if(tatListingList.size() == 0) {
                        TagListing tagListing = new TagListing();
                        tagListing.setActive(true);
                        tagListing.setCreatedBy(1);
                        tagListing.setItemId(itemId);
                        tagListing.setSupportPrice(priceSupport);
                        tagListing.setSellingPrice(sellingPrice);
                        tagListing.setMop(mop);
                        tagListing.setStartDate(startDate);
                        tagListing.setTagId(tagId);
                        tagListing.setMaxDiscountPrice(maxDiscount);
                        tagListingRepository.persist(tagListing);
                } else {
                        TagListing tl = tatListingList.get(0);
                        tl.setSellingPrice(sellingPrice);
                        tl.setMop(mop);
                        tl.setSupportPrice(priceSupport);
                        tl.setMaxDiscountPrice(maxDiscount);
                        tagListingRepository.persist(tl);
                        
                }
        }
        @Override
        public List<Retailer> getRetailersByTagId(int tagId) {
                List<RetailerTag> retailerTags = retailerTagRepository.selectByTagId(tagId);
                if(retailerTags.isEmpty()){
                        return new ArrayList<>();
                }
                List<Integer> retailerIds = new ArrayList<>();
                for(RetailerTag retailerTag : retailerTags){
                        retailerIds.add(retailerTag.getRetailerId());
                }
                return retailerRepository.selectByIds(retailerIds);
        }
        
        @Override
    public List<Tag> getTagsByPinCode(String pinCode) throws ProfitMandiBusinessException {
                Set<Integer> tagIds = this.getTagIdsByPinCode(pinCode);
                if(tagIds.isEmpty()){
                        return new ArrayList<>();
                }
                return tagRepository.selectByIds(tagIds);
        }
        
        @Override
        public Set<Integer> getTagIdsByPinCode(String pinCode){
                List<PinCodeTag> pinCodeTags = pinCodeTagRepository.selectByPinCode(pinCode);
                Set<Integer> tagIds = new HashSet<>();
                for(PinCodeTag pinCodeTag : pinCodeTags){
                        tagIds.add(pinCodeTag.getTagId());
                }
                return tagIds;
        }
        
        @Cacheable(value = "pinCodeByTagId", cacheManager = "timeoutCacheManager")
        @Override
        public Set<String> getPinCodesByTagId(int tagId) {
                List<PinCodeTag> pinCodeTags = pinCodeTagRepository.selectByTagId(tagId);
                Set<String> pinCodes = new HashSet<>();
                for(PinCodeTag pinCodeTag : pinCodeTags){
                        pinCodes.add(pinCodeTag.getPinCode());
                }
                return pinCodes;
        }
        @Override
    public List<Tag> getTagsByRetailerId(int retailerId) throws ProfitMandiBusinessException {
                List<RetailerTag> retailerTags = retailerTagRepository.selectByRetailerId(retailerId);
                if(!retailerTags.isEmpty()){
                        return new ArrayList<>();
                }
                Set<Integer> tagIds = new HashSet<>();
                for(RetailerTag retailerTag : retailerTags){
                        tagIds.add(retailerTag.getTagId());
                }
                return tagRepository.selectByIds(tagIds);
        }
        
        @Override
    public Set<Integer> getFofoTagIdsByPinCode(String pinCode) throws ProfitMandiBusinessException {
                List<Tag> pinCodeTags = this.getTagsByPinCode(pinCode);
                Set<Integer> pinCodeTagIds = new HashSet<>();
                for(Tag tag : pinCodeTags){
                        if(tag.getType() == TagType.FOFO){
                                pinCodeTagIds.add(tag.getId());
                        }
                }
                return pinCodeTagIds;
        }
        private Map<Integer, Map<String, Object>> createTagsMap(List<Tag> tags){
                Map<Integer, Map<String, Object>> tagsMap = new HashMap<>();
                for(Tag tag: tags){
                        if(!tagsMap.containsKey(tag.getId())){
                                Map<String, Object> tagMap = new HashMap<>();
                                tagMap.put(ProfitMandiConstants.LABEL, tag.getLabel());
                                tagMap.put(ProfitMandiConstants.CREATED_BY, tag.getCreatedBy());
                                tagMap.put(ProfitMandiConstants.CREATE_TIMESTAMP, tag.getCreateTimestamp());
                                tagMap.put(ProfitMandiConstants.DESCRIPTION, tag.getDescription());
                                tagMap.put(ProfitMandiConstants.TYPE, tag.getType());
                                tagMap.put(ProfitMandiConstants.UPDATE_TIMESTAMP, tag.getUpdateTimestamp());
                                tagsMap.put(tag.getId(), tagMap);
                        }
                }
                return tagsMap;
        }
        
        private Map<Integer, Map<String, Object>> createItemsMap(List<Item> items){
                Map<Integer, Map<String, Object>> itemsMap = new HashMap<>();
                for(Item item: items){
                        if(!itemsMap.containsKey(item.getId())){
                                Map<String, Object> itemMap = new HashMap<>();
                                itemMap.put(ProfitMandiConstants.BRAND, item.getBrand());
                                itemMap.put(ProfitMandiConstants.MODEL_NAME, item.getModelName());
                                itemMap.put(ProfitMandiConstants.MODEL_NUMBER, item.getModelNumber());
                                itemMap.put(ProfitMandiConstants.COLOR, item.getColor());
                                itemsMap.put(item.getId(), itemMap);
                        }
                }
                return itemsMap;
        }
        @Override
    public List<Map<String, Object>> getTagListingDetail(int tagId) throws ProfitMandiBusinessException {
                List<TagListing> tagListings = tagListingRepository.selectByTagId(tagId);
                List<Map<String, Object>> tagListingsList = new ArrayList<>();
                if(tagListings.isEmpty()){
                        return new ArrayList<>();
                }
                Set<Integer> tagIds = new HashSet<>();
                Set<Integer> itemIds = new HashSet<>();
                
                for(TagListing tagListing : tagListings){
                        tagIds.add(tagListing.getTagId());
                        itemIds.add(tagListing.getItemId());
                }
                List<Tag> tags = tagRepository.selectByIds(tagIds);
                Map<Integer, List<VendorItemPricing>> itemVipsMap = vendorItemPricingRepository.selectAll(new ArrayList<>(itemIds)).stream().collect(
                                Collectors.groupingBy(x->x.getItemId(), Collectors.toList()));
                Map<Integer, Map<String, Object>> tagsMap = this.createTagsMap(tags);
                
                for(TagListing tagListing : tagListings){
                        Map<String, Object> tagListingMap = new HashMap<>();
                        tagListingMap.put(ProfitMandiConstants.CREATED_BY, tagListing.getCreatedBy());
                        tagListingMap.put(ProfitMandiConstants.CREATE_TIMESTAMP, tagListing.getCreatedTimestamp());
                        if(itemVipsMap.get(tagListing.getItemId())!=null) {
                                tagListingMap.put("TP", itemVipsMap.get(tagListing.getItemId()).get(0).getTp());
                        } else {
                                tagListingMap.put("TP", null);
                        }
                        tagListingMap.put(ProfitMandiConstants.SELLING_PRICE, tagListing.getSellingPrice());
                        tagListingMap.put(ProfitMandiConstants.MOP, tagListing.getMop());
                        tagListingMap.put(ProfitMandiConstants.START_DATE, tagListing.getStartDate());
                        tagListingMap.put(ProfitMandiConstants.SUPPORT_PRICE, tagListing.getSupportPrice());
                        tagListingMap.put(ProfitMandiConstants.MAX_DISCOUNT, tagListing.getMaxDiscountPrice());
                        tagListingMap.put(ProfitMandiConstants.ITEM_ID, tagListing.getItemId());
                        tagListingMap.put(ProfitMandiConstants.TAG_ID, tagListing.getTagId());
                        tagListingMap.put(ProfitMandiConstants.TAG, tagsMap.get(tagListing.getTagId()));
                        tagListingMap.put(ProfitMandiConstants.ACTIVE, String.valueOf(tagListing.isActive()));
                        tagListingsList.add(tagListingMap);
                }
                return tagListingsList;
        }
        private Set<Integer> toTagIds(List<Tag> tags){
                if(tags.isEmpty()){
                        return new HashSet<>();
                }
                Set<Integer> tagIds = new HashSet<>();
                for(Tag tag : tags){
                        tagIds.add(tag.getId());
                }
                return tagIds;
        }
        @Cacheable(value = "pinCodeNegativesTagIds", cacheManager = "timeoutCacheManager")
        @Override
        public Set<Integer> getPinCodeNegativeTagIds() {
                List<Tag> tags = tagRepository.selectAllByPinAll(true);
                return this.toTagIds(tags);
        }
        @Cacheable(value = "pinCodePositiveTagIds", cacheManager = "timeoutCacheManager")
        @Override
        public Set<Integer> getPinCodePositiveTagIds() {
                List<Tag> tags = tagRepository.selectAllByPinAll(false);
                return this.toTagIds(tags);
        }
        @Cacheable(value = "userNegativesTagIds", cacheManager = "timeoutCacheManager")
        @Override
        public Set<Integer> getUserNegativeTagIds() {
                List<Tag> tags = tagRepository.selectAllByUserAll(true);
                return this.toTagIds(tags);
        }
        @Cacheable(value = "userPositiveTagIds", cacheManager = "timeoutCacheManager")
        @Override
        public Set<Integer> getUserPositiveTagIds() {
                List<Tag> tags = tagRepository.selectAllByUserAll(false);
                return this.toTagIds(tags);
        }
        @Override
    public Set<Integer> getUserTagIdsByTagIds(Set<Integer> tagIds) throws ProfitMandiBusinessException {
                List<RetailerTag> retailerTags = retailerTagRepository.selectByTagIds(tagIds);
                Set<Integer> retailerTagIds = new HashSet<>();
                if(retailerTags.isEmpty()){
                        return retailerTagIds;
                }
                for(RetailerTag retailerTag : retailerTags){
                        retailerTagIds.add(retailerTag.getTagId());
                }
                return retailerTagIds;
        }
}