package com.iamberry.rent.handlers.order;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.iamberry.wechat.core.entity.rent.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.rent.face.machine.MachineService;
import com.iamberry.rent.face.order.RentOrderService;
import com.iamberry.rent.face.order.ServiceCouponService;
import com.iamberry.rent.face.rebate.OrderRebateService;
import com.iamberry.rent.utils.RentOrderUtil;
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.SendPayDto;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.core.entity.pay.PayResult;
import com.iamberry.wechat.core.entity.product.Product;
import com.iamberry.wechat.core.entity.product.ProductColor;
import com.iamberry.wechat.face.admin.SystemService;
import com.iamberry.wechat.face.cart.ProductInfoService;
import com.iamberry.wechat.face.home.HomeService;
import com.iamberry.wechat.face.member.MemberService;
import com.iamberry.wechat.face.pay.PayService;
import com.iamberry.wechat.face.porduct.ProductColorService;
import com.iamberry.wechat.tools.NameUtils;
import com.iamberry.wechat.tools.OrderNOUtil;
import com.iamberry.wechat.tools.ResultInfo;

@Controller
@RequestMapping("/wechat/rent_order")
public class RentOrderHandler {
    @Autowired
    private RentOrderService rentOrderService;
    @Autowired
    private ProductColorService productColorService;
    @Autowired
    private OrderRebateService orderRebateService;
    @Autowired
    private PayService payService;
    @Autowired
    private ServiceCouponService serviceCouponService;
    @Autowired
    private ProductInfoService productInfoService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private HomeService homeService;
    @Autowired
    private SystemService systemService;
    
    /**
     * 订单详情接口
     * 2017年3月14日
     * @author muzx
     * @param req
     * @param pageSize
     * @param pageNO
     * @param rentOrderDTO
     * @return
     */
    @ResponseBody
    @RequestMapping("/list")
    public ResultMsg getRentOrderList(HttpServletRequest req,
            @RequestParam(value = "pageSize", defaultValue = "2", required = false) Integer pageSize,
            @RequestParam(value = "pageNO", defaultValue = "1", required = false) Integer pageNO,
            RentOrderDTO rentOrderDTO){
        ResultMsg rm = new ResultMsg();
        rm.setStatus(false);
        rm.setMessage("您没有租赁订单,请完成租赁后查看!");
        rm.setResultCode("400");
        String openId = WechatUtils.getUserBySession(req).getUserOpenid();
        rentOrderDTO.setRentOpenid(openId);
        rentOrderDTO.getPage().setPageSize(pageSize);
        rentOrderDTO.getPage().setPageNumber(pageNO);
        Map<String, Object> map = rentOrderService.listRentOrder(rentOrderDTO);
        if(map != null){
            rm.setData(map);
            rm.setStatus(true);
            rm.setResultCode("200");
            rm.setMessage("");
        }
        return rm;
    }
    
