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.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.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 boolean isProduct_server=Boolean.valueOf(((system)map.get("nwn_server")).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 List<String> getcode_id_List = new ArrayList<String>();//getcode的id 
	public static List<String> n_getcode_id_List = new ArrayList<String>();//getcode的id 
	public static List<String> movie_id_list = new ArrayList<String>();//movid_id的id 
	public static String level_id_List = "";//层级的id
	public static List<String> user_id_List = new ArrayList<String>();//user_id
	public static String template_id = "";
	public static String timestamp;//时间戳 
	public static String suprise_str;//特殊字符
//	public static String phone_code = "";//手机号验证码
//	
//	
//	public static List<String> t_exam_area = new ArrayList<String>();//t_exam_area_id
//	
//	public static String union_id_str = "" ;//t_user表union_id
//	public static String open_id_str = "";//t_user表open_id
//	
//	public static String tk_token="";//登录token
	public static HashMap<String,String> PublicParameter_NWN = null;
	public static HashMap<String,String> DynamicParameter_NWN = null;

	static{
		
		//设置静态公共参数
		PublicParameter_NWN=new HashMap<String,String>();
		PublicParameter_NWN.put("appid","jiaowu");
		
		//设置动态公共参数
		DynamicParameter_NWN=new HashMap<String,String>();
		Long timeString= System.currentTimeMillis();
		timestamp = timeString.toString().substring(0, 10);
		DynamicParameter_NWN.put("timestamp",timestamp);
//		DynamicParameter_NWN.put("timestamp","timestamp");

				
		String mysql_local_Online=ReadProperties.GetTestPropertyByKey("mysql_local_Online");
		//本地数据库连接
		if(mysql_local_Online.equals("local")){
			try {
				Class.forName("com.mysql.jdbc.Driver");
				conn = DriverManager.getConnection(((system)map.get("nwn")).getSqlurl(),
						((system)map.get("nwn")).getSqlname(),
						((system)map.get("nwn")).getSqlpwd());
				stmt = conn.createStatement();
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			try {
				Class.forName("com.mysql.jdbc.Driver");
				conn1 = DriverManager.getConnection(((system)map.get("nwn_template")).getSqlurl(),
						((system)map.get("nwn_template")).getSqlname(),
						((system)map.get("nwn_template")).getSqlpwd());
				stmt1 = conn1.createStatement();
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			try {
				Class.forName("com.mysql.jdbc.Driver");
				conn2 = DriverManager.getConnection(((system)map.get("nwn_package")).getSqlurl(),
						((system)map.get("nwn_package")).getSqlname(),
						((system)map.get("nwn_package")).getSqlpwd());
				stmt2 = conn2.createStatement();
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(mysql_local_Online.equals("Online")){
			//线上数据库连接
			try {
				Class.forName("com.mysql.jdbc.Driver");
				conn = DriverManager.getConnection(((system)map.get("nwnxs")).getSqlurl(),
						((system)map.get("nwnxs")).getSqlname(),
						((system)map.get("nwnxs")).getSqlpwd());
				stmt = conn.createStatement();
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public void cleann_n_admin_card_no_List_FromDB_beforeTest() {
		
		
			try {
				deleteFromDB("n_admin", "card_no", "qty50636");
				
			} catch (Exception e) {
				e.printStackTrace();
				Log.logInfo("清除数据失败n_admin_card_no_qty50636");
			}
			
		
		
//		System.out.println(user_idList.toString());
	}
	
	//清除注册数据根据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());
	}
	
	
	//清除注册数据根据
//	public void cleann_n_admin_card_id_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));
				//deleteFromDB("n_packcode", "package_id", n_package_idList.get(i));
				//deleteFromDB("n_packcode_record", "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());
	}
	
	public void cleann_template_FromDB() {
		List<String> user_List = new ArrayList<String>();
	
			try {
				deleteFromDB("n_template", "id", template_id);
				//deleteFromDB("n_packcode", "package_id", n_package_idList.get(i));
				//deleteFromDB("n_packcode_record", "package_id", n_package_idList.get(i));
				
			} catch (Exception e) {
				e.printStackTrace();
				Log.logInfo("清除数据失败n_template");
			}
			
		
//			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());
	}
	//清除getcode_id_List
	public void cleann_getcode_id_List_FromDB() {
		List<String> user_List = new ArrayList<String>();
		for(int i=0;i<getcode_id_List.size();i++){
			try {
				deleteFromDB("n_packcode", "package_id", getcode_id_List.get(i));
				deleteFromDB("n_packcode_record", "package_id", getcode_id_List.get(i));
				user_List.add(getcode_id_List.get(i));
			} catch (Exception e) {
				e.printStackTrace();
				Log.logInfo("清除数据失败getcode_id_List");
			}
			
		}
		getcode_id_List.removeAll(user_List);
//		System.out.println(user_idList.toString());
	}
	
	
	public   String getCurrentYear(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Date date = new Date();
		return sdf.format(date);
	}

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

		String parameter =null;
		HashMap<String, Object> dataNew=data;
		if(PublicParameter_NWN!=null){
			parameter = MapUtil.getValue("parameter", data);
			String Request = MapUtil.getValue("Request", data);
			Set<Map.Entry<String,String>> entrySet = PublicParameter_NWN.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+"=")&& !parameter.contains(key+"=")){
					if("get".equals(Request)){
						parameter=parameter+"&"+key+"="+value;
					}else{
						parameter=parameter+",\""+key+"\":\""+value+"\"";
					}
				}
			}
		}
		dataNew.put("parameter", parameter);
		return dataNew;
	}

	@Override
	public HashMap<String, Object> setDynamicParameter(HashMap<String, Object> data) {
		String parameter =null;
		HashMap<String, Object> dataNew=data;
		if(DynamicParameter_NWN!=null){
			parameter = MapUtil.getValue("parameter", data);
			String Request = MapUtil.getValue("Request", data);
			Set<Map.Entry<String,String>> entrySet = DynamicParameter_NWN.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+"=")&& !parameter.contains(key+"=")){
					if("get".equals(Request)){
						parameter=parameter+"&"+key+"="+timestamp;
					}else{
						parameter=parameter+",\""+key+"\":\""+timestamp+"\"";
					}
				}
			}
		}
		dataNew.put("parameter", parameter);
		return dataNew;
	}

	public static String GetSignValue(){
		return null;
	}
//	
//
//	//清除用户地区操作表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());
//	}
}
