Subversion Repositories SmartDukaan

Rev

Rev 26631 | Rev 26648 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
26607 amit.gupta 1
package com.spice.profitmandi.web.controller;
2
 
26628 amit.gupta 3
import java.time.LocalTime;
26607 amit.gupta 4
import java.util.ArrayList;
5
import java.util.Arrays;
6
import java.util.HashMap;
7
import java.util.HashSet;
8
import java.util.List;
9
import java.util.Map;
10
import java.util.Set;
11
import java.util.stream.Collectors;
12
 
13
import javax.servlet.http.HttpServletRequest;
14
 
15
import org.apache.commons.lang3.StringUtils;
16
import org.apache.http.conn.HttpHostConnectException;
17
import org.apache.logging.log4j.LogManager;
18
import org.apache.logging.log4j.Logger;
19
import org.json.JSONArray;
20
import org.json.JSONObject;
21
import org.springframework.beans.factory.annotation.Autowired;
22
import org.springframework.beans.factory.annotation.Value;
23
import org.springframework.http.MediaType;
24
import org.springframework.http.ResponseEntity;
25
import org.springframework.stereotype.Controller;
26
import org.springframework.transaction.annotation.Transactional;
27
import org.springframework.web.bind.annotation.RequestBody;
28
import org.springframework.web.bind.annotation.RequestMapping;
29
import org.springframework.web.bind.annotation.RequestMethod;
30
import org.springframework.web.bind.annotation.RequestParam;
31
 
32
import com.eclipsesource.json.JsonObject;
33
import com.google.gson.Gson;
34
import com.google.gson.reflect.TypeToken;
35
import com.spice.profitmandi.common.enumuration.SchemeType;
36
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
37
import com.spice.profitmandi.common.model.ProfitMandiConstants;
38
import com.spice.profitmandi.common.model.UserInfo;
39
import com.spice.profitmandi.common.solr.SolrService;
40
import com.spice.profitmandi.common.web.client.RestClient;
41
import com.spice.profitmandi.common.web.util.ResponseSender;
42
import com.spice.profitmandi.dao.entity.catalog.Item;
43
import com.spice.profitmandi.dao.entity.catalog.TagListing;
44
import com.spice.profitmandi.dao.entity.fofo.CurrentInventorySnapshot;
45
import com.spice.profitmandi.dao.entity.inventory.ItemAvailabilityCache;
26630 amit.gupta 46
import com.spice.profitmandi.dao.enumuration.dtr.OtpType;
26607 amit.gupta 47
import com.spice.profitmandi.dao.model.AddCartRequest;
48
import com.spice.profitmandi.dao.model.CartItem;
49
import com.spice.profitmandi.dao.model.CartItemResponseModel;
50
import com.spice.profitmandi.dao.model.CartResponse;
51
import com.spice.profitmandi.dao.repository.catalog.CategoryRepository;
52
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
53
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
54
import com.spice.profitmandi.dao.repository.dtr.Mongo;
55
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
56
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
57
import com.spice.profitmandi.dao.repository.inventory.ItemAvailabilityCacheRepository;
58
import com.spice.profitmandi.service.authentication.RoleManager;
59
import com.spice.profitmandi.service.inventory.FofoAvailabilityInfo;
60
import com.spice.profitmandi.service.inventory.FofoCatalogResponse;
61
import com.spice.profitmandi.service.pricing.PricingService;
26630 amit.gupta 62
import com.spice.profitmandi.web.processor.OtpProcessor;
26607 amit.gupta 63
import com.spice.profitmandi.web.res.DealBrands;
64
import com.spice.profitmandi.web.res.DealObjectResponse;
65
import com.spice.profitmandi.web.res.DealsResponse;
66
import com.spice.profitmandi.web.res.ValidateCartResponse;
67
 
68
import io.swagger.annotations.ApiImplicitParam;
69
import io.swagger.annotations.ApiImplicitParams;
70
import io.swagger.annotations.ApiOperation;
71
 
