Subversion Repositories SmartDukaan

Rev

Rev 24024 | Rev 24032 | 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;
23814 amit.gupta 10
import java.util.stream.Collectors;
21339 kshitij.so 11
 
12
import javax.servlet.http.HttpServletRequest;
13
 
22319 amit.gupta 14
import org.apache.commons.lang3.StringUtils;
23532 amit.gupta 15
import org.apache.http.conn.HttpHostConnectException;
23786 amit.gupta 16
import org.apache.logging.log4j.LogManager;
17
import org.apache.logging.log4j.Logger;
22319 amit.gupta 18
import org.json.JSONArray;
19
import org.json.JSONObject;
22273 amit.gupta 20
import org.springframework.beans.factory.annotation.Autowired;
21339 kshitij.so 21
import org.springframework.beans.factory.annotation.Value;
22
import org.springframework.http.HttpStatus;
23
import org.springframework.http.MediaType;
24
import org.springframework.http.ResponseEntity;
25
import org.springframework.stereotype.Controller;
22286 amit.gupta 26
import org.springframework.transaction.annotation.Transactional;
21339 kshitij.so 27
import org.springframework.web.bind.annotation.PathVariable;
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.Json;
33
import com.eclipsesource.json.JsonArray;
34
import com.eclipsesource.json.JsonObject;
35
import com.eclipsesource.json.JsonValue;
36
import com.google.gson.Gson;
21356 kshitij.so 37
import com.google.gson.reflect.TypeToken;
21643 ashik.ali 38
import com.spice.profitmandi.common.enumuration.SchemeType;
21339 kshitij.so 39
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
40
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22289 amit.gupta 41
import com.spice.profitmandi.common.model.UserInfo;
21643 ashik.ali 42
import com.spice.profitmandi.common.web.client.RestClient;
22319 amit.gupta 43
import com.spice.profitmandi.common.web.util.ResponseSender;
23426 amit.gupta 44
import com.spice.profitmandi.dao.entity.catalog.Item;
23814 amit.gupta 45
import com.spice.profitmandi.dao.entity.catalog.TagListing;
23861 amit.gupta 46
import com.spice.profitmandi.dao.entity.inventory.ItemAvailabilityCache;
22361 amit.gupta 47
import com.spice.profitmandi.dao.model.UserCart;
23426 amit.gupta 48
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
23814 amit.gupta 49
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
22333 amit.gupta 50
import com.spice.profitmandi.dao.repository.dtr.Mongo;
22361 amit.gupta 51
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
22989 amit.gupta 52
import com.spice.profitmandi.dao.repository.inventory.ItemAvailabilityCacheRepository;
23798 amit.gupta 53
import com.spice.profitmandi.service.authentication.RoleManager;
22287 amit.gupta 54
import com.spice.profitmandi.service.pricing.PricingService;
22952 amit.gupta 55
import com.spice.profitmandi.web.res.AvailabilityInfo;
21356 kshitij.so 56
import com.spice.profitmandi.web.res.DealBrands;
21339 kshitij.so 57
import com.spice.profitmandi.web.res.DealObjectResponse;
58
import com.spice.profitmandi.web.res.DealsResponse;
22328 amit.gupta 59
import com.spice.profitmandi.web.res.FofoAvailabilityInfo;
60
import com.spice.profitmandi.web.res.FofoCatalogResponse;
21339 kshitij.so 61
 
62
import io.swagger.annotations.ApiImplicitParam;
63
import io.swagger.annotations.ApiImplicitParams;
64
import io.swagger.annotations.ApiOperation;
65
 
