package com.iamberry.wechat.handles.wx;

import java.io.IOException;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.apache.commons.lang.StringUtils;
import com.iamberry.wechat.core.entity.WechatUtils;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.face.member.MemberService;
import com.iamberry.wechat.tools.NameUtils;

/**
 * 微代理 跳转
 * 2016年4月28日
 * @author 穆再兴
 *
 */
@Controller
@RequestMapping("/wechat/agentWechat")
public class AgentWechatHandler {
	@Autowired
	private WechatUtils wechatUtils;
	@Autowired
	private MemberService memberService;
	
	/**
	 * 2016年4月26日
	 * 微代理订单页面跳转
	 * @author 穆再兴
	 * @return
	 */
	@RequestMapping("/order")
	public void toAgentOrder(HttpServletRequest req,HttpServletResponse res){
		try {
			req.getRequestDispatcher("/WEB-INF/views/wechat/daili_order_list.html").forward(req,res);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/reback")
	public void toAgentReback(HttpServletRequest req,HttpServletResponse res){
		try {
			req.getRequestDispatcher("/WEB-INF/views/wechat/daili_rebackOrder_list.html").forward(req,res);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 会员<或微代理>中心页面跳转
	 * 2016年4月26日
	 * @author 穆再兴
	 * @param req
	 * @param res
	 */
	@RequestMapping("/member")
	public void toAgentMemberPage(HttpServletRequest req,HttpServletResponse res){
		try {
			req.getRequestDispatcher("/WEB-INF/views/wechat/vip_center.html").forward(req,res);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 微代理分销规则页面	跳转
	 * 2016年4月26日
	 * @author 穆再兴
	 */
	@RequestMapping("/fxRule")
	public void toAgentFXRule(HttpServletRequest req,HttpServletResponse res){
		try {
			req.getRequestDispatcher("/WEB-INF/views/wechat/fenxiao_guize.html").forward(req,res);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 我的下线页面跳转
	 * 2016年4月28日
	 * @author 穆再兴
	 * @param req
	 * @param res
	 */
	@RequestMapping("/agentNext")
	public void toAgentNext(HttpServletRequest req,HttpServletResponse res){
		try {
			req.getRequestDispatcher("/WEB-INF/views/wechat/xiaxian_list.html").forward(req,res);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 微代理规则
	 * 2016年4月28日
	 * @author 穆再兴
	 * @param req
	 * @param res
	 */
	@RequestMapping("/agentRule")
	public void toAgentRule(HttpServletRequest req,HttpServletResponse res){
		try {
			req.getRequestDispatcher("/WEB-INF/views/wechat/daili_guize.html").forward(req,res);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 冲奶机宣传片页面
	 * 2016年4月28日
	 * @author 穆再兴
	 * @param req
	 * @param res
	 */
	@RequestMapping("/goPublicity")
	public void toPublicity(HttpServletRequest req,HttpServletResponse res){
		//如果是分享 则绑定关系
		try {
			Member member =  wechatUtils.getUserBySession(req);
			String shareOpenid = req.getParameter("shareOpenid");
			String userType = req.getParameter("userType");	//分享人的类型 	1：会员	2：代理商
			String isDrp = req.getParameter("isDrp");	//代理商第一次分享后 将分享来源改为会员
			if(StringUtils.isNotEmpty(shareOpenid) && StringUtils.isNotEmpty(userType)){
				boolean flag = memberService.buildChildUser(shareOpenid, member,Integer.parseInt(userType));
				if(flag){
					memberService.updateUserFlagByOpenId(member.getUserOpenid());		
				}
			}
			req.getRequestDispatcher("/WEB-INF/views/wechat/iamberry_video.html?userType=" + userType + "&isDrp=" + isDrp).forward(req,res);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 用户可用金额提现
	 * 2016年5月5日
	 * @author 穆再兴
	 * @param req
	 * @param res
	 */
	@RequestMapping("/withdrawCashLog")
	public void goWithdrawCashLog(HttpServletRequest req,HttpServletResponse res){
		try {
			req.getRequestDispatcher(NameUtils.WECHAT_WITHDRAW_CASH_LOG_LIST).forward(req,res);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
