Subversion Repositories SmartDukaan

Rev

Rev 22550 | Rev 22591 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 22550 Rev 22580
Line 12... Line 12...
12
import org.slf4j.LoggerFactory;
12
import org.slf4j.LoggerFactory;
13
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.stereotype.Component;
14
import org.springframework.stereotype.Component;
15
 
15
 
16
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
16
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
17
import com.spice.profitmandi.common.model.MopPriceModel;
17
import com.spice.profitmandi.common.model.PriceModel;
18
import com.spice.profitmandi.common.model.ProfitMandiConstants;
18
import com.spice.profitmandi.common.model.ProfitMandiConstants;
19
import com.spice.profitmandi.dao.entity.catalog.TagListing;
19
import com.spice.profitmandi.dao.entity.catalog.TagListing;
20
import com.spice.profitmandi.dao.entity.dtr.GadgetCopsInsuranceCalc;
20
import com.spice.profitmandi.dao.entity.dtr.GadgetCopsInsuranceCalc;
21
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
21
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
22
import com.spice.profitmandi.dao.entity.user.Address;
22
import com.spice.profitmandi.dao.entity.user.Address;
Line 62... Line 62...
62
	
62
	
63
	private static final Logger LOGGER = LoggerFactory.getLogger(PricingServiceImpl.class);
63
	private static final Logger LOGGER = LoggerFactory.getLogger(PricingServiceImpl.class);
64
	
64
	
65
	@Override
65
	@Override
66
	public Map<Integer, Float> getPrices(Set<Integer> itemIds, int retailerId) {
66
	public Map<Integer, Float> getPrices(Set<Integer> itemIds, int retailerId) {
-
 
67
		Map<Integer, Float> itemIdPrice = this.preparePriceDefaultValues(itemIds);
67
		return getMopOrSellingPrice(itemIds, retailerId, false);
68
		List<TagListing> tagListings = this.getTagListing(itemIds, retailerId);
-
 
69
		for(TagListing tagListing : tagListings){
-
 
70
			if(itemIdPrice.get(tagListing.getItemId()) > tagListing.getSellingPrice()){
-
 
71
				itemIdPrice.put(tagListing.getItemId(), tagListing.getSellingPrice());
-
 
72
			}
-
 
73
		}
-
 
74
		return itemIdPrice;
68
	}
75
	}
69
	
76
	
70
	private Map<Integer, Float> preparePriceDefaultValues(Set<Integer> itemIds){
77
	private Map<Integer, Float> preparePriceDefaultValues(Set<Integer> itemIds){
71
		Map<Integer, Float> itemIdPrice = new HashMap<>(itemIds.size());
78
		Map<Integer, Float> itemIdPrice = new HashMap<>(itemIds.size());
72
		for(int itemId : itemIds){
79
		for(int itemId : itemIds){
73
			itemIdPrice.put(itemId, Float.MAX_VALUE);
80
			itemIdPrice.put(itemId, Float.MAX_VALUE);
74
		}
81
		}
75
		return itemIdPrice;
82
		return itemIdPrice;
76
	}
83
	}
77
	
84
	
-
 
85
	private Map<Integer, PriceModel> preparePriceModelDefaultValues(Set<Integer> itemIds){
-
 
86
		Map<Integer, PriceModel> itemIdPrice = new HashMap<>(itemIds.size());
-
 
87
		for(int itemId : itemIds){
-
 
88
			PriceModel priceModel = new PriceModel();
-
 
89
			priceModel.setPrice(Float.MAX_VALUE);
-
 
90
			itemIdPrice.put(itemId, priceModel);
-
 
91
		}
-
 
92
		return itemIdPrice;
-
 
93
	}
-
 
94
	
78
	@SuppressWarnings("unchecked")
95
	@SuppressWarnings("unchecked")
