Subversion Repositories SmartDukaan

Rev

Rev 26608 | Rev 26610 | 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
 
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.HashMap;
6
import java.util.HashSet;
7
import java.util.Iterator;
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.PathVariable;
28
import org.springframework.web.bind.annotation.RequestBody;
29
import org.springframework.web.bind.annotation.RequestMapping;
30
import org.springframework.web.bind.annotation.RequestMethod;
31
import org.springframework.web.bind.annotation.RequestParam;
32
 
33
import com.eclipsesource.json.Json;
34
import com.eclipsesource.json.JsonObject;
35
import com.google.gson.Gson;
36
import com.google.gson.reflect.TypeToken;
37
import com.spice.profitmandi.common.enumuration.SchemeType;
38
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
39
import com.spice.profitmandi.common.model.ProfitMandiConstants;
40
import com.spice.profitmandi.common.model.UserInfo;
41
import com.spice.profitmandi.common.solr.SolrService;
42
import com.spice.profitmandi.common.web.client.RestClient;
43
import com.spice.profitmandi.common.web.util.ResponseSender;
44
import com.spice.profitmandi.dao.entity.catalog.Item;
45
import com.spice.profitmandi.dao.entity.catalog.TagListing;
46
import com.spice.profitmandi.dao.entity.fofo.CurrentInventorySnapshot;
47
import com.spice.profitmandi.dao.entity.inventory.ItemAvailabilityCache;
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.model.UserCart;
53
import com.spice.profitmandi.dao.repository.catalog.CategoryRepository;
54
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
55
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
56
import com.spice.profitmandi.dao.repository.dtr.Mongo;
57
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
58
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
59
import com.spice.profitmandi.dao.repository.inventory.ItemAvailabilityCacheRepository;
60
import com.spice.profitmandi.service.authentication.RoleManager;
61
import com.spice.profitmandi.service.inventory.AvailabilityInfo;
62
import com.spice.profitmandi.service.inventory.FofoAvailabilityInfo;
63
import com.spice.profitmandi.service.inventory.FofoCatalogResponse;
64
import com.spice.profitmandi.service.pricing.PricingService;
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);
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
103
	private CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
104
 
26609 amit.gupta 105
	@Autowired
26607 amit.gupta 106
	private UserAccountRepository userAccountRepository;
107
 
108
	@Autowired
109
	private ResponseSender<?> responseSender;
110
 
111
	@Autowired
112
	private TagListingRepository tagListingRepository;
113
 
114
	@Autowired
115
	private ItemRepository itemRepository;
116
 
117
	@Autowired
118
	private ItemAvailabilityCacheRepository itemAvailabilityCacheRepository;
119
 
120
	@Autowired
121
	private RoleManager roleManagerService;
122
 
123
	List<String> filterableParams = Arrays.asList("brand");
124
 
