Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
21339 kshitij.so 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.util.ArrayList;
22336 amit.gupta 4
import java.util.Arrays;
21339 kshitij.so 5
import java.util.HashMap;
23814 amit.gupta 6
import java.util.HashSet;
22952 amit.gupta 7
import java.util.Iterator;
21339 kshitij.so 8
import java.util.List;
9
import java.util.Map;
25875 amit.gupta 10
import java.util.Optional;
25959 amit.gupta 11
import java.util.Set;
25010 amit.gupta 12
import java.util.concurrent.atomic.AtomicInteger;
23814 amit.gupta 13
import java.util.stream.Collectors;
21339 kshitij.so 14
 
15
import javax.servlet.http.HttpServletRequest;
16
 
22319 amit.gupta 17
import org.apache.commons.lang3.StringUtils;
23532 amit.gupta 18
import org.apache.http.conn.HttpHostConnectException;
23786 amit.gupta 19
import org.apache.logging.log4j.LogManager;
20
import org.apache.logging.log4j.Logger;
22319 amit.gupta 21
import org.json.JSONArray;
22
import org.json.JSONObject;
22273 amit.gupta 23
import org.springframework.beans.factory.annotation.Autowired;
21339 kshitij.so 24
import org.springframework.beans.factory.annotation.Value;
25
import org.springframework.http.HttpStatus;
26
import org.springframework.http.MediaType;
27
import org.springframework.http.ResponseEntity;
28
import org.springframework.stereotype.Controller;
22286 amit.gupta 29
import org.springframework.transaction.annotation.Transactional;
21339 kshitij.so 30
import org.springframework.web.bind.annotation.PathVariable;
31
import org.springframework.web.bind.annotation.RequestMapping;
32
import org.springframework.web.bind.annotation.RequestMethod;
33
import org.springframework.web.bind.annotation.RequestParam;
34
 
35
import com.eclipsesource.json.Json;
36
import com.eclipsesource.json.JsonArray;
37
import com.eclipsesource.json.JsonObject;
38
import com.eclipsesource.json.JsonValue;
39
import com.google.gson.Gson;
21356 kshitij.so 40
import com.google.gson.reflect.TypeToken;
25010 amit.gupta 41
import com.mongodb.BasicDBObject;
24163 amit.gupta 42
import com.mongodb.DBObject;
21643 ashik.ali 43
import com.spice.profitmandi.common.enumuration.SchemeType;
21339 kshitij.so 44
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
45
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22289 amit.gupta 46
import com.spice.profitmandi.common.model.UserInfo;
21643 ashik.ali 47
import com.spice.profitmandi.common.web.client.RestClient;
22319 amit.gupta 48
import com.spice.profitmandi.common.web.util.ResponseSender;
25010 amit.gupta 49
import com.spice.profitmandi.dao.entity.catalog.Category;
23426 amit.gupta 50
import com.spice.profitmandi.dao.entity.catalog.Item;
23814 amit.gupta 51
import com.spice.profitmandi.dao.entity.catalog.TagListing;
26847 tejbeer 52
import com.spice.profitmandi.dao.entity.fofo.SuggestedPo;
26846 tejbeer 53
import com.spice.profitmandi.dao.entity.fofo.SuggestedPoDetail;
23861 amit.gupta 54
import com.spice.profitmandi.dao.entity.inventory.ItemAvailabilityCache;
22361 amit.gupta 55
import com.spice.profitmandi.dao.model.UserCart;
25010 amit.gupta 56
import com.spice.profitmandi.dao.repository.catalog.CategoryRepository;
23426 amit.gupta 57
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
23814 amit.gupta 58
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
22333 amit.gupta 59
import com.spice.profitmandi.dao.repository.dtr.Mongo;
22361 amit.gupta 60
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
26846 tejbeer 61
import com.spice.profitmandi.dao.repository.fofo.SuggestedPoDetailRepository;
26847 tejbeer 62
import com.spice.profitmandi.dao.repository.fofo.SuggestedPoRepository;
22989 amit.gupta 63
import com.spice.profitmandi.dao.repository.inventory.ItemAvailabilityCacheRepository;
23798 amit.gupta 64
import com.spice.profitmandi.service.authentication.RoleManager;
25880 amit.gupta 65
import com.spice.profitmandi.service.inventory.AvailabilityInfo;
25882 amit.gupta 66
import com.spice.profitmandi.service.inventory.Bucket;
25880 amit.gupta 67
import com.spice.profitmandi.service.inventory.FofoAvailabilityInfo;
68
import com.spice.profitmandi.service.inventory.FofoCatalogResponse;
25875 amit.gupta 69
import com.spice.profitmandi.service.inventory.ItemBucketService;
25879 amit.gupta 70
import com.spice.profitmandi.service.inventory.ItemQuantityPojo;
22287 amit.gupta 71
import com.spice.profitmandi.service.pricing.PricingService;
26695 amit.gupta 72
import com.spice.profitmandi.service.scheme.SchemeService;
21356 kshitij.so 73
import com.spice.profitmandi.web.res.DealBrands;
21339 kshitij.so 74
import com.spice.profitmandi.web.res.DealObjectResponse;
75
import com.spice.profitmandi.web.res.DealsResponse;
76
 
77
import io.swagger.annotations.ApiImplicitParam;
78
import io.swagger.annotations.ApiImplicitParams;
79
import io.swagger.annotations.ApiOperation;
80
 
