package org.xmx0632.deliciousfruit.api.v1;
import javax.servlet.ServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
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 org.xmx0632.deliciousfruit.api.v1.bo.PromotionRuleRequest;
import org.xmx0632.deliciousfruit.api.v1.bo.PromotionRuleResponse;
import org.xmx0632.deliciousfruit.api.v1.bo.Result;
import org.xmx0632.deliciousfruit.api.v1.bo.TerminalType;
import org.xmx0632.deliciousfruit.api.v1.helper.TTLHelper;
import org.xmx0632.deliciousfruit.api.v1.helper.WebHelper;
import org.xmx0632.deliciousfruit.entity.UserAccount;
import org.xmx0632.deliciousfruit.service.ConfigService;
import org.xmx0632.deliciousfruit.service.FruitPromotionService;
/**
* 促销规则同步接口
*
* @author Jefferson-pengtao
*/
@Controller
@RequestMapping(value = "/api/v1/promotion")
public class PromotionRuleApiController {
private static Logger log = LoggerFactory
.getLogger(PromotionRuleApiController.class);
@Autowired
private ConfigService configService;
@Autowired
private FruitPromotionService fruitPromotionService;
@RequestMapping(value = "/getAll", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<PromotionRuleResponse> getAll(
@RequestBody PromotionRuleRequest promotionRuleRequest,
ServletRequest request) {
UserAccount userAccount = WebHelper.getCurrentUser(request);
log.debug("UserAccount:{}", userAccount);
log.debug("Request:{}", promotionRuleRequest);
PromotionRuleResponse response = new PromotionRuleResponse();
TerminalType type = TerminalType.toType(promotionRuleRequest
.getTerminalType());
// 检测是否有效的终端类型
if (type == null) {
response.setResult(new Result(Result.FAIL,
Result.MSG_ERR_NOT_VALID_TERMINAL_TYPE));
log.debug("response:{}", response);
return new ResponseEntity<PromotionRuleResponse>(response,
HttpStatus.OK);
}
PromotionRuleResponse.PromotionRuleBo rules = new PromotionRuleResponse.PromotionRuleBo();
response.setPromotionRules(rules);
rules.getPromotionsTotal().addAll(
fruitPromotionService.getAvailableTotalGift(type));
rules.getPromotionsTotal().addAll(
fruitPromotionService.getAvailableTotalPriceOff(type));
rules.getPromotionsProduct().addAll(
fruitPromotionService.getAvailableProductGift(type));
response.setTTL(TTLHelper.genTTL(configService));
response.setResult(Result.SUCCESS_RESULT);
log.debug("response:{}", response);
return new ResponseEntity<PromotionRuleResponse>(response,
HttpStatus.OK);
}
}