| 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;
|
| 37640 |
amit |
11 |
import com.spice.profitmandi.dao.repository.catalog.CatalogRepository;
|
| 22014 |
ashik.ali |
12 |
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
|
| 22216 |
ashik.ali |
13 |
import com.spice.profitmandi.dao.repository.dtr.GadgetCopsInsuranceCalcRepository;
|
|
|
14 |
import com.spice.profitmandi.dao.repository.dtr.InsuranceProviderRepository;
|
| 21801 |
ashik.ali |
15 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;
|
| 22251 |
ashik.ali |
16 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
| 32972 |
amit.gupta |
17 |
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
|
| 21801 |
ashik.ali |
18 |
import com.spice.profitmandi.dao.repository.user.AddressRepository;
|
| 28403 |
amit.gupta |
19 |
import com.spice.profitmandi.service.scheme.SchemeService;
|
| 21801 |
ashik.ali |
20 |
import com.spice.profitmandi.service.tag.TagService;
|
| 30121 |
amit.gupta |
21 |
import org.apache.commons.collections.CollectionUtils;
|
|
|
22 |
import org.apache.logging.log4j.LogManager;
|
|
|
23 |
import org.apache.logging.log4j.Logger;
|
|
|
24 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
25 |
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
26 |
import org.springframework.stereotype.Component;
|
| 21801 |
ashik.ali |
27 |
|
| 37283 |
amit |
28 |
import java.time.LocalDate;
|
| 30121 |
amit.gupta |
29 |
import java.util.*;
|
|
|
30 |
|
| 21895 |
ashik.ali |
31 |
@Component
|
| 21801 |
ashik.ali |
32 |
public class PricingServiceImpl implements PricingService{
|
|
|
33 |
|
|
|
34 |
@Autowired
|
| 22925 |
ashik.ali |
35 |
private RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
|
| 21801 |
ashik.ali |
36 |
|
|
|
37 |
@Autowired
|
| 22925 |
ashik.ali |
38 |
private AddressRepository addressRepository;
|
| 21801 |
ashik.ali |
39 |
|
|
|
40 |
@Autowired
|
| 28480 |
amit.gupta |
41 |
private ItemRepository itemRepository;
|
| 37283 |
amit |
42 |
|
|
|
43 |
@Autowired
|
| 37640 |
amit |
44 |
private CatalogRepository catalogRepository;
|
| 28480 |
amit.gupta |
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 |
}
|
| 32418 |
amit.gupta |
147 |
|
| 32972 |
amit.gupta |
148 |
@Autowired
|
|
|
149 |
PartnerTypeChangeService partnerTypeChangeService;
|
| 32420 |
amit.gupta |
150 |
|
| 32972 |
amit.gupta |
151 |
|
|
|
152 |
public static final List<Integer> mopVoilatedRetailerIds = Arrays.asList();
|
|
|
153 |
|
| 22101 |
ashik.ali |
154 |
@Override
|
| 32420 |
amit.gupta |
155 |
public List<Integer> getMopVoilatedRetailerIds() {
|
|
|
156 |
return mopVoilatedRetailerIds;
|
|
|
157 |
}
|
|
|
158 |
@Override
|
| 33247 |
ranu |
159 |
public Map<Integer, PriceModel> getMopPrices(Set<Integer> itemIds, int retailerId) throws ProfitMandiBusinessException {
|
| 28403 |
amit.gupta |
160 |
//Set Price with max value
|
|
|
161 |
Map<Integer, PriceModel> itemPriceMap = this.preparePriceModelDefaultValues(itemIds);
|
| 22580 |
ashik.ali |
162 |
List<TagListing> tagListings = this.getTagListing(itemIds, retailerId);
|
| 37640 |
amit |
163 |
//Hot deals are brand-driven: catalog.brand = 'Hot Deal'. No date window.
|
|
|
164 |
Set<Integer> hotDealCatalogIds = catalogRepository
|
|
|
165 |
.selectCatalogIdsByBrand(ProfitMandiConstants.HOT_DEAL_BRAND);
|
| 22580 |
ashik.ali |
166 |
for(TagListing tagListing : tagListings){
|
| 28480 |
amit.gupta |
167 |
Item item = null;
|
|
|
168 |
try {
|
|
|
169 |
item = itemRepository.selectById(tagListing.getItemId());
|
|
|
170 |
} catch (Exception e) {
|
|
|
171 |
LOGGER.info("Could not find item {}", tagListing.getItemId());
|
|
|
172 |
continue;
|
|
|
173 |
}
|
| 28403 |
amit.gupta |
174 |
PriceModel priceModel = itemPriceMap.get(tagListing.getItemId());
|
|
|
175 |
//In case of hot deal mop is set to retailer buying price
|
| 28480 |
amit.gupta |
176 |
priceModel.setPurchasePrice(tagListing.getSellingPrice());
|
|
|
177 |
priceModel.setMrp(tagListing.getMrp());
|
| 37283 |
amit |
178 |
if(hotDealCatalogIds.contains(item.getCatalogItemId())) {
|
| 25134 |
amit.gupta |
179 |
priceModel.setMop(true);
|
| 28403 |
amit.gupta |
180 |
priceModel.setPrice(tagListing.getSellingPrice());
|
| 31051 |
amit.gupta |
181 |
} else if (itemPriceMap.get(tagListing.getItemId()).getPrice() > tagListing.getMop()) {
|
| 25134 |
amit.gupta |
182 |
priceModel.setPrice(tagListing.getMop());
|
|
|
183 |
priceModel.setMop(true);
|
| 32972 |
amit.gupta |
184 |
Map<Integer, Float> catalogSchemeCashBack = schemeService.getCatalogSchemeCashBack(retailerId, Arrays.asList(item.getCatalogItemId()));
|
|
|
185 |
if(catalogSchemeCashBack.get(item.getCatalogItemId()) != null && catalogSchemeCashBack.get(item.getCatalogItemId()) > 0) {
|
|
|
186 |
priceModel.setMaxDiscountAmount(catalogSchemeCashBack.get(item.getCatalogItemId()));
|
| 28403 |
amit.gupta |
187 |
}
|
| 31051 |
amit.gupta |
188 |
} else if (item.getHsnCode().equals("85171400")) {
|
| 28480 |
amit.gupta |
189 |
priceModel.setMop(false);
|
|
|
190 |
priceModel.setPrice(tagListing.getSellingPrice());
|
| 22580 |
ashik.ali |
191 |
}
|
| 32672 |
amit.gupta |
192 |
if (mopVoilatedRetailerIds.contains(retailerId) || "almost new".equals(item.getBrand().toLowerCase())) {
|
| 32419 |
amit.gupta |
193 |
priceModel.setMop(false);
|
| 32418 |
amit.gupta |
194 |
}
|
| 22580 |
ashik.ali |
195 |
}
|
| 28403 |
amit.gupta |
196 |
return itemPriceMap;
|
| 21926 |
ashik.ali |
197 |
}
|
| 22216 |
ashik.ali |
198 |
|
|
|
199 |
@Override
|
|
|
200 |
public Map<Float, GadgetCopsInsuranceCalc> getInsurancePrices(Set<Float> prices, String providerName) throws ProfitMandiBusinessException{
|
|
|
201 |
insuranceProviderRepository.selectByName(providerName);
|
|
|
202 |
Map<Float, GadgetCopsInsuranceCalc> pricesMap = new HashMap<>();
|
|
|
203 |
if(ProfitMandiConstants.GADGET_COPS.equals(providerName)){
|
|
|
204 |
List<GadgetCopsInsuranceCalc> gadgetCopsInsuranceCalcs = gadgetCopsInsuranceCalcRepository.selectAll();
|
|
|
205 |
for(GadgetCopsInsuranceCalc gadgetCopsInsuranceCalc : gadgetCopsInsuranceCalcs){
|
|
|
206 |
for(float price : prices){
|
|
|
207 |
if(gadgetCopsInsuranceCalc.getPriceRangeMin()<= price && gadgetCopsInsuranceCalc.getPriceRangeMax() >= price){
|
|
|
208 |
pricesMap.put(price, gadgetCopsInsuranceCalc);
|
|
|
209 |
}
|
|
|
210 |
}
|
|
|
211 |
}
|
|
|
212 |
}else{
|
| 23781 |
ashik.ali |
213 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.PROVIDER_NAME, providerName, "INSRNC_PRVDR_1000");
|
| 22216 |
ashik.ali |
214 |
}
|
|
|
215 |
return pricesMap;
|
|
|
216 |
}
|
| 22251 |
ashik.ali |
217 |
|
| 22243 |
ashik.ali |
218 |
@Override
|
| 33247 |
ranu |
219 |
public Map<Integer, PriceModel> getPurchasePriceMopPriceNotFound(Set<Integer> itemIds, int retailerId) throws ProfitMandiBusinessException {
|
| 28403 |
amit.gupta |
220 |
return this.getMopPrices(itemIds, retailerId);
|
| 22243 |
ashik.ali |
221 |
}
|
| 22287 |
amit.gupta |
222 |
|
|
|
223 |
//Currently it only handles fofos
|
| 22580 |
ashik.ali |
224 |
@SuppressWarnings("unchecked")
|
| 33247 |
ranu |
225 |
public List<Integer> getTagsIdsByRetailerId(int retailerId) throws ProfitMandiBusinessException {
|
| 22362 |
amit.gupta |
226 |
Set<Integer> pinCodeTagIds = tagService.getFofoTagIdsByPinCode("110001");
|
| 22287 |
amit.gupta |
227 |
Set<Integer> pinNegativeTagIds = tagService.getPinCodeNegativeTagIds();
|
|
|
228 |
Set<Integer> pinCodeIntersection = new HashSet<>(CollectionUtils.intersection(pinCodeTagIds, pinNegativeTagIds));
|
|
|
229 |
|
|
|
230 |
Set<Integer> validTagIds = new HashSet<>(CollectionUtils.subtract(pinNegativeTagIds, pinCodeIntersection));
|
|
|
231 |
Set<Integer> pinPositiveTagIds = tagService.getPinCodePositiveTagIds();
|
|
|
232 |
validTagIds.addAll(CollectionUtils.intersection(pinPositiveTagIds, pinCodeTagIds));
|
|
|
233 |
if(validTagIds.isEmpty()){
|
|
|
234 |
return new ArrayList<>(validTagIds);
|
|
|
235 |
}
|
|
|
236 |
Set<Integer> retailerTagIds = tagService.getUserTagIdsByTagIds(validTagIds);
|
|
|
237 |
Set<Integer> userNegativeTagIds = tagService.getUserNegativeTagIds();
|
|
|
238 |
Set<Integer> userIntersection = new HashSet<>(CollectionUtils.intersection(retailerTagIds, userNegativeTagIds));
|
|
|
239 |
validTagIds = new HashSet<>(CollectionUtils.subtract(userNegativeTagIds, userIntersection));
|
|
|
240 |
Set<Integer> userPositiveTagIds = tagService.getUserPositiveTagIds();
|
|
|
241 |
validTagIds.addAll(CollectionUtils.intersection(userPositiveTagIds, retailerTagIds));
|
|
|
242 |
return new ArrayList<>(validTagIds);
|
|
|
243 |
}
|
| 21801 |
ashik.ali |
244 |
}
|