Subversion Repositories SmartDukaan

Rev

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