    /**
     * 查询我要租赁页面详情
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/selectRentProduct")
    public ResultMsg selectRentProduct(HttpServletRequest request) throws Exception {
        ResultMsg msg = new ResultMsg();
        Map<String,Object> map = new HashMap<String,Object>();
        List<Product> giftList = new ArrayList<Product>();
        List<Product> rentProductList = new ArrayList<Product>();
        RentTypeInfo rentTypeInfo = new RentTypeInfo();
        rentTypeInfo.setTypeStatus(1);
        Member member = WechatUtils.getUserBySession(request);
        
        //查询出租赁类型所对应的赠品id
        ShopSystemRule shopSystemRuleOne = systemService.selectOneShopRuleById(231);
        ShopSystemRule shopSystemRuleTwo = systemService.selectOneShopRuleById(232);
        ShopSystemRule shopSystemRuleThree = systemService.selectOneShopRuleById(233);
        
        //查询租赁方式列表
        List<RentTypeInfo> typeList = orderRebateService.listRebateType(rentTypeInfo);
        if (typeList == null || typeList.size() == 0) {
            msg.setMessage("没有查到租赁方式信息");
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
            return msg;
        }
        Product product = new Product();
        product.setProductRentType(2);
        //查询所有租赁类型的商品和赠品信息
        List<Product> productList = productInfoService.selectPageProduct(product);
        product.setProductStatus(2);
        giftList = productInfoService.listSelectProduct(product);
        //将商品和赠品拆分
        if (productList == null || productList.size() == 0) {
            msg.setMessage("没有查到租赁商品信息");
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
            return msg;
        }
        for (Product info : productList) {
            if (info.getProductType().getTypeId() != null && info.getProductType().getTypeId().intValue() == 1) {
                rentProductList.add(info);
            }
        }
        map.put("typeList", typeList);
        map.put("rentProductList", rentProductList);
        map.put("giftList", giftList);
        map.put("shareOpenid", member.getUserOpenid());
        map.put("ruleOne", shopSystemRuleOne.getRuleNum());
        map.put("ruleTwo", shopSystemRuleTwo.getRuleNum());
        map.put("ruleThree", shopSystemRuleThree.getRuleNum());
        msg.setData(map);
        msg.setResultCode(ResultInfo.SUCCESSCODE);
        msg.setStatus(true);
        return msg;
    }
    
    /**
     * 查询用户可使用服务费抵扣券
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/selectServiceCoupon")
    public ResultMsg selectServiceCoupon(HttpServletRequest request) throws Exception {
        ResultMsg msg = new ResultMsg();
        Member member = WechatUtils.getUserBySession(request);
        ServiceCouponInfo info = new ServiceCouponInfo();
        info.setServiceOpenId(member.getUserOpenid());
        info.setServiceStatus(1);
        List<ServiceCouponInfo> couponList = serviceCouponService.list(info);
        msg.setData(couponList);
        msg.setResultCode(ResultInfo.SUCCESSCODE);
        msg.setStatus(true);
        return msg;
    }
    /**
     * 租赁订单支付方法
     * @param request
     * @param rentOrderDTO
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/rentOrderPay")
    public ResultMsg rentOrderPay(HttpServletRequest request,RentOrderDTO rentOrderDTO) throws Exception {
        ResultMsg msg = new ResultMsg();
        RentOrder rentOrder = new RentOrder();
        List<RentOrderItem> orderItemList = new ArrayList<RentOrderItem>();
        ServiceCouponInfo couponInfo = null;
        SendPayDto dto = new SendPayDto();
        Date nowDate=new Date();
        int rentNum = 0;	//租赁机器数量
        int rentPayAmount = 0;	//应支付金额，包含押金，不包含抵扣金额
        int rentYetAmount = 0;	//实际支付总金额，包含押金和抵扣金额
        
        if (rentOrderDTO.getProductId() == null || rentOrderDTO.getGiftId() == null) {
            msg.setMessage("该订单不合法，请重新下单");
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
            return msg;
        }
        
        //将商品和赠品信息添加到集合中
        List<RentOrderItemDTO> itemList = new ArrayList<RentOrderItemDTO>();
        RentOrderItemDTO productItem = new RentOrderItemDTO();
        productItem.setItemColorId(rentOrderDTO.getProductId());
        itemList.add(productItem);
        RentOrderItemDTO giftItem = new RentOrderItemDTO();
        giftItem.setItemColorId(rentOrderDTO.getGiftId());
        itemList.add(giftItem);
        rentOrderDTO.setOrderItemList(itemList);
        
        Member member =  WechatUtils.getUserBySession(request);
        //生成订单id
        String orderId = OrderNOUtil.createOrderCode(member.getUserId());
        
        rentOrder.setRentAddressInfo(rentOrderDTO.getRentAddressInfo());
        rentOrder.setRentAddressName(rentOrderDTO.getRentAddressName());
        rentOrder.setRentAddressTel(rentOrderDTO.getRentAddressTel());
        rentOrder.setRentTypeId(rentOrderDTO.getRentTypeId());
        rentOrder.setRentServiceId(rentOrderDTO.getRentServiceId());
        rentOrder.setRentOrderId(orderId);
        rentOrder.setRentOpenid(member.getUserOpenid());
        rentOrder.setRentStatus(1);
        rentOrder.setRentTransactionId("8990"+nowDate.getTime());
        
        
        //如果存在优惠券id，则查询优惠券信息
        if (rentOrderDTO.getRentServiceId() != null && !"".equals(rentOrderDTO.getRentServiceId())) {
            ServiceCouponInfo serviceCouponInfo = new ServiceCouponInfo();
            serviceCouponInfo.setServiceId(rentOrderDTO.getRentServiceId());
            serviceCouponInfo.setServiceStatus(1);
            List<ServiceCouponInfo> couponList = serviceCouponService.list(serviceCouponInfo);
            if (couponList == null || couponList.size() == 0) {
                msg.setMessage("该优惠券已使用，请重新下单");
                msg.setResultCode(ResultInfo.ERRORCODE);
                msg.setStatus(false);
                return msg;
            }
            serviceCouponInfo = couponList.get(0);
            couponInfo = new ServiceCouponInfo();
            couponInfo.setServiceId(serviceCouponInfo.getServiceId());
            couponInfo.setServiceOpenId(member.getUserOpenid());
            couponInfo.setServiceOldStatus(1);
            couponInfo.setServiceStatus(2);
            rentOrder.setRentServiceMoney(serviceCouponInfo.getServiceMoney());
        } else {
        	rentOrder.setRentServiceMoney(0);
        }
        
        int num = 0;
        String pass =  "";
        //生成新的激活码，如果有重复则重新生成
        while (num < 1) {
            //随机生成12为激活码
            pass = RentOrderUtil.getPassward(12, 8);
            RentOrderItem rentOrderItem = new RentOrderItem();
            rentOrderItem.setItemInitCode(pass);
            //根据新生成的密码查询是否有重复，如果有则重新生成
            rentOrderItem = rentOrderService.getOrderItemByInitCode(rentOrderItem);
            if (rentOrderItem == null) {
                ++num;
            }
        }
        
        //根据id和状态查询租赁类型数据
        RentTypeInfo rentTypeInfo = new RentTypeInfo();
        rentTypeInfo.setTypeId(rentOrderDTO.getRentTypeId());
        rentTypeInfo.setTypeStatus(1);
        rentTypeInfo = orderRebateService.getRebateType(rentTypeInfo);
        
        for (RentOrderItemDTO rentOrderItemDTO : rentOrderDTO.getOrderItemList()) {
            RentOrderItem rentOrderItem = new RentOrderItem();
            //根据颜色id查询商品颜色信息
            ProductColor productColor = productColorService.selectProductColorById(rentOrderItemDTO.getItemColorId());
            if (productColor == null) {
                msg.setMessage("没有该商品信息，请重新下单");
                msg.setResultCode(ResultInfo.ERRORCODE);
                msg.setStatus(false);
                return msg;
            }
            
            //判断该订单项是否为赠品，如果是则激活码为“000000000000”
            if (productColor.getColorProductType().intValue() == 5) {
                rentOrderItem.setItemInitCode("000000000000");
                rentOrderItem.setRentDeposit(0);
                rentOrderItem.setRentRemainDeposit(0);
                rentOrderItem.setItemCodeState(2);
            } else {
            	rentNum++;
                rentOrderItem.setItemInitCode(pass);
                rentOrderItem.setRentDeposit(rentTypeInfo.getTypePledgeMoney());
                rentOrderItem.setRentRemainDeposit(rentTypeInfo.getTypePledgeMoney());
                rentOrderItem.setItemCodeState(1);
            }
            rentOrderItem.setItemProductType(productColor.getColorProductType());
            rentOrderItem.setRentIsOut(0);
            rentOrderItem.setItemOrderId(orderId);
            rentOrderItem.setItemProductId(productColor.getColorProductId());
            rentOrderItem.setItemColorId(productColor.getColorId());
            rentOrderItem.setItemStatus(1);
            rentOrderItem.setItemProductName(productColor.getColorProductName());
            rentOrderItem.setItemProductPic(productColor.getColorProductPic());
            rentOrderItem.setItemProductColorName(productColor.getColorName());
            rentOrderItem.setItemMachineNum(rentTypeInfo.getTypeWaterNum());
            rentOrderItem.setItemUseYears(rentTypeInfo.getTypeDuration());
            orderItemList.add(rentOrderItem);
        }
        
        //计算需要支付总金额
        rentYetAmount = rentTypeInfo.getTypeServiceMoney() * rentNum + rentNum * rentTypeInfo.getTypePledgeMoney();
        rentOrder.setRentAmount(rentYetAmount);
        //计算应支付金额
        if (rentOrder.getRentServiceMoney() != null) {
        	rentPayAmount = rentOrder.getRentAmount().intValue() - rentOrder.getRentServiceMoney().intValue();
        } else {
        	rentPayAmount = rentOrder.getRentAmount().intValue();
        }
        if (rentPayAmount <= 0) {
            rentPayAmount = 1;
        }
        rentOrder.setRentPayMoney(rentPayAmount);
      
        //添加订单和订单项，修改优惠券状态
        int sum = rentOrderService.insertRentOrderAndItem(rentOrder, orderItemList, couponInfo);
        
        if (sum > 0) {
            // 发起支付
            PayResult result = payService.requestPay(member.getUserOpenid(), orderId, rentPayAmount,
                    orderItemList.get(0).getItemProductName() + "...", NameUtils.getConfig("WECHAT_RENT_ORDER_BACK"));
            if (result.isPaySuccess()) {
            	dto.setInfo(result.getPayData());
                dto.setSuccess(true);
                dto.setOrderId(orderId);
                msg.setData(dto);
                msg.setMessage(NameUtils.getConfig("SUCCESSINFO"));
                msg.setMessage(result.getPayData());
                msg.setResultCode(ResultInfo.SUCCESSCODE);
                msg.setStatus(true);
                return msg;
            } else {
            	dto.setInfo(result.getPayData());
                dto.setSuccess(false);
                msg.setMessage("ERROR");
                msg.setResultCode(ResultInfo.ERRORCODE);
                msg.setStatus(false);
                return msg;
            }
        } else {
            msg.setMessage("生成订单失败");
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
            return msg;
        }
    }
    
    /**
     * 支付完成后查询订单状态看是否支付成功
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/check_rent_order_status")
    public ResultMsg checkRentOrderStatus(HttpServletRequest request) throws Exception {
    	ResultMsg msg = new ResultMsg();
    	String orderId = request.getParameter("orderId");
    	RentOrder rentOrder = new RentOrder();
    	rentOrder.setRentOrderId(orderId);
    	//根据订单号查询订单信息
    	RentOrder order = rentOrderService.getRentOrderByOrderId(rentOrder);
    	if (order == null) {
    		msg.setResultCode(ResultInfo.ERRORCODE);
    		msg.setStatus(false);
    	}
    	msg.setData(order);
    	msg.setResultCode(ResultInfo.SUCCESSCODE);
    	msg.setStatus(true);
    	return msg;
    }
    
    /**
     * 分享我要租赁页面，当用户点进来后即绑定关系
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/goWantLease", method = RequestMethod.GET)
	public void goWantLease(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		//如果是分享 则绑定关系
		Member member = WechatUtils.getUserBySession(request);
		
		String shareOpenid = request.getParameter("shareOpenid");
		if(StringUtils.isNotEmpty(shareOpenid)){
			memberService.buildChildUser(shareOpenid, member,1,2);
			member.setUserIsFlag(2);
			homeService.updateMemberIsFlagByOpenid(member);
		}
		request.getRequestDispatcher(NameUtils.getConfig("WECHAT_GO_I_WANT_LEASE")).forward(request, response);
	}
    
    /**
     * 根据openid查询该用户是否有租赁订单
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/select_rent_order_num")
    public ResultMsg selectRentOrderNum(HttpServletRequest request) throws Exception {
    	ResultMsg msg = new ResultMsg();
    	RentOrderDTO order = new RentOrderDTO();
    	Member member = WechatUtils.getUserBySession(request);
    	order.setRentOpenid(member.getUserOpenid());
    	order.setRentStatus(20);	//正常状态没有20的，只是用于区别
    	//根据openid查询该用户是否有租赁订单
    	int num = rentOrderService.countRentOrder(order);
		msg.setData(num);
		msg.setStatus(true);
		msg.setResultCode(ResultInfo.SUCCESSCODE);
    	return msg;
    }

    /**
     * 前台进入申请退货页面
     * 2017年5月10日
     * @author muzx
     * @param orderId
     * @param itemId
     * @return
     */
    @ResponseBody
    @RequestMapping("/to_apply_exit")
    public ResultMsg toApplyExitOrder(
            @RequestParam(value = "orderId", defaultValue = "", required = false)String orderId,
            @RequestParam(value = "itemId", defaultValue = "0", required = false)Integer itemId){
        ResultMsg rm = new ResultMsg();
        rm.setStatus(false);
        if(StringUtils.isEmpty(orderId) || itemId == 0){
            rm.setResultCode("401-1");
            rm.setMessage("请选择正确的订单!");
            return rm;
        }

        RentOrder rentOrder = new RentOrder();
        rentOrder.setRentOrderId(orderId);
        rentOrder = rentOrderService.getRentOrderByOrderId(rentOrder);
        if(rentOrder == null || StringUtils.isEmpty(rentOrder.getRentOrderId())
                || rentOrder.getList() == null || rentOrder.getList().isEmpty()){
            rm.setResultCode("402");
            rm.setMessage("订单不存在,请确认后重试!");
            return rm;
        }
        if(rentOrder.getList().get(0).getRentIsOut() == null
                || rentOrder.getList().get(0).getRentIsOut() == 0){
            rm.setStatus(true);
            rm.setResultCode("403");
            rm.setData(0);
            rm.setMessage("该订单押金已经提取,请确认!");
            return rm;
        }
        rm.setStatus(true);
        rm.setData(rentOrder.getList().get(0).getRentRemainDeposit());
        rm.setResultCode("200");
        return rm;
    }

