Subversion Repositories SmartDukaan

Rev

Rev 24032 | Rev 24035 | 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)
24034 amit.gupta 175
	public ResponseEntity<?> getFofo(HttpServletRequest request, 
176
			@RequestParam(value = "categoryId", required=false, defaultValue="(3 OR 6)") String categoryId,
22319 amit.gupta 177
			@RequestParam(value = "offset") String offset, @RequestParam(value = "limit") String limit,
23816 amit.gupta 178
			@RequestParam(value = "sort", required = false) String sort,
179
			@RequestParam(value = "brand", required = false) String brand,
24034 amit.gupta 180
			@RequestParam(value = "hotDeal", required = false) boolean hotDeal) 
181
		throws Throwable {
22328 amit.gupta 182
		List<FofoCatalogResponse> dealResponse = new ArrayList<>();
22319 amit.gupta 183
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
23786 amit.gupta 184
		if (roleManagerService.isPartner(userInfo.getRoleIds())) {
22361 amit.gupta 185
			UserCart uc = userAccountRepository.getUserCart(userInfo.getUserId());
186
			List<Integer> tagIds = pricingService.getTagsIdsByRetailerId(uc.getUserId());
23816 amit.gupta 187
			RestClient rc = new RestClient();
22319 amit.gupta 188
			Map<String, String> params = new HashMap<>();
22336 amit.gupta 189
			List<String> mandatoryQ = new ArrayList<>();
23816 amit.gupta 190
			if (brand != null) {
191
 
24032 amit.gupta 192
				mandatoryQ.add(String.format("+(categoryId_i:%s) +(brand_s:%s) +{!parent which=\"brand_s:%s\"} tagId_i:(%s)", categoryId, brand, brand,
23816 amit.gupta 193
						StringUtils.join(tagIds, " ")));
23814 amit.gupta 194
			} else if (hotDeal) {
23816 amit.gupta 195
				mandatoryQ.add(String.format("+{!parent which=\"hot_deals_b=true\"} tagId_i:(%s)",
196
						StringUtils.join(tagIds, " ")));
22347 amit.gupta 197
			} else {
23816 amit.gupta 198
				mandatoryQ.add(
199
						String.format("+{!parent which=\"id:catalog*\"} tagId_i:(%s)", StringUtils.join(tagIds, " ")));
22336 amit.gupta 200
			}
23816 amit.gupta 201
			params.put("q", StringUtils.join(mandatoryQ, " "));
22319 amit.gupta 202
			params.put("fl", "*, [child parentFilter=id:catalog*]");
24031 amit.gupta 203
			params.put("sort", "rank_i asc, create_s desc");
22319 amit.gupta 204
			params.put("start", String.valueOf(offset));
205
			params.put("rows", String.valueOf(limit));
206
			params.put("wt", "json");
23532 amit.gupta 207
			String response = null;
208
			try {
23816 amit.gupta 209
				response = rc.get(SchemeType.HTTP, "dtr", 8984, "solr/demo/select", params);
23532 amit.gupta 210
			} catch (HttpHostConnectException e) {
211
				throw new ProfitMandiBusinessException("", "", "Could not connect to host");
212
			}
22319 amit.gupta 213
			JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
214
			JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
23816 amit.gupta 215
			Map<Integer, TagListing> itemTagListingMap = null;
24024 amit.gupta 216
			if (docs.length() > 0) {
23816 amit.gupta 217
				HashSet<Integer> itemsSet = new HashSet<>();
218
				for (int i = 0; i < docs.length(); i++) {
219
					JSONObject doc = docs.getJSONObject(i);
220
					for (int j = 0; j < doc.getJSONArray("_childDocuments_").length(); j++) {
221
						JSONObject childItem = doc.getJSONArray("_childDocuments_").getJSONObject(j);
222
						int itemId = childItem.getInt("itemId_i");
223
						itemsSet.add(itemId);
224
					}
23814 amit.gupta 225
				}
23816 amit.gupta 226
				itemTagListingMap = tagListingRepository.selectByItemIdsAndTagIds(itemsSet, new HashSet<>(tagIds))
227
						.stream().collect(Collectors.toMap(x -> x.getItemId(), x -> x));
23814 amit.gupta 228
			}
23816 amit.gupta 229
 
230
			for (int i = 0; i < docs.length(); i++) {
23814 amit.gupta 231
				Map<Integer, FofoAvailabilityInfo> fofoAvailabilityInfoMap = new HashMap<>();
232
				JSONObject doc = docs.getJSONObject(i);
22328 amit.gupta 233
				FofoCatalogResponse ffdr = new FofoCatalogResponse();
22319 amit.gupta 234
				ffdr.setCatalogId(doc.getInt("catalogId_i"));
235
				ffdr.setImageUrl(doc.getString("imageUrl_s"));
236
				ffdr.setTitle(doc.getString("title_s"));
22333 amit.gupta 237
				ffdr.setBrand(doc.getString("brand_s"));
23816 amit.gupta 238
 
239
				for (int j = 0; j < doc.getJSONArray("_childDocuments_").length(); j++) {
22323 amit.gupta 240
					JSONObject childItem = doc.getJSONArray("_childDocuments_").getJSONObject(j);
22319 amit.gupta 241
					int itemId = childItem.getInt("itemId_i");
24009 amit.gupta 242
					TagListing tl = itemTagListingMap.get(itemId);
23816 amit.gupta 243
					if (hotDeal) {
23861 amit.gupta 244
						if (!tl.isHotDeals()) {
23816 amit.gupta 245
							continue;
246
						}
23814 amit.gupta 247
					}
23816 amit.gupta 248
					float sellingPrice = (float) childItem.getDouble("sellingPrice_f");
249
					if (fofoAvailabilityInfoMap.containsKey(itemId)) {
250
						if (fofoAvailabilityInfoMap.get(itemId).getSellingPrice() > sellingPrice) {
22328 amit.gupta 251
							fofoAvailabilityInfoMap.get(itemId).setSellingPrice(sellingPrice);
23816 amit.gupta 252
							fofoAvailabilityInfoMap.get(itemId).setMop((float) childItem.getDouble("mop_f"));
253
						}
22319 amit.gupta 254
					} else {
23816 amit.gupta 255
						FofoAvailabilityInfo fdi = new FofoAvailabilityInfo();
256
						fdi.setSellingPrice((float) childItem.getDouble("sellingPrice_f"));
257
						fdi.setMop((float) childItem.getDouble("mop_f"));
258
						fdi.setColor(childItem.has("color_s") ? childItem.getString("color_s") : "");
259
						fdi.setTagId(childItem.getInt("tagId_i"));
260
						fdi.setItem_id(itemId);
261
						Item item = itemRepository.selectById(itemId);
262
						// In case its tampered glass moq should be 5
263
						if (item.getCategoryId() == 10020) {
264
							fdi.setMinBuyQuantity(5);
265
						} else {
266
							fdi.setMinBuyQuantity(1);
267
						}
24009 amit.gupta 268
						if (hotDeal || !tl.isActive()) {
23861 amit.gupta 269
 
24016 amit.gupta 270
								int totalAvailability = 0; // Using item availability cache for now but can be changed to
23861 amit.gupta 271
														// use caching later.
272
							try {
273
								ItemAvailabilityCache iac = itemAvailabilityCacheRepository.selectByItemId(itemId);
274
								totalAvailability = iac.getTotalAvailability();
275
								fdi.setAvailability(totalAvailability);
276
							} catch (Exception e) {
277
								continue;
278
							}
279
							if (totalAvailability <= 0) {
280
								continue;
281
							}
282
						} else {
283
							fdi.setAvailability(10);
284
						}
23816 amit.gupta 285
						fdi.setQuantityStep(1);
286
						fdi.setMaxQuantity(Math.min(fdi.getAvailability(), 100));
287
						fofoAvailabilityInfoMap.put(itemId, fdi);
22319 amit.gupta 288
					}
289
				}
23816 amit.gupta 290
				if (fofoAvailabilityInfoMap.values().size() > 0) {
22988 amit.gupta 291
					ffdr.setItems(new ArrayList<FofoAvailabilityInfo>(fofoAvailabilityInfoMap.values()));
292
					dealResponse.add(ffdr);
293
				}
22319 amit.gupta 294
			}
23816 amit.gupta 295
 
22319 amit.gupta 296
		} else {
23816 amit.gupta 297
			return responseSender.badRequest(
298
					new ProfitMandiBusinessException("Retailer id", userInfo.getUserId(), "NOT_FOFO_RETAILER"));
22319 amit.gupta 299
		}
300
		return responseSender.ok(dealResponse);
301
	}
