package com.sino.rescue.oauth.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
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.RcWeixinUserPO;
import com.sino.rrs.oper.RcWeixinUserOper;
import com.sino.rrs.oper.UserOrderOper;
import com.sino.weixin.oauth.util.AdvancedUtil;
import com.sino.weixin.oauth.util.Constants;

public class TodayFinishedOrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private AdvancedUtil util = new AdvancedUtil();
	
	@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){
				   System.out.println(String.valueOf(weixinUser.getId())+"ooooooooooooooooo");
				   Connection conn=null;
				   try {
					  conn=ConnectionPool.getConnection();
					  RcWeixinUserOper weixinOper=new RcWeixinUserOper(conn);
					  UserOrderOper orderOper=new UserOrderOper(conn);
					  
					  Map<String,Object> regist = weixinOper.findUserByOpenID(openId);
						if(!regist.get("company_rescue_code").equals("")){  //该用户是服务商用户
							int dispatchMode=(Integer) regist.get("company_dispatch_mode");
							if(dispatchMode==3){   //微信调度
								String rescueCode=(String) regist.get("company_rescue_code");
								request.getRequestDispatcher("/supplier/companyFinishedOrder.jsp?rescueCode="+rescueCode).forward(request, response);
							}else{
								request.getRequestDispatcher("/supplier/power.jsp").forward(request, response); //权限页面
							}
							
						}else if(!regist.get("vehicle_rescue_code").equals("")){   //司机用户
							int dispatchMode=(Integer) regist.get("vehicle_dispatch_mode");
							if(dispatchMode==3){   //微信调度
								String rescueCode=(String) regist.get("vehicle_rescue_code");
								String rescueVehicleId=regist.get("ID").toString(); 
								request.getRequestDispatcher("/supplier/vehicleFinishedOrder.jsp?rescueCode=" + rescueCode + "&rescueVehicleId=" + rescueVehicleId).forward(request, response);
							}else{
								request.getRequestDispatcher("/supplier/power.jsp").forward(request, response); //权限页面
							}
						
						}else{
							 request.getRequestDispatcher("/supplier/userRegister.jsp?weixinUserId="+String.valueOf(weixinUser.getId())).forward(request, response);
						}
				   }catch (CException e) {
						e.printStackTrace();
					} catch (Exception 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); // 设置要传递的参数
					   
					   RcWeixinUserPO snsUser = new RcWeixinUserPO();
					   snsUser.setOpen_id(snsUserInfo.getOpenid());
					   snsUser.setNick_name(snsUserInfo.getNickname());
					   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();
					}
				}
				  
			   
			   }
			}
	}
	
	//获取当月时间
	public String getMonthTime(){
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//获取当月第一天时间
		Calendar c=Calendar.getInstance();
		c.add(Calendar.MONTH, 0);
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return(sf.format(c.getTime()));
	}
	//获取本周时间
	public String getWeekTime(){
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//获取本周第一天时间
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		System.err.println("####"+sf.format(cal.getTime())+"$$$$$$$$$"+cal.getTime());
		return(sf.format(cal.getTime()));
		
	}
	//获取当天时间
	public String getTodayTime(){
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//获取当天时间
		Calendar c=Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return(sf.format(c.getTime()));
	}
}
