package com.sino.rrs.action;


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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sino.publicclass.base.BaseAction;
import com.sino.publicclass.base.CException;
import com.sino.publicclass.database.ConnectionPool;
import com.sino.rrs.entity.RcRescueCompanyPO;
import com.sino.rrs.entity.RcRescueVehiclePO;
import com.sino.rrs.oper.RcRescueCompanyOper;
import com.sino.rrs.oper.RcRescueManagerOper;
import com.sino.rrs.oper.RcRescueVehicleOper;
import com.sino.weixin.oauth.util.Constants;
import com.sino.weixin.oauth.util.HttpsUtil;

public class RescueVehicleAction extends BaseAction{
	/**
	 * 修改身份之前判断是服务商还是司机
	 * @throws Exception 
	 */
	public void modifyIdentity() throws Exception{
		String weixinUserId=getParameter("weixinUserId","");
		JSONObject jsonObject=new JSONObject();
		boolean flag=false;
		RcRescueCompanyOper oper=new RcRescueCompanyOper(getConnection());
		Map<String,String> comapanyModify=oper.queryModifyIdentity(weixinUserId);
		Map<String,String> vehicleModify=oper.queryVehicleModifyIdentity(weixinUserId);
		if(comapanyModify!=null){   //该服务商已注册过
			oper.deleteModifyUser(weixinUserId);
			flag=true;
		}else if(vehicleModify!=null){   //司机注册过
			RcRescueVehicleOper vehicleOper=new RcRescueVehicleOper(getConnection());
			vehicleOper.deleteModifyUser(weixinUserId);
		}
		jsonObject.put("code", flag);
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(jsonObject.toJSONString());
		setNext(false);
	}
	
