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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.common.downgrade.persional.PersionalRateLimit;
import com.yoho.search.models.PromotionConditions;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.scene.promotion.PromotionSceneService;
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 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 PromotionSceneService promotionService;

	/**
	 * 促销列表接口[新]
	 */
	@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 promotionService.list(promotionConditions, paramMap);
		} 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 promotionService.list(promotionConditions, paramMap);
		} 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 promotionService.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 promotionService.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 promotionService.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 promotionService.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);
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
		}
		return map;
	}

}