Subversion Repositories SmartDukaan

Rev

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