Subversion Repositories SmartDukaan

Rev

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