125
	@ApiImplicitParams({
126
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
127
	@RequestMapping(value = "/store/fofo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
128
	public ResponseEntity<?> getFofo(HttpServletRequest request,
129
			@RequestParam(value = "categoryId", required = false, defaultValue = "(3 OR 6)") String categoryId,
130
			@RequestParam(value = "offset") String offset, @RequestParam(value = "limit") String limit,
131
			@RequestParam(value = "sort", required = false) String sort,
132
			@RequestParam(value = "brand", required = false) String brand,
133
			@RequestParam(value = "subCategoryId", required = false) int subCategoryId,
134
			@RequestParam(value = "q", required = false) String queryTerm,
135
			@RequestParam(value = "hotDeal", required = false) boolean hotDeal) throws Throwable {
136
		List<FofoCatalogResponse> dealResponse = new ArrayList<>();
137
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
138
		if (roleManagerService.isPartner(userInfo.getRoleIds())) {
139
			// UserCart uc = userAccountRepository.getUserCart(userInfo.getUserId());
140
			List<Integer> tagIds = pricingService.getTagsIdsByRetailerId(userInfo.getRetailerId());
141
			RestClient rc = new RestClient();
142
			Map<String, String> params = new HashMap<>();
143
			List<String> mandatoryQ = new ArrayList<>();
144
			if (queryTerm != null && !queryTerm.equals("null")) {
145
				mandatoryQ.add(String.format("+(%s)", queryTerm));
146
			} else {
147
				queryTerm = null;
148
			}
149
			if (subCategoryId != 0) {
150
				mandatoryQ
151
						.add(String.format("+(subCategoryId_i:%s) +{!parent which=\"subCategoryId_i:%s\"} tagId_i:(%s)",
152
								subCategoryId, subCategoryId, StringUtils.join(tagIds, " ")));
153
			} else if (hotDeal) {
154
				mandatoryQ.add(String.format("+{!parent which=\"hot_deals_b=true\"} tagId_i:(%s)",
155
						StringUtils.join(tagIds, " ")));
156
 
157
			} else if (StringUtils.isNotBlank(brand)) {
158
				mandatoryQ.add(
159
						String.format("+(categoryId_i:%s) +(brand_ss:%s) +{!parent which=\"brand_ss:%s\"} tagId_i:(%s)",
160
								categoryId, brand, brand, StringUtils.join(tagIds, " ")));
161
 
162
			} else {
163
				mandatoryQ.add(
164
						String.format("+{!parent which=\"id:catalog*\"} tagId_i:(%s)", StringUtils.join(tagIds, " ")));
165
			}
166
			params.put("q", StringUtils.join(mandatoryQ, " "));
167
			params.put("fl", "*, [child parentFilter=id:catalog*]");
168
			if (queryTerm == null) {
169
				params.put("sort", "create_s desc");
170
			}
171
			params.put("start", String.valueOf(offset));
172
			params.put("rows", String.valueOf(limit));
173
			params.put("wt", "json");
174
			String response = null;
175
			try {
176
				response = rc.get(SchemeType.HTTP, "50.116.10.120", 8984, "solr/demo/select", params);
177
			} catch (HttpHostConnectException e) {
178
				throw new ProfitMandiBusinessException("", "", "Could not connect to host");
179
			}
180
			JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
181
			JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
182
			dealResponse = getCatalogResponse(docs, hotDeal);
183
			/*
184
			 * if (Mongo.PARTNER_BLoCKED_BRANDS.containsKey(userInfo.getEmail())) {
185
			 * dealResponse.stream() .filter(x ->
186
			 * Mongo.PARTNER_BLoCKED_BRANDS.get(userInfo.getEmail()).contains(x.getBrand()))
187
			 * ; }
188
			 */
189
		} else {
190
			return responseSender.badRequest(
191
					new ProfitMandiBusinessException("Retailer id", userInfo.getUserId(), "NOT_FOFO_RETAILER"));
192
		}
193
		return responseSender.ok(dealResponse);
194
	}
195
 
196
	private Object toDealObject(JsonObject jsonObject) {
197
		if (jsonObject.get("dealObject") != null && jsonObject.get("dealObject").asInt() == 1) {
198
			return new Gson().fromJson(jsonObject.toString(), DealObjectResponse.class);
199
		}
200
		return new Gson().fromJson(jsonObject.toString(), DealsResponse.class);
201
	}
202
 
203
	@RequestMapping(value = "/store/brands", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
204
	@ApiImplicitParams({
205
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
206
	@ApiOperation(value = "Get brand list and count for category")
207
	public ResponseEntity<?> getBrands(HttpServletRequest request,
208
			@RequestParam(value = "category_id") String category_id) throws ProfitMandiBusinessException {
209
		logger.info("Request " + request.getParameterMap());
210
		String response = null;
211
		// TODO: move to properties
212
		String uri = ProfitMandiConstants.URL_BRANDS;
213
		RestClient rc = new RestClient();
214
		Map<String, String> params = new HashMap<>();
215
		params.put("category_id", category_id);
216
		List<DealBrands> dealBrandsResponse = null;
217
		try {
218
			response = rc.get(SchemeType.HTTP, host, port, uri, params);
219
		} catch (HttpHostConnectException e) {
220
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
221
		}
222
 
223
		dealBrandsResponse = new Gson().fromJson(response, new TypeToken<List<DealBrands>>() {
224
		}.getType());
225
 
226
		return responseSender.ok(dealBrandsResponse);
227
	}
228
 
229
	@RequestMapping(value = "/store/cart", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
230
	@ApiImplicitParams({
231
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
232
	@ApiOperation(value = "Get brand list and count for category")
233
	public ResponseEntity<?> cart(HttpServletRequest request, @RequestBody AddCartRequest cartRequest)
234
			throws Exception {
235
		CartResponse cartResponse = new CartResponse();
236
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
237
		Integer storeId = userInfo.getRetailerId();
238
		List<Integer> itemIds = cartRequest.getCartItems().stream().map(x -> x.getItemId())
239
				.collect(Collectors.toList());
240
		Set<Integer> itemsIdsSet = new HashSet<>(itemIds);
241
		List<CurrentInventorySnapshot> currentInventorySnapshot = currentInventorySnapshotRepository
242
				.selectByFofoItemIds(storeId, itemsIdsSet);
243
		Map<Integer, Integer> storeItemAvailabilityMap = currentInventorySnapshot.stream()
244
				.collect(Collectors.toMap(x -> x.getItemId(), x -> x.getAvailability()));
245
 
246
		Map<Integer, Item> itemsMap = itemRepository.selectByIds(itemsIdsSet).stream()
247
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
248
 
249
		Map<Integer, TagListing> sdItemAvailabilityMap = tagListingRepository
250
				.selectByItemIdsAndTagIds(new HashSet<>(itemIds), new HashSet<>(Arrays.asList(4))).stream()
251
				.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
252
 
253
		List<Integer> catalogIds = itemsMap.values().stream().map(x -> x.getCatalogItemId())
254
				.collect(Collectors.toList());
255
 
256
		Map<Integer, JSONObject> contentMap = commonSolrService.getContentByCatalogIds(catalogIds);
257
 
258
		// cartResponse.getCartItems()
259
		for (CartItem cartItem : cartRequest.getCartItems()) {
260
			Item item = itemsMap.get(cartItem.getItemId());
261
			TagListing tagListing = sdItemAvailabilityMap.get(cartItem.getItemId());
262
			CartItemResponseModel cartItemResponseModel = new CartItemResponseModel();
263
			if (storeItemAvailabilityMap.containsKey(cartItem.getItemId())) {
264
				if (storeItemAvailabilityMap.get(cartItem.getItemId()) >= cartItem.getQuantity()) {
265
					cartItemResponseModel.setEstimate(1);
266
				} else if (tagListing.isActive()) {
267
					cartItemResponseModel.setEstimate(3);
268
				} else {
269
					cartItemResponseModel.setEstimate(-1);
270
				}
271
			}
272
			cartItemResponseModel.setSellingPrice(tagListing.getMop());
273
			cartItemResponseModel.setMaxQuantity(10);
274
			cartItemResponseModel.setCatalogItemId(item.getCatalogItemId());
275
			cartItemResponseModel.setImageUrl(contentMap.get(item.getCatalogItemId()).getString("imageUrl_s"));
276
			cartItemResponseModel.setColor(item.getColor());
277
		}
278
		ValidateCartResponse vc = new ValidateCartResponse(cartResponse, "Success", "Items added to cart successfully");
279
		return responseSender.ok(vc);
280
	}
281
 
282
	private List<FofoCatalogResponse> getCatalogResponse(JSONArray docs, boolean hotDeal)
283
			throws ProfitMandiBusinessException {
284
		Map<Integer, TagListing> itemTagListingMap = null;
285
		List<FofoCatalogResponse> dealResponse = new ArrayList<>();
286
		List<Integer> tagIds = Arrays.asList(4);
287
		if (docs.length() > 0) {
288
			HashSet<Integer> itemsSet = new HashSet<>();
289
			for (int i = 0; i < docs.length(); i++) {
290
				JSONObject doc = docs.getJSONObject(i);
291
				if (doc.has("_childDocuments_")) {
292
					for (int j = 0; j < doc.getJSONArray("_childDocuments_").length(); j++) {
293
						JSONObject childItem = doc.getJSONArray("_childDocuments_").getJSONObject(j);
294
						int itemId = childItem.getInt("itemId_i");
295
						itemsSet.add(itemId);
296
					}
297
				}
298
			}
299
			if (itemsSet.size() == 0) {
300
				return dealResponse;
301
			}
302
			itemTagListingMap = tagListingRepository.selectByItemIdsAndTagIds(itemsSet, new HashSet<>(tagIds)).stream()
303
					.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
304
		}
305
 
306
		for (int i = 0; i < docs.length(); i++) {
307
			Map<Integer, FofoAvailabilityInfo> fofoAvailabilityInfoMap = new HashMap<>();
308
			JSONObject doc = docs.getJSONObject(i);
309
			FofoCatalogResponse ffdr = new FofoCatalogResponse();
310
			ffdr.setCatalogId(doc.getInt("catalogId_i"));
311
			ffdr.setImageUrl(doc.getString("imageUrl_s"));
312
			ffdr.setTitle(doc.getString("title_s"));
313
			try {
314
				ffdr.setFeature(doc.getString("feature_s"));
315
			} catch (Exception e) {
316
				ffdr.setFeature(null);
317
				logger.info("Could not find Feature_s for {}", ffdr.getCatalogId());
318
			}
319
			ffdr.setBrand(doc.getJSONArray("brand_ss").getString(0));
320
			if (doc.has("_childDocuments_")) {
321
				for (int j = 0; j < doc.getJSONArray("_childDocuments_").length(); j++) {
322
					JSONObject childItem = doc.getJSONArray("_childDocuments_").getJSONObject(j);
323
					int itemId = childItem.getInt("itemId_i");
324
					TagListing tl = itemTagListingMap.get(itemId);
325
					if (tl == null) {
326
						logger.warn("Could not find item id {}", itemId);
327
						continue;
328
					}
329
					if (hotDeal) {
330
						if (!tl.isHotDeals()) {
331
							continue;
332
						}
333
					}
334
					float sellingPrice = (float) childItem.getDouble("sellingPrice_f");
335
					if (fofoAvailabilityInfoMap.containsKey(itemId)) {
336
						if (fofoAvailabilityInfoMap.get(itemId).getSellingPrice() > sellingPrice) {
337
							fofoAvailabilityInfoMap.get(itemId).setSellingPrice(sellingPrice);
338
							fofoAvailabilityInfoMap.get(itemId).setMop((float) childItem.getDouble("mop_f"));
339
						}
340
					} else {
341
						FofoAvailabilityInfo fdi = new FofoAvailabilityInfo();
342
						fdi.setSellingPrice((float) childItem.getDouble("sellingPrice_f"));
343
						fdi.setMop((float) childItem.getDouble("mop_f"));
344
						fdi.setColor(childItem.has("color_s") ? childItem.getString("color_s") : "");
345
						fdi.setTagId(childItem.getInt("tagId_i"));
346
						fdi.setItem_id(itemId);
347
						Item item = itemRepository.selectById(itemId);
348
						// In case its tampered glass moq should be 5
349
						if (item.getCategoryId() == 10020) {
350
							fdi.setMinBuyQuantity(10);
351
						} else {
352
							fdi.setMinBuyQuantity(1);
353
						}
354
						if (hotDeal || !tl.isActive()) {
355
 
356
							int totalAvailability = 0; // Using item availability
357
							// cache for now but can be
358
							// changed to
359
							// use caching later.
360
							try {
361
								ItemAvailabilityCache iac = itemAvailabilityCacheRepository.selectByItemId(itemId);
362
								totalAvailability = iac.getTotalAvailability();
363
								fdi.setAvailability(totalAvailability);
364
							} catch (Exception e) {
365
								continue;
366
							}
367
							if (totalAvailability <= 0) {
368
								continue;
369
							}
370
						} else {
371
							// For accessories item availability should at be ordered for Rs.1000
372
							fdi.setAvailability(100);
373
						}
374
						fdi.setQuantityStep(1);
375
						fdi.setMaxQuantity(Math.min(fdi.getAvailability(), 100));
376
						fofoAvailabilityInfoMap.put(itemId, fdi);
377
					}
378
				}
379
			}
380
			if (fofoAvailabilityInfoMap.values().size() > 0) {
381
				ffdr.setItems(new ArrayList<FofoAvailabilityInfo>(fofoAvailabilityInfoMap.values()));
382
				dealResponse.add(ffdr);
383
			}
384
		}
385
		return dealResponse;
386
 
387
	}
388
 
389
}