| 21801 |
ashik.ali |
1 |
package com.spice.profitmandi.service.pricing;
|
|
|
2 |
|
| 22287 |
amit.gupta |
3 |
import java.util.ArrayList;
|
| 21926 |
ashik.ali |
4 |
import java.util.HashMap;
|
| 21801 |
ashik.ali |
5 |
import java.util.HashSet;
|
|
|
6 |
import java.util.List;
|
| 21926 |
ashik.ali |
7 |
import java.util.Map;
|
| 21801 |
ashik.ali |
8 |
import java.util.Set;
|
|
|
9 |
|
| 21867 |
ashik.ali |
10 |
import org.apache.commons.collections.CollectionUtils;
|
| 22353 |
ashik.ali |
11 |
import org.slf4j.Logger;
|
|
|
12 |
import org.slf4j.LoggerFactory;
|
| 21801 |
ashik.ali |
13 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 21895 |
ashik.ali |
14 |
import org.springframework.stereotype.Component;
|
| 21801 |
ashik.ali |
15 |
|
|
|
16 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 22580 |
ashik.ali |
17 |
import com.spice.profitmandi.common.model.PriceModel;
|
| 22216 |
ashik.ali |
18 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 22014 |
ashik.ali |
19 |
import com.spice.profitmandi.dao.entity.catalog.TagListing;
|
| 22216 |
ashik.ali |
20 |
import com.spice.profitmandi.dao.entity.dtr.GadgetCopsInsuranceCalc;
|
| 22251 |
ashik.ali |
21 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
| 21801 |
ashik.ali |
22 |
import com.spice.profitmandi.dao.entity.user.Address;
|
| 22014 |
ashik.ali |
23 |
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
|
| 22216 |
ashik.ali |
24 |
import com.spice.profitmandi.dao.repository.dtr.GadgetCopsInsuranceCalcRepository;
|
|
|
25 |
import com.spice.profitmandi.dao.repository.dtr.InsuranceProviderRepository;
|
| 21801 |
ashik.ali |
26 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;
|
| 22251 |
ashik.ali |
27 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
| 21801 |
ashik.ali |
28 |
import com.spice.profitmandi.dao.repository.user.AddressRepository;
|
|
|
29 |
import com.spice.profitmandi.service.tag.TagService;
|
|
|
30 |
|
| 21895 |
ashik.ali |
31 |
@Component
|
| 21801 |
ashik.ali |
32 |
public class PricingServiceImpl implements PricingService{
|
|
|
33 |
|
|
|
34 |
@Autowired
|
| 22925 |
ashik.ali |
35 |
private RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
|
| 21801 |
ashik.ali |
36 |
|
|
|
37 |
@Autowired
|
| 22925 |
ashik.ali |
38 |
private AddressRepository addressRepository;
|
| 21801 |
ashik.ali |
39 |
|
|
|
40 |
@Autowired
|
| 22925 |
ashik.ali |
41 |
private TagService tagService;
|
| 21801 |
ashik.ali |
42 |
|
|
|
43 |
@Autowired
|
| 22925 |
ashik.ali |
44 |
private TagListingRepository tagListingRepository;
|
| 21801 |
ashik.ali |
45 |
|
|
|
46 |
@Autowired
|
| 22925 |
ashik.ali |
47 |
private InsuranceProviderRepository insuranceProviderRepository;
|
| 21801 |
ashik.ali |
48 |
|
|
|
49 |
@Autowired
|
| 22925 |
ashik.ali |
50 |
private GadgetCopsInsuranceCalcRepository gadgetCopsInsuranceCalcRepository;
|
| 21801 |
ashik.ali |
51 |
|
| 22216 |
ashik.ali |
52 |
@Autowired
|
| 22925 |
ashik.ali |
53 |
private InventoryItemRepository inventoryItemRepository;
|
| 22216 |
ashik.ali |
54 |
|
| 22353 |
ashik.ali |
55 |
private static final Logger LOGGER = LoggerFactory.getLogger(PricingServiceImpl.class);
|
|
|
56 |
|
| 21801 |
ashik.ali |
57 |
@Override
|
| 22216 |
ashik.ali |
58 |
public Map<Integer, Float> getPrices(Set<Integer> itemIds, int retailerId) {
|
| 22580 |
ashik.ali |
59 |
Map<Integer, Float> itemIdPrice = this.preparePriceDefaultValues(itemIds);
|
|
|
60 |
List<TagListing> tagListings = this.getTagListing(itemIds, retailerId);
|
|
|
61 |
for(TagListing tagListing : tagListings){
|
|
|
62 |
if(itemIdPrice.get(tagListing.getItemId()) > tagListing.getSellingPrice()){
|
|
|
63 |
itemIdPrice.put(tagListing.getItemId(), tagListing.getSellingPrice());
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
return itemIdPrice;
|
| 22101 |
ashik.ali |
67 |
}
|
|
|
68 |
|
|
|
69 |
private Map<Integer, Float> preparePriceDefaultValues(Set<Integer> itemIds){
|
|
|
70 |
Map<Integer, Float> itemIdPrice = new HashMap<>(itemIds.size());
|
|
|
71 |
for(int itemId : itemIds){
|
|
|
72 |
itemIdPrice.put(itemId, Float.MAX_VALUE);
|
|
|
73 |
}
|
|
|
74 |
return itemIdPrice;
|
|
|
75 |
}
|
|
|
76 |
|
| 22580 |
ashik.ali |
77 |
private Map<Integer, PriceModel> preparePriceModelDefaultValues(Set<Integer> itemIds){
|
|
|
78 |
Map<Integer, PriceModel> itemIdPrice = new HashMap<>(itemIds.size());
|
|
|
79 |
for(int itemId : itemIds){
|
|
|
80 |
PriceModel priceModel = new PriceModel();
|
|
|
81 |
priceModel.setPrice(Float.MAX_VALUE);
|
|
|
82 |
itemIdPrice.put(itemId, priceModel);
|
|
|
83 |
}
|
|
|
84 |
return itemIdPrice;
|
|
|
85 |
}
|
|
|
86 |
|
| 22101 |
ashik.ali |
87 |
@SuppressWarnings("unchecked")
|
| 22580 |
ashik.ali |
88 |
private List<TagListing> getTagListing(Set<Integer> itemIds, int retailerId){
|
|
|
89 |
Map<Integer, TagListing> itemIdTagListing = new HashMap<>();
|
|
|
90 |
List<TagListing> tagListings = new ArrayList<>();
|
| 21801 |
ashik.ali |
91 |
try {
|
| 23384 |
ashik.ali |
92 |
if(itemIds.isEmpty()) {
|
|
|
93 |
return tagListings;
|
|
|
94 |
}
|
| 22580 |
ashik.ali |
95 |
int addressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailerId);
|
| 21801 |
ashik.ali |
96 |
Address retailerAddress = addressRepository.selectById(addressId);
|
|
|
97 |
Set<Integer> pinCodeTagIds = tagService.getFofoTagIdsByPinCode(retailerAddress.getPinCode());
|
| 22009 |
ashik.ali |
98 |
Set<Integer> pinNegativeTagIds = tagService.getPinCodeNegativeTagIds();
|
|
|
99 |
Set<Integer> pinCodeIntersection = new HashSet<>(CollectionUtils.intersection(pinCodeTagIds, pinNegativeTagIds));
|
|
|
100 |
|
| 22580 |
ashik.ali |
101 |
|
| 22009 |
ashik.ali |
102 |
Set<Integer> filterPinCodeTagIds = new HashSet<>(CollectionUtils.subtract(pinNegativeTagIds, pinCodeIntersection));
|
| 21867 |
ashik.ali |
103 |
Set<Integer> pinPositiveTagIds = tagService.getPinCodePositiveTagIds();
|
| 22009 |
ashik.ali |
104 |
filterPinCodeTagIds.addAll(pinPositiveTagIds);
|
| 21867 |
ashik.ali |
105 |
if(filterPinCodeTagIds.isEmpty()){
|
| 22580 |
ashik.ali |
106 |
return tagListings;
|
| 21867 |
ashik.ali |
107 |
}
|
| 22009 |
ashik.ali |
108 |
Set<Integer> retailerTagIds = tagService.getUserTagIdsByTagIds(filterPinCodeTagIds);
|
|
|
109 |
Set<Integer> userNegativeTagIds = tagService.getUserNegativeTagIds();
|
|
|
110 |
Set<Integer> userIntersection = new HashSet<>(CollectionUtils.intersection(retailerTagIds, userNegativeTagIds));
|
|
|
111 |
Set<Integer> filterUserTagIds = new HashSet<>(CollectionUtils.subtract(userNegativeTagIds, userIntersection));
|
| 21867 |
ashik.ali |
112 |
Set<Integer> userPositiveTagIds = tagService.getUserPositiveTagIds();
|
| 22009 |
ashik.ali |
113 |
filterUserTagIds.addAll(userPositiveTagIds);
|
| 21867 |
ashik.ali |
114 |
if(filterUserTagIds.isEmpty()){
|
| 22580 |
ashik.ali |
115 |
return tagListings;
|
| 21867 |
ashik.ali |
116 |
}
|
| 22580 |
ashik.ali |
117 |
tagListings = tagListingRepository.selectByItemIdsAndTagIds(itemIds, filterUserTagIds);
|
| 21926 |
ashik.ali |
118 |
|
| 21801 |
ashik.ali |
119 |
if(tagListings.isEmpty()){
|
| 22580 |
ashik.ali |
120 |
return tagListings;
|
| 21801 |
ashik.ali |
121 |
}
|
|
|
122 |
for(TagListing tagListing : tagListings){
|
| 22591 |
ashik.ali |
123 |
if(!itemIdTagListing.containsKey(tagListing.getItemId()))
|
|
|
124 |
{
|
| 22580 |
ashik.ali |
125 |
itemIdTagListing.put(tagListing.getItemId(), tagListing);
|
| 22591 |
ashik.ali |
126 |
}else{
|
|
|
127 |
if(itemIdTagListing.get(tagListing.getItemId()).getSellingPrice() > tagListing.getSellingPrice()){
|
|
|
128 |
itemIdTagListing.put(tagListing.getItemId(), tagListing);
|
|
|
129 |
}
|
| 21801 |
ashik.ali |
130 |
}
|
|
|
131 |
}
|
|
|
132 |
} catch (ProfitMandiBusinessException e) {
|
|
|
133 |
e.printStackTrace();
|
|
|
134 |
}
|
| 22580 |
ashik.ali |
135 |
return tagListings;
|
| 21801 |
ashik.ali |
136 |
}
|
|
|
137 |
|
| 22101 |
ashik.ali |
138 |
@Override
|
| 22580 |
ashik.ali |
139 |
public Map<Integer, PriceModel> getMopPrices(Set<Integer> itemIds, int retailerId) {
|
|
|
140 |
Map<Integer, PriceModel> itemIdPrice = this.preparePriceModelDefaultValues(itemIds);
|
|
|
141 |
List<TagListing> tagListings = this.getTagListing(itemIds, retailerId);
|
|
|
142 |
for(TagListing tagListing : tagListings){
|
|
|
143 |
if(itemIdPrice.get(tagListing.getItemId()).getPrice() > tagListing.getMop()){
|
|
|
144 |
itemIdPrice.get(tagListing.getItemId()).setPrice(tagListing.getMop());
|
|
|
145 |
itemIdPrice.get(tagListing.getItemId()).setMop(true);
|
|
|
146 |
itemIdPrice.get(tagListing.getItemId()).setMaxDiscountAmount(tagListing.getMaxDiscountPrice());
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
return itemIdPrice;
|
| 21926 |
ashik.ali |
150 |
}
|
| 22216 |
ashik.ali |
151 |
|
|
|
152 |
@Override
|
|
|
153 |
public Map<Float, GadgetCopsInsuranceCalc> getInsurancePrices(Set<Float> prices, String providerName) throws ProfitMandiBusinessException{
|
|
|
154 |
insuranceProviderRepository.selectByName(providerName);
|
|
|
155 |
Map<Float, GadgetCopsInsuranceCalc> pricesMap = new HashMap<>();
|
|
|
156 |
if(ProfitMandiConstants.GADGET_COPS.equals(providerName)){
|
|
|
157 |
List<GadgetCopsInsuranceCalc> gadgetCopsInsuranceCalcs = gadgetCopsInsuranceCalcRepository.selectAll();
|
|
|
158 |
for(GadgetCopsInsuranceCalc gadgetCopsInsuranceCalc : gadgetCopsInsuranceCalcs){
|
|
|
159 |
for(float price : prices){
|
|
|
160 |
if(gadgetCopsInsuranceCalc.getPriceRangeMin()<= price && gadgetCopsInsuranceCalc.getPriceRangeMax() >= price){
|
|
|
161 |
pricesMap.put(price, gadgetCopsInsuranceCalc);
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
}else{
|
|
|
166 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.PROVIDER_NAME, providerName, "");
|
|
|
167 |
}
|
|
|
168 |
return pricesMap;
|
|
|
169 |
}
|
| 22251 |
ashik.ali |
170 |
|
| 22243 |
ashik.ali |
171 |
@Override
|
| 22251 |
ashik.ali |
172 |
public Map<Integer, Float> getPurchasePrices(Set<Integer> itemIds, int retailerId) {
|
|
|
173 |
Map<Integer, Float> purchasePriceMap = this.preparePriceDefaultValues(itemIds);
|
|
|
174 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectByFofoIdItemIds(retailerId, itemIds);
|
|
|
175 |
for(InventoryItem inventoryItem : inventoryItems){
|
| 22276 |
ashik.ali |
176 |
if(inventoryItem.getUnitPrice() < purchasePriceMap.get(inventoryItem.getItemId())){
|
| 22251 |
ashik.ali |
177 |
purchasePriceMap.put(inventoryItem.getItemId(), inventoryItem.getUnitPrice());
|
|
|
178 |
}
|
| 22243 |
ashik.ali |
179 |
}
|
| 22251 |
ashik.ali |
180 |
return purchasePriceMap;
|
| 22243 |
ashik.ali |
181 |
}
|
| 21926 |
ashik.ali |
182 |
|
| 22353 |
ashik.ali |
183 |
|
| 22243 |
ashik.ali |
184 |
@Override
|
| 22580 |
ashik.ali |
185 |
public Map<Integer, PriceModel> getPurchasePriceMopPriceNotFound(Set<Integer> itemIds, int retailerId) {
|
|
|
186 |
Map<Integer, PriceModel> mopPriceModelMap = this.getMopPrices(itemIds, retailerId);
|
|
|
187 |
Set<Integer> purchasePriceItemIds = new HashSet<>();
|
|
|
188 |
for(Map.Entry<Integer, PriceModel> entry : mopPriceModelMap.entrySet()){
|
|
|
189 |
if(entry.getValue().getPrice() == Float.MAX_VALUE || entry.getValue().getPrice() == 0f) {
|
|
|
190 |
purchasePriceItemIds.add(entry.getKey());
|
| 22243 |
ashik.ali |
191 |
}
|
|
|
192 |
}
|
| 22550 |
ashik.ali |
193 |
|
| 22580 |
ashik.ali |
194 |
if(!purchasePriceItemIds.isEmpty()){
|
|
|
195 |
Map<Integer, Float> purchasePriceMap = this.getPurchasePrices(purchasePriceItemIds, retailerId);
|
|
|
196 |
for(Map.Entry<Integer, Float> entry : purchasePriceMap.entrySet()){
|
|
|
197 |
mopPriceModelMap.get(entry.getKey()).setMop(false);
|
|
|
198 |
mopPriceModelMap.get(entry.getKey()).setPrice(entry.getValue());
|
|
|
199 |
mopPriceModelMap.get(entry.getKey()).setMaxDiscountAmount(0);
|
| 22243 |
ashik.ali |
200 |
}
|
|
|
201 |
}
|
| 22550 |
ashik.ali |
202 |
|
|
|
203 |
return mopPriceModelMap;
|
| 22243 |
ashik.ali |
204 |
}
|
| 22287 |
amit.gupta |
205 |
|
|
|
206 |
//Currently it only handles fofos
|
| 22580 |
ashik.ali |
207 |
@SuppressWarnings("unchecked")
|
| 22925 |
ashik.ali |
208 |
public List<Integer> getTagsIdsByRetailerId(int retailerId){
|
| 22362 |
amit.gupta |
209 |
Set<Integer> pinCodeTagIds = tagService.getFofoTagIdsByPinCode("110001");
|
| 22287 |
amit.gupta |
210 |
Set<Integer> pinNegativeTagIds = tagService.getPinCodeNegativeTagIds();
|
|
|
211 |
Set<Integer> pinCodeIntersection = new HashSet<>(CollectionUtils.intersection(pinCodeTagIds, pinNegativeTagIds));
|
|
|
212 |
|
|
|
213 |
Set<Integer> validTagIds = new HashSet<>(CollectionUtils.subtract(pinNegativeTagIds, pinCodeIntersection));
|
|
|
214 |
Set<Integer> pinPositiveTagIds = tagService.getPinCodePositiveTagIds();
|
|
|
215 |
validTagIds.addAll(CollectionUtils.intersection(pinPositiveTagIds, pinCodeTagIds));
|
|
|
216 |
if(validTagIds.isEmpty()){
|
|
|
217 |
return new ArrayList<>(validTagIds);
|
|
|
218 |
}
|
|
|
219 |
Set<Integer> retailerTagIds = tagService.getUserTagIdsByTagIds(validTagIds);
|
|
|
220 |
Set<Integer> userNegativeTagIds = tagService.getUserNegativeTagIds();
|
|
|
221 |
Set<Integer> userIntersection = new HashSet<>(CollectionUtils.intersection(retailerTagIds, userNegativeTagIds));
|
|
|
222 |
validTagIds = new HashSet<>(CollectionUtils.subtract(userNegativeTagIds, userIntersection));
|
|
|
223 |
Set<Integer> userPositiveTagIds = tagService.getUserPositiveTagIds();
|
|
|
224 |
validTagIds.addAll(CollectionUtils.intersection(userPositiveTagIds, retailerTagIds));
|
|
|
225 |
return new ArrayList<>(validTagIds);
|
|
|
226 |
}
|
| 21801 |
ashik.ali |
227 |
}
|