72
@Controller
73
@Transactional(rollbackFor = Throwable.class)
74
public class StoreController {
75
 
76
	private static final Logger logger = LogManager.getLogger(StoreController.class);
26630 amit.gupta 77
 
26628 amit.gupta 78
	private static final LocalTime CUTOFF_TIME = LocalTime.of(15, 0);
26607 amit.gupta 79
 
80
	@Value("${python.api.host}")
81
	private String host;
82
 
83
	@Value("${python.api.port}")
84
	private int port;
85
 
86
	// This is now unused as we are not supporting multiple companies.
87
	@Value("${gadgetCops.invoice.cc}")
88
	private String[] ccGadgetCopInvoiceTo;
89
 
90
	@Autowired
91
	private PricingService pricingService;
92
 
93
	@Autowired
94
	private CategoryRepository categoryRepository;
95
 
96
	@Autowired
97
	private SolrService commonSolrService;
98
 
99
	@Autowired
100
	private Mongo mongoClient;
101
 
102
	@Autowired
26630 amit.gupta 103
	private OtpProcessor otpProcessor;
104
 
105
	@Autowired
26607 amit.gupta 106
	private CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
107
 
26609 amit.gupta 108
	@Autowired
26607 amit.gupta 109
	private UserAccountRepository userAccountRepository;
110
 
111
	@Autowired
112
	private ResponseSender<?> responseSender;
113
 
114
	@Autowired
115
	private TagListingRepository tagListingRepository;
116
 
117
	@Autowired
118
	private ItemRepository itemRepository;
119
 
120
	@Autowired
121
	private ItemAvailabilityCacheRepository itemAvailabilityCacheRepository;
122
 
123
	@Autowired
124
	private RoleManager roleManagerService;
125
 
126
	List<String> filterableParams = Arrays.asList("brand");
127
 
128
	@ApiImplicitParams({
129
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
130
	@RequestMapping(value = "/store/fofo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
131
	public ResponseEntity<?> getFofo(HttpServletRequest request,
132
			@RequestParam(value = "categoryId", required = false, defaultValue = "(3 OR 6)") String categoryId,
133
			@RequestParam(value = "offset") String offset, @RequestParam(value = "limit") String limit,
134
			@RequestParam(value = "sort", required = false) String sort,
135
			@RequestParam(value = "brand", required = false) String brand,
136
			@RequestParam(value = "subCategoryId", required = false) int subCategoryId,
137
			@RequestParam(value = "q", required = false) String queryTerm,
138
			@RequestParam(value = "hotDeal", required = false) boolean hotDeal) throws Throwable {
139
		List<FofoCatalogResponse> dealResponse = new ArrayList<>();
140
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
141
		if (roleManagerService.isPartner(userInfo.getRoleIds())) {
142
			// UserCart uc = userAccountRepository.getUserCart(userInfo.getUserId());
143
			List<Integer> tagIds = pricingService.getTagsIdsByRetailerId(userInfo.getRetailerId());
144
			RestClient rc = new RestClient();
145
			Map<String, String> params = new HashMap<>();
146
			List<String> mandatoryQ = new ArrayList<>();
147
			if (queryTerm != null && !queryTerm.equals("null")) {
148
				mandatoryQ.add(String.format("+(%s)", queryTerm));
149
			} else {
150
				queryTerm = null;
151
			}
152
			if (subCategoryId != 0) {
153
				mandatoryQ
154
						.add(String.format("+(subCategoryId_i:%s) +{!parent which=\"subCategoryId_i:%s\"} tagId_i:(%s)",
155
								subCategoryId, subCategoryId, StringUtils.join(tagIds, " ")));
156
			} else if (hotDeal) {
157
				mandatoryQ.add(String.format("+{!parent which=\"hot_deals_b=true\"} tagId_i:(%s)",
158
						StringUtils.join(tagIds, " ")));
159
 
160
			} else if (StringUtils.isNotBlank(brand)) {
161
				mandatoryQ.add(
162
						String.format("+(categoryId_i:%s) +(brand_ss:%s) +{!parent which=\"brand_ss:%s\"} tagId_i:(%s)",
163
								categoryId, brand, brand, StringUtils.join(tagIds, " ")));
164
 
165
			} else {
166
				mandatoryQ.add(
167
						String.format("+{!parent which=\"id:catalog*\"} tagId_i:(%s)", StringUtils.join(tagIds, " ")));
168
			}
169
			params.put("q", StringUtils.join(mandatoryQ, " "));
170
			params.put("fl", "*, [child parentFilter=id:catalog*]");
171
			if (queryTerm == null) {
172
				params.put("sort", "create_s desc");
173
			}
174
			params.put("start", String.valueOf(offset));
175
			params.put("rows", String.valueOf(limit));
176
			params.put("wt", "json");
177
			String response = null;
178
			try {
179
				response = rc.get(SchemeType.HTTP, "50.116.10.120", 8984, "solr/demo/select", params);
180
			} catch (HttpHostConnectException e) {
181
				throw new ProfitMandiBusinessException("", "", "Could not connect to host");
182
			}
183
			JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
184
			JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
185
			dealResponse = getCatalogResponse(docs, hotDeal);
186
			/*
187
			 * if (Mongo.PARTNER_BLoCKED_BRANDS.containsKey(userInfo.getEmail())) {
188
			 * dealResponse.stream() .filter(x ->
189
			 * Mongo.PARTNER_BLoCKED_BRANDS.get(userInfo.getEmail()).contains(x.getBrand()))
190
			 * ; }
191
			 */
192
		} else {
193
			return responseSender.badRequest(
194
					new ProfitMandiBusinessException("Retailer id", userInfo.getUserId(), "NOT_FOFO_RETAILER"));
195
		}
196
		return responseSender.ok(dealResponse);
197
	}
198
 
199
	private Object toDealObject(JsonObject jsonObject) {
200
		if (jsonObject.get("dealObject") != null && jsonObject.get("dealObject").asInt() == 1) {
201
			return new Gson().fromJson(jsonObject.toString(), DealObjectResponse.class);
202
		}
203
		return new Gson().fromJson(jsonObject.toString(), DealsResponse.class);
204
	}
205
 
206
	@RequestMapping(value = "/store/brands", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
207
	@ApiImplicitParams({
208
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
209
	@ApiOperation(value = "Get brand list and count for category")
210
	public ResponseEntity<?> getBrands(HttpServletRequest request,
211
			@RequestParam(value = "category_id") String category_id) throws ProfitMandiBusinessException {
212
		logger.info("Request " + request.getParameterMap());
213
		String response = null;
214
		// TODO: move to properties
215
		String uri = ProfitMandiConstants.URL_BRANDS;
216
		RestClient rc = new RestClient();
217
		Map<String, String> params = new HashMap<>();
218
		params.put("category_id", category_id);
219
		List<DealBrands> dealBrandsResponse = null;
220
		try {
221
			response = rc.get(SchemeType.HTTP, host, port, uri, params);
222
		} catch (HttpHostConnectException e) {
223
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
224
		}
225
 
226
		dealBrandsResponse = new Gson().fromJson(response, new TypeToken<List<DealBrands>>() {
227
		}.getType());
228
 
229
		return responseSender.ok(dealBrandsResponse);
230
	}
231
 
26632 amit.gupta 232
	@RequestMapping(value = "/store/otp/generateOTP", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
26630 amit.gupta 233
	public ResponseEntity<?> cart(HttpServletRequest request, @RequestParam String email, @RequestParam String phone)
234
			throws Exception {
235
 
236
		return responseSender.ok(otpProcessor.generateOtp(email, phone, OtpType.PREBOOKING_ORDER));
237
 
238
	}
239
 
26610 amit.gupta 240
	@RequestMapping(value = "/store/cart", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
26607 amit.gupta 241
	@ApiImplicitParams({
242
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
243
	@ApiOperation(value = "Get brand list and count for category")
244
	public ResponseEntity<?> cart(HttpServletRequest request, @RequestBody AddCartRequest cartRequest)
245
			throws Exception {
246
		CartResponse cartResponse = new CartResponse();
26612 amit.gupta 247
		List<CartItemResponseModel> cartItemResponseModels = new ArrayList<>();
248
		cartResponse.setCartItems(cartItemResponseModels);
26620 amit.gupta 249
 
26607 amit.gupta 250
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
251
		Integer storeId = userInfo.getRetailerId();
252
		List<Integer> itemIds = cartRequest.getCartItems().stream().map(x -> x.getItemId())
253
				.collect(Collectors.toList());
254
		Set<Integer> itemsIdsSet = new HashSet<>(itemIds);
255
		List<CurrentInventorySnapshot> currentInventorySnapshot = currentInventorySnapshotRepository
256
				.selectByFofoItemIds(storeId, itemsIdsSet);
257
		Map<Integer, Integer> storeItemAvailabilityMap = currentInventorySnapshot.stream()
26620 amit.gupta 258
				.filter(x -> x.getAvailability() > 0)
26607 amit.gupta 259
				.collect(Collectors.toMap(x -> x.getItemId(), x -> x.getAvailability()));
260
 
261
		Map<Integer, Item> itemsMap = itemRepository.selectByIds(itemsIdsSet).stream()
262
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
263
 
264
		Map<Integer, TagListing> sdItemAvailabilityMap = tagListingRepository
265
				.selectByItemIdsAndTagIds(new HashSet<>(itemIds), new HashSet<>(Arrays.asList(4))).stream()
266
				.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
267
 
268
		List<Integer> catalogIds = itemsMap.values().stream().map(x -> x.getCatalogItemId())
269
				.collect(Collectors.toList());
270
 
271
		Map<Integer, JSONObject> contentMap = commonSolrService.getContentByCatalogIds(catalogIds);
272
 
273
		// cartResponse.getCartItems()
274
		for (CartItem cartItem : cartRequest.getCartItems()) {
26620 amit.gupta 275
			if (cartItem.getQuantity() == 0) {
26617 amit.gupta 276
				continue;
277
			}
26607 amit.gupta 278
			Item item = itemsMap.get(cartItem.getItemId());
279
			TagListing tagListing = sdItemAvailabilityMap.get(cartItem.getItemId());
280
			CartItemResponseModel cartItemResponseModel = new CartItemResponseModel();
26628 amit.gupta 281
			int estimate = -2;
26607 amit.gupta 282
			if (storeItemAvailabilityMap.containsKey(cartItem.getItemId())) {
283
				if (storeItemAvailabilityMap.get(cartItem.getItemId()) >= cartItem.getQuantity()) {
26628 amit.gupta 284
					estimate = 0;
26607 amit.gupta 285
				} else if (tagListing.isActive()) {
26628 amit.gupta 286
					estimate = 2;
26607 amit.gupta 287
				} else {
26628 amit.gupta 288
					estimate = -2;
26607 amit.gupta 289
				}
26620 amit.gupta 290
			} else if (tagListing.isActive()) {
26628 amit.gupta 291
				estimate = 2;
26620 amit.gupta 292
			} else {
26628 amit.gupta 293
				estimate = -2;
26607 amit.gupta 294
			}
26630 amit.gupta 295
			if (estimate >= 0 && LocalTime.now().isAfter(CUTOFF_TIME)) {
26628 amit.gupta 296
				estimate = estimate + 1;
297
			}
298
			cartItemResponseModel.setEstimate(estimate);
26616 amit.gupta 299
			cartItemResponseModel.setTitle(item.getItemDescriptionNoColor());
26614 amit.gupta 300
			cartItemResponseModel.setItemId(cartItem.getItemId());
26615 amit.gupta 301
			cartItemResponseModel.setMinBuyQuantity(1);
302
			cartItemResponseModel.setQuantity(cartItem.getQuantity());
26621 amit.gupta 303
			cartItemResponseModel.setQuantityStep(1);
26607 amit.gupta 304
			cartItemResponseModel.setSellingPrice(tagListing.getMop());
305
			cartItemResponseModel.setMaxQuantity(10);
306
			cartItemResponseModel.setCatalogItemId(item.getCatalogItemId());
307
			cartItemResponseModel.setImageUrl(contentMap.get(item.getCatalogItemId()).getString("imageUrl_s"));
308
			cartItemResponseModel.setColor(item.getColor());
26620 amit.gupta 309
			cartItemResponseModels.add(cartItemResponseModel);
26607 amit.gupta 310
		}
311
		ValidateCartResponse vc = new ValidateCartResponse(cartResponse, "Success", "Items added to cart successfully");
312
		return responseSender.ok(vc);
313
	}
314
 
315
	private List<FofoCatalogResponse> getCatalogResponse(JSONArray docs, boolean hotDeal)
316
			throws ProfitMandiBusinessException {
317
		Map<Integer, TagListing> itemTagListingMap = null;
318
		List<FofoCatalogResponse> dealResponse = new ArrayList<>();
319
		List<Integer> tagIds = Arrays.asList(4);
320
		if (docs.length() > 0) {
321
			HashSet<Integer> itemsSet = new HashSet<>();
322
			for (int i = 0; i < docs.length(); i++) {
323
				JSONObject doc = docs.getJSONObject(i);
324
				if (doc.has("_childDocuments_")) {
325
					for (int j = 0; j < doc.getJSONArray("_childDocuments_").length(); j++) {
326
						JSONObject childItem = doc.getJSONArray("_childDocuments_").getJSONObject(j);
327
						int itemId = childItem.getInt("itemId_i");
328
						itemsSet.add(itemId);
329
					}
330
				}
331
			}
332
			if (itemsSet.size() == 0) {
333
				return dealResponse;
334
			}
335
			itemTagListingMap = tagListingRepository.selectByItemIdsAndTagIds(itemsSet, new HashSet<>(tagIds)).stream()
336
					.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
337
		}
338
 
339
		for (int i = 0; i < docs.length(); i++) {
340
			Map<Integer, FofoAvailabilityInfo> fofoAvailabilityInfoMap = new HashMap<>();
341
			JSONObject doc = docs.getJSONObject(i);
342
			FofoCatalogResponse ffdr = new FofoCatalogResponse();
343
			ffdr.setCatalogId(doc.getInt("catalogId_i"));
344
			ffdr.setImageUrl(doc.getString("imageUrl_s"));
345
			ffdr.setTitle(doc.getString("title_s"));
346
			try {
347
				ffdr.setFeature(doc.getString("feature_s"));
348
			} catch (Exception e) {
349
				ffdr.setFeature(null);
350
				logger.info("Could not find Feature_s for {}", ffdr.getCatalogId());
351
			}
352
			ffdr.setBrand(doc.getJSONArray("brand_ss").getString(0));
353
			if (doc.has("_childDocuments_")) {
354
				for (int j = 0; j < doc.getJSONArray("_childDocuments_").length(); j++) {
355
					JSONObject childItem = doc.getJSONArray("_childDocuments_").getJSONObject(j);
356
					int itemId = childItem.getInt("itemId_i");
357
					TagListing tl = itemTagListingMap.get(itemId);
358
					if (tl == null) {
359
						logger.warn("Could not find item id {}", itemId);
360
						continue;
361
					}
362
					if (hotDeal) {
363
						if (!tl.isHotDeals()) {
364
							continue;
365
						}
366
					}
367
					float sellingPrice = (float) childItem.getDouble("sellingPrice_f");
368
					if (fofoAvailabilityInfoMap.containsKey(itemId)) {
369
						if (fofoAvailabilityInfoMap.get(itemId).getSellingPrice() > sellingPrice) {
370
							fofoAvailabilityInfoMap.get(itemId).setSellingPrice(sellingPrice);
371
							fofoAvailabilityInfoMap.get(itemId).setMop((float) childItem.getDouble("mop_f"));
372
						}
373
					} else {
374
						FofoAvailabilityInfo fdi = new FofoAvailabilityInfo();
375
						fdi.setSellingPrice((float) childItem.getDouble("sellingPrice_f"));
376
						fdi.setMop((float) childItem.getDouble("mop_f"));
377
						fdi.setColor(childItem.has("color_s") ? childItem.getString("color_s") : "");
378
						fdi.setTagId(childItem.getInt("tagId_i"));
379
						fdi.setItem_id(itemId);
380
						Item item = itemRepository.selectById(itemId);
381
						// In case its tampered glass moq should be 5
382
						if (item.getCategoryId() == 10020) {
383
							fdi.setMinBuyQuantity(10);
384
						} else {
385
							fdi.setMinBuyQuantity(1);
386
						}
387
						if (hotDeal || !tl.isActive()) {
388
 
389
							int totalAvailability = 0; // Using item availability
390
							// cache for now but can be
391
							// changed to
392
							// use caching later.
393
							try {
394
								ItemAvailabilityCache iac = itemAvailabilityCacheRepository.selectByItemId(itemId);
395
								totalAvailability = iac.getTotalAvailability();
396
								fdi.setAvailability(totalAvailability);
397
							} catch (Exception e) {
398
								continue;
399
							}
400
							if (totalAvailability <= 0) {
401
								continue;
402
							}
403
						} else {
404
							// For accessories item availability should at be ordered for Rs.1000
405
							fdi.setAvailability(100);
406
						}
407
						fdi.setQuantityStep(1);
408
						fdi.setMaxQuantity(Math.min(fdi.getAvailability(), 100));
409
						fofoAvailabilityInfoMap.put(itemId, fdi);
410
					}
411
				}
412
			}
413
			if (fofoAvailabilityInfoMap.values().size() > 0) {
414
				ffdr.setItems(new ArrayList<FofoAvailabilityInfo>(fofoAvailabilityInfoMap.values()));
415
				dealResponse.add(ffdr);
416
			}
417
		}
418
		return dealResponse;
419
 
420
	}
421
 
422
}