Subversion Repositories SmartDukaan

Rev

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