	/**
	 * 供应商运营注册
	 * @throws SQLException 
	 * @throws CException
	 * @throws IOException
	 * @throws Exception
	 */
	public void managerRegist() throws SQLException{
		String loginName=getParameter("loginName","");
		String password=getParameter("password","");
		String weixinUserId=getParameter("weixinUserId", "");
		String identity=getParameter("identity","");     //1 大区经理    2 供应商
		
		JSONObject jsonObject = new JSONObject();
		Connection conn=null;
		boolean flag=false;
		
		jsonObject.put("loginName", loginName);
		jsonObject.put("password", password);
		jsonObject.put("action", 1);
		
		try {
			conn=ConnectionPool.getConnection();
			JSONObject httpResponse=JSON.parseObject(HttpsUtil.doPost(Constants.url, jsonObject, "utf-8")); //远程掉注册方法
			if(httpResponse!=null){  //判断数据库里是否有这个对象
				if(loginName.equals(httpResponse.get("login_name"))&&password.equals(httpResponse.get("password"))){
					//注册成功
					RcRescueCompanyPO rcRescueCompanyPO=new RcRescueCompanyPO();
					rcRescueCompanyPO.setRescueCompanyCode(httpResponse.getString("rescue_company_code"));
					rcRescueCompanyPO.setWeixinUserId(Integer.parseInt(weixinUserId));
					rcRescueCompanyPO.setCompanyName(httpResponse.getString("companyName"));
					rcRescueCompanyPO.setDispatchMode(Integer.parseInt(httpResponse.getString("dispatchMode")));
					
					RcRescueCompanyOper oper=new RcRescueCompanyOper(conn);
					RcRescueManagerOper manageOper=new RcRescueManagerOper(conn);
					
					if(null!=identity&&!("").equals(identity)){
						if(identity.equals("1")){  //大区经理
							Map<String,String> manager=manageOper.queryManagerByWeixinId(weixinUserId);
							if(manager==null){
								int result= manageOper.insertAreaManager(loginName,password,rcRescueCompanyPO); //添加大区经理信息
								if(result>0){   //添加成功
									flag=true;
								}else{
									flag=false;
								}
							}
						}
						if(identity.equals("2")){  //供应商
							//查询该人是否成功注册过供应商
							Map<String,String>map= oper.queryModifyIdentity(weixinUserId);
							if(map==null){
								int result= oper.createPatch(rcRescueCompanyPO); //添加服务商信息
								if(result>0){   //添加成功
									flag=true;
								}else{
									flag=false;
								}
								
							}
						}
						jsonObject.put("code", flag); 
						response.setContentType("text/html;charset=UTF-8");
						response.getWriter().print(jsonObject.toJSONString());
						setNext(false);
					}
				
				}else{
					//注册失败
					System.err.println("failth");
					jsonObject.put("code", flag); 
					response.setContentType("text/html;charset=UTF-8");
					response.getWriter().print(jsonObject.toJSONString());
					setNext(false);
				}
			}else{
				//注册失败
				jsonObject.put("code", flag);
				response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print(jsonObject.toJSONString());
				setNext(false);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			conn.close();
		}
	}
	
	/**
	 * 验证服务商编号是否存在
	 * @throws CException
	 * @throws IOException
	 * @throws Exception
	 */
	public void checkLoginName() throws IOException{
		JSONObject json=new JSONObject();
		JSONObject httpRequest=null;
		boolean checkFlag = false;  
		
		String rescueCode=getParameter("rescueCode","");
		json.put("rescueCode", rescueCode);
		json.put("action", Integer.valueOf(2));
		
		httpRequest=JSON.parseObject(HttpsUtil.doPost(Constants.url, json,"UTF-8"));  
		if(httpRequest.getIntValue("errorCode") == 0){
			checkFlag = true;
		}
		json.put("code", checkFlag); 
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(json.toJSONString());
		setNext(false);
	}
	
	/**
	 * 获取服务商下救援车辆列表
	 * @throws CException
	 * @throws IOException
	 * @throws Exception
	 */
	public void queryRescueVehicle() throws IOException{
		String rescueCode=getParameter("rescueCode","");
		JSONObject jsonObject=new JSONObject();
		JSONObject htppRequest=null;
		jsonObject.put("rescueCode", rescueCode);
		jsonObject.put("action", 3);
		
		htppRequest = JSON.parseObject(HttpsUtil.doPost(Constants.url, jsonObject,"UTF-8"));
		
		jsonObject.put("vehicleList", htppRequest.get("vehicleList")); 
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(jsonObject.toJSONString());
		setNext(false);
	}
	
	/**
	 * 救援司机注册
	 * @throws SQLException 
	 * @throws CException
	 * @throws IOException
	 * @throws Exception
	 */
	public void regist() throws SQLException {
		String weixinUserID=getParameter("weixinUserId","");
		String name=getParameter("name","");
		String phone=getParameter("phone","");
		String rescueCode=getParameter("rescueCode","");
		String vehicleId=getParameter("vehicleId","");
		String vehicleName=getParameter("vehicleName","");
		
		JSONObject jsonObject = new JSONObject();
		Connection conn=null;
		
		jsonObject.put("rescueCode", rescueCode);  //服务商登入名
		jsonObject.put("action", 2);
		 Map rescueVehiclePO = JSON.parseObject(HttpsUtil.doPost(Constants.url, jsonObject, "utf-8"));  //远程掉司机注册方法
		try {
			conn=ConnectionPool.getConnection();
			if(rescueVehiclePO !=null){  //判断数据库里是否有这个对象
				RcRescueVehiclePO rcRescueVehiclePO=new RcRescueVehiclePO();
				rcRescueVehiclePO.setRescue_company_code(rescueVehiclePO.get("CODE").toString());
				rcRescueVehiclePO.setDispatchMode(Integer.parseInt(rescueVehiclePO.get("dispatchMode").toString()));
				rcRescueVehiclePO.setWeixin_user_id(Integer.parseInt(weixinUserID));
				rcRescueVehiclePO.setComapny_login_name(rescueCode);
				rcRescueVehiclePO.setStaff_name(name);
				rcRescueVehiclePO.setStaff_phone(phone);
				rcRescueVehiclePO.setVehicle_id(Integer.parseInt(vehicleId));
				rcRescueVehiclePO.setVehicle_name(vehicleName);
				
				RcRescueVehicleOper vehicleOper=new RcRescueVehicleOper(conn);
				vehicleOper.insertVehicle(rcRescueVehiclePO); //添加司机信息
				jsonObject.put("code", true); 
				response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print(jsonObject.toJSONString());
				setNext(false);
				
			}else{
				jsonObject.put("code", false); 
				response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print(jsonObject.toJSONString());
				setNext(false);
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (CException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			conn.close();
		}
	}

//修改司机电话信息
	public void update() throws Exception{
		String phone=getParameter("phone");
		int id=Integer.parseInt(getParameter("id"));
		JSONObject jsonObject = new JSONObject();
		RcRescueVehicleOper vehicleOper=new RcRescueVehicleOper(getConnection());
		vehicleOper.updateVehicle(id, phone);
		jsonObject.put("code", true); 
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(jsonObject.toJSONString());
		setNext(false);
	}

	//修改司机车辆信息
	public void updateVehicle() throws Exception{
		String vehicle_name=getParameter("vehicle_name");
		int id=Integer.parseInt(getParameter("id"));
		int vehicleID=Integer.parseInt(getParameter("vehicleID"));
		JSONObject jsonObject = new JSONObject();
		RcRescueVehicleOper vehicleOper=new RcRescueVehicleOper(getConnection());
		vehicleOper.updateVehicleMessage(id, vehicleID,vehicle_name);
		jsonObject.put("code", true); 
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(jsonObject.toJSONString());
		setNext(false);
	}
	
	//查询该车辆是否被司机选择过
	public void choosedVehicleName() throws Exception{
		int vehicleID=Integer.parseInt(getParameter("vehicleID"));
		JSONObject jsonObject = new JSONObject();
		RcRescueVehicleOper vehicleOper=new RcRescueVehicleOper(getConnection());
		long count=vehicleOper.choosedVehicleName(vehicleID);
		jsonObject.put("code", count); 
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(jsonObject.toJSONString());
		setNext(false);
	}
}