66
@Controller
22319 amit.gupta 67
@Transactional(rollbackFor = Throwable.class)
21339 kshitij.so 68
public class DealsController {
69
 
23568 govind 70
	private static final Logger logger = LogManager.getLogger(DealsController.class);
21339 kshitij.so 71
 
72
	@Value("${python.api.host}")
73
	private String host;
23816 amit.gupta 74
 
21339 kshitij.so 75
	@Value("${python.api.port}")
76
	private int port;
23816 amit.gupta 77
 
78
	// This is now unused as we are not supporting multiple companies.
23300 amit.gupta 79
	@Value("${gadgetCops.invoice.cc}")
23816 amit.gupta 80
	private String[] ccGadgetCopInvoiceTo;
22319 amit.gupta 81
 
82
	@Autowired
83
	private PricingService pricingService;
23816 amit.gupta 84
 
22273 amit.gupta 85
	@Autowired
22333 amit.gupta 86
	private Mongo mongoClient;
23816 amit.gupta 87
 
22333 amit.gupta 88
	@Autowired
22361 amit.gupta 89
	private UserAccountRepository userAccountRepository;
23816 amit.gupta 90
 
22989 amit.gupta 91
	@Autowired
22931 ashik.ali 92
	private ResponseSender<?> responseSender;
23816 amit.gupta 93
 
22554 amit.gupta 94
	@Autowired
23814 amit.gupta 95
	private TagListingRepository tagListingRepository;
23816 amit.gupta 96
 
23814 amit.gupta 97
	@Autowired
23426 amit.gupta 98
	private ItemRepository itemRepository;
23816 amit.gupta 99
 
23786 amit.gupta 100
	@Autowired
23861 amit.gupta 101
	private ItemAvailabilityCacheRepository itemAvailabilityCacheRepository;
102
 
103
	@Autowired
23798 amit.gupta 104
	private RoleManager roleManagerService;
23816 amit.gupta 105
 
22336 amit.gupta 106
	List<String> filterableParams = Arrays.asList("brand");
21339 kshitij.so 107
 
22319 amit.gupta 108
	@RequestMapping(value = ProfitMandiConstants.URL_DEALS, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
21339 kshitij.so 109
	@ApiImplicitParams({
22319 amit.gupta 110
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
21339 kshitij.so 111
	@ApiOperation(value = "Get deals")
22319 amit.gupta 112
	public ResponseEntity<?> getDeals(HttpServletRequest request, @RequestParam(value = "categoryId") String categoryId,
113
			@RequestParam(value = "offset") String offset, @RequestParam(value = "limit") String limit,
114
			@RequestParam(value = "sort", required = false) String sort,
115
			@RequestParam(value = "direction", required = false) String direction,
23816 amit.gupta 116
			@RequestParam(value = "filterData", required = false) String filterData)
117
			throws ProfitMandiBusinessException {
22319 amit.gupta 118
		logger.info("Request " + request.getParameterMap());
21339 kshitij.so 119
		String response = null;
22319 amit.gupta 120
		int userId = (int) request.getAttribute("userId");
23816 amit.gupta 121
 
122
		// If pincode belongs to Specific warehouse marked
123
		// availability should be fetched for that warehouse only
124
		// show only skus belonging to that specific
125
		// else use normal flow
22319 amit.gupta 126
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
127
		// TODO: move to properties
128
		String uri = "/deals/" + userId;
23532 amit.gupta 129
		RestClient rc = new RestClient();
21339 kshitij.so 130
		Map<String, String> params = new HashMap<>();
131
		params.put("offset", offset);
132
		params.put("limit", limit);
133
		params.put("categoryId", categoryId);
134
		params.put("direction", direction);
135
		params.put("sort", sort);
136
		params.put("filterData", filterData);
22272 amit.gupta 137
		params.put("source", "deals");
23786 amit.gupta 138
		if (roleManagerService.isPartner(userInfo.getRoleIds())) {
22289 amit.gupta 139
			params.put("tag_ids", getCommaSeparateTags(userId));
140
		}
21356 kshitij.so 141
		List<Object> responseObject = new ArrayList<>();
23532 amit.gupta 142
		try {
23816 amit.gupta 143
			response = rc.get(SchemeType.HTTP, host, port, uri, params);
23532 amit.gupta 144
		} catch (HttpHostConnectException e) {
145
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
146
		}
23816 amit.gupta 147
 
21339 kshitij.so 148
		JsonArray result_json = Json.parse(response).asArray();
22319 amit.gupta 149
		for (JsonValue j : result_json) {
23816 amit.gupta 150
			// logger.info("res " + j.asArray());
21339 kshitij.so 151
			List<Object> innerObject = new ArrayList<>();
22319 amit.gupta 152
			for (JsonValue jsonObject : j.asArray()) {
21356 kshitij.so 153
				innerObject.add(toDealObject(jsonObject.asObject()));
21339 kshitij.so 154
			}
22319 amit.gupta 155
			if (innerObject.size() > 0) {
21339 kshitij.so 156
				responseObject.add(innerObject);
157
			}
158
		}
23022 ashik.ali 159
		return responseSender.ok(responseObject);
21339 kshitij.so 160
	}
161
 
23816 amit.gupta 162
	private String getCommaSeparateTags(int userId) {
22361 amit.gupta 163
		UserCart uc = userAccountRepository.getUserCart(userId);
164
		List<Integer> tagIds = pricingService.getTagsIdsByRetailerId(uc.getUserId());
22287 amit.gupta 165
		List<String> strTagIds = new ArrayList<>();
166
		for (Integer tagId : tagIds) {
167
			strTagIds.add(String.valueOf(tagId));
22273 amit.gupta 168
		}
22287 amit.gupta 169
		return String.join(",", strTagIds);
22273 amit.gupta 170
	}
171
 
22319 amit.gupta 172
	@ApiImplicitParams({
173
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
174
	@RequestMapping(value = "/fofo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
175
	public ResponseEntity<?> getFofo(HttpServletRequest request, @RequestParam(value = "categoryId") String categoryId,
176
			@RequestParam(value = "offset") String offset, @RequestParam(value = "limit") String limit,
23816 amit.gupta 177
			@RequestParam(value = "sort", required = false) String sort,
178
			@RequestParam(value = "brand", required = false) String brand,
23814 amit.gupta 179
			@RequestParam(value = "hotDeal", required = false) boolean hotDeal) throws Throwable {
22328 amit.gupta 180
		List<FofoCatalogResponse> dealResponse = new ArrayList<>();
22319 amit.gupta 181
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
23786 amit.gupta 182
		if (roleManagerService.isPartner(userInfo.getRoleIds())) {
22361 amit.gupta 183
			UserCart uc = userAccountRepository.getUserCart(userInfo.getUserId());
184
			List<Integer> tagIds = pricingService.getTagsIdsByRetailerId(uc.getUserId());
23816 amit.gupta 185
			RestClient rc = new RestClient();
22319 amit.gupta 186
			Map<String, String> params = new HashMap<>();
22336 amit.gupta 187
			List<String> mandatoryQ = new ArrayList<>();
23816 amit.gupta 188
			if (brand != null) {
189
 
24031 amit.gupta 190
				mandatoryQ.add(String.format("+(categoryId_i:%d) +(brand_s:%s) +{!parent which=\"brand_s:%s\"} tagId_i:(%s)", categoryId, brand, brand,
23816 amit.gupta 191
						StringUtils.join(tagIds, " ")));
23814 amit.gupta 192
			} else if (hotDeal) {
23816 amit.gupta 193
				mandatoryQ.add(String.format("+{!parent which=\"hot_deals_b=true\"} tagId_i:(%s)",
194
						StringUtils.join(tagIds, " ")));
22347 amit.gupta 195
			} else {
23816 amit.gupta 196
				mandatoryQ.add(
197
						String.format("+{!parent which=\"id:catalog*\"} tagId_i:(%s)", StringUtils.join(tagIds, " ")));
22336 amit.gupta 198
			}
23816 amit.gupta 199
			params.put("q", StringUtils.join(mandatoryQ, " "));
22319 amit.gupta 200
			params.put("fl", "*, [child parentFilter=id:catalog*]");
24031 amit.gupta 201
			params.put("sort", "rank_i asc, create_s desc");
22319 amit.gupta 202
			params.put("start", String.valueOf(offset));
203
			params.put("rows", String.valueOf(limit));
204
			params.put("wt", "json");
23532 amit.gupta 205
			String response = null;
206
			try {
23816 amit.gupta 207
				response = rc.get(SchemeType.HTTP, "dtr", 8984, "solr/demo/select", params);
23532 amit.gupta 208
			} catch (HttpHostConnectException e) {
209
				throw new ProfitMandiBusinessException("", "", "Could not connect to host");
210
			}
22319 amit.gupta 211
			JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
212
			JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
23816 amit.gupta 213
			Map<Integer, TagListing> itemTagListingMap = null;
24024 amit.gupta 214
			if (docs.length() > 0) {
23816 amit.gupta 215
				HashSet<Integer> itemsSet = new HashSet<>();
216
				for (int i = 0; i < docs.length(); i++) {
217
					JSONObject doc = docs.getJSONObject(i);
218
					for (int j = 0; j < doc.getJSONArray("_childDocuments_").length(); j++) {
219
						JSONObject childItem = doc.getJSONArray("_childDocuments_").getJSONObject(j);
220
						int itemId = childItem.getInt("itemId_i");
221
						itemsSet.add(itemId);
222
					}
23814 amit.gupta 223
				}
23816 amit.gupta 224
				itemTagListingMap = tagListingRepository.selectByItemIdsAndTagIds(itemsSet, new HashSet<>(tagIds))
225
						.stream().collect(Collectors.toMap(x -> x.getItemId(), x -> x));
23814 amit.gupta 226
			}
23816 amit.gupta 227
 
228
			for (int i = 0; i < docs.length(); i++) {
23814 amit.gupta 229
				Map<Integer, FofoAvailabilityInfo> fofoAvailabilityInfoMap = new HashMap<>();
230
				JSONObject doc = docs.getJSONObject(i);
22328 amit.gupta 231
				FofoCatalogResponse ffdr = new FofoCatalogResponse();
22319 amit.gupta 232
				ffdr.setCatalogId(doc.getInt("catalogId_i"));
233
				ffdr.setImageUrl(doc.getString("imageUrl_s"));
234
				ffdr.setTitle(doc.getString("title_s"));
22333 amit.gupta 235
				ffdr.setBrand(doc.getString("brand_s"));
23816 amit.gupta 236
 
237
				for (int j = 0; j < doc.getJSONArray("_childDocuments_").length(); j++) {
22323 amit.gupta 238
					JSONObject childItem = doc.getJSONArray("_childDocuments_").getJSONObject(j);
22319 amit.gupta 239
					int itemId = childItem.getInt("itemId_i");
24009 amit.gupta 240
					TagListing tl = itemTagListingMap.get(itemId);
23816 amit.gupta 241
					if (hotDeal) {
23861 amit.gupta 242
						if (!tl.isHotDeals()) {
23816 amit.gupta 243
							continue;
244
						}
23814 amit.gupta 245
					}
23816 amit.gupta 246
					float sellingPrice = (float) childItem.getDouble("sellingPrice_f");
247
					if (fofoAvailabilityInfoMap.containsKey(itemId)) {
248
						if (fofoAvailabilityInfoMap.get(itemId).getSellingPrice() > sellingPrice) {
22328 amit.gupta 249
							fofoAvailabilityInfoMap.get(itemId).setSellingPrice(sellingPrice);
23816 amit.gupta 250
							fofoAvailabilityInfoMap.get(itemId).setMop((float) childItem.getDouble("mop_f"));
251
						}
22319 amit.gupta 252
					} else {
23816 amit.gupta 253
						FofoAvailabilityInfo fdi = new FofoAvailabilityInfo();
254
						fdi.setSellingPrice((float) childItem.getDouble("sellingPrice_f"));
255
						fdi.setMop((float) childItem.getDouble("mop_f"));
256
						fdi.setColor(childItem.has("color_s") ? childItem.getString("color_s") : "");
257
						fdi.setTagId(childItem.getInt("tagId_i"));
258
						fdi.setItem_id(itemId);
259
						Item item = itemRepository.selectById(itemId);
260
						// In case its tampered glass moq should be 5
261
						if (item.getCategoryId() == 10020) {
262
							fdi.setMinBuyQuantity(5);
263
						} else {
264
							fdi.setMinBuyQuantity(1);
265
						}
24009 amit.gupta 266
						if (hotDeal || !tl.isActive()) {
23861 amit.gupta 267
 
24016 amit.gupta 268
								int totalAvailability = 0; // Using item availability cache for now but can be changed to
23861 amit.gupta 269
														// use caching later.
270
							try {
271
								ItemAvailabilityCache iac = itemAvailabilityCacheRepository.selectByItemId(itemId);
272
								totalAvailability = iac.getTotalAvailability();
273
								fdi.setAvailability(totalAvailability);
274
							} catch (Exception e) {
275
								continue;
276
							}
277
							if (totalAvailability <= 0) {
278
								continue;
279
							}
280
						} else {
281
							fdi.setAvailability(10);
282
						}
23816 amit.gupta 283
						fdi.setQuantityStep(1);
284
						fdi.setMaxQuantity(Math.min(fdi.getAvailability(), 100));
285
						fofoAvailabilityInfoMap.put(itemId, fdi);
22319 amit.gupta 286
					}
287
				}
23816 amit.gupta 288
				if (fofoAvailabilityInfoMap.values().size() > 0) {
22988 amit.gupta 289
					ffdr.setItems(new ArrayList<FofoAvailabilityInfo>(fofoAvailabilityInfoMap.values()));
290
					dealResponse.add(ffdr);
291
				}
22319 amit.gupta 292
			}
23816 amit.gupta 293
 
22319 amit.gupta 294
		} else {
23816 amit.gupta 295
			return responseSender.badRequest(
296
					new ProfitMandiBusinessException("Retailer id", userInfo.getUserId(), "NOT_FOFO_RETAILER"));
22319 amit.gupta 297
		}
298
		return responseSender.ok(dealResponse);
299
	}
22273 amit.gupta 300
 
22319 amit.gupta 301
	@RequestMapping(value = "/online-deals", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
22272 amit.gupta 302
	@ApiImplicitParams({
22319 amit.gupta 303
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
22272 amit.gupta 304
	@ApiOperation(value = "Get online deals")
22319 amit.gupta 305
	public ResponseEntity<?> getOnlineDeals(HttpServletRequest request,
306
			@RequestParam(value = "categoryId") String categoryId, @RequestParam(value = "offset") String offset,
307
			@RequestParam(value = "limit") String limit, @RequestParam(value = "sort", required = false) String sort,
308
			@RequestParam(value = "direction", required = false) String direction,
309
			@RequestParam(value = "filterData", required = false) String filterData) throws Throwable {
310
		logger.info("Request " + request.getParameterMap());
22272 amit.gupta 311
		String response = null;
22319 amit.gupta 312
		int userId = (int) request.getAttribute("userId");
22289 amit.gupta 313
 
22319 amit.gupta 314
		String uri = "/deals/" + userId;
23532 amit.gupta 315
		RestClient rc = new RestClient();
22272 amit.gupta 316
		Map<String, String> params = new HashMap<>();
317
		params.put("offset", offset);
318
		params.put("limit", limit);
319
		params.put("categoryId", categoryId);
320
		params.put("direction", direction);
321
		params.put("sort", sort);
322
		params.put("source", "online");
323
		params.put("filterData", filterData);
23816 amit.gupta 324
		/*
325
		 * if (userInfo.getRoleNames().contains(RoleType.FOFO.toString())) {
326
		 * params.put("tag_ids", getCommaSeparateTags(userId)); }
327
		 */
22272 amit.gupta 328
		List<Object> responseObject = new ArrayList<>();
23532 amit.gupta 329
		try {
330
			response = rc.get(SchemeType.HTTP, host, port, uri, params);
331
		} catch (HttpHostConnectException e) {
332
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
333
		}
22931 ashik.ali 334
 
22272 amit.gupta 335
		JsonArray result_json = Json.parse(response).asArray();
22319 amit.gupta 336
		for (JsonValue j : result_json) {
23816 amit.gupta 337
			// logger.info("res " + j.asArray());
22272 amit.gupta 338
			List<Object> innerObject = new ArrayList<>();
22319 amit.gupta 339
			for (JsonValue jsonObject : j.asArray()) {
22272 amit.gupta 340
				innerObject.add(toDealObject(jsonObject.asObject()));
341
			}
22319 amit.gupta 342
			if (innerObject.size() > 0) {
22272 amit.gupta 343
				responseObject.add(innerObject);
344
			}
345
		}
23022 ashik.ali 346
		return responseSender.ok(responseObject);
22272 amit.gupta 347
	}
348
 
22319 amit.gupta 349
	/*
23816 amit.gupta 350
	 * @RequestMapping(value = "/direct-deals", method=RequestMethod.GET,produces =
351
	 * MediaType.APPLICATION_JSON_VALUE)
22319 amit.gupta 352
	 * 
353
	 * @ApiImplicitParams({
354
	 * 
23816 amit.gupta 355
	 * @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true,
356
	 * dataType = "string", paramType = "header") }) public ResponseEntity<?>
357
	 * getDirectDeals(HttpServletRequest request, @RequestParam(value="categoryId")
358
	 * String categoryId,@RequestParam(value="offset") String offset,
22319 amit.gupta 359
	 * 
360
	 * @RequestParam(value="limit") String limit, @RequestParam(value="sort",
23816 amit.gupta 361
	 * required=false) String sort, @RequestParam(value="direction", required=false)
362
	 * String direction,
22319 amit.gupta 363
	 * 
364
	 * @RequestParam(value="filterData", required=false) String filterData ){
365
	 * 
366
	 * return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK); }
367
	 */
368
 
369
	private Object toDealObject(JsonObject jsonObject) {
370
		if (jsonObject.get("dealObject") != null && jsonObject.get("dealObject").asInt() == 1) {
21339 kshitij.so 371
			return new Gson().fromJson(jsonObject.toString(), DealObjectResponse.class);
372
		}
373
		return new Gson().fromJson(jsonObject.toString(), DealsResponse.class);
374
	}
22319 amit.gupta 375
 
376
	@RequestMapping(value = ProfitMandiConstants.URL_BRANDS, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
21356 kshitij.so 377
	@ApiImplicitParams({
22319 amit.gupta 378
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
21356 kshitij.so 379
	@ApiOperation(value = "Get brand list and count for category")
22319 amit.gupta 380
	public ResponseEntity<?> getBrands(HttpServletRequest request,
23816 amit.gupta 381
			@RequestParam(value = "category_id") String category_id) throws ProfitMandiBusinessException {
22319 amit.gupta 382
		logger.info("Request " + request.getParameterMap());
21356 kshitij.so 383
		String response = null;
22319 amit.gupta 384
		// TODO: move to properties
21356 kshitij.so 385
		String uri = ProfitMandiConstants.URL_BRANDS;
23532 amit.gupta 386
		RestClient rc = new RestClient();
21356 kshitij.so 387
		Map<String, String> params = new HashMap<>();
388
		params.put("category_id", category_id);
21358 kshitij.so 389
		List<DealBrands> dealBrandsResponse = null;
23532 amit.gupta 390
		try {
391
			response = rc.get(SchemeType.HTTP, host, port, uri, params);
392
		} catch (HttpHostConnectException e) {
393
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
394
		}
23816 amit.gupta 395
 
22319 amit.gupta 396
		dealBrandsResponse = new Gson().fromJson(response, new TypeToken<List<DealBrands>>() {
397
		}.getType());
23022 ashik.ali 398
 
399
		return responseSender.ok(dealBrandsResponse);
21356 kshitij.so 400
	}
22319 amit.gupta 401
 
402
	@RequestMapping(value = ProfitMandiConstants.URL_UNIT_DEAL, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
21445 kshitij.so 403
	@ApiImplicitParams({
22319 amit.gupta 404
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
21445 kshitij.so 405
	@ApiOperation(value = "Get unit deal object")
23816 amit.gupta 406
	public ResponseEntity<?> getUnitDeal(HttpServletRequest request, @PathVariable(value = "id") long id)
407
			throws ProfitMandiBusinessException {
21445 kshitij.so 408
		String response = null;
22319 amit.gupta 409
		// TODO: move to properties
410
		String uri = "getDealById/" + id;
411
		System.out.println("Unit deal " + uri);
23532 amit.gupta 412
		RestClient rc = new RestClient();
21445 kshitij.so 413
		Map<String, String> params = new HashMap<>();
414
		DealsResponse dealsResponse = null;
23532 amit.gupta 415
		try {
416
			response = rc.get(SchemeType.HTTP, host, port, uri, params);
417
		} catch (HttpHostConnectException e) {
418
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
419
		}
23816 amit.gupta 420
 
21445 kshitij.so 421
		JsonObject result_json = Json.parse(response).asObject();
22319 amit.gupta 422
		if (!result_json.isEmpty()) {
21445 kshitij.so 423
			dealsResponse = new Gson().fromJson(response, DealsResponse.class);
22952 amit.gupta 424
			Iterator<AvailabilityInfo> iter = dealsResponse.getAvailabilityInfo().iterator();
23816 amit.gupta 425
			while (iter.hasNext()) {
22952 amit.gupta 426
				AvailabilityInfo ai = iter.next();
23816 amit.gupta 427
				if (ai.getAvailability() <= 0)
22952 amit.gupta 428
					iter.remove();
429
			}
21445 kshitij.so 430
		}
23816 amit.gupta 431
		/*
432
		 * final ProfitMandiResponse<?> profitMandiResponse = new
433
		 * ProfitMandiResponse<>(LocalDateTime.now(),
434
		 * request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK,
435
		 * ResponseStatus.SUCCESS, dealsResponse);
436
		 */
22952 amit.gupta 437
		return responseSender.ok(dealsResponse);
21445 kshitij.so 438
	}
23816 amit.gupta 439
 
22333 amit.gupta 440
	@RequestMapping(value = "/fofo/brands", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
24031 amit.gupta 441
	public ResponseEntity<?> getBrandsToDisplay(@RequestParam(required=false) int categoryId) {
442
		return new ResponseEntity<>(mongoClient.getBrandsToDisplay(categoryId), HttpStatus.OK);
22333 amit.gupta 443
	}
23816 amit.gupta 444
 
22446 amit.gupta 445
	@RequestMapping(value = "/banners/{bannerType}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
22448 amit.gupta 446
	public ResponseEntity<?> getBanners(@PathVariable String bannerType) {
22447 amit.gupta 447
		return new ResponseEntity<>(mongoClient.getBannersByType(bannerType), HttpStatus.OK);
22446 amit.gupta 448
	}
23816 amit.gupta 449
 
23793 tejbeer 450
	@RequestMapping(value = "/deals/subCategories", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
451
	public ResponseEntity<?> getSubcategoriesToDisplay() {
452
		return new ResponseEntity<>(mongoClient.getSubcategoriesToDisplay(), HttpStatus.OK);
453
	}
23816 amit.gupta 454
 
22406 amit.gupta 455
	@ApiImplicitParams({
23816 amit.gupta 456
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
22401 amit.gupta 457
	@RequestMapping(value = "/deals/skus/{skus}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
22931 ashik.ali 458
	public ResponseEntity<?> getDealsBySkus(@PathVariable String skus) throws ProfitMandiBusinessException {
22401 amit.gupta 459
		StringBuffer sb = new StringBuffer("/getDealsForNotification/");
460
		String uri = sb.append(skus).toString();
23532 amit.gupta 461
		RestClient rc = new RestClient();
462
		String response;
463
		try {
464
			response = rc.get(SchemeType.HTTP, host, port, uri, new HashMap<>());
23816 amit.gupta 465
		} catch (HttpHostConnectException e) {
23532 amit.gupta 466
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
467
		}
22406 amit.gupta 468
		JsonArray result_json = Json.parse(response).asArray();
22407 amit.gupta 469
		List<Object> responseObject = new ArrayList<>();
470
		for (JsonValue j : result_json) {
23816 amit.gupta 471
			// logger.info("res " + j.asArray());
22407 amit.gupta 472
			List<Object> innerObject = new ArrayList<>();
473
			for (JsonValue jsonObject : j.asArray()) {
474
				innerObject.add(toDealObject(jsonObject.asObject()));
475
			}
476
			if (innerObject.size() > 0) {
477
				responseObject.add(innerObject);
478
			}
479
		}
22408 amit.gupta 480
		return responseSender.ok(responseObject);
22401 amit.gupta 481
	}
21339 kshitij.so 482
 
483
}