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