    /**
     * 退货申请前台接口
     * 2017年4月25日
     * @author muzx
     * @return
     */
    @ResponseBody
    @RequestMapping("/apply_exit")
    public ResultMsg applyExitOrder(
            @RequestParam(value = "orderId", defaultValue = "", required = false)String orderId,
            @RequestParam(value = "itemId", defaultValue = "0", required = false)Integer itemId,
            RentOrder rentOrderReq){
        ResultMsg rm = new ResultMsg();
        rm.setStatus(false);
        if(StringUtils.isEmpty(orderId) || itemId == 0){
            rm.setMessage("请选择正确的订单!");
            rm.setResultCode("401");
            return rm;
        }
        if(StringUtils.isEmpty(rentOrderReq.getRentPostNum())){
            rm.setResultCode("401-2");
            rm.setMessage("请输入快递单号!");
            return rm;
        }

        RentOrder rentOrder = new RentOrder();
        rentOrder.setRentOrderId(orderId);
        rentOrder.setRentStatus(6);
        rentOrder.setRentPostNum(rentOrderReq.getRentPostNum());
        RentOrder oldRentOrder = rentOrderService.getRentOrderByOrderId(rentOrder);
        if (oldRentOrder == null || oldRentOrder.getRentStatus() != 5
                || oldRentOrder.getList() == null || oldRentOrder.getList().size() < 1) {
            rm.setMessage("该订单有误,不能退货!");
            rm.setResultCode("402");
            return rm;
        }
        RentOrderItem item = new RentOrderItem();
        item.setItemOrderId(orderId);
        item.setItemId(itemId);
        item.setItemStatus(6);
        item.setItemReturnDate(new Date());
        rentOrder.getList().add(item);
        boolean flag = rentOrderService.rentExitOrder(rentOrder);
        rm.setStatus(true);
        rm.setMessage("申请退货成功!");
        rm.setResultCode("200");
        return rm;
    }

