SearchController.java 8.6 KB
package com.yoho.search.restapi;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yoho.search.service.SearchProductsService;
import com.yoho.search.service.SearchService;
import com.yoho.search.service.SearchShopService;
import com.yoho.search.service.SearchSortSizeService;
import com.yoho.search.service.SearchSuggestService;
import com.yoho.search.utils.JsonUtil;
import com.yoho.search.vo.SearchApiResult;
import com.yoho.search.vo.SizeSortReqBO;

@Controller
public class SearchController {

	private static Logger logger = LoggerFactory.getLogger(SearchController.class);

	@Autowired
	private SearchService searchService;
	@Autowired
	private SearchSortSizeService searchSortSizeService;
	@Autowired
	private SearchShopService searchShopService;
	@Autowired
	private SearchSuggestService searchSuggestService;
	@Autowired
	private SearchProductsService searchProductsService;

	/**
	 * 搜索商品列表以及聚合结果的接口
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/search")
	@ResponseBody
	public Map<String, Object> searchProducts(HttpServletRequest request) {
		Map<String, String> paramMap = this.transParamType(request);
		try {
			return searchProductsService.searchProducts(paramMap, null);
		} catch (Exception e) {
			return errorReturn("searchProducts", paramMap, e);
		}
	}

	/**
	 * 获取按折扣聚合的结果
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/discount")
	@ResponseBody
	public Map<String, Object> discount(HttpServletRequest request) {
		Map<String, String> paramMap = this.transParamType(request);
		try {
			return searchService.searchProductsDiscount(paramMap);
		} catch (Exception e) {
			return errorReturn("discount", paramMap, e);
		}
	}

	/**
	 * 获取按上架日期聚合的结果
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/recent")
	@ResponseBody
	public Map<String, Object> recent(HttpServletRequest request) {
		Map<String, String> paramMap = this.transParamType(request);
		try {
			return searchService.searchProductsRecent(paramMap);
		} catch (Exception e) {
			return errorReturn("recent", paramMap, e);
		}
	}

	/**
	 * 获取最新上架的商品列表【已过滤相同的品牌】
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/new-shelve")
	@ResponseBody
	public Map<String, Object> searchNewestProductWithDiffBrand(HttpServletRequest request) {
		Map<String, String> paramMap = this.transParamType(request);
		try {
			return searchService.searchNewestProductWithDiffBrand(paramMap);
		} catch (Exception e) {
			return errorReturn("searchNewestProductWithDiffBrand", paramMap, e);
		}
	}

	/**
	 * 某些品牌最新上架的商品【按order参数对商品排序】
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/new_product")
	@ResponseBody
	public Map<String, Object> searchNewestProductWithParamBrand(HttpServletRequest request) {
		Map<String, String> paramMap = this.transParamType(request);
		try {
			return searchService.searchNewestProductWithParamBrand(paramMap);
		} catch (Exception e) {
			return errorReturn("newProduct", paramMap, e);
		}
	}

	/**
	 * 获取按品牌聚合的结果[品牌列表]
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/brands")
	@ResponseBody
	public Map<String, Object> brands(HttpServletRequest request) {
		Map<String, String> paramMap = this.transParamType(request);
		try {
			return searchService.brands(paramMap);
		} catch (Exception e) {
			return errorReturn("brands", paramMap, e);
		}
	}

	/**
	 * 批量获取brandIds对应的商品数量和商品列表[我收藏的品牌]
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/group_brands")
	@ResponseBody
	public Map<String, Object> groupBrands(HttpServletRequest request) {
		Map<String, String> paramMap = this.transParamType(request);
		try {
			return searchService.groupBrands(paramMap);
		} catch (Exception e) {
			return errorReturn("groupBrands", paramMap, e);
		}
	}

	/**
	 * 对分类进行分组查询
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/sortgroup")
	@ResponseBody
	public Map<String, Object> sortGroup(HttpServletRequest request) {
		Map<String, String> paramMap = this.transParamType(request);
		try {
			return searchService.sortGroup(paramMap);
		} catch (Exception e) {
			return errorReturn("sortGroup", paramMap, e);
		}
	}

	/**
	 * 根据query关键字获取商品总数
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/count")
	@ResponseBody
	public long searchCount(HttpServletRequest request) {
		Map<String, String> paramMap = this.transParamType(request);
		try {
			return searchService.searchCount(paramMap);
		} catch (Exception e) {
			logger.error("[※查询]失败:" + e.getMessage(), e);
			return 0;
		}
	}

	/**
	 * 搜索建议接口
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/suggest")
	@ResponseBody
	public Map<String, Object> suggest(HttpServletRequest request) {
		Map<String, String> paramMap = this.transParamType(request);
		try {
			return searchSuggestService.suggest(paramMap);
		} catch (Exception e) {
			return errorReturn("suggest", paramMap, e);
		}
	}

	@RequestMapping(method = RequestMethod.GET, value = "/shops")
	@ResponseBody
	public Map<String, Object> shops(HttpServletRequest request) {
		Map<String, String> paramMap = this.transParamType(request);
		try {
			return searchShopService.searchShops(paramMap);
		} catch (Exception e) {
			return errorReturn("shops", paramMap, e);
		}
	}

	@RequestMapping(method = RequestMethod.GET, value = "/group_shops")
	@ResponseBody
	public Map<String, Object> groupShops(HttpServletRequest request) {
		Map<String, String> paramMap = this.transParamType(request);
		try {
			return searchShopService.groupShops(paramMap);
		} catch (Exception e) {
			return errorReturn("groupShops", paramMap, e);
		}
	}

	/**
	 * 获取分类,以及分类下的型号列表(with storage_sku)
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/sort_sizes")
	@ResponseBody
	public SearchApiResult sort_sizes(@ModelAttribute SizeSortReqBO sizeSortReqBO) {
		return searchSortSizeService.sortSizes(sizeSortReqBO);
	}

	/**
	 * 按分类和型号获取商品(with storage_sku)
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/sort_size_products")
	@ResponseBody
	public Map<String, Object> sort_size_products(HttpServletRequest request) {
		Map<String, String> paramMap = this.transParamType(request);
		try {
			return searchSortSizeService.sortSizeProducts(paramMap);
		} catch (Exception e) {
			return errorReturn("sort_size_products", paramMap, e);
		}
	}

	/**
	 * 将HttpServletRequest中被锁定的ParameterMap转化为普通的HashMap
	 * */
	@SuppressWarnings("unchecked")
	private Map<String, String> transParamType(HttpServletRequest request) {
		Map<String, Object> paramMap = request.getParameterMap();
		Map<String, String> resultMap = new HashMap<String, String>();
		Iterator<String> itKeys = paramMap.keySet().iterator();
		while (itKeys.hasNext()) {
			String key = itKeys.next();
			String val = ((String[]) paramMap.get(key))[0];
			resultMap.put(key, val);
		}
		return resultMap;
	}

	private Map<String, Object> errorReturn(final String funName, final Map<String, ?> paramMap, final Exception e) {
		logger.error("[※查询]失败:[func={}][param={}][message={}][stack={}]", funName, JsonUtil.toJson(paramMap), e.getMessage(),e);
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		rtnMap.put("code", 400);
		rtnMap.put("status", "error");
		rtnMap.put("errmsg", e.getMessage());
		return rtnMap;
	}
}