79
	public Map<Integer, Float> getMopOrSellingPrice(Set<Integer> itemIds, int retailerId, boolean mop){
96
	private List<TagListing> getTagListing(Set<Integer> itemIds, int retailerId){
80
		Map<Integer, Float> itemIdPrice = this.preparePriceDefaultValues(itemIds);
97
		Map<Integer, TagListing> itemIdTagListing = new HashMap<>();
81
		int addressId;
98
		List<TagListing> tagListings = new ArrayList<>();
82
		try {
99
		try {
83
			
100
			
84
			addressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailerId);
101
			int addressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailerId);
85
			Address retailerAddress = addressRepository.selectById(addressId);
102
			Address retailerAddress = addressRepository.selectById(addressId);
86
			Set<Integer> pinCodeTagIds = tagService.getFofoTagIdsByPinCode(retailerAddress.getPinCode());
103
			Set<Integer> pinCodeTagIds = tagService.getFofoTagIdsByPinCode(retailerAddress.getPinCode());
87
			Set<Integer> pinNegativeTagIds = tagService.getPinCodeNegativeTagIds();
104
			Set<Integer> pinNegativeTagIds = tagService.getPinCodeNegativeTagIds();
88
			Set<Integer> pinCodeIntersection = new HashSet<>(CollectionUtils.intersection(pinCodeTagIds, pinNegativeTagIds));
105
			Set<Integer> pinCodeIntersection = new HashSet<>(CollectionUtils.intersection(pinCodeTagIds, pinNegativeTagIds));
89
			
106
			
-
 
107
			
90
			Set<Integer> filterPinCodeTagIds = new HashSet<>(CollectionUtils.subtract(pinNegativeTagIds, pinCodeIntersection));
108
			Set<Integer> filterPinCodeTagIds = new HashSet<>(CollectionUtils.subtract(pinNegativeTagIds, pinCodeIntersection));
91
			Set<Integer> pinPositiveTagIds = tagService.getPinCodePositiveTagIds();
109
			Set<Integer> pinPositiveTagIds = tagService.getPinCodePositiveTagIds();
92
			filterPinCodeTagIds.addAll(pinPositiveTagIds);
110
			filterPinCodeTagIds.addAll(pinPositiveTagIds);
93
			if(filterPinCodeTagIds.isEmpty()){
111
			if(filterPinCodeTagIds.isEmpty()){
94
				return itemIdPrice;
112
				return tagListings;
95
			}
113
			}
96
			Set<Integer> retailerTagIds = tagService.getUserTagIdsByTagIds(filterPinCodeTagIds);
114
			Set<Integer> retailerTagIds = tagService.getUserTagIdsByTagIds(filterPinCodeTagIds);
97
			Set<Integer> userNegativeTagIds = tagService.getUserNegativeTagIds();
115
			Set<Integer> userNegativeTagIds = tagService.getUserNegativeTagIds();
98
			Set<Integer> userIntersection = new HashSet<>(CollectionUtils.intersection(retailerTagIds, userNegativeTagIds));
116
			Set<Integer> userIntersection = new HashSet<>(CollectionUtils.intersection(retailerTagIds, userNegativeTagIds));
99
			Set<Integer> filterUserTagIds = new HashSet<>(CollectionUtils.subtract(userNegativeTagIds, userIntersection));
117
			Set<Integer> filterUserTagIds = new HashSet<>(CollectionUtils.subtract(userNegativeTagIds, userIntersection));
100
			Set<Integer> userPositiveTagIds = tagService.getUserPositiveTagIds();
118
			Set<Integer> userPositiveTagIds = tagService.getUserPositiveTagIds();
101
			filterUserTagIds.addAll(userPositiveTagIds);
119
			filterUserTagIds.addAll(userPositiveTagIds);
102
			if(filterUserTagIds.isEmpty()){
120
			if(filterUserTagIds.isEmpty()){
103
				return itemIdPrice;
121
				return tagListings;
104
			}
122
			}
105
			List<TagListing> tagListings = tagListingRepository.selectByItemIdsAndTagIds(itemIds, filterUserTagIds);
123
			tagListings = tagListingRepository.selectByItemIdsAndTagIds(itemIds, filterUserTagIds);
106
			
124
			
107
			if(tagListings.isEmpty()){
125
			if(tagListings.isEmpty()){
108
				return itemIdPrice;
126
				return tagListings;
109
			}
127
			}
110
			for(TagListing tagListing : tagListings){
128
			for(TagListing tagListing : tagListings){
111
				if(itemIdPrice.get(tagListing.getItemId()) > tagListing.getSellingPrice()){
129
				if(itemIdTagListing.get(tagListing.getItemId()).getSellingPrice() > tagListing.getSellingPrice()){
112
					itemIdPrice.put(tagListing.getItemId(), mop ? tagListing.getMop(): tagListing.getSellingPrice());
130
					itemIdTagListing.put(tagListing.getItemId(), tagListing);
113
				}
131
				}
114
			}
132
			}
115
		} catch (ProfitMandiBusinessException e) {
133
		} catch (ProfitMandiBusinessException e) {
116
			e.printStackTrace();
134
			e.printStackTrace();
117
		}
135
		}
