Subversion Repositories SmartDukaan

Rev

Rev 21801 | Rev 21867 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.service.pricing;

import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.dao.entity.fofo.RetailerTag;
import com.spice.profitmandi.dao.entity.fofo.TagListing;
import com.spice.profitmandi.dao.entity.user.Address;
import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;
import com.spice.profitmandi.dao.repository.fofo.PinCodeTagRepository;
import com.spice.profitmandi.dao.repository.fofo.RetailerTagRepository;
import com.spice.profitmandi.dao.repository.fofo.TagListingRepository;
import com.spice.profitmandi.dao.repository.user.AddressRepository;
import com.spice.profitmandi.service.tag.TagService;

public class PricingServiceImpl implements PricingService{

        @Autowired
        RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
        
        @Autowired
        AddressRepository addressRepository;
        
        @Autowired
        PinCodeTagRepository pinCodeTagRepository;
        
        @Autowired
        TagService tagService;
        
        @Autowired
        RetailerTagRepository retailerTagRepository;
        
        @Autowired
        TagListingRepository tagListingRepository;
        
        @Override
        public float getPrice(int itemId, int retailerId) {
                int addressId;
                try {
                        float price = Float.MAX_VALUE;
                        addressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailerId);
                        Address retailerAddress = addressRepository.selectById(addressId);
                        Set<Integer> pinCodeTagIds = tagService.getFofoTagIdsByPinCode(retailerAddress.getPinCode());
                        if(pinCodeTagIds.isEmpty()){
                                return price;
                        }
                        List<RetailerTag> retailerTags = retailerTagRepository.selectByTagIds(pinCodeTagIds);
                        if(retailerTags.isEmpty()){
                                return price;
                        }
                        Set<Integer> retailerTagIds = new HashSet<>();
                        for(RetailerTag retailerTag : retailerTags){
                                retailerTagIds.add(retailerTag.getTagId());
                        }
                        List<TagListing> tagListings = tagListingRepository.selectByItemIdAndTagIds(itemId, retailerTagIds);
                        if(tagListings.isEmpty()){
                                return price;
                        }
                        
                        for(TagListing tagListing : tagListings){
                                if(price > tagListing.getSellingPrice()){
                                        price = tagListing.getSellingPrice();
                                }
                        }
                        
                } catch (ProfitMandiBusinessException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                
                return 0;
        }
        
}