package com.sino.rescue.oauth.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

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

import com.sino.publicclass.base.CException;
import com.sino.publicclass.database.ConnectionPool;
import com.sino.rescue.oauth.entity.SNSUserInfo;
import com.sino.rescue.oauth.entity.WeixinOauth2Token;
import com.sino.rrs.business.SnsVehicleUserManager;
import com.sino.rrs.entity.RcRescueCompanyPO;
import com.sino.rrs.entity.RcRescueVehiclePO;
import com.sino.rrs.entity.RcWeixinUserPO;
import com.sino.rrs.oper.RcWeixinUserOper;
import com.sino.rrs.oper.RescueCompanyDispatchOper;
import com.sino.rrs.oper.RescueVehicleDispatchOper;
import com.sino.weixin.oauth.util.AdvancedUtil;
import com.sino.weixin.oauth.util.Constants;

public class UserRegisterServiceServlet extends HttpServlet{
	private AdvancedUtil util=new AdvancedUtil();
	
	private static final long serialVersionUID = -1847238807216447030L;
    @Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	  response.setCharacterEncoding("gb2312");  // 用户同意授权后，能获取到code
	  String code=request.getParameter("code");
	  if (!"authdeny".equals(code)) {
		   // 获取网页授权access_token
		   WeixinOauth2Token weixinOauth2Token = util.getOauth2AccessToken(Constants.AppId, Constants.AppSecret, code);
		   // 网页授权接口访问凭证
		   String accessToken = weixinOauth2Token.getAccess_token();
		   // 用户标识
		   String openId = weixinOauth2Token.getOpenid();
		   
		   RcWeixinUserPO weixinUser=SnsVehicleUserManager.getWeixinDispatcherUserByOpenId(openId);
		   if(weixinUser!=null){
			   request.setAttribute("weixinUserId", String.valueOf(weixinUser.getId()));
			   System.out.println(String.valueOf(weixinUser.getId())+"ooooooooooooooooo");
			   Connection conn=null;
			   try {
				 conn=ConnectionPool.getConnection();
				 RcWeixinUserOper weixinOper=new RcWeixinUserOper(conn);
				 //判断该用户是否注册过
				 Map<String,Object> register=weixinOper.findUserByOpenID(openId);
					if(!register.get("company_rescue_code").equals("")){  //服务商注册过
						//查询服务商的信息
						RescueCompanyDispatchOper oper=new RescueCompanyDispatchOper(conn);
						RcRescueCompanyPO companyInfo = oper.findRescueCompanyInfo(weixinUser.getId());
						request.setAttribute("companyInfo",companyInfo);
						request.getRequestDispatcher("/supplier/userManInfo.jsp?weixinUserId="+String.valueOf(weixinUser.getId())+"&openId="+openId+"&flag="+companyInfo.getAcceptFlag()).forward(request, response);
					}else if(!register.get("vehicle_rescue_code").equals("")){ //司机注册过
						//查询司机的信息
						RescueVehicleDispatchOper vehicleOper=new RescueVehicleDispatchOper(conn);
						RcRescueVehiclePO vehicleInfo = vehicleOper.findRescueVehicleInfo(weixinUser.getId());
						request.setAttribute("vehicleInfo",vehicleInfo);
						request.getRequestDispatcher("/supplier/vehicleManInfo.jsp?weixinUserId="+String.valueOf(weixinUser.getId())+"&openId="+openId+"&flag="+vehicleInfo.getAcceptFlag()).forward(request, response);
					}else{
					 request.getRequestDispatcher("/supplier/userRegister.jsp?weixinUserId="+String.valueOf(weixinUser.getId())).forward(request, response);
				}
			   } catch (CException e) {
				e.printStackTrace();
			}finally{
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			  
		   }else{
			// 获取用户信息
			   Connection conn=null;
			 try {
			   conn=ConnectionPool.getConnection();
			   SNSUserInfo snsUserInfo = util.getSNSUserInfo(accessToken, openId); // 设置要传递的参数
			   
			   System.out.println(snsUserInfo.getNickname()+"昵称");
			   
			   RcWeixinUserPO snsUser = new RcWeixinUserPO();
			   snsUser.setOpen_id(snsUserInfo.getOpenid());
			   snsUser.setNick_name(snsUserInfo.getNickname());
			//   snsUser.setNick_name("");
			   snsUser.setSex(snsUserInfo.getSex());
			   snsUser.setProvince(snsUserInfo.getProvince());
			   snsUser.setCity(snsUserInfo.getCity());
			   snsUser.setCountry(snsUserInfo.getCountry());
			   SnsVehicleUserManager.createWeixinUserDispatch(snsUser);
			   RcWeixinUserOper dispatchOper = new RcWeixinUserOper(conn);
			   int weixinUserId= dispatchOper.findRescueCode(openId).getId();
			   request.setAttribute("weixinUserId", String.valueOf(weixinUserId));
			   request.getRequestDispatcher("/supplier/userRegister.jsp?weixinUserId="+String.valueOf(weixinUserId)).forward(request, response);
			} catch (CException e) {
				e.printStackTrace();
			}finally{
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			  
		   }
		}
    }
}