    /**
     * 前台进入申请换货页面
     * 2017年4月25日
     * @author muzx
     * @param orderId
     * @param itemId
     * @return
     */
    @ResponseBody
    @RequestMapping("/to_apply_exchange")
    public ResultMsg toApplyExchangeOrder(
            @RequestParam(value = "orderId", defaultValue = "", required = false)String orderId,
            @RequestParam(value = "itemId", defaultValue = "0", required = false)Integer itemId){
        ResultMsg rm = new ResultMsg();
        rm.setStatus(false);
        if(StringUtils.isEmpty(orderId) || itemId == 0){
            rm.setResultCode("401");
            rm.setMessage("请选择正确的订单!");
            return rm;
        }

        RentOrder rentOrder = new RentOrder();
        rentOrder.setRentOrderId(orderId);
        rentOrder = rentOrderService.getRentOrderByOrderId(rentOrder);
        if(rentOrder == null || StringUtils.isEmpty(rentOrder.getRentOrderId())
                || rentOrder.getList() == null || rentOrder.getList().isEmpty()){
            rm.setResultCode("402");
            rm.setMessage("订单不存在,请确认后重试!");
            return rm;
        }
        rm.setStatus(true);
        rm.setData(rentOrder);
        rm.setResultCode("200");
        return rm;
    }

