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