22273 amit.gupta 302
 
22319 amit.gupta 303
	@RequestMapping(value = "/online-deals", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
22272 amit.gupta 304
	@ApiImplicitParams({
22319 amit.gupta 305
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
22272 amit.gupta 306
	@ApiOperation(value = "Get online deals")
22319 amit.gupta 307
	public ResponseEntity<?> getOnlineDeals(HttpServletRequest request,
308
			@RequestParam(value = "categoryId") String categoryId, @RequestParam(value = "offset") String offset,
309
			@RequestParam(value = "limit") String limit, @RequestParam(value = "sort", required = false) String sort,
310
			@RequestParam(value = "direction", required = false) String direction,
311
			@RequestParam(value = "filterData", required = false) String filterData) throws Throwable {
312
		logger.info("Request " + request.getParameterMap());
22272 amit.gupta 313
		String response = null;
22319 amit.gupta 314
		int userId = (int) request.getAttribute("userId");
22289 amit.gupta 315
 
22319 amit.gupta 316
		String uri = "/deals/" + userId;
23532 amit.gupta 317
		RestClient rc = new RestClient();
22272 amit.gupta 318
		Map<String, String> params = new HashMap<>();
319
		params.put("offset", offset);
320
		params.put("limit", limit);
321
		params.put("categoryId", categoryId);
322
		params.put("direction", direction);
323
		params.put("sort", sort);
324
		params.put("source", "online");
325
		params.put("filterData", filterData);
23816 amit.gupta 326
		/*
327
		 * if (userInfo.getRoleNames().contains(RoleType.FOFO.toString())) {
328
		 * params.put("tag_ids", getCommaSeparateTags(userId)); }
329
		 */
22272 amit.gupta 330
		List<Object> responseObject = new ArrayList<>();
23532 amit.gupta 331
		try {
332
			response = rc.get(SchemeType.HTTP, host, port, uri, params);
333
		} catch (HttpHostConnectException e) {
334
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
335
		}
22931 ashik.ali 336
 
22272 amit.gupta 337
		JsonArray result_json = Json.parse(response).asArray();
22319 amit.gupta 338
		for (JsonValue j : result_json) {
23816 amit.gupta 339
			// logger.info("res " + j.asArray());
22272 amit.gupta 340
			List<Object> innerObject = new ArrayList<>();
22319 amit.gupta 341
			for (JsonValue jsonObject : j.asArray()) {
22272 amit.gupta 342
				innerObject.add(toDealObject(jsonObject.asObject()));
343
			}
22319 amit.gupta 344
			if (innerObject.size() > 0) {
22272 amit.gupta 345
				responseObject.add(innerObject);
346
			}
347
		}
23022 ashik.ali 348
		return responseSender.ok(responseObject);
22272 amit.gupta 349
	}
350
 
22319 amit.gupta 351
	/*
23816 amit.gupta 352
	 * @RequestMapping(value = "/direct-deals", method=RequestMethod.GET,produces =
353
	 * MediaType.APPLICATION_JSON_VALUE)
22319 amit.gupta 354
	 * 
355
	 * @ApiImplicitParams({
356
	 * 
23816 amit.gupta 357
	 * @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true,
358
	 * dataType = "string", paramType = "header") }) public ResponseEntity<?>
359
	 * getDirectDeals(HttpServletRequest request, @RequestParam(value="categoryId")
360
	 * String categoryId,@RequestParam(value="offset") String offset,
22319 amit.gupta 361
	 * 
362
	 * @RequestParam(value="limit") String limit, @RequestParam(value="sort",
23816 amit.gupta 363
	 * required=false) String sort, @RequestParam(value="direction", required=false)
364
	 * String direction,
22319 amit.gupta 365
	 * 
366
	 * @RequestParam(value="filterData", required=false) String filterData ){
367
	 * 
368
	 * return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK); }
369
	 */
370
 
371
	private Object toDealObject(JsonObject jsonObject) {
372
		if (jsonObject.get("dealObject") != null && jsonObject.get("dealObject").asInt() == 1) {
21339 kshitij.so 373
			return new Gson().fromJson(jsonObject.toString(), DealObjectResponse.class);
374
		}
375
		return new Gson().fromJson(jsonObject.toString(), DealsResponse.class);
376
	}
22319 amit.gupta 377
 
378
	@RequestMapping(value = ProfitMandiConstants.URL_BRANDS, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
21356 kshitij.so 379
	@ApiImplicitParams({
22319 amit.gupta 380
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
21356 kshitij.so 381
	@ApiOperation(value = "Get brand list and count for category")
22319 amit.gupta 382
	public ResponseEntity<?> getBrands(HttpServletRequest request,
23816 amit.gupta 383
			@RequestParam(value = "category_id") String category_id) throws ProfitMandiBusinessException {
22319 amit.gupta 384
		logger.info("Request " + request.getParameterMap());
21356 kshitij.so 385
		String response = null;
22319 amit.gupta 386
		// TODO: move to properties
21356 kshitij.so 387
		String uri = ProfitMandiConstants.URL_BRANDS;
23532 amit.gupta 388
		RestClient rc = new RestClient();
21356 kshitij.so 389
		Map<String, String> params = new HashMap<>();
390
		params.put("category_id", category_id);
21358 kshitij.so 391
		List<DealBrands> dealBrandsResponse = null;
23532 amit.gupta 392
		try {
393
			response = rc.get(SchemeType.HTTP, host, port, uri, params);
394
		} catch (HttpHostConnectException e) {
395
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
396
		}
23816 amit.gupta 397
 
22319 amit.gupta 398
		dealBrandsResponse = new Gson().fromJson(response, new TypeToken<List<DealBrands>>() {
399
		}.getType());
23022 ashik.ali 400
 
401
		return responseSender.ok(dealBrandsResponse);
21356 kshitij.so 402
	}
22319 amit.gupta 403
 
404
	@RequestMapping(value = ProfitMandiConstants.URL_UNIT_DEAL, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
21445 kshitij.so 405
	@ApiImplicitParams({
22319 amit.gupta 406
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
21445 kshitij.so 407
	@ApiOperation(value = "Get unit deal object")
23816 amit.gupta 408
	public ResponseEntity<?> getUnitDeal(HttpServletRequest request, @PathVariable(value = "id") long id)
409
			throws ProfitMandiBusinessException {
21445 kshitij.so 410
		String response = null;
22319 amit.gupta 411
		// TODO: move to properties
412
		String uri = "getDealById/" + id;
413
		System.out.println("Unit deal " + uri);
23532 amit.gupta 414
		RestClient rc = new RestClient();
21445 kshitij.so 415
		Map<String, String> params = new HashMap<>();
416
		DealsResponse dealsResponse = null;
23532 amit.gupta 417
		try {
418
			response = rc.get(SchemeType.HTTP, host, port, uri, params);
419
		} catch (HttpHostConnectException e) {
420
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
421
		}
23816 amit.gupta 422
 
21445 kshitij.so 423
		JsonObject result_json = Json.parse(response).asObject();
22319 amit.gupta 424
		if (!result_json.isEmpty()) {
21445 kshitij.so 425
			dealsResponse = new Gson().fromJson(response, DealsResponse.class);
22952 amit.gupta 426
			Iterator<AvailabilityInfo> iter = dealsResponse.getAvailabilityInfo().iterator();
23816 amit.gupta 427
			while (iter.hasNext()) {
22952 amit.gupta 428
				AvailabilityInfo ai = iter.next();
23816 amit.gupta 429
				if (ai.getAvailability() <= 0)
22952 amit.gupta 430
					iter.remove();
431
			}
21445 kshitij.so 432
		}
23816 amit.gupta 433
		/*
434
		 * final ProfitMandiResponse<?> profitMandiResponse = new
435
		 * ProfitMandiResponse<>(LocalDateTime.now(),
436
		 * request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK,
437
		 * ResponseStatus.SUCCESS, dealsResponse);
438
		 */
22952 amit.gupta 439
		return responseSender.ok(dealsResponse);
21445 kshitij.so 440
	}
23816 amit.gupta 441
 
22333 amit.gupta 442
	@RequestMapping(value = "/fofo/brands", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
24034 amit.gupta 443
	public ResponseEntity<?> getBrandsToDisplay(@RequestParam(required=false, defaultValue="0") int categoryId) {
24031 amit.gupta 444
		return new ResponseEntity<>(mongoClient.getBrandsToDisplay(categoryId), HttpStatus.OK);
22333 amit.gupta 445
	}
23816 amit.gupta 446
 
22446 amit.gupta 447
	@RequestMapping(value = "/banners/{bannerType}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
22448 amit.gupta 448
	public ResponseEntity<?> getBanners(@PathVariable String bannerType) {
22447 amit.gupta 449
		return new ResponseEntity<>(mongoClient.getBannersByType(bannerType), HttpStatus.OK);
22446 amit.gupta 450
	}
23816 amit.gupta 451
 
23793 tejbeer 452
	@RequestMapping(value = "/deals/subCategories", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
453
	public ResponseEntity<?> getSubcategoriesToDisplay() {
454
		return new ResponseEntity<>(mongoClient.getSubcategoriesToDisplay(), HttpStatus.OK);
455
	}
23816 amit.gupta 456
 
22406 amit.gupta 457
	@ApiImplicitParams({
23816 amit.gupta 458
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
22401 amit.gupta 459
	@RequestMapping(value = "/deals/skus/{skus}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
22931 ashik.ali 460
	public ResponseEntity<?> getDealsBySkus(@PathVariable String skus) throws ProfitMandiBusinessException {
22401 amit.gupta 461
		StringBuffer sb = new StringBuffer("/getDealsForNotification/");
462
		String uri = sb.append(skus).toString();
23532 amit.gupta 463
		RestClient rc = new RestClient();
464
		String response;
465
		try {
466
			response = rc.get(SchemeType.HTTP, host, port, uri, new HashMap<>());
23816 amit.gupta 467
		} catch (HttpHostConnectException e) {
23532 amit.gupta 468
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
469
		}
22406 amit.gupta 470
		JsonArray result_json = Json.parse(response).asArray();
22407 amit.gupta 471
		List<Object> responseObject = new ArrayList<>();
472
		for (JsonValue j : result_json) {
23816 amit.gupta 473
			// logger.info("res " + j.asArray());
22407 amit.gupta 474
			List<Object> innerObject = new ArrayList<>();
475
			for (JsonValue jsonObject : j.asArray()) {
476
				innerObject.add(toDealObject(jsonObject.asObject()));
477
			}
478
			if (innerObject.size() > 0) {
479
				responseObject.add(innerObject);
480
			}
481
		}
22408 amit.gupta 482
		return responseSender.ok(responseObject);
22401 amit.gupta 483
	}
21339 kshitij.so 484
 
485
}