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 STUDY extends BasicsGM{
	
	public static boolean isClearMysql =false;
	
	public static boolean isProduct= Boolean.valueOf(((system)map.get("study")).getIsProduct());
	//public static boolean isProduct_server=Boolean.valueOf(((system)map.get("nwn_server")).getIsProduct());
	public static String Ticket = "";//mis口令
	public static String Token;//特殊字符
	
	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 HashMap<String,String> PublicParameter_STUDY = null;
	public static HashMap<String,String> DynamicParameter_STUDY = null;

	static{
		
		//设置静态公共参数
		PublicParameter_STUDY=new HashMap<String,String>();
		//PublicParameter_STUDY.put("appid","jiaowu");
		
		//设置动态公共参数
		DynamicParameter_STUDY=new HashMap<String,String>();
		Long timeString= System.currentTimeMillis();
		timestamp = timeString.toString().substring(0, 10);
	//	DynamicParameter_STUDY.put("timestamp",timestamp);
//		DynamicParameter_STUDY.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("study")).getSqlurl(),
						((system)map.get("study")).getSqlname(),
						((system)map.get("study")).getSqlpwd());
				stmt = conn.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("studyxs")).getSqlurl(),
						((system)map.get("studyxs")).getSqlname(),
						((system)map.get("studyxs")).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());
	}
	
	
	//清除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_STUDY!=null){
			parameter = MapUtil.getValue("parameter", data);
			String Request = MapUtil.getValue("Request", data);
			Set<Map.Entry<String,String>> entrySet = PublicParameter_STUDY.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_STUDY!=null){
			parameter = MapUtil.getValue("parameter", data);
			String Request = MapUtil.getValue("Request", data);
			Set<Map.Entry<String,String>> entrySet = DynamicParameter_STUDY.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;
	}
//	
//

}
