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