package com.xmomen.module.wb.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xmomen.module.base.constant.AppConstants;
import com.xmomen.module.base.service.CouponService;
import com.xmomen.module.wx.module.coupon.model.CouponQueryModel;
import com.xmomen.module.wx.module.coupon.model.WxCouponModel;

@RestController
@RequestMapping("/wb/coupon")
public class PcCouponController {

	@Autowired
	private CouponService couponService;

	// 使用GET方法请求
	@RequestMapping(method = RequestMethod.GET)
	// 返回WxCouponModel类型的列表
	List<WxCouponModel> getCoupons(@RequestParam(value="couponType") Integer couponType,
			@RequestParam(value="useable", required = false) Boolean useable) {
		// 获取当前用户的ID
		Integer memberId = (Integer) SecurityUtils.getSubject().getSession().getAttribute(AppConstants.SESSION_USER_ID_KEY);
		// 创建CouponQueryModel对象
		CouponQueryModel couponQueryModel = new CouponQueryModel();
		// 设置用户ID
		couponQueryModel.setCdUserId(memberId);
		// 设置优惠券类型
		couponQueryModel.setCouponType(couponType);
		// 设置是否可用
		couponQueryModel.setUseable(useable);
		// 返回用户的优惠券列表
	// 绑定优惠券
		return couponService.getMyCouponList(couponQueryModel);
		// 获取当前登录用户的ID
	}
		// 调用couponService的bindMember方法绑定优惠券

	@RequestMapping(value="/bind", method = RequestMethod.POST)
	Boolean bindCoupon(@RequestParam(value="couponNumber", required=true) String couponNumber,
			@RequestParam(value="password", required = false) String password) throws Exception {
		Integer memberId = (Integer) SecurityUtils.getSubject().getSession().getAttribute(AppConstants.SESSION_USER_ID_KEY);
		return couponService.bindMember(couponNumber, memberId, password);
	// 定义一个GET请求，用于验证优惠券
	}
	// 定义一个方法，用于验证优惠券，参数为优惠券号码和密码

	@RequestMapping(value="/validate", method = RequestMethod.GET)
		// 调用couponService的validate方法，验证优惠券
	Boolean validateCoupon(@RequestParam(value="couponNumber", required=true) String couponNumber,
			@RequestParam(value="password", required = false) String password) {
		return couponService.validate(couponNumber, password);
	// 定义一个POST请求，用于重置优惠券
	}
	// 处理重置密码的请求

	@RequestMapping(value="/reset", method = RequestMethod.POST)
	Boolean resetPasword(@RequestParam(value="couponNumber", required=true) String couponNumber,
		// 获取当前登录用户的ID
			@RequestParam(value="password", required = false) String password,
		// 调用couponService的resetPasword方法，重置密码
			@RequestParam(value="newPassword", required = true) String newPassword) throws Exception {
		Integer memberId = (Integer) SecurityUtils.getSubject().getSession().getAttribute(AppConstants.SESSION_USER_ID_KEY);
		return couponService.resetPasword(couponNumber, password, newPassword, memberId);
	}
}
