package com.iamberry.wechat.handles.cart;

import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.iamberry.wechat.core.entity.coupon.Exchange;
import com.iamberry.wechat.core.entity.coupon.ExchangeItem;
import com.iamberry.wechat.core.entity.eo.GiftPool;
import com.iamberry.wechat.core.entity.eo.ItemGift;
import com.iamberry.wechat.core.entity.product.ProductColor;
import com.iamberry.wechat.face.coupon.ExchangeService;
import com.iamberry.wechat.face.eo.GiftPoolService;
import com.iamberry.wechat.face.eo.ItemGiftService;
import com.iamberry.wechat.face.ism.IntegralLogService;
import com.iamberry.wechat.face.porduct.ProductColorService;
import com.iamberry.wechat.service.ActivityUtil;
import com.iamberry.wechat.service.eo.ItemGiftServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
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 com.iamberry.wechat.core.entity.PaperBean;
import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.WechatUtils;
import com.iamberry.wechat.core.entity.admin.ShopSystemRule;
import com.iamberry.wechat.core.entity.cart.CartDto;
import com.iamberry.wechat.core.entity.cart.SendPayDto;
import com.iamberry.wechat.core.entity.cart.ShopOrderAndItemDto;
import com.iamberry.wechat.core.entity.cart.ShopOrderItemDto;
import com.iamberry.wechat.core.entity.cart.ShopSalesOrderDto;
import com.iamberry.wechat.core.entity.cart.UserGiftDto;
import com.iamberry.wechat.core.entity.coupon.CouponItem;
import com.iamberry.wechat.core.entity.coupon.CouponType;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.core.entity.mq.MQMessage;
import com.iamberry.wechat.core.entity.order.Order;
import com.iamberry.wechat.core.entity.order.OrderItem;
import com.iamberry.wechat.core.entity.pay.PayResult;
import com.iamberry.wechat.face.admin.SystemService;
import com.iamberry.wechat.face.cart.CartService;
import com.iamberry.wechat.face.cart.ProductInfoService;
import com.iamberry.wechat.face.coupon.CouponItemService;
import com.iamberry.wechat.face.member.MemberService;
import com.iamberry.wechat.face.order.AdminOrderService;
import com.iamberry.wechat.face.pay.PayService;
import com.iamberry.wechat.handles.mq.MQServiceProxy;
import com.iamberry.wechat.tools.NameUtils;
import com.iamberry.wechat.tools.ResultInfo;
import com.iamberry.wechat.tools.StaticInfo;

@Controller
@RequestMapping("/wechat/cart")
public class CartHandlers {
	private static final Logger logger = LoggerFactory.getLogger(CartHandlers.class);
	
	@Autowired
	private CartService cartService;

	@Autowired
	private AdminOrderService adminOrderService;
	
	@Autowired
	private PayService payService;

	@Autowired
	private MemberService memberService;
	
	@Autowired
	private CouponItemService couponItemService;

	@Autowired
	private ProductColorService productColorService;
	@Autowired
	ExchangeService exchangeService;
	@Autowired
	GiftPoolService giftPoolService;
	@Autowired
	ItemGiftService itemGiftService;
	@Autowired
	private ActivityUtil activityUtil;
	@Autowired
	private IntegralLogService integralLogService;
	