118
		return itemIdPrice;
136
		return tagListings;
119
	}
137
	}
120
	
138
	
121
	@Override
139
	@Override
122
	public Map<Integer, Float> getMopPrices(Set<Integer> itemIds, int retailerId) {
140
	public Map<Integer, PriceModel> getMopPrices(Set<Integer> itemIds, int retailerId) {
-
 
141
		Map<Integer, PriceModel> itemIdPrice = this.preparePriceModelDefaultValues(itemIds);
-
 
142
		List<TagListing> tagListings = this.getTagListing(itemIds, retailerId);
-
 
143
		for(TagListing tagListing : tagListings){
-
 
144
			if(itemIdPrice.get(tagListing.getItemId()).getPrice() > tagListing.getMop()){
-
 
145
				itemIdPrice.get(tagListing.getItemId()).setPrice(tagListing.getMop());
123
		return getMopOrSellingPrice(itemIds, retailerId, true);
146
				itemIdPrice.get(tagListing.getItemId()).setMop(true);
-
 
147
				itemIdPrice.get(tagListing.getItemId()).setMaxDiscountAmount(tagListing.getMaxDiscountPrice());
-
 
148
			}
-
 
149
		}
-
 
150
		return itemIdPrice;
124
	}
151
	}
125
 
152
 
126
	@Override
153
	@Override
127
	public Map<Float, GadgetCopsInsuranceCalc> getInsurancePrices(Set<Float> prices, String providerName) throws ProfitMandiBusinessException{
154
	public Map<Float, GadgetCopsInsuranceCalc> getInsurancePrices(Set<Float> prices, String providerName) throws ProfitMandiBusinessException{
128
		insuranceProviderRepository.selectByName(providerName);
155
		insuranceProviderRepository.selectByName(providerName);
Line 154... Line 181...
154
		return purchasePriceMap;
181
		return purchasePriceMap;
155
	}
182
	}
156
	
183
	
157
	
184
	
158
	@Override
185
	@Override
