package com.offcn.process;

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.Set;

import com.offcn.TestUnti.Log;
import com.offcn.TestUnti.MapUtil;
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 url = "";//后台登录得到的nwn登录地址
	public static String nwn_url = "";//后台登录得到的nwn登录地址
	public static String PHPSESSID = "";//后台登录nwn，最终得到的PHPSESSID
	public static String go_session_id = "";//后台登录电商，最终得到的go_session_id
	public static String Content_Length = "";//后台登录电商，最终得到的go_session_id  
	public static String admin_32_encode = "";//后台登录nwn，最终得到的admin_32_encode
	public static String admin_31_encode = "";//后台登录nwn，最终得到的PHPSESSID
	public static String location = "";//后台登录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 practiceId;//练习册id
	public static String menuId;//menuId  直播level_id
	public static String liveId;//  直播id
	public static String handoutsId;//  讲义id
	public static String jw_token;//  教务token
	public static String i_module_id;//  小表ID
	public static String i_v_module_id;//  视频小表ID
	public static String p_v_module_id;//  练习册小表ID
	public static String t_v_module_id;//  试卷册小表ID
	public static String e_taskId;//  试卷作业id
//	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 void Connectionsa(){
		//================设置数据库连接池=====================
		ConnectionsMap=getConnections(new String[]{"iip","zxkt"});
		StatementsMap=getStatements(ConnectionsMap);
		conn=ConnectionsMap.get("iip");//设置默认的连接对象
		stmt=StatementsMap.get("iip");//设置默认的连接对象
		
		//================设置公共参数========================
		PublicParameter.put("appid","jiaowu");//静态公共参数
		
		Long timeString= System.currentTimeMillis();
		timestamp = timeString.toString().substring(0, 10);
		DynamicParameter.put("timestamp",timestamp);//动态公共参数

	}
	public NWN(){
		Connectionsa();
	}

	
	public   String getCurrentYear(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Date date = new Date();
		return sdf.format(date);
	}

	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;
						}else{
							parameter=parameter+"&"+key+"="+value;
						}
					}else{
						if(parameter==null || parameter.length()==0){
							parameter=key+"\":\""+value+"\"";
						}else{
							parameter=parameter+",\""+key+"\":\""+value+"\"";
						}
					}
				}
			}
			dataNew.put("parameter", parameter);
		}
		return dataNew;
	}

	//动态公共参数
		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+"="+timestamp;//需要根据实际变化
							}else{
								parameter=parameter+"&"+key+"="+timestamp;//需要根据实际变化
							}
						}else{
							if(parameter==null || parameter.length()==0){
								parameter=key+"\":\""+timestamp+"\"";//需要根据实际变化+"\"";
							}else{
								parameter=parameter+",\""+key+"\":\""+timestamp+"\"";//需要根据实际变化+"\"";
							}
						}
					}
				}
				dataNew.put("parameter", parameter);
			}
			return dataNew;
		}
	
		
	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));
				//软删除
//				updateOrderStatus("n_package", "", 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");
			}

	}
	//清除注册数据根据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 void cleanMysqlData() {
		 if (!isClearMysql) {
			 Log.logInfo("nwn=====cleanMysqlData");
			 conn=ConnectionsMap.get("iip");//设置默认的连接对象
			 stmt=StatementsMap.get("iip");//设置默认的连接对象

			 this.cleann_getcode_id_List_FromDB();
			 this.cleann_user_id_List_FromDB();
			 this.cleann_template_FromDB();
			 this.cleann_n_package_List_FromDB();
			 this.cleann_n_admin_card_no_List_FromDB();
			 
			 //切库清
			 conn=ConnectionsMap.get("iip");//设置默认的连接对象
			 stmt=StatementsMap.get("iip");//设置默认的连接对象
			 this.cleann_n_admin_card_no_List_FromDB_beforeTest();
		  }
	}
	
}
