| 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;
|
| 23781 |
ashik.ali |
11 |
import org.apache.logging.log4j.LogManager;
|
| 23568 |
govind |
12 |
import org.apache.logging.log4j.Logger;
|
| 21801 |
ashik.ali |
13 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 23781 |
ashik.ali |
14 |
import org.springframework.beans.factory.annotation.Qualifier;
|
| 21895 |
ashik.ali |
15 |
import org.springframework.stereotype.Component;
|
| 21801 |
ashik.ali |
16 |
|
|
|
17 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 22580 |
ashik.ali |
18 |
import com.spice.profitmandi.common.model.PriceModel;
|
| 22216 |
ashik.ali |
19 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 28480 |
amit.gupta |
20 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
| 22014 |
ashik.ali |
21 |
import com.spice.profitmandi.dao.entity.catalog.TagListing;
|
| 22216 |
ashik.ali |
22 |
import com.spice.profitmandi.dao.entity.dtr.GadgetCopsInsuranceCalc;
|
| 21801 |
ashik.ali |
23 |
import com.spice.profitmandi.dao.entity.user.Address;
|
| 28480 |
amit.gupta |
24 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
| 22014 |
ashik.ali |
25 |
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
|
| 22216 |
ashik.ali |
26 |
import com.spice.profitmandi.dao.repository.dtr.GadgetCopsInsuranceCalcRepository;
|
|
|
27 |
import com.spice.profitmandi.dao.repository.dtr.InsuranceProviderRepository;
|
| 21801 |
ashik.ali |
28 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;
|
| 22251 |
ashik.ali |
29 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
| 21801 |
ashik.ali |
30 |
import com.spice.profitmandi.dao.repository.user.AddressRepository;
|
| 28403 |
amit.gupta |
31 |
import com.spice.profitmandi.service.scheme.SchemeService;
|
| 21801 |
ashik.ali |
32 |
import com.spice.profitmandi.service.tag.TagService;
|
|
|
33 |
|
| 21895 |
ashik.ali |
34 |
@Component
|
| 21801 |
ashik.ali |
35 |
public class PricingServiceImpl implements PricingService{
|
|
|
36 |
|
|
|
37 |
@Autowired
|
| 22925 |
ashik.ali |
38 |
private RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
|
| 21801 |
ashik.ali |
39 |
|
|
|
40 |
@Autowired
|
| 22925 |
ashik.ali |
41 |
private AddressRepository addressRepository;
|
| 21801 |
ashik.ali |
42 |
|
|
|
43 |
@Autowired
|
| 28480 |
amit.gupta |
44 |
private ItemRepository itemRepository;
|
|
|
45 |
|
|
|
46 |
@Autowired
|
| 28403 |
amit.gupta |
47 |
private SchemeService schemeService;
|
|
|
48 |
|
|
|
49 |
@Autowired
|
| 22925 |
ashik.ali |
50 |
private TagService tagService;
|
| 21801 |
ashik.ali |
51 |
|
|
|
52 |
@Autowired
|
| 22925 |
ashik.ali |
53 |
private TagListingRepository tagListingRepository;
|
| 21801 |
ashik.ali |
54 |
|
|
|
55 |
@Autowired
|
| 22925 |
ashik.ali |
56 |
private InsuranceProviderRepository insuranceProviderRepository;
|
| 21801 |
ashik.ali |
57 |
|
|
|
58 |
@Autowired
|
| 22925 |
ashik.ali |
59 |
private GadgetCopsInsuranceCalcRepository gadgetCopsInsuranceCalcRepository;
|
| 21801 |
ashik.ali |
60 |
|
| 22216 |
ashik.ali |
61 |
@Autowired
|
| 23781 |
ashik.ali |
62 |
@Qualifier("fofoInventoryItemRepository")
|
| 22925 |
ashik.ali |
63 |
private InventoryItemRepository inventoryItemRepository;
|
| 22216 |
ashik.ali |
64 |
|
| 23568 |
govind |
65 |
private static final Logger LOGGER = LogManager.getLogger(PricingServiceImpl.class);
|
| 22353 |
ashik.ali |
66 |
|
| 21801 |
ashik.ali |
67 |
@Override
|
| 22216 |
ashik.ali |
68 |
public Map<Integer, Float> getPrices(Set<Integer> itemIds, int retailerId) {
|
| 22580 |
ashik.ali |
69 |
Map<Integer, Float> itemIdPrice = this.preparePriceDefaultValues(itemIds);
|
|
|
70 |
List<TagListing> tagListings = this.getTagListing(itemIds, retailerId);
|
|
|
71 |
for(TagListing tagListing : tagListings){
|
|
|
72 |
if(itemIdPrice.get(tagListing.getItemId()) > tagListing.getSellingPrice()){
|
|
|
73 |
itemIdPrice.put(tagListing.getItemId(), tagListing.getSellingPrice());
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
return itemIdPrice;
|
| 22101 |
ashik.ali |
77 |
}
|
|
|
78 |
|
|
|
79 |
private Map<Integer, Float> preparePriceDefaultValues(Set<Integer> itemIds){
|
|
|
80 |
Map<Integer, Float> itemIdPrice = new HashMap<>(itemIds.size());
|
|
|
81 |
for(int itemId : itemIds){
|
|
|
82 |
itemIdPrice.put(itemId, Float.MAX_VALUE);
|
|
|
83 |
}
|
|
|
84 |
return itemIdPrice;
|
|
|
85 |
}
|
|
|
86 |
|
| 22580 |
ashik.ali |
87 |
private Map<Integer, PriceModel> preparePriceModelDefaultValues(Set<Integer> itemIds){
|
|
|
88 |
Map<Integer, PriceModel> itemIdPrice = new HashMap<>(itemIds.size());
|
|
|
89 |
for(int itemId : itemIds){
|
|
|
90 |
PriceModel priceModel = new PriceModel();
|
|
|
91 |
priceModel.setPrice(Float.MAX_VALUE);
|
|
|
92 |
itemIdPrice.put(itemId, priceModel);
|
|
|
93 |
}
|
|
|
94 |
return itemIdPrice;
|
|
|
95 |
}
|
|
|
96 |
|
| 22101 |
ashik.ali |
97 |
@SuppressWarnings("unchecked")
|
| 22580 |
ashik.ali |
98 |
private List<TagListing> getTagListing(Set<Integer> itemIds, int retailerId){
|
|
|
99 |
Map<Integer, TagListing> itemIdTagListing = new HashMap<>();
|
|
|
100 |
List<TagListing> tagListings = new ArrayList<>();
|
| 21801 |
ashik.ali |
101 |
try {
|
| 23384 |
ashik.ali |
102 |
if(itemIds.isEmpty()) {
|
|
|
103 |
return tagListings;
|
|
|
104 |
}
|
| 22580 |
ashik.ali |
105 |
int addressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailerId);
|
| 21801 |
ashik.ali |
106 |
Address retailerAddress = addressRepository.selectById(addressId);
|
|
|
107 |
Set<Integer> pinCodeTagIds = tagService.getFofoTagIdsByPinCode(retailerAddress.getPinCode());
|
| 22009 |
ashik.ali |
108 |
Set<Integer> pinNegativeTagIds = tagService.getPinCodeNegativeTagIds();
|
|
|
109 |
Set<Integer> pinCodeIntersection = new HashSet<>(CollectionUtils.intersection(pinCodeTagIds, pinNegativeTagIds));
|
|
|
110 |
|
| 22580 |
ashik.ali |
111 |
|
| 22009 |
ashik.ali |
112 |
Set<Integer> filterPinCodeTagIds = new HashSet<>(CollectionUtils.subtract(pinNegativeTagIds, pinCodeIntersection));
|
| 21867 |
ashik.ali |
113 |
Set<Integer> pinPositiveTagIds = tagService.getPinCodePositiveTagIds();
|
| 22009 |
ashik.ali |
114 |
filterPinCodeTagIds.addAll(pinPositiveTagIds);
|
| 21867 |
ashik.ali |
115 |
if(filterPinCodeTagIds.isEmpty()){
|
| 22580 |
ashik.ali |
116 |
return tagListings;
|
| 21867 |
ashik.ali |
117 |
}
|
| 22009 |
ashik.ali |
118 |
Set<Integer> retailerTagIds = tagService.getUserTagIdsByTagIds(filterPinCodeTagIds);
|
|
|
119 |
Set<Integer> userNegativeTagIds = tagService.getUserNegativeTagIds();
|
|
|
120 |
Set<Integer> userIntersection = new HashSet<>(CollectionUtils.intersection(retailerTagIds, userNegativeTagIds));
|
|
|
121 |
Set<Integer> filterUserTagIds = new HashSet<>(CollectionUtils.subtract(userNegativeTagIds, userIntersection));
|
| 21867 |
ashik.ali |
122 |
Set<Integer> userPositiveTagIds = tagService.getUserPositiveTagIds();
|
| 22009 |
ashik.ali |
123 |
filterUserTagIds.addAll(userPositiveTagIds);
|
| 21867 |
ashik.ali |
124 |
if(filterUserTagIds.isEmpty()){
|
| 22580 |
ashik.ali |
125 |
return tagListings;
|
| 21867 |
ashik.ali |
126 |
}
|
| 22580 |
ashik.ali |
127 |
tagListings = tagListingRepository.selectByItemIdsAndTagIds(itemIds, filterUserTagIds);
|
| 21926 |
ashik.ali |
128 |
|
| 21801 |
ashik.ali |
129 |
if(tagListings.isEmpty()){
|
| 22580 |
ashik.ali |
130 |
return tagListings;
|
| 21801 |
ashik.ali |
131 |
}
|
|
|
132 |
for(TagListing tagListing : tagListings){
|
| 22591 |
ashik.ali |
133 |
if(!itemIdTagListing.containsKey(tagListing.getItemId()))
|
|
|
134 |
{
|
| 22580 |
ashik.ali |
135 |
itemIdTagListing.put(tagListing.getItemId(), tagListing);
|
| 22591 |
ashik.ali |
136 |
}else{
|
|
|
137 |
if(itemIdTagListing.get(tagListing.getItemId()).getSellingPrice() > tagListing.getSellingPrice()){
|
|
|
138 |
itemIdTagListing.put(tagListing.getItemId(), tagListing);
|
|
|
139 |
}
|
| 21801 |
ashik.ali |
140 |
}
|
|
|
141 |
}
|
|
|
142 |
} catch (ProfitMandiBusinessException e) {
|
|
|
143 |
e.printStackTrace();
|
|
|
144 |
}
|
| 22580 |
ashik.ali |
145 |
return tagListings;
|
| 21801 |
ashik.ali |
146 |
}
|
|
|
147 |
|
| 22101 |
ashik.ali |
148 |
@Override
|
| 22580 |
ashik.ali |
149 |
public Map<Integer, PriceModel> getMopPrices(Set<Integer> itemIds, int retailerId) {
|
| 28403 |
amit.gupta |
150 |
//Set Price with max value
|
|
|
151 |
Map<Integer, PriceModel> itemPriceMap = this.preparePriceModelDefaultValues(itemIds);
|
| 22580 |
ashik.ali |
152 |
List<TagListing> tagListings = this.getTagListing(itemIds, retailerId);
|
|
|
153 |
for(TagListing tagListing : tagListings){
|
| 28480 |
amit.gupta |
154 |
Item item = null;
|
|
|
155 |
try {
|
|
|
156 |
item = itemRepository.selectById(tagListing.getItemId());
|
|
|
157 |
} catch (Exception e) {
|
|
|
158 |
LOGGER.info("Could not find item {}", tagListing.getItemId());
|
|
|
159 |
continue;
|
|
|
160 |
}
|
| 28403 |
amit.gupta |
161 |
PriceModel priceModel = itemPriceMap.get(tagListing.getItemId());
|
|
|
162 |
//In case of hot deal mop is set to retailer buying price
|
| 28480 |
amit.gupta |
163 |
priceModel.setPurchasePrice(tagListing.getSellingPrice());
|
|
|
164 |
priceModel.setMrp(tagListing.getMrp());
|
| 25134 |
amit.gupta |
165 |
if(tagListing.isHotDeals()) {
|
|
|
166 |
priceModel.setMop(true);
|
| 28403 |
amit.gupta |
167 |
priceModel.setPrice(tagListing.getSellingPrice());
|
|
|
168 |
} else if(itemPriceMap.get(tagListing.getItemId()).getPrice() > tagListing.getMop()){
|
| 25134 |
amit.gupta |
169 |
priceModel.setPrice(tagListing.getMop());
|
|
|
170 |
priceModel.setMop(true);
|
| 28403 |
amit.gupta |
171 |
if(schemeService.getItemSchemeCashBack().get(tagListing.getItemId())!= null) {
|
|
|
172 |
priceModel.setMaxDiscountAmount(schemeService.getItemSchemeCashBack().get(tagListing.getItemId()));
|
|
|
173 |
}
|
| 28480 |
amit.gupta |
174 |
} else if (item.getHsnCode().equals("85171210")) {
|
|
|
175 |
priceModel.setMop(false);
|
|
|
176 |
priceModel.setPrice(tagListing.getSellingPrice());
|
| 22580 |
ashik.ali |
177 |
}
|
|
|
178 |
}
|
| 28403 |
amit.gupta |
179 |
return itemPriceMap;
|
| 21926 |
ashik.ali |
180 |
}
|
| 22216 |
ashik.ali |
181 |
|
|
|
182 |
@Override
|
|
|
183 |
public Map<Float, GadgetCopsInsuranceCalc> getInsurancePrices(Set<Float> prices, String providerName) throws ProfitMandiBusinessException{
|
|
|
184 |
insuranceProviderRepository.selectByName(providerName);
|
|
|
185 |
Map<Float, GadgetCopsInsuranceCalc> pricesMap = new HashMap<>();
|
|
|
186 |
if(ProfitMandiConstants.GADGET_COPS.equals(providerName)){
|
|
|
187 |
List<GadgetCopsInsuranceCalc> gadgetCopsInsuranceCalcs = gadgetCopsInsuranceCalcRepository.selectAll();
|
|
|
188 |
for(GadgetCopsInsuranceCalc gadgetCopsInsuranceCalc : gadgetCopsInsuranceCalcs){
|
|
|
189 |
for(float price : prices){
|
|
|
190 |
if(gadgetCopsInsuranceCalc.getPriceRangeMin()<= price && gadgetCopsInsuranceCalc.getPriceRangeMax() >= price){
|
|
|
191 |
pricesMap.put(price, gadgetCopsInsuranceCalc);
|
|
|
192 |
}
|
|
|
193 |
}
|
|
|
194 |
}
|
|
|
195 |
}else{
|
| 23781 |
ashik.ali |
196 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.PROVIDER_NAME, providerName, "INSRNC_PRVDR_1000");
|
| 22216 |
ashik.ali |
197 |
}
|
|
|
198 |
return pricesMap;
|
|
|
199 |
}
|
| 22251 |
ashik.ali |
200 |
|
| 22243 |
ashik.ali |
201 |
@Override
|
| 22580 |
ashik.ali |
202 |
public Map<Integer, PriceModel> getPurchasePriceMopPriceNotFound(Set<Integer> itemIds, int retailerId) {
|
| 28403 |
amit.gupta |
203 |
return this.getMopPrices(itemIds, retailerId);
|
| 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 |
}
|