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