PromotionSceneController.java 7.14 KB
package com.yoho.search.restapi.scene;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.SearchPageIdDefine;
import com.yoho.search.aop.downgrade.PersionalRateLimit;
import com.yoho.search.common.utils.HttpServletRequestUtils;
import com.yoho.search.models.PromotionConditions;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.common.SearchRequestParams;
import com.yoho.search.service.scene.pages.promotion.PromotionAggregationsService;
import com.yoho.search.service.scene.pages.promotion.PromotionProductListService;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 商品详情页的促销列表接口
 * 
 * @author gufei.hu
 */
@Controller
public class PromotionSceneController {

	private static Logger PROMOTION = LoggerFactory.getLogger("PROMOTION");

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

	@Autowired
	private PromotionAggregationsService promotionAggregationsService;
	@Autowired
	private PromotionProductListService promotionProductListService;

	/**
	 * 促销列表接口[新]
	 */
	@PersionalRateLimit(isOrderUseable = true)
	@RequestMapping(method = RequestMethod.POST, value = "/promotion/productList")
	@ResponseBody
	public SearchApiResult productList(@RequestBody JSONObject param) {
		try {
			PROMOTION.info("get /promotion/productList.json,params is{}", param.toJSONString());
			PromotionConditions promotionConditions = this.genPromotionConditions(param);
			Map<String, String> paramMap = this.genOtherParamMap(param);
			return promotionProductListService.promotionProductList(paramMap,promotionConditions);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new SearchApiResult().setCode(400).setMessage("参数解析错误");
		}
	}

	/**
	 * 促销列表接口[旧]
	 */
	@PersionalRateLimit(isOrderUseable = true, name = "/promotion/productList")
	@RequestMapping(method = RequestMethod.POST, value = "/promotion/list")
	@ResponseBody
	public SearchApiResult list(@RequestBody JSONObject param) {
		try {
			PROMOTION.info("get /promotion/list.json,params is{}", param.toJSONString());
			PromotionConditions promotionConditions = this.genPromotionConditions(param);
			Map<String, String> paramMap = this.genOtherParamMap(param);
			return promotionProductListService.promotionProductList(paramMap,promotionConditions);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new SearchApiResult().setCode(400).setMessage("参数解析错误");
		}
	}

	/**
	 * 促销列表接口[新]
	 */
	@PersionalRateLimit(isOrderUseable = false)
	@RequestMapping(method = RequestMethod.POST, value = "/promotion/aggregations")
	@ResponseBody
	public SearchApiResult promotionAggregations(@RequestBody JSONObject param) {
		try {
			PROMOTION.info("get /promotion/aggregations.json,params is{}", param.toJSONString());
			PromotionConditions promotionConditions = this.genPromotionConditions(param);
			Map<String, String> paramMap = this.genOtherParamMap(param);
			return promotionAggregationsService.aggregations(promotionConditions, paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new SearchApiResult().setCode(400).setMessage("参数解析错误");
		}
	}

	/**
	 * 促销聚合接口[for app]
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/promotion/selectionsForApp")
	@ResponseBody
	public SearchApiResult selectionsForApp(@RequestBody JSONObject param) {
		try {
			PROMOTION.info("get /promotion/selectionsForApp.json,params is{}", param.toJSONString());
			PromotionConditions promotionConditions = this.genPromotionConditions(param);
			Map<String, String> paramMap = this.genOtherParamMap(param);
			return promotionAggregationsService.selectionsForApp(promotionConditions, paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new SearchApiResult().setCode(400).setMessage("参数解析错误");
		}
	}

	/**
	 * 促销聚合接口[for pc]
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/promotion/selectionsForPc")
	@ResponseBody
	public SearchApiResult selectionsForPc(@RequestBody JSONObject param) {
		try {
			PROMOTION.info("get /promotion/selectionsForPc.json,params is{}", param.toJSONString());
			PromotionConditions promotionConditions = this.genPromotionConditions(param);
			Map<String, String> paramMap = this.genOtherParamMap(param);
			return promotionAggregationsService.selectionsForPc(promotionConditions, paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new SearchApiResult().setCode(400).setMessage("参数解析错误");
		}
	}

	/**
	 * 促销聚合品牌的接口
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/promotion/aggBrands")
	@ResponseBody
	public SearchApiResult aggBrands(@RequestBody JSONObject param) {
		try {
			PROMOTION.info("get /promotion/aggBrands.json,params is{}", param.toJSONString());
			PromotionConditions promotionConditions = this.genPromotionConditions(param);
			Map<String, String> paramMap = this.genOtherParamMap(param);
			return promotionAggregationsService.aggPromotionBrands(promotionConditions, paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new SearchApiResult().setCode(400).setMessage("参数解析错误");
		}
	}

	@RequestMapping(method = RequestMethod.POST, value = "/promotion/selections")
	@ResponseBody
	public SearchApiResult selections(@RequestBody JSONObject param) {
		return selectionsForApp(param);
	}

	private PromotionConditions genPromotionConditions(JSONObject param) {
		try {
			JSONObject jsonObject = param.getJSONObject("promotion_condition");
			if (jsonObject == null) {
				return null;
			}
			return JSON.parseObject(jsonObject.toJSONString(), PromotionConditions.class);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return null;
		} finally {
			// param.remove("promotion_condition");
		}
	}

	private Map<String, String> genOtherParamMap(JSONObject param) {
		Map<String, String> map = new HashMap<String, String>();
		for (Map.Entry<String, Object> entry : param.entrySet()) {
			try {
				Object value = entry.getValue();
				String valueString = value == null ? null : value.toString();
				map.put(entry.getKey(), valueString);
				map.put(SearchRequestParams.PARAM_SEARCH_PAGEID, SearchPageIdDefine.PAGE_ID_PROMOTION);
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
		}
		return map;
	}

	@RequestMapping(method = RequestMethod.GET, value = "/promotion/aggPromotions")
	@ResponseBody
	public SearchApiResult aggPromotions(HttpServletRequest request){
		Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
		return promotionAggregationsService.aggPromotions(paramMap);
	}

}