package com.iamberry.rent.handlers.coupon;

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

import javax.servlet.http.HttpServletRequest;

import com.iamberry.rent.face.order.RentOrderService;
import com.iamberry.wechat.core.entity.rent.RentOrderItem;
import com.iamberry.wechat.tools.ResponseJson;

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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iamberry.rent.face.order.ServiceCouponService;
import com.iamberry.rent.face.pay.RentPayService;
import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.WechatUtils;
import com.iamberry.wechat.core.entity.coupon.CouponItemDto;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.core.entity.rent.PledgeCouponInfo;
import com.iamberry.wechat.core.entity.rent.ServiceCouponInfo;
import com.iamberry.wechat.face.coupon.CouponItemService;
import com.iamberry.wechat.tools.ResultInfo;

@Controller
@RequestMapping("/wechat/rent_coupon")
public class RentCouponHandler {
	
	@Autowired
	private ServiceCouponService serviceCouponService;
	@Autowired
	private CouponItemService couponItemService;
	@Autowired
	private RentOrderService rentOrderService;
	@Autowired
	private RentPayService rentPayService;

	/**
	 * 查询个人所有优惠券总数
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/select_coupon_sum")
    public ResultMsg selectCouponSum(HttpServletRequest request) throws Exception {
    	ResultMsg msg = new ResultMsg();
    	int serviceNum = 0;		//租赁服务费抵扣券数量
    	int pledgeNum = 0;		//租赁押金提取券数量
    	int couponNum = 0;	//旧的优惠券数量
    	Member member = WechatUtils.getUserBySession(request);
    	ServiceCouponInfo serviceCouponInfo = new ServiceCouponInfo();
    	PledgeCouponInfo pledgeCouponInfo = new PledgeCouponInfo();
    	serviceCouponInfo.setServiceOpenId(member.getUserOpenid());
    	CouponItemDto couponItemDto = new CouponItemDto();
    	couponItemDto.setUseropenid(member.getUserOpenid());
    	//查询个人所有服务费抵扣券
    	List<ServiceCouponInfo> serviceList = serviceCouponService.list(serviceCouponInfo);
    	if (serviceList != null) {
    		serviceNum = serviceList.size();
    	}
    	pledgeCouponInfo.setPledgeOpenId(member.getUserOpenid());
    	//查询个人所有押金提取券
    	List<PledgeCouponInfo> pledgeList = serviceCouponService.listPledgeCoupon(pledgeCouponInfo);
    	if (pledgeList != null) {
    		pledgeNum = pledgeList.size();
    	}
    	List<CouponItemDto> couponList = couponItemService.getCouponItemDtoList(couponItemDto);
    	if (couponList != null) {
    		couponNum = couponList.size();
    	}
    	int couponSum = serviceNum + pledgeNum + couponNum;
    	msg.setData(couponSum);
    	msg.setResultCode(ResultInfo.SUCCESSCODE);
    	msg.setStatus(true);
    	return msg;
    }
	
	/**
	 * 查询个人所有的服务费抵扣券列表
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/select_service_coupon_list")
	public ResultMsg selectServiceCouponList(HttpServletRequest request) throws Exception {
		ResultMsg msg = new ResultMsg();
		Member member = WechatUtils.getUserBySession(request);
		ServiceCouponInfo serviceCouponInfo = new ServiceCouponInfo();
    	serviceCouponInfo.setServiceOpenId(member.getUserOpenid());
    	//查询个人所有服务费抵扣券
    	List<ServiceCouponInfo> serviceList = serviceCouponService.list(serviceCouponInfo);
    	msg.setData(serviceList);
    	msg.setResultCode(ResultInfo.SUCCESSCODE);
    	msg.setStatus(true);
    	return msg;
	}
	
	/**
	 * 查询个人所有的押金提取券列表
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/select_pledge_coupon_list")
	public ResultMsg selectPledgeCouponList(HttpServletRequest request) throws Exception {
		ResultMsg msg = new ResultMsg();
		Member member = WechatUtils.getUserBySession(request);
		PledgeCouponInfo pledgeCouponInfo = new PledgeCouponInfo();
		pledgeCouponInfo.setPledgeOpenId(member.getUserOpenid());
		//查询个人所有押金提取券
    	List<PledgeCouponInfo> pledgeList = serviceCouponService.listPledgeCoupon(pledgeCouponInfo);
    	msg.setData(pledgeList);
    	msg.setResultCode(ResultInfo.SUCCESSCODE);
    	msg.setStatus(true);
    	return msg;
	}

	/**
	 * 进入押金提取页面
	 * @param request
	 * @param pledgeId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/toExtractDeposit")
	public ResultMsg toExtractDeposit(HttpServletRequest request,
						@RequestParam("pledgeId") Integer pledgeId) throws Exception{
		ResultMsg msg = new ResultMsg();
		Member member = WechatUtils.getUserBySession(request);
		if(null == pledgeId || pledgeId.equals("")){
			msg.setResultCode(ResultInfo.ERRORCODE);
			msg.setStatus(false);
			return msg;
		}
		PledgeCouponInfo couponInfo = new PledgeCouponInfo();
		couponInfo.setPledgeId(pledgeId);
		couponInfo.setPledgeStatus(1);
		List<PledgeCouponInfo> listcouponInfo = serviceCouponService.listPledgeCoupon(couponInfo);
		if(listcouponInfo == null || listcouponInfo.size() == 0){
			msg.setResultCode(ResultInfo.ERRORCODE);
			msg.setStatus(false);
			return msg;
		}
		couponInfo = listcouponInfo.get(0);
		//查询所有订单
		List<RentOrderItem> listOrderItem = rentOrderService.selectRentOrderByOpenId(member.getUserOpenid());
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("couponInfo",couponInfo);
		map.put("listOrderItem",listOrderItem);
		msg.setResultCode(ResultInfo.SUCCESSCODE);
		msg.setStatus(true);
		msg.setData(map);
		return msg;
	}


	/**
	 * 发起提取押金操作
	 * @param request
	 * @param orderItemId
	 * @param pledgeId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/extractDeposit")
	public ResultMsg extractDeposit(HttpServletRequest request,
					@RequestParam("orderItemId") Integer orderItemId,
					@RequestParam("pledgeId") Integer pledgeId) throws  Exception{
		ResultMsg msg = new ResultMsg();
		Member member = WechatUtils.getUserBySession(request);
		if(null == orderItemId || orderItemId.equals("")){
			msg.setResultCode(ResultInfo.ERRORCODE);
			msg.setStatus(false);
			msg.setMessage("押金提取失败！");
			return msg;
		}
		if(null == pledgeId || pledgeId.equals("")){
			msg.setResultCode(ResultInfo.ERRORCODE);
			msg.setStatus(false);
			msg.setMessage("押金提取失败！");
			return msg;
		}
		//查询提取卷信息
		PledgeCouponInfo couponInfo = new PledgeCouponInfo();
		couponInfo.setPledgeId(pledgeId);
		couponInfo.setPledgeStatus(1);
		List<PledgeCouponInfo> listcouponInfo = serviceCouponService.listPledgeCoupon(couponInfo);
		if(listcouponInfo == null || listcouponInfo.size() == 0){
			msg.setResultCode(ResultInfo.ERRORCODE);
			msg.setStatus(false);
			return msg;
		}

		couponInfo = listcouponInfo.get(0);
		//查询订单项信息
		RentOrderItem itenInfo = new RentOrderItem();
		itenInfo.setItemId(orderItemId);
		itenInfo = rentOrderService.getOrderItemByInitCode(itenInfo);

		//判断是否符合提取条件
		if(couponInfo == null || itenInfo == null){
			msg.setResultCode(ResultInfo.ERRORCODE);
			msg.setStatus(false);
			msg.setMessage("数据异常，押金提取失败！");
			return msg;
		}
		if (itenInfo.getRentIsOut() == 1 || couponInfo.getPledgeMoney() > itenInfo.getRentRemainDeposit()){
			msg.setResultCode(ResultInfo.ERRORCODE);
			msg.setStatus(false);
			msg.setMessage("抵扣卷金额不得大于待提取金额！");
			return msg;
		}
			//执行转账操作
			ResponseJson responseJson = rentPayService.payTransfer(member.getUserOpenid(),itenInfo.getItemOrderId(),couponInfo.getPledgeMoney()*100,1,"使用押金抵扣卷提取租赁押金");
			if (responseJson.getReturnCode() == 200){
			//转账成功
			//修改订单项信息
	 		RentOrderItem rentOrderItem = new RentOrderItem();
			rentOrderItem.setRentIsOut(1);
			rentOrderItem.setRentRemainDeposit(itenInfo.getRentRemainDeposit()-couponInfo.getPledgeMoney());
			rentOrderItem.setRentPledgeId(String.valueOf(couponInfo.getPledgeId()));
			rentOrderItem.setItemId(itenInfo.getItemId());
			rentOrderService.update(rentOrderItem);
			//修改押金提取卷信息
			PledgeCouponInfo pledgeCouponInfo = new PledgeCouponInfo();
			pledgeCouponInfo.setPledgeId(couponInfo.getPledgeId());
			pledgeCouponInfo.setPledgeStatus(2);
			serviceCouponService.updatePledgeCouponStatus(pledgeCouponInfo);
			msg.setResultCode(ResultInfo.SUCCESSCODE);
			msg.setStatus(true);
			msg.setMessage("押金提取成功！");
		}else{
			msg.setResultCode(ResultInfo.ERRORCODE);
			msg.setStatus(false);
			msg.setMessage("数据异常，押金提取失败！");
		}
		return msg;
	}
}