81
@Controller
22319 amit.gupta 82
@Transactional(rollbackFor = Throwable.class)
21339 kshitij.so 83
public class DealsController {
84
 
23568 govind 85
	private static final Logger logger = LogManager.getLogger(DealsController.class);
21339 kshitij.so 86
 
87
	@Value("${python.api.host}")
88
	private String host;
23816 amit.gupta 89
 
21339 kshitij.so 90
	@Value("${python.api.port}")
91
	private int port;
23816 amit.gupta 92
 
93
	// This is now unused as we are not supporting multiple companies.
23300 amit.gupta 94
	@Value("${gadgetCops.invoice.cc}")
23816 amit.gupta 95
	private String[] ccGadgetCopInvoiceTo;
22319 amit.gupta 96
 
97
	@Autowired
98
	private PricingService pricingService;
23816 amit.gupta 99
 
22273 amit.gupta 100
	@Autowired
25010 amit.gupta 101
	private CategoryRepository categoryRepository;
102
 
103
	@Autowired
26695 amit.gupta 104
	private SchemeService schemeService;
105
 
106
	@Autowired
22333 amit.gupta 107
	private Mongo mongoClient;
25879 amit.gupta 108
 
25875 amit.gupta 109
	@Autowired
110
	private ItemBucketService itemBucketService;
23816 amit.gupta 111
 
22333 amit.gupta 112
	@Autowired
22361 amit.gupta 113
	private UserAccountRepository userAccountRepository;
23816 amit.gupta 114
 
22989 amit.gupta 115
	@Autowired
22931 ashik.ali 116
	private ResponseSender<?> responseSender;
23816 amit.gupta 117
 
22554 amit.gupta 118
	@Autowired
25010 amit.gupta 119
	private CategoryRepository repository;
120
 
121
	@Autowired
23814 amit.gupta 122
	private TagListingRepository tagListingRepository;
23816 amit.gupta 123
 
23814 amit.gupta 124
	@Autowired
23426 amit.gupta 125
	private ItemRepository itemRepository;
23816 amit.gupta 126
 
23786 amit.gupta 127
	@Autowired
23861 amit.gupta 128
	private ItemAvailabilityCacheRepository itemAvailabilityCacheRepository;
129
 
130
	@Autowired
23798 amit.gupta 131
	private RoleManager roleManagerService;
23816 amit.gupta 132
 
26846 tejbeer 133
	@Autowired
134
	private SuggestedPoDetailRepository monthlyPoDetailRepository;
135
 
26847 tejbeer 136
	@Autowired
137
	private SuggestedPoRepository suggestedPoRepository;
138
 
22336 amit.gupta 139
	List<String> filterableParams = Arrays.asList("brand");
24949 amit.gupta 140
 
25876 amit.gupta 141
	@RequestMapping(value = "/fofo/buckets", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
25879 amit.gupta 142
	public ResponseEntity<?> getBuckets(HttpServletRequest request) throws ProfitMandiBusinessException {
25875 amit.gupta 143
		logger.info("Request " + request.getParameterMap());
144
		return responseSender.ok(itemBucketService.getBuckets(Optional.of(true)));
145
	}
25879 amit.gupta 146
 
147
	@RequestMapping(value = "/fofo/bucket", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
25880 amit.gupta 148
	public ResponseEntity<?> getBucketDetails(HttpServletRequest request, @RequestParam int id)
25879 amit.gupta 149
			throws ProfitMandiBusinessException {
25880 amit.gupta 150
		List<ItemQuantityPojo> iqPojo = itemBucketService.getBucketDetails(id);
25968 amit.gupta 151
		Map<Integer, Integer> itemIdsQtyMap = iqPojo.stream()
152
				.collect(Collectors.toMap(x -> x.getItemId(), x -> x.getQuantity()));
153
		Set<Integer> catalogIds = itemRepository.selectByIds(itemIdsQtyMap.keySet()).stream()
154
				.map(x -> x.getCatalogItemId()).collect(Collectors.toSet());
25879 amit.gupta 155
		RestClient rc = new RestClient();
156
		Map<String, String> params = new HashMap<>();
157
		List<String> mandatoryQ = new ArrayList<>();
25968 amit.gupta 158
		mandatoryQ.add(
159
				String.format("+catalogId_i:(%s) +{!parent which=\"id:catalog*\"}", StringUtils.join(catalogIds, " ")));
25959 amit.gupta 160
		params.put("start", "0");
161
		params.put("rows", "100");
25879 amit.gupta 162
		params.put("q", StringUtils.join(mandatoryQ, " "));
163
		params.put("fl", "*, [child parentFilter=id:catalog*]");
164
 
165
		params.put("wt", "json");
166
		String response = null;
167
		try {
26573 amit.gupta 168
			response = rc.get(SchemeType.HTTP, "50.116.10.120", 8984, "solr/demo/select", params);
25879 amit.gupta 169
		} catch (HttpHostConnectException e) {
170
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
171
		}
172
		JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
173
		JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
25968 amit.gupta 174
		List<FofoCatalogResponse> dealResponse = getCatalogSingleSkuResponse(docs, itemIdsQtyMap, false);
26051 amit.gupta 175
 
26589 amit.gupta 176
		Bucket bucket = itemBucketService.getBuckets(Optional.of(true)).stream().filter(x -> x.getId() == id)
177
				.collect(Collectors.toList()).get(0);
25882 amit.gupta 178
		bucket.setFofoCatalogResponses(dealResponse);
179
		return responseSender.ok(bucket);
25879 amit.gupta 180
	}
181
 
26846 tejbeer 182
	@RequestMapping(value = "/fofo/suggestedPo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
183
	public ResponseEntity<?> getSuggestedPo(HttpServletRequest request, @RequestParam int id)
184
			throws ProfitMandiBusinessException {
185
 
186
		List<SuggestedPoDetail> mpd = monthlyPoDetailRepository.selectByPoId(id);
187
		Map<Integer, Integer> itemIdsQtyMap = mpd.stream()
188
				.collect(Collectors.toMap(x -> x.getItemId(), x -> x.getQuantity()));
189
 
190
		Set<Integer> catalogIds = itemRepository.selectByIds(itemIdsQtyMap.keySet()).stream()
191
				.map(x -> x.getCatalogItemId()).collect(Collectors.toSet());
192
		RestClient rc = new RestClient();
193
		Map<String, String> params = new HashMap<>();
194
		List<String> mandatoryQ = new ArrayList<>();
195
		mandatoryQ.add(
196
				String.format("+catalogId_i:(%s) +{!parent which=\"id:catalog*\"}", StringUtils.join(catalogIds, " ")));
197
		params.put("start", "0");
198
		params.put("rows", "100");
199
		params.put("q", StringUtils.join(mandatoryQ, " "));
200
		params.put("fl", "*, [child parentFilter=id:catalog*]");
201
 
202
		params.put("wt", "json");
203
		String response = null;
204
		try {
205
			response = rc.get(SchemeType.HTTP, "50.116.10.120", 8984, "solr/demo/select", params);
206
		} catch (HttpHostConnectException e) {
207
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
208
		}
209
		JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
210
		JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
211
		List<FofoCatalogResponse> dealResponse = getCatalogSingleSkuResponse(docs, itemIdsQtyMap, false);
212
 
213
		return responseSender.ok(dealResponse);
214
	}
215
 
26847 tejbeer 216
	@RequestMapping(value = "/suggestedPo/status", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
26848 tejbeer 217
	@ApiImplicitParams({
218
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
219
	@ApiOperation(value = "")
26847 tejbeer 220
	public ResponseEntity<?> changeSuggestedPoStatus(HttpServletRequest request, @RequestParam int id)
221
			throws ProfitMandiBusinessException {
222
		SuggestedPo suggestedPo = suggestedPoRepository.selectById(id);
223
		suggestedPo.setStatus("closed");
224
 
225
		return responseSender.ok(true);
226
	}
227
 
23816 amit.gupta 228
	private String getCommaSeparateTags(int userId) {
22361 amit.gupta 229
		UserCart uc = userAccountRepository.getUserCart(userId);
230
		List<Integer> tagIds = pricingService.getTagsIdsByRetailerId(uc.getUserId());
22287 amit.gupta 231
		List<String> strTagIds = new ArrayList<>();
232
		for (Integer tagId : tagIds) {
233
			strTagIds.add(String.valueOf(tagId));
22273 amit.gupta 234
		}
22287 amit.gupta 235
		return String.join(",", strTagIds);
22273 amit.gupta 236
	}
237
 
22319 amit.gupta 238
	@ApiImplicitParams({
239
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
240
	@RequestMapping(value = "/fofo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
24091 tejbeer 241
	public ResponseEntity<?> getFofo(HttpServletRequest request,
26758 amit.gupta 242
			@RequestParam(value = "categoryId", required = false, defaultValue = "3") String categoryId,
22319 amit.gupta 243
			@RequestParam(value = "offset") String offset, @RequestParam(value = "limit") String limit,
23816 amit.gupta 244
			@RequestParam(value = "sort", required = false) String sort,
245
			@RequestParam(value = "brand", required = false) String brand,
25011 amit.gupta 246
			@RequestParam(value = "subCategoryId", required = false) int subCategoryId,
24946 amit.gupta 247
			@RequestParam(value = "q", required = false) String queryTerm,
24091 tejbeer 248
			@RequestParam(value = "hotDeal", required = false) boolean hotDeal) throws Throwable {
22328 amit.gupta 249
		List<FofoCatalogResponse> dealResponse = new ArrayList<>();
22319 amit.gupta 250
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
23786 amit.gupta 251
		if (roleManagerService.isPartner(userInfo.getRoleIds())) {
25879 amit.gupta 252
			// UserCart uc = userAccountRepository.getUserCart(userInfo.getUserId());
25389 tejbeer 253
			List<Integer> tagIds = pricingService.getTagsIdsByRetailerId(userInfo.getRetailerId());
23816 amit.gupta 254
			RestClient rc = new RestClient();
22319 amit.gupta 255
			Map<String, String> params = new HashMap<>();
22336 amit.gupta 256
			List<String> mandatoryQ = new ArrayList<>();
24974 amit.gupta 257
			if (queryTerm != null && !queryTerm.equals("null")) {
24975 amit.gupta 258
				mandatoryQ.add(String.format("+(%s)", queryTerm));
259
			} else {
260
				queryTerm = null;
24971 amit.gupta 261
			}
25015 amit.gupta 262
			if (subCategoryId != 0) {
26883 amit.gupta 263
				mandatoryQ.add(String.format("{!parent which=\"subCategoryId_i:%s AND child_b:true\"}tagId_i:(%s)",
264
						subCategoryId, StringUtils.join(tagIds, " ")));
23814 amit.gupta 265
			} else if (hotDeal) {
26884 amit.gupta 266
				mandatoryQ.add(String.format("{!parent which=\"hot_deals_b=true AND child_b:true\"}tagId_i:(%s)",
23816 amit.gupta 267
						StringUtils.join(tagIds, " ")));
25015 amit.gupta 268
 
269
			} else if (StringUtils.isNotBlank(brand)) {
26883 amit.gupta 270
				mandatoryQ.add(String.format(
26884 amit.gupta 271
						"{!parent which=\"brand_ss:%s AND categoryId_i:%s " + "AND child_b:true\"}tagId_i:(%s)", brand,
26883 amit.gupta 272
						categoryId, StringUtils.join(tagIds, " ")));
25015 amit.gupta 273
 
22347 amit.gupta 274
			} else {
26883 amit.gupta 275
				mandatoryQ.add(String.format("{!parent which=\"subCategoryId_i:%s\"}tagId_i:(%s) AND child_b:true",
276
						categoryId, StringUtils.join(tagIds, " ")));
22336 amit.gupta 277
			}
23816 amit.gupta 278
			params.put("q", StringUtils.join(mandatoryQ, " "));
22319 amit.gupta 279
			params.put("fl", "*, [child parentFilter=id:catalog*]");
24995 amit.gupta 280
			if (queryTerm == null) {
24975 amit.gupta 281
				params.put("sort", "create_s desc");
282
			}
22319 amit.gupta 283
			params.put("start", String.valueOf(offset));
284
			params.put("rows", String.valueOf(limit));
285
			params.put("wt", "json");
23532 amit.gupta 286
			String response = null;
287
			try {
26575 amit.gupta 288
				response = rc.get(SchemeType.HTTP, "50.116.10.120", 8984, "solr/demo/select", params);
23532 amit.gupta 289
			} catch (HttpHostConnectException e) {
290
				throw new ProfitMandiBusinessException("", "", "Could not connect to host");
291
			}
22319 amit.gupta 292
			JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
293
			JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
26883 amit.gupta 294
			dealResponse = this.getCatalogResponse(docs, hotDeal);
26589 amit.gupta 295
			/*
296
			 * if (Mongo.PARTNER_BLoCKED_BRANDS.containsKey(userInfo.getEmail())) {
297
			 * dealResponse.stream() .filter(x ->
298
			 * Mongo.PARTNER_BLoCKED_BRANDS.get(userInfo.getEmail()).contains(x.getBrand()))
299
			 * ; }
300
			 */
301
		} else {
302
			return responseSender.badRequest(
303
					new ProfitMandiBusinessException("Retailer id", userInfo.getUserId(), "NOT_FOFO_RETAILER"));
304
		}
305
		return responseSender.ok(dealResponse);
306
	}
307
 
308
	@ApiImplicitParams({
309
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
310
	@RequestMapping(value = "/partnerStock", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
311
	public ResponseEntity<?> partnerStock(HttpServletRequest request,
26758 amit.gupta 312
			@RequestParam(value = "categoryId", required = false, defaultValue = "3") String categoryId,
26589 amit.gupta 313
			@RequestParam(value = "offset") String offset, @RequestParam(value = "limit") String limit,
314
			@RequestParam(value = "sort", required = false) String sort,
315
			@RequestParam(value = "brand", required = false) String brand,
316
			@RequestParam(value = "subCategoryId", required = false) int subCategoryId,
317
			@RequestParam(value = "q", required = false) String queryTerm,
318
			@RequestParam(value = " ", required = false, defaultValue = "true") boolean partnerStockOnly)
319
			throws Throwable {
320
		List<FofoCatalogResponse> dealResponse = new ArrayList<>();
321
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
322
		UserCart uc = userAccountRepository.getUserCart(userInfo.getUserId());
26846 tejbeer 323
 
26589 amit.gupta 324
		logger.info("Retiler Id ==> {}", uc.getUserId());
325
		if (roleManagerService.isPartner(userInfo.getRoleIds())) {
26846 tejbeer 326
			if (partnerStockOnly) {
327
 
24168 amit.gupta 328
			}
26589 amit.gupta 329
			List<Integer> tagIds = pricingService.getTagsIdsByRetailerId(userInfo.getRetailerId());
330
			RestClient rc = new RestClient();
331
			Map<String, String> params = new HashMap<>();
332
			List<String> mandatoryQ = new ArrayList<>();
333
			if (queryTerm != null && !queryTerm.equals("null")) {
334
				mandatoryQ.add(String.format("+(%s)", queryTerm));
335
			} else {
336
				queryTerm = null;
337
			}
338
			if (subCategoryId != 0) {
339
				mandatoryQ
340
						.add(String.format("+(subCategoryId_i:%s) +{!parent which=\"subCategoryId_i:%s\"} tagId_i:(%s)",
341
								subCategoryId, subCategoryId, StringUtils.join(tagIds, " ")));
342
			} else if (StringUtils.isNotBlank(brand)) {
343
				mandatoryQ.add(
344
						String.format("+(categoryId_i:%s) +(brand_ss:%s) +{!parent which=\"brand_ss:%s\"} tagId_i:(%s)",
345
								categoryId, brand, brand, StringUtils.join(tagIds, " ")));
346
 
347
			} else {
348
				mandatoryQ.add(
349
						String.format("+{!parent which=\"id:catalog*\"} tagId_i:(%s)", StringUtils.join(tagIds, " ")));
350
			}
351
			params.put("q", StringUtils.join(mandatoryQ, " "));
352
			params.put("fl", "*, [child parentFilter=id:catalog*]");
353
			if (queryTerm == null) {
354
				params.put("sort", "create_s desc");
355
			}
356
			params.put("start", String.valueOf(offset));
357
			params.put("rows", String.valueOf(limit));
358
			params.put("wt", "json");
359
			String response = null;
360
			try {
361
				response = rc.get(SchemeType.HTTP, "50.116.10.120", 8984, "solr/demo/select", params);
362
			} catch (HttpHostConnectException e) {
363
				throw new ProfitMandiBusinessException("", "", "Could not connect to host");
364
			}
365
			JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
366
			JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
367
			dealResponse = getCatalogResponse(docs, false);
368
			/*
369
			 * if (Mongo.PARTNER_BLoCKED_BRANDS.containsKey(userInfo.getEmail())) {
370
			 * dealResponse.stream() .filter(x ->
371
			 * Mongo.PARTNER_BLoCKED_BRANDS.get(userInfo.getEmail()).contains(x.getBrand()))
372
			 * ; }
373
			 */
22319 amit.gupta 374
		} else {
23816 amit.gupta 375
			return responseSender.badRequest(
376
					new ProfitMandiBusinessException("Retailer id", userInfo.getUserId(), "NOT_FOFO_RETAILER"));
22319 amit.gupta 377
		}
378
		return responseSender.ok(dealResponse);
379
	}
22273 amit.gupta 380
 
22319 amit.gupta 381
	@RequestMapping(value = "/online-deals", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
22272 amit.gupta 382
	@ApiImplicitParams({
22319 amit.gupta 383
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
22272 amit.gupta 384
	@ApiOperation(value = "Get online deals")
22319 amit.gupta 385
	public ResponseEntity<?> getOnlineDeals(HttpServletRequest request,
386
			@RequestParam(value = "categoryId") String categoryId, @RequestParam(value = "offset") String offset,
387
			@RequestParam(value = "limit") String limit, @RequestParam(value = "sort", required = false) String sort,
388
			@RequestParam(value = "direction", required = false) String direction,
389
			@RequestParam(value = "filterData", required = false) String filterData) throws Throwable {
390
		logger.info("Request " + request.getParameterMap());
22272 amit.gupta 391
		String response = null;
22319 amit.gupta 392
		int userId = (int) request.getAttribute("userId");
22289 amit.gupta 393
 
22319 amit.gupta 394
		String uri = "/deals/" + userId;
23532 amit.gupta 395
		RestClient rc = new RestClient();
22272 amit.gupta 396
		Map<String, String> params = new HashMap<>();
397
		params.put("offset", offset);
398
		params.put("limit", limit);
399
		params.put("categoryId", categoryId);
400
		params.put("direction", direction);
401
		params.put("sort", sort);
402
		params.put("source", "online");
403
		params.put("filterData", filterData);
23816 amit.gupta 404
		/*
405
		 * if (userInfo.getRoleNames().contains(RoleType.FOFO.toString())) {
406
		 * params.put("tag_ids", getCommaSeparateTags(userId)); }
407
		 */
22272 amit.gupta 408
		List<Object> responseObject = new ArrayList<>();
23532 amit.gupta 409
		try {
410
			response = rc.get(SchemeType.HTTP, host, port, uri, params);
411
		} catch (HttpHostConnectException e) {
412
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
413
		}
22931 ashik.ali 414
 
22272 amit.gupta 415
		JsonArray result_json = Json.parse(response).asArray();
22319 amit.gupta 416
		for (JsonValue j : result_json) {
23816 amit.gupta 417
			// logger.info("res " + j.asArray());
22272 amit.gupta 418
			List<Object> innerObject = new ArrayList<>();
22319 amit.gupta 419
			for (JsonValue jsonObject : j.asArray()) {
22272 amit.gupta 420
				innerObject.add(toDealObject(jsonObject.asObject()));
421
			}
22319 amit.gupta 422
			if (innerObject.size() > 0) {
22272 amit.gupta 423
				responseObject.add(innerObject);
424
			}
425
		}
23022 ashik.ali 426
		return responseSender.ok(responseObject);
22272 amit.gupta 427
	}
428
 
22319 amit.gupta 429
	private Object toDealObject(JsonObject jsonObject) {
430
		if (jsonObject.get("dealObject") != null && jsonObject.get("dealObject").asInt() == 1) {
21339 kshitij.so 431
			return new Gson().fromJson(jsonObject.toString(), DealObjectResponse.class);
432
		}
433
		return new Gson().fromJson(jsonObject.toString(), DealsResponse.class);
434
	}
22319 amit.gupta 435
 
436
	@RequestMapping(value = ProfitMandiConstants.URL_BRANDS, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
21356 kshitij.so 437
	@ApiImplicitParams({
22319 amit.gupta 438
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
21356 kshitij.so 439
	@ApiOperation(value = "Get brand list and count for category")
22319 amit.gupta 440
	public ResponseEntity<?> getBrands(HttpServletRequest request,
23816 amit.gupta 441
			@RequestParam(value = "category_id") String category_id) throws ProfitMandiBusinessException {
22319 amit.gupta 442
		logger.info("Request " + request.getParameterMap());
21356 kshitij.so 443
		String response = null;
22319 amit.gupta 444
		// TODO: move to properties
21356 kshitij.so 445
		String uri = ProfitMandiConstants.URL_BRANDS;
23532 amit.gupta 446
		RestClient rc = new RestClient();
21356 kshitij.so 447
		Map<String, String> params = new HashMap<>();
448
		params.put("category_id", category_id);
21358 kshitij.so 449
		List<DealBrands> dealBrandsResponse = null;
23532 amit.gupta 450
		try {
451
			response = rc.get(SchemeType.HTTP, host, port, uri, params);
452
		} catch (HttpHostConnectException e) {
453
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
454
		}
23816 amit.gupta 455
 
22319 amit.gupta 456
		dealBrandsResponse = new Gson().fromJson(response, new TypeToken<List<DealBrands>>() {
457
		}.getType());
23022 ashik.ali 458
 
459
		return responseSender.ok(dealBrandsResponse);
21356 kshitij.so 460
	}
22319 amit.gupta 461
 
462
	@RequestMapping(value = ProfitMandiConstants.URL_UNIT_DEAL, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
21445 kshitij.so 463
	@ApiImplicitParams({
22319 amit.gupta 464
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
21445 kshitij.so 465
	@ApiOperation(value = "Get unit deal object")
23816 amit.gupta 466
	public ResponseEntity<?> getUnitDeal(HttpServletRequest request, @PathVariable(value = "id") long id)
467
			throws ProfitMandiBusinessException {
21445 kshitij.so 468
		String response = null;
22319 amit.gupta 469
		// TODO: move to properties
470
		String uri = "getDealById/" + id;
471
		System.out.println("Unit deal " + uri);
23532 amit.gupta 472
		RestClient rc = new RestClient();
21445 kshitij.so 473
		Map<String, String> params = new HashMap<>();
474
		DealsResponse dealsResponse = null;
23532 amit.gupta 475
		try {
476
			response = rc.get(SchemeType.HTTP, host, port, uri, params);
477
		} catch (HttpHostConnectException e) {
478
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
479
		}
23816 amit.gupta 480
 
21445 kshitij.so 481
		JsonObject result_json = Json.parse(response).asObject();
22319 amit.gupta 482
		if (!result_json.isEmpty()) {
21445 kshitij.so 483
			dealsResponse = new Gson().fromJson(response, DealsResponse.class);
22952 amit.gupta 484
			Iterator<AvailabilityInfo> iter = dealsResponse.getAvailabilityInfo().iterator();
23816 amit.gupta 485
			while (iter.hasNext()) {
22952 amit.gupta 486
				AvailabilityInfo ai = iter.next();
23816 amit.gupta 487
				if (ai.getAvailability() <= 0)
22952 amit.gupta 488
					iter.remove();
489
			}
21445 kshitij.so 490
		}
22952 amit.gupta 491
		return responseSender.ok(dealsResponse);
21445 kshitij.so 492
	}
23816 amit.gupta 493
 
24091 tejbeer 494
	@RequestMapping(value = "/partnerdeals/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
495
	@ApiImplicitParams({
496
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
497
	@ApiOperation(value = "Get unit deal object")
498
	public ResponseEntity<?> getUnitFocoDeal(HttpServletRequest request, @PathVariable(value = "id") long id)
499
			throws ProfitMandiBusinessException {
500
		List<FofoCatalogResponse> dealResponse = new ArrayList<>();
501
		List<Integer> tagIds = Arrays.asList(4);
502
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
503
		if (roleManagerService.isPartner(userInfo.getRoleIds())) {
504
			String categoryId = "(3 OR 6)";
505
			UserCart uc = userAccountRepository.getUserCart(userInfo.getUserId());
506
			RestClient rc = new RestClient();
507
			Map<String, String> params = new HashMap<>();
508
			List<String> mandatoryQ = new ArrayList<>();
509
			String catalogString = "catalog" + id;
510
 
24149 amit.gupta 511
			mandatoryQ.add(String.format("+(categoryId_i:%s) +(id:%s) +{!parent which=\"id:%s\"} tagId_i:(%s)",
512
					categoryId, catalogString, catalogString, StringUtils.join(tagIds, " ")));
513
 
24091 tejbeer 514
			params.put("q", StringUtils.join(mandatoryQ, " "));
515
			params.put("fl", "*, [child parentFilter=id:catalog*]");
516
			params.put("sort", "rank_i asc, create_s desc");
517
			params.put("wt", "json");
518
			String response = null;
519
			try {
26575 amit.gupta 520
				response = rc.get(SchemeType.HTTP, "50.116.10.120", 8984, "solr/demo/select", params);
24091 tejbeer 521
			} catch (HttpHostConnectException e) {
522
				throw new ProfitMandiBusinessException("", "", "Could not connect to host");
523
			}
524
			JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
525
			JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
24149 amit.gupta 526
			dealResponse = getCatalogResponse(docs, false);
24091 tejbeer 527
		} else {
528
			return responseSender.badRequest(
529
					new ProfitMandiBusinessException("Retailer id", userInfo.getUserId(), "NOT_FOFO_RETAILER"));
530
		}
531
		return responseSender.ok(dealResponse.get(0));
532
	}
533
 
22333 amit.gupta 534
	@RequestMapping(value = "/fofo/brands", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
24949 amit.gupta 535
	public ResponseEntity<?> getBrandsToDisplay(HttpServletRequest request,
25010 amit.gupta 536
			@RequestParam(required = false, defaultValue = "0") int categoryId) throws Exception {
24163 amit.gupta 537
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
25543 amit.gupta 538
		logger.info("userInfo [{}]", userInfo);
25879 amit.gupta 539
		List<DBObject> brandsDisplay = mongoClient.getMongoBrands(userInfo.getRetailerId(), userInfo.getEmail(),
540
				categoryId);
24163 amit.gupta 541
		return new ResponseEntity<>(brandsDisplay, HttpStatus.OK);
22333 amit.gupta 542
	}
25879 amit.gupta 543
 
25813 amit.gupta 544
	@RequestMapping(value = "/fofo/accessory/all-categories", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
545
	public ResponseEntity<?> getSubCategoriesToDisplay(HttpServletRequest request) throws Exception {
546
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
547
		logger.info("userInfo [{}]", userInfo);
548
		List<DBObject> subCateogriesDisplay = this.getSubCategoriesToDisplay();
549
		return new ResponseEntity<>(subCateogriesDisplay, HttpStatus.OK);
550
	}
23816 amit.gupta 551
 
25011 amit.gupta 552
	private List<DBObject> getSubCategoriesToDisplay() throws Exception {
25010 amit.gupta 553
		List<DBObject> subCategories = new ArrayList<>();
554
		RestClient rc = new RestClient();
555
		Map<String, String> params = new HashMap<>();
556
		params.put("q", "categoryId_i:6");
557
		params.put("group", "true");
558
		params.put("group.field", "subCategoryId_i");
559
		params.put("wt", "json");
25126 amit.gupta 560
		params.put("rows", "50");
25014 amit.gupta 561
		params.put("fl", "subCategoryId_i");
25010 amit.gupta 562
		String response = null;
563
		try {
26575 amit.gupta 564
			response = rc.get(SchemeType.HTTP, "50.116.10.120", 8984, "solr/demo/select", params);
25010 amit.gupta 565
		} catch (HttpHostConnectException e) {
566
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
567
		}
568
		JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("grouped");
569
		JSONArray groups = solrResponseJSONObj.getJSONObject("subCategoryId_i").getJSONArray("groups");
570
		List<Integer> categoryIds = new ArrayList<>();
25015 amit.gupta 571
		for (int i = 0; i < groups.length(); i++) {
572
			JSONObject groupObject = groups.getJSONObject(i);
573
			int subCategoryId = groupObject.getInt("groupValue");
25010 amit.gupta 574
			int quantity = groupObject.getJSONObject("doclist").getInt("numFound");
25013 amit.gupta 575
			categoryIds.add(subCategoryId);
25010 amit.gupta 576
		}
25015 amit.gupta 577
 
25010 amit.gupta 578
		List<Category> categories = categoryRepository.selectByIds(categoryIds);
25015 amit.gupta 579
		AtomicInteger i = new AtomicInteger(0);
580
		categories.forEach(x -> {
25011 amit.gupta 581
			DBObject dbObject = new BasicDBObject();
582
			dbObject.put("name", x.getLabel());
583
			dbObject.put("subCategoryId", x.getId());
584
			dbObject.put("rank", i.incrementAndGet());
585
			dbObject.put("categoryId", 6);
26441 tejbeer 586
			dbObject.put("url", "https://images.smartdukaan.com/uploads/campaigns/" + x.getId() + ".png");
25011 amit.gupta 587
			subCategories.add(dbObject);
25010 amit.gupta 588
		});
25015 amit.gupta 589
 
25011 amit.gupta 590
		return subCategories;
25015 amit.gupta 591
 
25010 amit.gupta 592
	}
593
 
22446 amit.gupta 594
	@RequestMapping(value = "/banners/{bannerType}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
22448 amit.gupta 595
	public ResponseEntity<?> getBanners(@PathVariable String bannerType) {
22447 amit.gupta 596
		return new ResponseEntity<>(mongoClient.getBannersByType(bannerType), HttpStatus.OK);
22446 amit.gupta 597
	}
23816 amit.gupta 598
 
23793 tejbeer 599
	@RequestMapping(value = "/deals/subCategories", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
600
	public ResponseEntity<?> getSubcategoriesToDisplay() {
601
		return new ResponseEntity<>(mongoClient.getSubcategoriesToDisplay(), HttpStatus.OK);
602
	}
23816 amit.gupta 603
 
22406 amit.gupta 604
	@ApiImplicitParams({
23816 amit.gupta 605
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
22401 amit.gupta 606
	@RequestMapping(value = "/deals/skus/{skus}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
22931 ashik.ali 607
	public ResponseEntity<?> getDealsBySkus(@PathVariable String skus) throws ProfitMandiBusinessException {
22401 amit.gupta 608
		StringBuffer sb = new StringBuffer("/getDealsForNotification/");
609
		String uri = sb.append(skus).toString();
23532 amit.gupta 610
		RestClient rc = new RestClient();
611
		String response;
612
		try {
613
			response = rc.get(SchemeType.HTTP, host, port, uri, new HashMap<>());
23816 amit.gupta 614
		} catch (HttpHostConnectException e) {
23532 amit.gupta 615
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
616
		}
22406 amit.gupta 617
		JsonArray result_json = Json.parse(response).asArray();
22407 amit.gupta 618
		List<Object> responseObject = new ArrayList<>();
619
		for (JsonValue j : result_json) {
23816 amit.gupta 620
			// logger.info("res " + j.asArray());
22407 amit.gupta 621
			List<Object> innerObject = new ArrayList<>();
622
			for (JsonValue jsonObject : j.asArray()) {
623
				innerObject.add(toDealObject(jsonObject.asObject()));
624
			}
625
			if (innerObject.size() > 0) {
626
				responseObject.add(innerObject);
627
			}
628
		}
22408 amit.gupta 629
		return responseSender.ok(responseObject);
22401 amit.gupta 630
	}
21339 kshitij.so 631
 
24149 amit.gupta 632
	private List<FofoCatalogResponse> getCatalogResponse(JSONArray docs, boolean hotDeal)
633
			throws ProfitMandiBusinessException {
24091 tejbeer 634
		Map<Integer, TagListing> itemTagListingMap = null;
635
		List<FofoCatalogResponse> dealResponse = new ArrayList<>();
636
		List<Integer> tagIds = Arrays.asList(4);
637
		if (docs.length() > 0) {
638
			HashSet<Integer> itemsSet = new HashSet<>();
639
			for (int i = 0; i < docs.length(); i++) {
640
				JSONObject doc = docs.getJSONObject(i);
26589 amit.gupta 641
				if (doc.has("_childDocuments_")) {
26515 amit.gupta 642
					for (int j = 0; j < doc.getJSONArray("_childDocuments_").length(); j++) {
643
						JSONObject childItem = doc.getJSONArray("_childDocuments_").getJSONObject(j);
644
						int itemId = childItem.getInt("itemId_i");
645
						itemsSet.add(itemId);
646
					}
24091 tejbeer 647
				}
648
			}
26589 amit.gupta 649
			if (itemsSet.size() == 0) {
26573 amit.gupta 650
				return dealResponse;
651
			}
24091 tejbeer 652
			itemTagListingMap = tagListingRepository.selectByItemIdsAndTagIds(itemsSet, new HashSet<>(tagIds)).stream()
653
					.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
654
		}
655
 
656
		for (int i = 0; i < docs.length(); i++) {
657
			Map<Integer, FofoAvailabilityInfo> fofoAvailabilityInfoMap = new HashMap<>();
658
			JSONObject doc = docs.getJSONObject(i);
659
			FofoCatalogResponse ffdr = new FofoCatalogResponse();
660
			ffdr.setCatalogId(doc.getInt("catalogId_i"));
661
			ffdr.setImageUrl(doc.getString("imageUrl_s"));
662
			ffdr.setTitle(doc.getString("title_s"));
24117 amit.gupta 663
			try {
664
				ffdr.setFeature(doc.getString("feature_s"));
24149 amit.gupta 665
			} catch (Exception e) {
24117 amit.gupta 666
				ffdr.setFeature(null);
24149 amit.gupta 667
				logger.info("Could not find Feature_s for {}", ffdr.getCatalogId());
24117 amit.gupta 668
			}
24091 tejbeer 669
			ffdr.setBrand(doc.getJSONArray("brand_ss").getString(0));
26589 amit.gupta 670
			if (doc.has("_childDocuments_")) {
26515 amit.gupta 671
				for (int j = 0; j < doc.getJSONArray("_childDocuments_").length(); j++) {
672
					JSONObject childItem = doc.getJSONArray("_childDocuments_").getJSONObject(j);
673
					int itemId = childItem.getInt("itemId_i");
674
					TagListing tl = itemTagListingMap.get(itemId);
675
					if (tl == null) {
676
						logger.warn("Could not find item id {}", itemId);
24091 tejbeer 677
						continue;
678
					}
26515 amit.gupta 679
					if (hotDeal) {
680
						if (!tl.isHotDeals()) {
681
							continue;
682
						}
24091 tejbeer 683
					}
26515 amit.gupta 684
					float sellingPrice = (float) childItem.getDouble("sellingPrice_f");
685
					if (fofoAvailabilityInfoMap.containsKey(itemId)) {
686
						if (fofoAvailabilityInfoMap.get(itemId).getSellingPrice() > sellingPrice) {
687
							fofoAvailabilityInfoMap.get(itemId).setSellingPrice(sellingPrice);
688
							fofoAvailabilityInfoMap.get(itemId).setMop((float) childItem.getDouble("mop_f"));
689
						}
24091 tejbeer 690
					} else {
26515 amit.gupta 691
						FofoAvailabilityInfo fdi = new FofoAvailabilityInfo();
692
						fdi.setSellingPrice((float) childItem.getDouble("sellingPrice_f"));
26846 tejbeer 693
						fdi.setMrp((double) tl.getMrp());
26515 amit.gupta 694
						fdi.setMop((float) childItem.getDouble("mop_f"));
695
						fdi.setColor(childItem.has("color_s") ? childItem.getString("color_s") : "");
696
						fdi.setTagId(childItem.getInt("tagId_i"));
697
						fdi.setItem_id(itemId);
26696 amit.gupta 698
						Float cashBack = schemeService.getItemSchemeCashBack().get(itemId);
26846 tejbeer 699
						cashBack = cashBack == null ? 0 : cashBack;
26696 amit.gupta 700
						fdi.setCashback(cashBack);
26515 amit.gupta 701
						Item item = itemRepository.selectById(itemId);
702
						// In case its tampered glass moq should be 5
703
						if (item.getCategoryId() == 10020) {
704
							fdi.setMinBuyQuantity(10);
705
						} else {
706
							fdi.setMinBuyQuantity(1);
24091 tejbeer 707
						}
26515 amit.gupta 708
						if (hotDeal || !tl.isActive()) {
26589 amit.gupta 709
 
26515 amit.gupta 710
							int totalAvailability = 0; // Using item availability
711
							// cache for now but can be
712
							// changed to
713
							// use caching later.
714
							try {
715
								ItemAvailabilityCache iac = itemAvailabilityCacheRepository.selectByItemId(itemId);
716
								totalAvailability = iac.getTotalAvailability();
717
								fdi.setAvailability(totalAvailability);
718
							} catch (Exception e) {
719
								continue;
720
							}
721
							if (totalAvailability <= 0) {
722
								continue;
723
							}
724
						} else {
725
							// For accessories item availability should at be ordered for Rs.1000
726
							fdi.setAvailability(100);
24091 tejbeer 727
						}
26515 amit.gupta 728
						fdi.setQuantityStep(1);
729
						fdi.setMaxQuantity(Math.min(fdi.getAvailability(), 100));
730
						fofoAvailabilityInfoMap.put(itemId, fdi);
24091 tejbeer 731
					}
732
				}
733
			}
734
			if (fofoAvailabilityInfoMap.values().size() > 0) {
735
				ffdr.setItems(new ArrayList<FofoAvailabilityInfo>(fofoAvailabilityInfoMap.values()));
736
				dealResponse.add(ffdr);
737
			}
738
		}
739
		return dealResponse;
740
 
741
	}
742
 
25968 amit.gupta 743
	private List<FofoCatalogResponse> getCatalogSingleSkuResponse(JSONArray docs, Map<Integer, Integer> itemFilter,
744
			boolean hotDeal) throws ProfitMandiBusinessException {
25879 amit.gupta 745
		Map<Integer, TagListing> itemTagListingMap = null;
746
		List<FofoCatalogResponse> dealResponse = new ArrayList<>();
747
		List<Integer> tagIds = Arrays.asList(4);
26589 amit.gupta 748
 
25968 amit.gupta 749
		itemTagListingMap = tagListingRepository.selectByItemIdsAndTagIds(itemFilter.keySet(), new HashSet<>(tagIds))
750
				.stream().collect(Collectors.toMap(x -> x.getItemId(), x -> x));
25880 amit.gupta 751
 
25879 amit.gupta 752
		for (int i = 0; i < docs.length(); i++) {
753
			Map<Integer, FofoAvailabilityInfo> fofoAvailabilityInfoMap = new HashMap<>();
754
			JSONObject doc = docs.getJSONObject(i);
25880 amit.gupta 755
 
25879 amit.gupta 756
			for (int j = 0; j < doc.getJSONArray("_childDocuments_").length(); j++) {
757
				JSONObject childItem = doc.getJSONArray("_childDocuments_").getJSONObject(j);
758
				int itemId = childItem.getInt("itemId_i");
759
				TagListing tl = itemTagListingMap.get(itemId);
26589 amit.gupta 760
				if (tl == null) {
25968 amit.gupta 761
					continue;
762
				}
25879 amit.gupta 763
				if (hotDeal) {
764
					if (!tl.isHotDeals()) {
765
						continue;
766
					}
767
				}
768
				float sellingPrice = (float) childItem.getDouble("sellingPrice_f");
769
				if (fofoAvailabilityInfoMap.containsKey(itemId)) {
770
					if (fofoAvailabilityInfoMap.get(itemId).getSellingPrice() > sellingPrice) {
771
						fofoAvailabilityInfoMap.get(itemId).setSellingPrice(sellingPrice);
772
						fofoAvailabilityInfoMap.get(itemId).setMop((float) childItem.getDouble("mop_f"));
773
					}
774
				} else {
775
					FofoAvailabilityInfo fdi = new FofoAvailabilityInfo();
776
					fdi.setSellingPrice((float) childItem.getDouble("sellingPrice_f"));
777
					fdi.setMop((float) childItem.getDouble("mop_f"));
26665 amit.gupta 778
					fdi.setMop((float) tl.getMrp());
25879 amit.gupta 779
					fdi.setColor(childItem.has("color_s") ? childItem.getString("color_s") : "");
780
					fdi.setTagId(childItem.getInt("tagId_i"));
781
					fdi.setItem_id(itemId);
782
					Item item = itemRepository.selectById(itemId);
783
					// In case its tampered glass moq should be 5
784
					if (item.getCategoryId() == 10020) {
785
						fdi.setMinBuyQuantity(10);
786
					} else {
787
						fdi.setMinBuyQuantity(1);
788
					}
26050 amit.gupta 789
					fdi.setAvailability(itemFilter.get(itemId));
25879 amit.gupta 790
					fdi.setQuantityStep(1);
791
					fdi.setMaxQuantity(Math.min(fdi.getAvailability(), 100));
792
					fofoAvailabilityInfoMap.put(itemId, fdi);
793
				}
794
			}
795
			if (fofoAvailabilityInfoMap.values().size() > 0) {
25880 amit.gupta 796
				for (FofoAvailabilityInfo fofoAvailabilityInfo : fofoAvailabilityInfoMap.values()) {
25879 amit.gupta 797
					FofoCatalogResponse ffdr = new FofoCatalogResponse();
798
					ffdr.setCatalogId(doc.getInt("catalogId_i"));
799
					ffdr.setImageUrl(doc.getString("imageUrl_s"));
800
					ffdr.setTitle(doc.getString("title_s"));
801
					try {
802
						ffdr.setFeature(doc.getString("feature_s"));
803
					} catch (Exception e) {
804
						ffdr.setFeature(null);
805
						logger.info("Could not find Feature_s for {}", ffdr.getCatalogId());
806
					}
807
					ffdr.setBrand(doc.getJSONArray("brand_ss").getString(0));
808
					ffdr.setItems(Arrays.asList(fofoAvailabilityInfo));
809
					dealResponse.add(ffdr);
810
				}
811
			}
812
		}
813
		return dealResponse;
25880 amit.gupta 814
 
25879 amit.gupta 815
	}
25967 amit.gupta 816
}