Rev 26652 | Rev 26659 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.web.controller;import java.time.LocalTime;import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;import java.util.stream.Collectors;import javax.servlet.http.HttpServletRequest;import org.apache.commons.lang3.StringUtils;import org.apache.http.conn.HttpHostConnectException;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.json.JSONArray;import org.json.JSONObject;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.http.MediaType;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Controller;import org.springframework.transaction.annotation.Transactional;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import com.eclipsesource.json.JsonObject;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;import com.spice.profitmandi.common.enumuration.SchemeType;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.model.CreatePendingOrderRequest;import com.spice.profitmandi.common.model.CustomRetailer;import com.spice.profitmandi.common.model.ProfitMandiConstants;import com.spice.profitmandi.common.model.UserInfo;import com.spice.profitmandi.common.solr.SolrService;import com.spice.profitmandi.common.web.client.RestClient;import com.spice.profitmandi.common.web.util.ResponseSender;import com.spice.profitmandi.dao.entity.catalog.Item;import com.spice.profitmandi.dao.entity.catalog.TagListing;import com.spice.profitmandi.dao.entity.fofo.CurrentInventorySnapshot;import com.spice.profitmandi.dao.entity.inventory.ItemAvailabilityCache;import com.spice.profitmandi.dao.enumuration.dtr.OtpType;import com.spice.profitmandi.dao.model.AddCartRequest;import com.spice.profitmandi.dao.model.CartItem;import com.spice.profitmandi.dao.model.CartItemResponseModel;import com.spice.profitmandi.dao.model.CartResponse;import com.spice.profitmandi.dao.repository.catalog.CategoryRepository;import com.spice.profitmandi.dao.repository.catalog.ItemRepository;import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;import com.spice.profitmandi.dao.repository.dtr.Mongo;import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;import com.spice.profitmandi.dao.repository.fofo.PendingOrderService;import com.spice.profitmandi.dao.repository.inventory.ItemAvailabilityCacheRepository;import com.spice.profitmandi.service.authentication.RoleManager;import com.spice.profitmandi.service.inventory.FofoAvailabilityInfo;import com.spice.profitmandi.service.inventory.FofoCatalogResponse;import com.spice.profitmandi.service.pricing.PricingService;import com.spice.profitmandi.service.user.RetailerService;import com.spice.profitmandi.web.processor.OtpProcessor;import com.spice.profitmandi.web.res.DealBrands;import com.spice.profitmandi.web.res.DealObjectResponse;import com.spice.profitmandi.web.res.DealsResponse;import com.spice.profitmandi.web.res.ValidateCartResponse;import io.swagger.annotations.ApiImplicitParam;import io.swagger.annotations.ApiImplicitParams;import io.swagger.annotations.ApiOperation;@Controller@Transactional(rollbackFor = Throwable.class)public class StoreController {private static final Logger logger = LogManager.getLogger(StoreController.class);private static final LocalTime CUTOFF_TIME = LocalTime.of(15, 0);@Value("${python.api.host}")private String host;@Value("${python.api.port}")private int port;// This is now unused as we are not supporting multiple companies.@Value("${gadgetCops.invoice.cc}")private String[] ccGadgetCopInvoiceTo;@Autowiredprivate PricingService pricingService;@Autowiredprivate RetailerService retailerService;@Autowiredprivate PendingOrderService pendingOrderService;@Autowiredprivate CategoryRepository categoryRepository;@Autowiredprivate SolrService commonSolrService;@Autowiredprivate Mongo mongoClient;@Autowiredprivate OtpProcessor otpProcessor;@Autowiredprivate CurrentInventorySnapshotRepository currentInventorySnapshotRepository;@Autowiredprivate UserAccountRepository userAccountRepository;@Autowiredprivate ResponseSender<?> responseSender;@Autowiredprivate TagListingRepository tagListingRepository;@Autowiredprivate ItemRepository itemRepository;@Autowiredprivate ItemAvailabilityCacheRepository itemAvailabilityCacheRepository;@Autowiredprivate RoleManager roleManagerService;List<String> filterableParams = Arrays.asList("brand");private Set<Integer> bestSellers = new HashSet<>(Arrays.asList(1022090, 1022090, 1022090, 1022024, 1022024, 1022024,1021868, 1021868, 1022346, 1022346, 1022337, 1022337, 1022355, 1022355, 1022344, 1022344, 1022343, 1022343,1022336, 1022336, 1022336, 1021933, 1021933, 1022025, 1022025, 1022025, 1003800, 1003800, 1003800, 1022322,1022322, 1022322, 1022307, 1022307, 1022304, 1022304, 1022004, 1022004, 1022004, 1021934, 1021934, 1021897,1021897, 1021768, 1021768));private Set<Integer> latestArrivals = new HashSet<>(Arrays.asList(1022386, 1022386, 1022381, 1022381, 1022381,1022380, 1022380, 1022377, 1022377, 1022376, 1022376, 1022375, 1022375, 1022374, 1022374, 1022374, 1022373,1022373, 1022372, 1022372, 1022371, 1022371, 1022370, 1022370));@ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })@RequestMapping(value = "/store/fofo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)public ResponseEntity<?> getFofo(HttpServletRequest request,@RequestParam(value = "categoryId", required = false, defaultValue = "(3 OR 6)") String categoryId,@RequestParam(value = "offset") String offset, @RequestParam(value = "limit") String limit,@RequestParam(value = "sort", required = false) String sort,@RequestParam(value = "brand", required = false) String brand,@RequestParam(value = "subCategoryId", required = false) int subCategoryId,@RequestParam(value = "q", required = false) String queryTerm,@RequestParam(value = "hotDeal", required = false) boolean hotDeal) throws Throwable {List<FofoCatalogResponse> dealResponse = new ArrayList<>();UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");if (roleManagerService.isPartner(userInfo.getRoleIds())) {// UserCart uc = userAccountRepository.getUserCart(userInfo.getUserId());List<Integer> tagIds = pricingService.getTagsIdsByRetailerId(userInfo.getRetailerId());RestClient rc = new RestClient();Map<String, String> params = new HashMap<>();List<String> mandatoryQ = new ArrayList<>();if (queryTerm != null && !queryTerm.equals("null")) {mandatoryQ.add(String.format("+(%s)", queryTerm));} else {queryTerm = null;}if (subCategoryId != 0) {mandatoryQ.add(String.format("+(subCategoryId_i:%s) +{!parent which=\"subCategoryId_i:%s\"} tagId_i:(%s)",subCategoryId, subCategoryId, StringUtils.join(tagIds, " ")));} else if (hotDeal) {mandatoryQ.add(String.format("+{!parent which=\"hot_deals_b=true\"} tagId_i:(%s)",StringUtils.join(tagIds, " ")));} else if (StringUtils.isNotBlank(brand)) {mandatoryQ.add(String.format("+(categoryId_i:%s) +(brand_ss:%s) +{!parent which=\"brand_ss:%s\"} tagId_i:(%s)",categoryId, brand, brand, StringUtils.join(tagIds, " ")));} else {mandatoryQ.add(String.format("+{!parent which=\"id:catalog*\"} tagId_i:(%s)", StringUtils.join(tagIds, " ")));}params.put("q", StringUtils.join(mandatoryQ, " "));params.put("fl", "*, [child parentFilter=id:catalog*]");if (queryTerm == null) {params.put("sort", "create_s desc");}params.put("start", String.valueOf(offset));params.put("rows", String.valueOf(limit));params.put("wt", "json");String response = null;try {response = rc.get(SchemeType.HTTP, "50.116.10.120", 8984, "solr/demo/select", params);} catch (HttpHostConnectException e) {throw new ProfitMandiBusinessException("", "", "Could not connect to host");}JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");JSONArray docs = solrResponseJSONObj.getJSONArray("docs");dealResponse = getCatalogResponse(docs, hotDeal);/** if (Mongo.PARTNER_BLoCKED_BRANDS.containsKey(userInfo.getEmail())) {* dealResponse.stream() .filter(x ->* Mongo.PARTNER_BLoCKED_BRANDS.get(userInfo.getEmail()).contains(x.getBrand()))* ; }*/} else {return responseSender.badRequest(new ProfitMandiBusinessException("Retailer id", userInfo.getUserId(), "NOT_FOFO_RETAILER"));}return responseSender.ok(dealResponse);}private Object toDealObject(JsonObject jsonObject) {if (jsonObject.get("dealObject") != null && jsonObject.get("dealObject").asInt() == 1) {return new Gson().fromJson(jsonObject.toString(), DealObjectResponse.class);}return new Gson().fromJson(jsonObject.toString(), DealsResponse.class);}@RequestMapping(value = "/store/brands", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)@ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })@ApiOperation(value = "Get brand list and count for category")public ResponseEntity<?> getBrands(HttpServletRequest request,@RequestParam(value = "category_id") String category_id) throws ProfitMandiBusinessException {logger.info("Request " + request.getParameterMap());String response = null;// TODO: move to propertiesString uri = ProfitMandiConstants.URL_BRANDS;RestClient rc = new RestClient();Map<String, String> params = new HashMap<>();params.put("category_id", category_id);List<DealBrands> dealBrandsResponse = null;try {response = rc.get(SchemeType.HTTP, host, port, uri, params);} catch (HttpHostConnectException e) {throw new ProfitMandiBusinessException("", "", "Could not connect to host");}dealBrandsResponse = new Gson().fromJson(response, new TypeToken<List<DealBrands>>() {}.getType());return responseSender.ok(dealBrandsResponse);}@RequestMapping(value = "/store/tag/{tag}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)public ResponseEntity<?> bestSellers(HttpServletRequest request, String tag) throws Exception {List<FofoCatalogResponse> dealResponse = new ArrayList<>();UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");// UserCart uc = userAccountRepository.getUserCart(userInfo.getUserId());List<Integer> tagIds = pricingService.getTagsIdsByRetailerId(userInfo.getRetailerId());RestClient rc = new RestClient();Map<String, String> params = new HashMap<>();List<String> mandatoryQ = new ArrayList<>();Set<Integer> catalogIds = this.bestSellers;if (tag == "latestArrivals") {catalogIds = this.latestArrivals;}mandatoryQ.add(String.format("+{!parent which=\"catalogId_i:" + StringUtils.join(catalogIds, " ") + "\"} tagId_i:(%s)",StringUtils.join(tagIds, " ")));params.put("q", StringUtils.join(mandatoryQ, " "));params.put("fl", "*, [child parentFilter=id:catalog*]");// params.put("sort", "create_s desc");params.put("start", String.valueOf(0));params.put("rows", String.valueOf(30));params.put("wt", "json");String response = null;try {response = rc.get(SchemeType.HTTP, "50.116.10.120", 8984, "solr/demo/select", params);} catch (HttpHostConnectException e) {throw new ProfitMandiBusinessException("", "", "Could not connect to host");}JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");JSONArray docs = solrResponseJSONObj.getJSONArray("docs");dealResponse = getCatalogResponse(docs, false);return responseSender.ok(dealResponse);}@RequestMapping(value = "/store/otp/generateOTP", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)public ResponseEntity<?> generateOtp(HttpServletRequest request, @RequestParam String email,@RequestParam String phone) throws Exception {return responseSender.ok(otpProcessor.generateOtp(email, phone, OtpType.PREBOOKING_ORDER));}@RequestMapping(value = "/store/confirmOrder", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)public ResponseEntity<?> confirmCart(HttpServletRequest request,@RequestBody CreatePendingOrderRequest createPendingOrderRequest) throws Exception {UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");Integer storeId = userInfo.getRetailerId();createPendingOrderRequest.setFofoId(storeId);this.pendingOrderService.createPendingOrder(createPendingOrderRequest);return responseSender.ok(true);}@RequestMapping(value = "/store/address", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)@ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })@ApiOperation(value = "Get brand list and count for category")public ResponseEntity<?> getAddress(HttpServletRequest request) throws Exception {UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");Integer storeId = userInfo.getRetailerId();CustomRetailer customRetailer = retailerService.getFofoRetailer(storeId);return responseSender.ok(customRetailer.getAddress());}@RequestMapping(value = "/store/cart", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)@ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })@ApiOperation(value = "Get brand list and count for category")public ResponseEntity<?> cart(HttpServletRequest request, @RequestBody AddCartRequest cartRequest)throws Exception {CartResponse cartResponse = new CartResponse();List<CartItemResponseModel> cartItemResponseModels = new ArrayList<>();cartResponse.setCartItems(cartItemResponseModels);UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");Integer storeId = userInfo.getRetailerId();List<Integer> itemIds = cartRequest.getCartItems().stream().map(x -> x.getItemId()).collect(Collectors.toList());Set<Integer> itemsIdsSet = new HashSet<>(itemIds);List<CurrentInventorySnapshot> currentInventorySnapshot = currentInventorySnapshotRepository.selectByFofoItemIds(storeId, itemsIdsSet);Map<Integer, Integer> storeItemAvailabilityMap = currentInventorySnapshot.stream().filter(x -> x.getAvailability() > 0).collect(Collectors.toMap(x -> x.getItemId(), x -> x.getAvailability()));Map<Integer, Item> itemsMap = itemRepository.selectByIds(itemsIdsSet).stream().collect(Collectors.toMap(x -> x.getId(), x -> x));Map<Integer, TagListing> sdItemAvailabilityMap = tagListingRepository.selectByItemIdsAndTagIds(new HashSet<>(itemIds), new HashSet<>(Arrays.asList(4))).stream().collect(Collectors.toMap(x -> x.getItemId(), x -> x));List<Integer> catalogIds = itemsMap.values().stream().map(x -> x.getCatalogItemId()).collect(Collectors.toList());Map<Integer, JSONObject> contentMap = commonSolrService.getContentByCatalogIds(catalogIds);// cartResponse.getCartItems()for (CartItem cartItem : cartRequest.getCartItems()) {if (cartItem.getQuantity() == 0) {continue;}Item item = itemsMap.get(cartItem.getItemId());TagListing tagListing = sdItemAvailabilityMap.get(cartItem.getItemId());CartItemResponseModel cartItemResponseModel = new CartItemResponseModel();int estimate = -2;if (storeItemAvailabilityMap.containsKey(cartItem.getItemId())) {if (storeItemAvailabilityMap.get(cartItem.getItemId()) >= cartItem.getQuantity()) {estimate = 0;} else if (tagListing.isActive()) {estimate = 2;} else {estimate = -2;}} else if (tagListing.isActive()) {estimate = 2;} else {estimate = -2;}if (estimate >= 0 && LocalTime.now().isAfter(CUTOFF_TIME)) {estimate = estimate + 1;}cartItemResponseModel.setEstimate(estimate);cartItemResponseModel.setTitle(item.getItemDescriptionNoColor());cartItemResponseModel.setItemId(cartItem.getItemId());cartItemResponseModel.setMinBuyQuantity(1);cartItemResponseModel.setQuantity(cartItem.getQuantity());cartItemResponseModel.setQuantityStep(1);cartItemResponseModel.setSellingPrice(tagListing.getMop());cartItemResponseModel.setMaxQuantity(10);cartItemResponseModel.setCatalogItemId(item.getCatalogItemId());cartItemResponseModel.setImageUrl(contentMap.get(item.getCatalogItemId()).getString("imageUrl_s"));cartItemResponseModel.setColor(item.getColor());cartItemResponseModels.add(cartItemResponseModel);}ValidateCartResponse vc = new ValidateCartResponse(cartResponse, "Success", "Items added to cart successfully");return responseSender.ok(vc);}private boolean validateCart(int storeId, List<CartItem> cartItems) {return false;}private List<FofoCatalogResponse> getCatalogResponse(JSONArray docs, boolean hotDeal)throws ProfitMandiBusinessException {Map<Integer, TagListing> itemTagListingMap = null;List<FofoCatalogResponse> dealResponse = new ArrayList<>();List<Integer> tagIds = Arrays.asList(4);if (docs.length() > 0) {HashSet<Integer> itemsSet = new HashSet<>();for (int i = 0; i < docs.length(); i++) {JSONObject doc = docs.getJSONObject(i);if (doc.has("_childDocuments_")) {for (int j = 0; j < doc.getJSONArray("_childDocuments_").length(); j++) {JSONObject childItem = doc.getJSONArray("_childDocuments_").getJSONObject(j);int itemId = childItem.getInt("itemId_i");itemsSet.add(itemId);}}}if (itemsSet.size() == 0) {return dealResponse;}itemTagListingMap = tagListingRepository.selectByItemIdsAndTagIds(itemsSet, new HashSet<>(tagIds)).stream().collect(Collectors.toMap(x -> x.getItemId(), x -> x));}for (int i = 0; i < docs.length(); i++) {Map<Integer, FofoAvailabilityInfo> fofoAvailabilityInfoMap = new HashMap<>();JSONObject doc = docs.getJSONObject(i);FofoCatalogResponse ffdr = new FofoCatalogResponse();ffdr.setCatalogId(doc.getInt("catalogId_i"));ffdr.setImageUrl(doc.getString("imageUrl_s"));ffdr.setTitle(doc.getString("title_s"));try {ffdr.setFeature(doc.getString("feature_s"));} catch (Exception e) {ffdr.setFeature(null);logger.info("Could not find Feature_s for {}", ffdr.getCatalogId());}ffdr.setBrand(doc.getJSONArray("brand_ss").getString(0));if (doc.has("_childDocuments_")) {for (int j = 0; j < doc.getJSONArray("_childDocuments_").length(); j++) {JSONObject childItem = doc.getJSONArray("_childDocuments_").getJSONObject(j);int itemId = childItem.getInt("itemId_i");TagListing tl = itemTagListingMap.get(itemId);if (tl == null) {logger.warn("Could not find item id {}", itemId);continue;}if (hotDeal) {if (!tl.isHotDeals()) {continue;}}float sellingPrice = (float) childItem.getDouble("sellingPrice_f");if (fofoAvailabilityInfoMap.containsKey(itemId)) {if (fofoAvailabilityInfoMap.get(itemId).getSellingPrice() > sellingPrice) {fofoAvailabilityInfoMap.get(itemId).setSellingPrice(sellingPrice);fofoAvailabilityInfoMap.get(itemId).setMop((float) childItem.getDouble("mop_f"));}} else {FofoAvailabilityInfo fdi = new FofoAvailabilityInfo();fdi.setSellingPrice((float) childItem.getDouble("sellingPrice_f"));fdi.setMop((float) childItem.getDouble("mop_f"));fdi.setColor(childItem.has("color_s") ? childItem.getString("color_s") : "");fdi.setTagId(childItem.getInt("tagId_i"));fdi.setItem_id(itemId);Item item = itemRepository.selectById(itemId);// In case its tampered glass moq should be 5if (item.getCategoryId() == 10020) {fdi.setMinBuyQuantity(10);} else {fdi.setMinBuyQuantity(1);}if (hotDeal || !tl.isActive()) {int totalAvailability = 0; // Using item availability// cache for now but can be// changed to// use caching later.try {ItemAvailabilityCache iac = itemAvailabilityCacheRepository.selectByItemId(itemId);totalAvailability = iac.getTotalAvailability();fdi.setAvailability(totalAvailability);} catch (Exception e) {continue;}if (totalAvailability <= 0) {continue;}} else {// For accessories item availability should at be ordered for Rs.1000fdi.setAvailability(100);}fdi.setQuantityStep(1);fdi.setMaxQuantity(Math.min(fdi.getAvailability(), 100));fofoAvailabilityInfoMap.put(itemId, fdi);}}}if (fofoAvailabilityInfoMap.values().size() > 0) {ffdr.setItems(new ArrayList<FofoAvailabilityInfo>(fofoAvailabilityInfoMap.values()));dealResponse.add(ffdr);}}return dealResponse;}}