    /**
     * 换货申请前台接口
     * 2017年4月25日
     * @author muzx
     * @return
     */
    @ResponseBody
    @RequestMapping("/apply_exchange")
    public ResultMsg applyExchangeOrder(
            @RequestParam(value = "orderId", defaultValue = "", required = false)String orderId,
            @RequestParam(value = "itemId", defaultValue = "0", required = false)Integer itemId,
            RentExchangeInfo exchangeInfo){
        ResultMsg rm = new ResultMsg();
        rm.setStatus(false);
        if(StringUtils.isEmpty(orderId) || itemId == 0){
            rm.setMessage("请选择正确的订单!");
            rm.setResultCode("400");
            return rm;
        }
        RentOrder rentOrder = new RentOrder();
        rentOrder.setRentOrderId(orderId);
        RentOrder oldRentOrder = rentOrderService.getRentOrderByOrderId(rentOrder);
        if (oldRentOrder == null || oldRentOrder.getRentStatus() != 5
                || oldRentOrder.getList() == null || oldRentOrder.getList().isEmpty()) {
            rm.setMessage("该订单有误,不能换货!");
            rm.setResultCode("402");
            return rm;
        }
        oldRentOrder.setRentStatus(11);
        oldRentOrder.getList().get(0).setItemStatus(11);
        boolean flag = rentOrderService.rentExchangeOrder(oldRentOrder,exchangeInfo);

        if (!flag) {
            rm.setMessage("申请换货出错请刷新后重试!");
            rm.setResultCode("402");
            return rm;
        }
        rm.setStatus(true);
        rm.setMessage("申请换货成功!");
        rm.setResultCode("200");
        return rm;
    }

