package com.ifish.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ifish.dto.JsonResult;
import com.ifish.dto.PageingDto;
import com.ifish.enums.ResultEnum;
import com.ifish.exception.IfishException;
import com.ifish.service.CouponService;
import com.ifish.validator.PageParam;

/**
 * 优惠券
 * @author Administrator
 *
 */
@RestController
public class CouponAction {
	
	@Autowired
	private CouponService couponService;
	
	/**
	 * 获取所有有效优惠券
	 * @return
	 */
	@RequestMapping(value="/validatingCoupons",method=RequestMethod.GET)
	public JsonResult<?> getAllValidCoupon(){
		return couponService.getAllValidCoupon();
	}
	
	/**
	 * 兑换优惠券
	 * @param userId
	 * @param couponId
	 * @return
	 */
	@RequestMapping(value="/exchangeCoupon/{userId}/{couponId}",method=RequestMethod.POST)//
	public JsonResult<?> exchangeCoupon(@PathVariable("userId")Integer userId,@PathVariable("couponId")Integer couponId){
		return couponService.exchangeCoupon(userId, couponId);
	}
	
	/**
	 * 分页获取兑换记录
	 * @param param
	 * @return
	 */
	@RequestMapping(value="/couponRecords",method=RequestMethod.GET)
	public PageingDto<?> getCouponRecordByPage(@Validated PageParam param,BindingResult result){
		//参数校验
		if(result.hasErrors()){
			throw new IfishException(ResultEnum.error401);
		}
		return couponService.getCouponRecordByPage(param);
	}
	
}