159
	public Map<Integer, MopPriceModel> getPurchasePriceMopPriceNotFound(Set<Integer> itemIds, int retailerId) {
186
	public Map<Integer, PriceModel> getPurchasePriceMopPriceNotFound(Set<Integer> itemIds, int retailerId) {
160
		Map<Integer, Float> mopPriceMap = this.getMopPrices(itemIds, retailerId);
187
		Map<Integer, PriceModel> mopPriceModelMap = this.getMopPrices(itemIds, retailerId);
161
		Map<Integer, MopPriceModel> mopPriceModelMap = new HashMap<>();
188
		Set<Integer> purchasePriceItemIds = new HashSet<>();
162
		boolean fetchPurchasePrice = false;
-
 
163
		for(Map.Entry<Integer, Float> entry : mopPriceMap.entrySet()){
189
		for(Map.Entry<Integer, PriceModel> entry : mopPriceModelMap.entrySet()){
164
			MopPriceModel mopPriceModel = new MopPriceModel();
-
 
165
			mopPriceModel.setPrice(entry.getValue());
-
 
166
			if(entry.getValue() == Float.MAX_VALUE || entry.getValue() == 0f) {
190
			if(entry.getValue().getPrice() == Float.MAX_VALUE || entry.getValue().getPrice() == 0f) {
167
				fetchPurchasePrice = true;
191
				purchasePriceItemIds.add(entry.getKey());
168
				mopPriceModel.setMop(false);
-
 
169
			}else{
-
 
170
				mopPriceModel.setMop(false);
-
 
171
			}
192
			}
172
			mopPriceModelMap.put(entry.getKey(), mopPriceModel);
-
 
173
		}
193
		}
174
		
194
		
175
		if(fetchPurchasePrice){
195
		if(!purchasePriceItemIds.isEmpty()){
176
			Map<Integer, Float> purchasePriceMap = this.getPurchasePrices(itemIds, retailerId);
196
			Map<Integer, Float> purchasePriceMap = this.getPurchasePrices(purchasePriceItemIds, retailerId);
177
			for(Map.Entry<Integer, MopPriceModel> entry : mopPriceModelMap.entrySet()){
197
			for(Map.Entry<Integer, Float> entry : purchasePriceMap.entrySet()){
178
				if(!entry.getValue().isMop()){
198
				mopPriceModelMap.get(entry.getKey()).setMop(false);
179
					entry.getValue().setPrice(purchasePriceMap.get(entry.getKey()));
199
				mopPriceModelMap.get(entry.getKey()).setPrice(entry.getValue());
180
				}
-
 
-
 
200
				mopPriceModelMap.get(entry.getKey()).setMaxDiscountAmount(0);
181
			}
201
			}
182
		}
202
		}
183
		
203
		
184
		return mopPriceModelMap;
204
		return mopPriceModelMap;
185
	}
205
	}
186
	
206
	
187
	//Currently it only handles fofos
207
	//Currently it only handles fofos
-
 
208
	@SuppressWarnings("unchecked")
188
	public List<Integer> getTagsIdsByRetailerId(int retailerId) throws Throwable{
209
	public List<Integer> getTagsIdsByRetailerId(int retailerId) throws Throwable{
189
		//int addressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailerId);
-
 
190
		/*Address retailerAddress = addressRepository.selectById(addressId);
-
 
191
		Set<Integer> pinCodeTagIds = tagService.getFofoTagIdsByPinCode(retailerAddress.getPinCode());*/
-
 
192
		//Hardcoded temporarily
-
 
193
		Set<Integer> pinCodeTagIds = tagService.getFofoTagIdsByPinCode("110001");
210
		Set<Integer> pinCodeTagIds = tagService.getFofoTagIdsByPinCode("110001");
194
		Set<Integer> pinNegativeTagIds = tagService.getPinCodeNegativeTagIds();
211
		Set<Integer> pinNegativeTagIds = tagService.getPinCodeNegativeTagIds();
195
		Set<Integer> pinCodeIntersection = new HashSet<>(CollectionUtils.intersection(pinCodeTagIds, pinNegativeTagIds));
212
		Set<Integer> pinCodeIntersection = new HashSet<>(CollectionUtils.intersection(pinCodeTagIds, pinNegativeTagIds));
196
		
213
		
197
		Set<Integer> validTagIds = new HashSet<>(CollectionUtils.subtract(pinNegativeTagIds, pinCodeIntersection));
214
		Set<Integer> validTagIds = new HashSet<>(CollectionUtils.subtract(pinNegativeTagIds, pinCodeIntersection));