    /**
     * 退款申请前台接口
     * 2017年5月5日
     * @author muzx
     * @return
     */
    @ResponseBody
    @RequestMapping("/apply_return_money")
    public ResultMsg applyReturnMoney(@RequestParam(value = "orderId", defaultValue = "", required = false)String orderId,
                                    @RequestParam(value = "itemId", defaultValue = "0", required = false)Integer itemId){
        ResultMsg rm = new ResultMsg();
        rm.setStatus(false);
        rm.setMessage("订单退款失败,请刷新重试!");
        if(StringUtils.isEmpty(orderId) || itemId == 0){
            rm.setMessage("请选择正确的订单!");
            rm.setResultCode("400");
            return rm;
        }
        RentOrder rentOrder = new RentOrder();
        rentOrder.setRentOrderId(orderId);
        RentOrder oldRentOrder = rentOrderService.getRentOrderByOrderId(rentOrder);
        if (oldRentOrder == null || oldRentOrder.getRentStatus() != 2
                || oldRentOrder.getList() == null || oldRentOrder.getList().isEmpty()) {
            rm.setMessage("该订单有误,不能退款!");
            rm.setResultCode("402");
            return rm;
        }
        oldRentOrder.setRentStatus(3);
        oldRentOrder.getList().get(0).setItemStatus(3);
        oldRentOrder.getList().get(0).setItemReturnDate(new Date());
        boolean flag = rentOrderService.updateOrderStatusAndItemStatus(oldRentOrder);
        if(!flag){
            rm.setMessage("订单退款出错,请刷新重试!");
            rm.setResultCode("403");
            return rm;
        }
        rm.setStatus(true);
        rm.setResultCode("200");
        rm.setMessage("");
        return rm;
    }
    /**
     * 根据openid判断当前用户是否关注
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/isFocus")
    public ResultMsg isFocus(HttpServletRequest request) throws Exception {
        ResultMsg msg = new ResultMsg();
        RentOrderDTO order = new RentOrderDTO();
        Member member = WechatUtils.getUserBySession(request);
        //根据openid查询该用户是否有租赁订单
        member = memberService.getMemberByUserOpenId(member.getUserOpenid());
        if(member.getUserStatus() == 2){
            msg.setStatus(true);
            msg.setResultCode(ResultInfo.SUCCESSCODE);
        }else{
            msg.setStatus(false);
            msg.setResultCode(ResultInfo.ERRORCODE);
        }
        return msg;
    }
}