	/**
	 * 进入购物车页面
	 * @param request
	 * @return	ResultMsg
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/selectCartByOpenId",method = RequestMethod.POST)
	public ResultMsg selectCartByOpenId(HttpServletRequest request) throws Exception {
		// 得到用户信息
		Member member =  WechatUtils.getUserBySession(request);
		// 查询用户的购物车项
		List<CartDto> cartList = cartService.selectCartByOpenId(member.getUserOpenid());
		if (cartList == null || cartList.isEmpty()) {
			return ResultMsg.ERROR();
		}
		/*
		 * 金额统计
		 */
		int total = 0;
		activityUtil.getColorSpike(cartList);
		for (CartDto cartDto : cartList) {
			if (cartDto.getProductStatus() != null && cartDto.getProductStatus() == 1) {
				// 产品状态:在售
				total += cartDto.getCartNum() * cartDto.getProductDiscount();
			}
		}
		/*
		 * 组装返回的数据
		 */
		Map<String, Object> map = new HashMap<String, Object>(5);
		map.put("isShow", ResultInfo.ISSHOW);
		map.put("cartDesc", ResultInfo.CARTDESC);
		map.put("cart_url", ResultInfo.CART_URL);
		map.put("cartList", cartList);
		map.put("total", total);
		return ResultMsg.SUCCESS().setData(map);
	}
	
	/**
	 * 获取购物车数量
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/getCartNumByCartOpenId")
	public ResultMsg getCartNumByCartOpenId(HttpServletRequest request) throws Exception {
		return ResultMsg.SUCCESS().setData(cartService.getCartNumByCartOpenId(WechatUtils.getUserBySession(request).getUserOpenid()));
	}

	/**
	 * 获取购物车数量
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/getCartColorNum")
	public ResultMsg getCartColorNum(HttpServletRequest request,Integer colorId) throws Exception {
		// 查询登陆的用户
		Member member = WechatUtils.getUserBySession(request);
		// 返回的数据
		if(colorId == null){
			return ResultMsg.ERROR();
		}
		// 查询数据库
		CartDto cartDto = new CartDto();
		cartDto.setCartOpenId(member.getUserOpenid());
		cartDto.setCartColorId(colorId);
		// 查询数据
		List<CartDto> cartDtos = this.cartService.getCartDtoList(cartDto);
		if(cartDtos != null && cartDtos.size() >= 1){
			return ResultMsg.SUCCESS().setData(cartDtos.get(0));
		}
		return ResultMsg.SUCCESS();
	}
	
	/**
	 * 首页添加商品到购物车
	 * @param productId
	 * @param cartNum
	 * @param request
	 * @return ResultMsg
	 */
	@ResponseBody
	@RequestMapping("/addProductToCart")
	public ResultMsg addProductToCart(
			@RequestParam("productId") String productId,
			@RequestParam("colorId") Integer colorId,
			@RequestParam(value = "cartNum", required = false, defaultValue = "1") Integer cartNum,
			HttpServletRequest request) throws Exception {
		// 判断内容是否为空
		if (productId == null || "".equals(productId) || "null".equals(productId)) {
			return ResultMsg.ERROR();
		}
		// 获取当前登陆的用户数据
		String openId  = WechatUtils.getUserBySession(request).getUserOpenid();
		// 获取产品id
		int pid = 0;
		if (StaticInfo.pattern.matcher(productId).find()) {
			pid = Integer.parseInt(productId);
		} else {
			try {
				String productIdString = StaticInfo.md5.decrypt(productId);
				pid = Integer.parseInt(productIdString);
			} catch (Exception e) {
				return ResultMsg.ERROR();
			}
		}
		// 准备
		CartDto cartDto = new CartDto();
		cartDto.setCartOpenId(openId);
		cartDto.setCartProductId(pid);
		cartDto.setCartColorId(colorId);
		//根据用户id和商品id查询购物车中是否存在
		CartDto cart = cartService.getCartByCartProductId(cartDto);
		if (null != cart) {
			//存在，则修改数量
			cartDto.setCartNum(cartNum + cart.getCartNum());
			int num = cartService.updateCartByOpenId(cartDto);
			return num >= 1 ? ResultMsg.SUCCESS().setData(cartDto.getCartId()) : ResultMsg.ERROR();
		}
		//不存在，则添加商品信息
		cartDto.setCartNum(cartNum);
		int num = cartService.addProductToCart(cartDto);
		return num >= 1 ? ResultMsg.SUCCESS().setData(cartDto.getCartId()) : ResultMsg.ERROR();
	}
	
	/**
	 * 	点击加号按钮添加商品到购物车
	 * @param request
	 * @param productId
	 * @param cartNum
	 * @return	ResultMsg
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/addCart",method = RequestMethod.GET)
	public ResultMsg  addCart(
			HttpServletRequest request,
			@RequestParam("productId") Integer productId,
			@RequestParam("colorId") Integer colorId,
			@RequestParam(value = "cartNum", required = false, defaultValue = "1") Integer cartNum) throws Exception {
		// 当前用户信息
		String openId  = WechatUtils.getUserBySession(request).getUserOpenid();
		// 查询用户的数量
		CartDto cartDto = new CartDto();
		cartDto.setCartOpenId(openId);
		cartDto.setCartColorId(colorId);
		cartDto.setCartProductId(productId);
		Integer productNum = cartService.getCartNumByOpenId(cartDto);
		cartDto.setCartNum(productNum + cartNum);
		// 返回
		return cartService.updateCartByOpenId(cartDto) >= 1 ? ResultMsg.SUCCESS() : ResultMsg.ERROR();
	}
	
	/**
	 * 	点击减号按钮删除商品
	 * @param request
	 * @param productId
	 * @param cartNum
	 * @return	ResultMsg
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteCart",method = RequestMethod.GET)
	public ResultMsg  deleteCart(
			HttpServletRequest request,
			@RequestParam("productId") Integer productId,
			@RequestParam("colorId") Integer colorId,
			@RequestParam(value = "cartNum", required = false, defaultValue = "1") Integer cartNum) throws Exception {
		// 获取登陆用户
		String openId  = WechatUtils.getUserBySession(request).getUserOpenid();
		// 查询购物车项目
		CartDto cartDto = new CartDto();
		cartDto.setCartOpenId(openId);
		cartDto.setCartColorId(colorId);
		cartDto.setCartProductId(productId);
		Integer productNum = cartService.getCartNumByOpenId(cartDto);
		int num = 0;
		if (productNum != null && productNum > 1) {
			cartDto.setCartNum(productNum - cartNum);
			num = cartService.updateCartByOpenId(cartDto);
		} 
		return num >= 1 ? ResultMsg.SUCCESS() : ResultMsg.ERROR();
	}
	
	/**
	 * 点击垃圾桶按钮删除购物车物品信息
	 * @param request
	 * @param cartId
	 * @return ResultMsg
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteCartByProductId",method = RequestMethod.GET)
	public ResultMsg deleteCartByProductId(
			HttpServletRequest request,@RequestParam("productId") Integer productId,
			@RequestParam("colorId") Integer colorId) throws Exception{
		// 查询当前登陆的用户
		String openId  = WechatUtils.getUserBySession(request).getUserOpenid();
		// 删除操作
		CartDto cartDto = new CartDto();
		cartDto.setCartOpenId(openId);
		cartDto.setCartColorId(colorId);
		cartDto.setCartProductId(productId);
		// 返回
		return cartService.deleteCartByCartId(cartDto) >= 1 ? ResultMsg.SUCCESS() : ResultMsg.ERROR();
	}
	
	/**
	 * 
	 * 查询订单列表页面
	 * @param request
	 * @param pageSize
	 * @param pageNO
	 * @param salesStatus
	 * @param text
	 * @return ResultMsg
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/selectShopOrder")
	public  ResultMsg selectShopOrder(HttpServletRequest request,
			@RequestParam(value= "pageSize",defaultValue= "10" ,required=false) Integer pageSize, 
			@RequestParam(value = "pageNO", defaultValue = "1") Integer pageNO,
			@RequestParam(value = "salesStatus",required = false) String salesStatus,
			@RequestParam(value = "beginDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate,
			@RequestParam(value = "text", required = false)String text) throws Exception {
		// 当前登陆的用户
		String openId  = WechatUtils.getUserBySession(request).getUserOpenid();
		// 组装查询的数据
		PaperBean bean = new PaperBean();
		bean.setStatus(salesStatus);
		bean.setBeginDate(beginDate);
		bean.setStr(text);
		bean.setText(openId);
		bean.setPaperData(pageNO, pageSize);
		// 查询数据
		List<ShopOrderAndItemDto> shopOrderList = cartService.selectShopOrder(bean);
		ShopSystemRule shopSystemRule = this.adminOrderService.getReluByRid(109);
		
		//查询订单列表时同步百胜信息
		if (null != shopOrderList && shopOrderList.size() > 0) {
			for (ShopOrderAndItemDto orderDto : shopOrderList) {
				//判断该订单能否进行支付
				if ("1".equals(orderDto.getSalesStatus()) && orderDto.getOrderItemList() != null && orderDto.getOrderItemList().size() > 0) {
					orderDto.setEfficacious(true);
					for (OrderItem orderItem : orderDto.getOrderItemList()) {
						if (orderItem.getProductStatus() != 1 && orderItem.getItemProductType() != shopSystemRule.getRuleNum().intValue()) {
							orderDto.setEfficacious(false);
						}
					}
				}
			}
		}
		// 查询页码数
		int pageCount = cartService.getOrderNumByOpenId(bean);
		int count = pageCount % pageSize == 0? pageCount/pageSize:(pageCount/pageSize)+1;
		// 返回数据
		Map<String,Object> map = new HashMap<String,Object>(3);
		map.put("pageCount", count);
		map.put("orderList", shopOrderList);
		map.put("productType", shopSystemRule.getRuleNum());
		return ResultMsg.SUCCESS().setData(map);
	}
	
	
	/**
	 * 查询订单详情
	 * @param request
	 * @param orderId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/getShopOrderByOrderId")
	public ResultMsg getShopOrderByOrderId(HttpServletRequest request, @RequestParam("orderId") String orderId) throws Exception{
		// 查询当前登陆的用户
		String openId  = WechatUtils.getUserBySession(request).getUserOpenid();
		// 系统规则
		ShopSystemRule shopSystemRule = this.adminOrderService.getReluByRid(109);
		ResultMsg remsg=new ResultMsg();
		// 准备查询数据
		ShopSalesOrderDto shopSalesOrderDto = cartService.getShopOrderByOrderId(orderId, openId);
		if (shopSalesOrderDto == null) {
			return ResultMsg.ERROR();
		}
		// 计算订单的支付金额
		if (null != shopSalesOrderDto.getSalesCouponReceiveId() && !"".equals(shopSalesOrderDto.getSalesCouponReceiveId())) {
			CouponItem couponItem = new CouponItem();
			couponItem.setCouponItemId(shopSalesOrderDto.getSalesCouponReceiveId());
			couponItem.setCouponItemUseropenid(openId);
			CouponType couponType = cartService.getCouponReduceByItemId(couponItem);
			if (couponType != null) {
				int money = 0;
				switch (couponType.getCouponType()) {
					case 1:
						money = couponType.getCouponReduce();
						break;
					case 2:
						money = shopSalesOrderDto.getSalesAmount() - shopSalesOrderDto.getSalesAmount() * couponType.getCouponReduce()/100;
						break;
					default: break;
				}
				shopSalesOrderDto.setCouponReduce(money);
			}
		}
		// 查询订单项
		List<ShopOrderItemDto> orderItemList = cartService.selectShopOrderItem(orderId);
		if (orderItemList == null || orderItemList.isEmpty()) {
			return ResultMsg.ERROR();
		}
		shopSalesOrderDto.setOrderItemList(orderItemList);
		//判断该订单是否可以支付
		if ("1".equals(shopSalesOrderDto.getSalesStatus())) {
			shopSalesOrderDto.setEfficacious(true);
			for (ShopOrderItemDto shopOrderItemDto : orderItemList) {
				if (!"1".equals(shopOrderItemDto.getProductStatus()) && shopOrderItemDto.getItemProductType() != shopSystemRule.getRuleNum().intValue()) {
					shopSalesOrderDto.setEfficacious(false);
				}
			}
		}
		// 返回数据
		return ResultMsg.SUCCESS().setData(shopSalesOrderDto);
	}
	
	
	/**
	 * 订单页面支付方法
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/orderPay")
	public ResultMsg orderPay(HttpServletRequest request, @RequestParam("orderId") String orderId) throws Exception {
		// 查询登陆的用户
		String openId  = WechatUtils.getUserBySession(request).getUserOpenid();
		Member member = memberService.getMemberByUserOpenId(openId);
		// 查询订单信息
		Order order = new Order();
		order.setSalesOrderid(orderId);
		order.setSalesOpenid(openId);
		ShopSalesOrderDto shopSalesOrderDto = cartService.getShopOrderByOrderId(orderId, openId);
		List<ShopOrderItemDto> orderItemList = cartService.selectShopOrderItem(orderId);
		//获取账户余额，判断是否在可用范围内
		int balancePayAmount=shopSalesOrderDto.getBalancePayAmount();
		int total = shopSalesOrderDto.getSalesYetAmount();
		//判断余额支付金额是否大于用户余额
		if(balancePayAmount > 0 && member.getUserIncome() > balancePayAmount) {
			total = total - balancePayAmount > 0 ? total - balancePayAmount : 0;
		} else if(balancePayAmount > 0 && member.getUserIncome() < balancePayAmount) {
			return ResultMsg.ERROR().setData(ResultInfo.NO_ENOUGH_FOR_PAY);
		}
		// 发起支付
		SendPayDto dto = new SendPayDto();
		dto.setSuccess(false);
		PayResult result = payService.requestPay(openId, orderId, total, orderItemList.get(0).getItemProductName() + "...", NameUtils.getConfig("WECHAT_ORDER_BACK"));
		if (result.isPaySuccess()) {
			dto.setInfo(result.getPayData());
			dto.setSuccess(true);
			dto.setOrderId(orderId);
		} else {
			dto.setInfo(result.getPayData());
			dto.setOrderId(orderId);
		}
		return ResultMsg.SUCCESS().setData(dto);
	}
	
	/**
	 * 	取消订单
	 * @param request
	 * @param orderId
	 * @return ResultMsg
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/cancelOrder")
	public ResultMsg cancelOrder(
			HttpServletRequest request,
			@RequestParam(value = "orderId")String orderId,
			@RequestParam(value = "status",defaultValue = "-1") Integer status) throws Exception{
		// 获取当前登陆的用户信息
		String openId  = WechatUtils.getUserBySession(request).getUserOpenid();
		// 查询订单信息
		ShopSalesOrderDto orderDto = cartService.getOpenIdAndStatus(orderId);
		if (orderDto == null || !openId.equals(orderDto.getSalesOpenid()) || Integer.parseInt(orderDto.getSalesStatus()) != 1) {
			return ResultMsg.ERROR();
		}
		// 恢复优惠券信息
		if (null != orderDto.getSalesCouponReceiveId() && !"".equals(orderDto.getSalesCouponReceiveId())) {
			CouponItem couponItem = new CouponItem();
			couponItem.setCouponItemId(orderDto.getSalesCouponReceiveId());
			couponItem.setCouponItemUseropenid(openId);
			couponItem.setCouponUseStatus(1);
			couponItem.setOldCouponUseStatus(7);
			couponItemService.updateCouponItemById(couponItem);
		}
		// 取消订单
		Order order = new Order();
		order.setSalesStatus(0);
		order.setSalesOrderid(orderId);
		order.setOldStatus(status);
		adminOrderService.updateOrderStatusByOrderId(order);
		//恢复赠品的库存
		List<OrderItem> orderItemList = adminOrderService.getShopOrderItemByOrderId(orderId);
		for (OrderItem orderItem:orderItemList) {
			// 查询赠品信息
			ItemGift itemGift = new ItemGift();
			itemGift.setItemId(orderItem.getItemId());
			List<ItemGift> itemGiftList = itemGiftService.getItemGiftList(itemGift);
			if (itemGiftList == null || itemGiftList.isEmpty()) {
				break;
			}
			for (ItemGift ig:itemGiftList) {
				// 取消赠品
				GiftPool gp = new GiftPool();
				gp.setGiftPoolId(ig.getGiftPoolId());
				gp.setGiftPoolStock(ig.getItemGiftNum());
				giftPoolService.updateAddStock(gp);
			}
		}
		//恢复积分
		try {
			integralLogService.cancelSurplusIntegral(orderId);
		}catch (Exception e){
			e.printStackTrace();
			logger.info("取消订单，积分退回失败，订单id："+ orderId);
		}
		return ResultMsg.SUCCESS();
	}
	
	
	/**
	 * 	订单退货或退款
	 * @param request
	 * @param orderId
	 * @param remark
	 * @param status
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/returnOrder")
	public ResultMsg returnOrder(HttpServletRequest request,
			@RequestParam(value = "orderId")String orderId,
			@RequestParam(value = "remark",defaultValue = "")String remark,
			@RequestParam(value = "returnOrderId",defaultValue = "")String returnOrderId,
			@RequestParam(value = "status",defaultValue = "-1") Integer status) throws Exception {
		// 获取当前登陆的用户
		String openId  = WechatUtils.getUserBySession(request).getUserOpenid();
		// 查询规则
		ShopSystemRule shopSystemRule = adminOrderService.getReluByRid(102);
		// 查询用户信息
		Order order = new Order();
		order.setSalesOrderid(orderId);
		ShopSalesOrderDto orderDto = this.cartService.getOpenIdAndStatus(orderId);
		if (orderDto == null) {
			return ResultMsg.ERROR();
		}
		int sta = Integer.valueOf(orderDto.getSalesStatus());
		// 判断订单状态，如果非2/5，不允许
		if (sta != 2 && sta != 5) {
			return ResultMsg.ERROR();

		}
		if (!openId.equals(orderDto.getSalesOpenid())) {
			return ResultMsg.ERROR();
		}
		//判断该订单是否被锁定，如果被锁定，则不能退款或是退换货
		if (orderDto.getSalesLockType() != null && "2".equals(orderDto.getSalesLockType().toString())) {
			return ResultMsg.ERROR();
		}
		//判断该订单是否可以退款
		if (status == 2) {
			order.setSalesStatus(3);
			order.setSalesReturnDes(remark);
			cartService.updateOrderByOrderId(order);
			return ResultMsg.SUCCESS();
		}
		// 7天内用户可以退货
		if ((System.currentTimeMillis() - orderDto.getSalesSendDate().getTime()) > shopSystemRule.getRuleNum() * (1000*3600*24)) {
			return ResultMsg.ERROR();
		}
		order.setSalesStatus(6);
		order.setSalesReturnDes(remark);
		order.setSalesChangeOrderid(returnOrderId);
		cartService.updateOrderByOrderId(order);
		return ResultMsg.SUCCESS();
	}
	
	/**
	 * 订单换货
	 * @param request
	 * @param orderId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/exchangeOrder")
	public ResultMsg exchangeOrder(HttpServletRequest request, 
			@RequestParam(value = "orderId")String orderId,
			@RequestParam(value = "status",defaultValue = "-1") Integer status,
			@RequestParam(value = "remake",defaultValue = "") String remake,
			@RequestParam(value = "changeOrderId",defaultValue = "") String changeOrderId) throws Exception{
		// 查询当前登陆的用户
		String openId  = WechatUtils.getUserBySession(request).getUserOpenid();
		// 查询规则信息
		ShopSystemRule shopSystemRule = adminOrderService.getReluByRid(103);
		// 修改的订单信息
		Order order = new Order();
		order.setSalesOrderid(orderId);
		order.setSalesChangeOrderid(changeOrderId);
		order.setSalesExchangeDes(remake);
		// 查询订单信息
		ShopSalesOrderDto orderDto = cartService.getOpenIdAndStatus(orderId);
		if (orderDto == null) {
			return ResultMsg.ERROR();
		}
		// 判断是否在可换货的范围内
		order.setOldStatus(Integer.valueOf(orderDto.getSalesStatus()));
		int sta = Integer.valueOf(orderDto.getSalesStatus());
		if (sta!=5 && sta!=12) {
			return ResultMsg.ERROR();
		}
		if (!openId.equals(orderDto.getSalesOpenid())) {
			return ResultMsg.ERROR();
		}
		if (Integer.parseInt(orderDto.getSalesStatus()) != 5 && Integer.parseInt(orderDto.getSalesStatus()) != 12) {
			return ResultMsg.ERROR();
		}
		if ((System.currentTimeMillis() - orderDto.getSalesSendDate().getTime()) >= shopSystemRule.getRuleNum() * (1000*3600*24)) {
			return ResultMsg.ERROR();
		}
		// 1年内用户可以换货
		order.setSalesStatus(11);
		adminOrderService.updateOrderStatusByOrderId(order);
		return ResultMsg.SUCCESS();
	}



	/**
	 * 获取T1/T2滤芯
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/get_exchange")
	public ResultMsg getExchange(HttpServletRequest request,Integer couponId) throws Exception{
		if(couponId == null){
			return ResultMsg.ERROR();
		}
		List<ProductColor> productColorList = new ArrayList<>(10);
		
		Exchange exchange = new Exchange();
		exchange.setCouponId(couponId);
		List<Exchange>  exchangeList = exchangeService.getExchangeList(exchange);
		for (Exchange ex : exchangeList) {
			List<ExchangeItem> exchangeItemList = ex.getExchangeItemList();
			for (ExchangeItem exchangeItem:exchangeItemList) {
				ProductColor productColor = productColorService.selectProductColorById(exchangeItem.getColorId());
				productColorList.add(productColor);
			}
		}
		return ResultMsg.SUCCESS().setData(productColorList);
	}


	/**
	 * 获取38节的权益
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/get_38Interests")
	public ResultMsg get38Interests(HttpServletRequest request) throws Exception{
		List<ProductColor> productColorList = new ArrayList<>();
		ProductColor productColor = productColorService.selectProductColorById(48);
		productColorList.add(productColor);
		return ResultMsg.SUCCESS().setData(productColorList);
	}

}
