package com.offcn.process;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.offcn.TestUnti.Log;
import com.offcn.TestUnti.MD5keyUtil;
import com.offcn.TestUnti.MapUtil;
import com.offcn.TestUnti.ReadProperties;
import com.offcn.TestUnti.cmdUtil;
import com.offcn.system.system;


public class NWN extends BasicsGM{
	
	public static boolean isClearMysql =false;
	
	public static boolean isProduct=Boolean.valueOf(((system)map.get("nwn")).getIsProduct());
	
	public static String nwn_sign = "";//mis口令
	public static String admin_login_ssid = "";//nwn登录得到的口令
	public static String nwn_url = "";//后台登录得到的nwn登录地址
	public static String PHPSESSID = "";//后台登录nwn，最终得到的PHPSESSID
	public static String admin_31_encode = "";//后台登录nwn，最终得到的PHPSESSID
	
	public static List<String> n_package_idList = new ArrayList<String>();//n_package的id
	
	public static List<String> n_admin_card_no_List = new ArrayList<String>();//n_admin的card_no
	public static List<String> n_admin_id_List = new ArrayList<String>();//n_admin的id
	public static List<String> n_admin_phone_List = new ArrayList<String>();//n_admin的phone
	public static List<String> n_admin_username_List = new ArrayList<String>();//n_admin的username
	
	public static String level_id_List = "";//层级的id
	public static List<String> user_id_List = new ArrayList<String>();//user_id
	
	public NWN(){
		Connectionsa();
	}
	
	public void Connectionsa(){
		//================设置数据库连接池=====================
		ConnectionsMap=getConnections(new String[]{"nwn"});
		StatementsMap=getStatements(ConnectionsMap);
		conn=ConnectionsMap.get("nwn");//设置默认的连接对象
		stmt=StatementsMap.get("nwn");//设置默认的连接对象
		//================设置公共参数========================
//		PublicParameter.put("appid","tiku");//静态公共参数
//		DynamicParameter.put("sign","code");//动态公共参数
	}
	

	
	//动态公共参数
		public HashMap<String, Object> setDynamicParameter(HashMap<String, Object> data) {
			String parameter =null;
			HashMap<String, Object> dataNew=data;
			if(DynamicParameter.size()!=0){
				parameter = MapUtil.getValue("parameter", data);
				String Request = MapUtil.getValue("Request", data);
				Set<Map.Entry<String,String>> entrySet = DynamicParameter.entrySet();
				Iterator<Map.Entry<String,String>> it = entrySet.iterator();
				while(it.hasNext()){
					Map.Entry<String,String> me = it.next();
					String key = me.getKey();
					String value = me.getValue();
//					Log.logInfo("公共动态参数："+key+":"+value);
					if(!parameter.contains("\""+key+"\"") && !parameter.contains(key+"=")){
						if("get".equals(Request)){
							if(parameter==null || parameter.length()==0){
								parameter=key+"="+"value";//value 需要自己写
							}else{
								parameter=parameter+"&"+key+"="+"value";//value 需要自己写
							}
						}else{
							if(parameter==null || parameter.length()==0){
								parameter=key+"\":\""+"value"+"\"";//value 需要自己写
							}else{
								parameter=parameter+",\""+key+"\":\""+"value"+"\"";//value 需要自己写
							}
						}
					}
				}
				dataNew.put("parameter", parameter);
			}
			return dataNew;
		}


		public HashMap<String, Object> setPublicParameter(HashMap<String, Object> data) {

			String parameter =null;
			HashMap<String, Object> dataNew=data;
			if(PublicParameter.size()!=0){
				parameter = MapUtil.getValue("parameter", data);
				String Request = MapUtil.getValue("Request", data);
				Set<Map.Entry<String,String>> entrySet = PublicParameter.entrySet();
				Iterator<Map.Entry<String,String>> it = entrySet.iterator();
				while(it.hasNext()){
					Map.Entry<String,String> me = it.next();
					String key = me.getKey();
					String value = me.getValue();
//					Log.logInfo("公共静态参数："+key+":"+value);
					if(!parameter.contains("\""+key+"\"") && !parameter.contains(key+"=")){
						if("get".equals(Request)){
							if(parameter==null || parameter.length()==0){
								parameter=key+"="+value;//value 需要自己写
							}else{
								parameter=parameter+"&"+key+"="+value;//value 需要自己写
							}
						}else{
							if(parameter==null || parameter.length()==0){
								parameter=key+"\":\""+value+"\"";//value 需要自己写
							}else{
								parameter=parameter+",\""+key+"\":\""+value+"\"";//value 需要自己写
							}
						}
					}
				}
				dataNew.put("parameter", parameter);
			}
			return dataNew;
		}
	
	
	//清除注册数据根据n_admin_card_no_List
	public void cleann_n_admin_card_no_List_FromDB() {
		List<String> user_List = new ArrayList<String>();
		for(int i=0;i<n_admin_card_no_List.size();i++){
			try {
				deleteFromDB("n_admin", "card_no", n_admin_card_no_List.get(i));
				user_List.add(n_admin_card_no_List.get(i));
			} catch (Exception e) {
				e.printStackTrace();
				Log.logInfo("清除数据失败n_admin_card_no_List");
			}
			
		}
		n_admin_card_no_List.removeAll(user_List);
//		System.out.println(user_idList.toString());
	}
	
	//清除注册数据根据n_package_idList
	public void cleann_n_package_List_FromDB() {
		List<String> user_List = new ArrayList<String>();
		for(int i=0;i<n_package_idList.size();i++){
			try {
				deleteFromDB("n_package", "id", n_package_idList.get(i));
				user_List.add(n_package_idList.get(i));
			} catch (Exception e) {
				e.printStackTrace();
				Log.logInfo("清除数据失败n_package_idList");
			}
			
		}
		n_package_idList.removeAll(user_List);
//			System.out.println(user_idList.toString());
	}
	
	//清除注册数据根据n_package_idList
	public void cleann_user_id_List_FromDB() {
		List<String> user_List = new ArrayList<String>();
		for(int i=0;i<user_id_List.size();i++){
			try {
				deleteFromDB("n_user", "id", user_id_List.get(i));
				user_List.add(user_id_List.get(i));
			} catch (Exception e) {
				e.printStackTrace();
				Log.logInfo("清除数据失败user_id_List");
			}
			
		}
		user_id_List.removeAll(user_List);
//		System.out.println(user_idList.toString());
	}

	public void cleanMysqlData() {
		if (!isClearMysql) {
			Log.logInfo("nwn=====cleanMysqlData");
			conn=ConnectionsMap.get("nwn");//设置默认的连接对象
			stmt=StatementsMap.get("nwn");//设置默认的连接对象
			cleann_n_admin_card_no_List_FromDB();//清除注册数据根据n_admin_card_no_List
			cleann_n_package_List_FromDB();//清除注册user_id，
			cleann_user_id_List_FromDB();//清除用户地区操作表t_exam_area
		  }
		
	}
	
	
//	
//
//	//清除用户地区操作表t_exam_area
//	public void cleanexam_FromDB() {
//		List<String> user_List = new ArrayList<String>();
//		for(int i=0;i<t_exam_area.size();i++){
//			try {
//				deleteFromDB("t_exam_area", "id", t_exam_area.get(i));
//				user_List.add(t_exam_area.get(i));
//			} catch (Exception e) {
//				e.printStackTrace();
//				Log.logInfo("清除数据失败t_exam_area");
//			}
//			
//		}
//		t_exam_area.removeAll(user_List);
////			System.out.println(user_idList.toString());
//	}
}
