package com.offcn.process;

import io.restassured.http.Headers;

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.MYSQLConnection;
import com.offcn.TestUnti.MapUtil;
import com.offcn.TestUnti.ReadProperties;
import com.offcn.TestUnti.cmdUtil;
import com.offcn.system.system;


public class TK extends BasicsGM{
	
	public static boolean isClearMysql =false;
	public static boolean isProduct=Boolean.valueOf(((system)map.get("tk")).getIsProduct());
	public static String phoneNeb = "";//手机号
	public static String phone_code = "";//手机号验证码
	public static List<String> user_idList = new ArrayList<String>();//user_id
	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 String practice_id_L="";//21接口提供，练习册id
	public static String cate_id_L="";//21接口提供，练习册里面的小类别
	public static String paper_id_L="";//27.getExamPaperList 返回试卷id 27、88接口提供
	public static String record_id_L="";//28.getpaperBlocks 返回做题记录id 28、47、88接口提供
	public static String examine_paper_id_L="";//88接口提供
	public static String examine_id_L="";//88接口提供
	public static String image_id_L="";//100接口提供
	public static String question_id_L="";//试题id
	public static String question_id_list="";//试题id集合，47接口提供
	public static String item_id_L="";//考试项目的id
	public static String question_number_L="";//47练习册提交做准备
	public static String is_hf_L="";//47
	public static String type_L="";//47
	public static String recordId_L="";//110申论，使用记录id 提交做题记录时需要用到
	public static String admin_login_ssid="";//后台登录cookie
	public static String admin_28_encode="";//后台登录cookie
	public static String PHPSESSID="";//Cookie，后台第77个接口使用
	
	public static String Location="";//Location，调用接口发现还是需要登录，获取到的连接
	public static String Location1="";//Location，调用接口发现还是需要登录，获取到的连接
//	tiku2接口数据
	public static String L_record_id="";//apiv2 1接口，用户选择考试的记录id
	public static String L_mock_id="";
	public static String L_exam_id="";//考试id
	public static String L_mock_subject_id="";
	public static String L_mock_sub_id="";
	public static String L_record_sub_id="";
//	public static String exam_id_L="";//考试id
//	public static String Record_id="";//apiv2 1接口，用户选择考试的记录id
	public static String L_exam_subtype_id="";//专项层级id
//	tiku3接口数据
	public static String L_shelf_id="";//书架id
	
	
	
	
	//必有
	public void Connectionsa(){
		//================设置数据库连接池=====================
		ConnectionsMap=getConnections(new String[]{"tk"});
		StatementsMap=getStatements(ConnectionsMap);
		conn=ConnectionsMap.get("tk");//设置默认的连接对象
		stmt=StatementsMap.get("tk");//设置默认的连接对象
		//================设置公共参数========================
		PublicParameter.put("appid","tiku");//静态公共参数
		DynamicParameter.put("sign","code");//动态公共参数
	}
	
	//必有
	public TK(){
		Connectionsa();
	}
	
	//必有//动态公共参数
	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+"="+GetSignValue(parameter);
						}else{
							parameter=parameter+"&"+key+"="+GetSignValue(parameter);
						}
					}else{
						if(parameter==null || parameter.length()==0){
							parameter=key+"\":\""+GetSignValue(parameter)+"\"";
						}else{
							parameter=parameter+",\""+key+"\":\""+GetSignValue(parameter)+"\"";
						}
					}
				}
			}
			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;
						}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 static String GetSign(String parameter){
		parameter=parameter+",\"sign\":\""+GetSignValue(parameter)+"\"";
		return parameter;
	}
	
	
	//验签值
	public static String GetSignValue(String parameter){
		parameter=parameter.replace("\"", "");
		parameter=parameter.replace(":", "=");
		String arr[]=parameter.split(",");
		Arrays.sort(arr);
		parameter=Arrays.toString(arr);
		parameter=parameter.toString().replace(", ", "&");
		parameter=(parameter.substring(1,parameter.length()-1))+"&123456";
		return MD5keyUtil.getMD5Str(parameter);
	}
	
	//必有
	public void cleanMysqlData() {
		 if (!isClearMysql) {
			Log.logInfo("tk=====cleanMysqlData");
			conn=ConnectionsMap.get("tk");//设置默认的连接对象
			stmt=StatementsMap.get("tk");//设置默认的连接对象
			cleanUser_FromDB();//清除注册user_id，
			cleanexam_FromDB();//清除用户地区操作表t_exam_area
		  }
	}
	
	//清除注册数据根据user_idList
	public void cleanUser_FromDB() {
		List<String> user_List = new ArrayList<String>();
		for(int i=0;i<user_idList.size();i++){
			try {
				deleteFromDB("t_user", "user_id", user_idList.get(i));
				user_List.add(user_idList.get(i));
			} catch (Exception e) {
				e.printStackTrace();
				Log.logInfo("清除数据失败t_user");
			}
			
		}
		user_idList.removeAll(user_List);
//			System.out.println(user_idList.toString());
	}
	

	//清除用户地区操作表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());
	}
	
}
