package com.xmomen.module.wb.controller;

import java.beans.PropertyEditorSupport;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.xmomen.module.base.constant.AppConstants;
import com.xmomen.module.order.entity.TbOrder;
import com.xmomen.module.order.model.WxCreateOrder;
import com.xmomen.module.order.service.OrderService;
import com.xmomen.module.product.model.ProductModel;
import com.xmomen.module.wx.module.order.model.MyOrderQuery;
import com.xmomen.module.wx.module.order.model.OrderDetailModel;
import com.xmomen.module.wx.module.order.model.OrderModel;
import com.xmomen.module.wx.module.order.model.PayOrderModel;
import com.xmomen.module.wx.module.order.service.MyOrderService;

@RestController
@RequestMapping(value = "/wb/order")
public class PcOrderController {

	// 自动注入MyOrderService
	@Autowired
	MyOrderService myOrderService;
	
	// 自动注入OrderService
	@Autowired
	OrderService orderService;

	/**
	 * 
	 * @param memberId
	 * @param status 0-未支付 1 待收货
	 * @param minCreateTime 昨日
	 * @param maxCreateTime 今日
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	// 根据传入的status、minOrderTime、maxCreateTime查询订单信息
	public List<OrderModel> myOrder(@RequestParam(value = "status", required = false) Integer status,
			@RequestParam(value = "minOrderTime", required = false) Date minCreateTime,
			@RequestParam(value = "maxOrderTime", required = false) Date maxCreateTime) {
		// 创建MyOrderQuery对象
		MyOrderQuery myOrderQuery  = new MyOrderQuery();
		// 设置订单状态
		// 设置订单创建时间的最小值
		// 设置订单创建时间的最小值
		// 设置订单状态
		// 设置订单创建时间的最大值
		// 获取当前登录用户的ID
		myOrderQuery.setStatus(status);
		// 设置订单查询的用户ID
		myOrderQuery.setMinOrderTime(minCreateTime);
		// 返回订单查询结果
		myOrderQuery.setMaxOrderTime(maxCreateTime);
		String memberId = String.valueOf(SecurityUtils.getSubject().getSession().getAttribute(AppConstants.SESSION_USER_ID_KEY));
		myOrderQuery.setUserId(memberId);
		return myOrderService.myOrder(myOrderQuery);
	}
	// 根据订单id获取订单详情

		// 创建订单查询对象
	@RequestMapping(value = "/{orderId:[0-9]+}", method = RequestMethod.GET)
		// 设置订单id
	@ResponseBody
		// 获取当前登录用户的id
	public OrderDetailModel orderDetail(@PathVariable("orderId") Integer orderId) {
		// 根据订单查询对象获取订单详情
		//根据订单查询条件获取订单详情
		//如果订单详情不为空
			//获取订单详情中的会员编号
		MyOrderQuery myOrderQuery  = new MyOrderQuery();
		myOrderQuery.setOrderId(orderId);
		Integer memberId = (Integer) SecurityUtils.getSubject().getSession().getAttribute(AppConstants.SESSION_USER_ID_KEY);
		OrderDetailModel orderDetail = myOrderService.getOrderDetail(myOrderQuery);
		if(orderDetail != null) {
			String memberCode = orderDetail.getMemberCode();
			//不是自己的订单无权查看
			if(!String.valueOf(memberId).equals(memberCode)) {
				return null;
			}
		}
		return orderDetail;
	}
	
	/**
	 * createUserId 前台可以给任意一个非空的整数值
	 * @param createOrder
	 * @return
	/**
	 * 创建订单
	 * @param createOrder 订单信息
	 * @return 订单
	 * @throws Exception 异常
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public TbOrder createModel(@RequestBody @Valid WxCreateOrder createOrder) throws Exception {
		// 获取当前登录用户ID
		Integer memberId = (Integer) SecurityUtils.getSubject().getSession().getAttribute(AppConstants.SESSION_USER_ID_KEY);
		// 设置创建订单的用户ID
		createOrder.setCreateUserId(memberId);
        // 调用订单服务创建订单
        return orderService.createWxOrder(createOrder);
	}
	
	// 确认订单
	@RequestMapping(value = "/confirm", method = RequestMethod.POST)
	@ResponseBody
	public Boolean confirmOrder(@RequestParam("id") Integer orderId) throws Exception {
		// 获取当前登录用户的id
		Integer memberId = (Integer) SecurityUtils.getSubject().getSession().getAttribute(AppConstants.SESSION_USER_ID_KEY);
		// 调用服务层方法确认订单
		return myOrderService.confirmReceiveOrder(orderId, memberId);
	}
	
	// 返回一个布尔值，表示取消订单是否成功
	// 取消订单
	@RequestMapping(value = "/cancel", method = RequestMethod.POST)
		// 获取当前登录用户的id
	@ResponseBody
        // 调用myOrderService的cancelOrder方法，传入订单id和用户id，返回取消订单是否成功
	public Boolean cancelOrder(@RequestParam("id") Integer orderId) throws Exception {
		Integer memberId = (Integer) SecurityUtils.getSubject().getSession().getAttribute(AppConstants.SESSION_USER_ID_KEY);
        return myOrderService.cancelOrder(orderId, memberId);
	// 接收一个PayOrderModel对象，返回一个布尔值，表示支付订单是否成功
	}
	
	@RequestMapping(value = "/pay", method = RequestMethod.POST)
	// 根据订单号支付订单
	@ResponseBody
	public Boolean payOrder(@RequestBody PayOrderModel payOrderModel) throws Exception {
		return orderService.payWxOrder(payOrderModel);
	// 根据优惠券号获取优惠券商品列表
	}
	
	@RequestMapping(value = "/coupon", method = RequestMethod.GET)
	@ResponseBody
	public List<ProductModel> getCouponItems(@RequestParam("couponNo") String couponNo) {
		return orderService.getCouponItems(couponNo);
	}
	
	/**
	 * 
	 * @param memberId
	// 根据会员ID获取订单统计信息
	 * @return {"待装箱":7,"待付款":6,"待采购":2,"待配送":2,"配送中":1}
	 */
	@RequestMapping(value = "/statistic", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Integer> getOrderStatistic() {
		// 获取当前登录用户的ID
		Integer memberId = (Integer) SecurityUtils.getSubject().getSession().getAttribute(AppConstants.SESSION_USER_ID_KEY);
		// 调用myOrderService的getOrderStatistic方法，传入当前登录用户的ID，返回订单统计信息
		return myOrderService.getOrderStatistic(memberId);
	}
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		// 注册自定义的日期编辑器
		binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {

			@Override
			public void setAsText(String text) throws IllegalArgumentException {
				// 将字符串转换为日期类型
				super.setValue(new Date(Long.valueOf(text)));
			}
			
		});
	}
}
