package com.offcn.process;

import io.restassured.response.Response;

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

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


public abstract class BasicsGM {
//	//数据提取
//	public static HashMap<String, Object> data_ext=new HashMap<String, Object>();
//	
//	//用户验证码<电话号码，验证码>
//	public static HashMap<String, String> verifyCode=new HashMap<String, String>();
//	
//	public static List<String> cust_ids = new ArrayList<String>();//客户表id，
//	
//	public static List<String> contractId_dd = new ArrayList<String>();//任买合同id，
//	
//	public static List<String> prod_ids = new ArrayList<String>();//商品信息表id
//	
//	public static List<String> order_ids = new ArrayList<String>();//订单表id
//	
//	public static HashMap<String, String> thirdBusi = new HashMap<String, String>();//子商户表id
//	
//	public static List<String> rulesEngine_ids = new ArrayList<String>();//决策引擎customer表 id
//	
	public static Connection conn = null;
	public static Statement stmt = null;
//	
	public static Map<String,Object> map=new XMLread().getSystem();// 读取XML配置文件
//	
//	public static boolean isProduct =  false;
//	
	//更改huimai.bestbuy_order表，条件是id=orderId，值该为orderStatus
	public static Integer updateOrderStatus(String fromName,String setCondition, String whereCondition) {
		Integer result=null;
		String sql =null;
		try {
			sql = " update  "+fromName+"  set  "+setCondition+"  where  " + whereCondition;
			result = stmt.executeUpdate(sql);//
			if (result == -1) {
				Log.logInfo("update failed! "+sql);
			}else{
				Log.logInfo("update  OK! " + sql);
			}
		} catch (SQLException e) {
			Log.logError("update failed! "+sql);
		}
		return result;
	}
	
	//查询表名tableName的条件是condition的结果集
	public ResultSet selectFromDB(String tableName, String condition) throws Exception {
		String sql;
		ResultSet rs = null;
		sql = " select  *  from  " + tableName + "  where  " + condition;
//		System.out.println(sql);
		rs = stmt.executeQuery(sql);
		return rs;

	}
	
	//查询表名tableName的条件是condition的结果集
		public ResultSet sqlFromDB(String sql) throws Exception {
			ResultSet rs = stmt.executeQuery(sql);
			return rs;

		}
	
	//查询表名tableName的条件是condition的结果集,返回数量
	public ResultSet selectCountFromDB(String tableName, String condition) throws Exception {
		String sql;
		ResultSet rs = null;
		sql = " select  count(*)  from  " + tableName + "  where  " + condition;
		Log.logInfo(sql);
		rs = stmt.executeQuery(sql);
		return rs;

	}
	
	//具体的删除动作，tableName=表名，key列，value值使用的like删除
	public void deleteFromDB(String tableName, String key, String value) throws Exception {
		String sql = "delete from " + tableName + " where " + key + " like '" + value + "'";
		Log.logInfo("sql==="+sql);
		int result = stmt.executeUpdate(sql);
		Log.logInfo("result==="+result);
		if (result == -1) {
			Log.logError("Clean failed!");
		}

	}
	
	// 如果cust_ids集合有值，则循环删除map_table集合内，表名=KEY，列名=value，值=cust_ids得遍历结果
	public void cleanCustomerInfo() {
		HashMap<String, String> map_table = new HashMap<String, String>();
		//用户验证所需的表格
		map_table.put("bestbuy_third_user", "cust_id");//任买外接第三方用户表
		map_table.put("bestbuy_customer", "id");//用户表
		//工作族基本信息
		map_table.put("bestbuy_doc", "rela_id");//用户身份证照片
		map_table.put("bestbuy_customer_info", "customer_id");//用户信息表
		map_table.put("bestbuy_bankcard", "customer_id");//银行卡信息表
		map_table.put("bestbuy_customer_book", "customer_id");//用户电话簿信息表
		map_table.put("bestbuy_customer_licensesinfo", "customer_id");//用户第三方授权信息表
		
		map_table.put("bestbuy_module_auth", "cust_id");//用户授权表
		map_table.put("bestbuy_audit_record", "cust_id");//人工信审工单处理记录表
		
		/*
		map_table.put("bestbuy_customer_auth", "customer_id");//用户授权表 ---APP端
		map_table.put("bestbuy_bank_record", "customer_id");//绑卡记录表
		*/
		map_table.put("bestbuy_customer_com", "customer_id");//客户公司信息表
		map_table.put("bestbuy_customer_location", "customer_id");//客户地址信息
		map_table.put("bestbuy_relationship", "customer_id");//用户关系表
		map_table.put("bestbuy_order", "customer_id");//订单表
		
		
		
		//合同表bestbuy_contract，暂时不清除，因为需要orderid，并且合同表关联到保理业务。。
		
		
		
		
//			Iterator<String> it_cust = cust_ids.iterator();//客户表id
//
//			while (it_cust.hasNext()) {
//				Iterator<Entry<String, String>> it_table = map_table.entrySet().iterator();
//				String cust = it_cust.next();
//				while (it_table.hasNext()) {
//					Map.Entry<String, String> entry = (Map.Entry<String, String>) it_table.next();
//					try {
//						this.deleteFromDB(entry.getKey(), entry.getValue(), cust);
//					} catch (Exception e) {
//						System.out.println("Fail to clean customer info!");
//					}
//				}
//			}

//			cust_ids.clear();

	}
//
//		//按照customer_id删除决策引擎中用户的数据-预授信模式
//		public void cleanStreamingCustomerInfo(String customer_id) {
//			HashMap<String, String> map_table = new HashMap<String, String>();
//			map_table.put("streaming.customer", "id");//决策引擎用户表
//			map_table.put("streaming.customer_info", "customer_id");//决策引擎用户表
//			map_table.put("streaming.customer_data_info", "customer_id");//决策引擎用户数据表
//			map_table.put("streaming.customer_limit_account_log", "customer_id");//用户身份证照片
//			map_table.put("streaming.customer_limit_account", "customer_id");//客户额度账户表
//			map_table.put("streaming.customer_merchant_info", "customer_id");//商户信息表
//			
//			Iterator<Entry<String, String>> it_table = map_table.entrySet().iterator();
//			while (it_table.hasNext()) {
//					Map.Entry<String, String> entry = (Map.Entry<String, String>) it_table.next();
//					try {
//						this.deleteFromDB(entry.getKey(), entry.getValue(), customer_id);
//					} catch (Exception e) {
//						System.out.println("Fail to clean customer info!");
//					}
//			}
//		}
//		
//		//按照streaming_process_info_id删除决策引擎中用户的数据-订单模式
//		public void cleanStreamingOrder(String id) {
//			HashMap<String, String> map_table = new HashMap<String, String>();
//			map_table.put("streaming.third_ylzc_advanced_data", "id");//决策引擎用户表
//			map_table.put("streaming.third_ylzc_base_data", "id");//决策引擎用户数据表
//			map_table.put("streaming.third_ylzc_basic_data_past_all_month", "id");//用户身份证照片
//			map_table.put("streaming.third_ylzc_basic_data_recent_1_3_month", "id");//客户额度账户表
//			map_table.put("streaming.third_ylzc_basic_data_recent_12_month", "id");//商户信息表
//			map_table.put("streaming.third_ylzc_basic_data_recent_6_month", "id");//商户信息表
//			map_table.put("streaming.third_ylzc_classical_data", "id");//商户信息表
//			map_table.put("streaming.third_ylzc_customized_data", "id");//商户信息表
//			map_table.put("streaming.streaming_customer_licensesinfo", "work_id");//用户授权信息表
//			map_table.put("streaming.streaming_process_info", "id");//流程信息
//			
//			Iterator<Entry<String, String>> it_table = map_table.entrySet().iterator();
//			while (it_table.hasNext()) {
//					Map.Entry<String, String> entry = (Map.Entry<String, String>) it_table.next();
//					try {
//						this.deleteFromDB(entry.getKey(), entry.getValue(), id);
//					} catch (Exception e) {
//						System.out.println("Fail to clean customer info!");
//					}
//			}
//		}
//		
//		
//		
//		
//		
//		
//		//清除list集合prod_ids，表名"bestbuy_goods"，列名id，值prod_ids得遍历结果
//		
//		 
//		public void cleanProductsFromDB() {
//			Iterator<String> it_prod = prod_ids.iterator();
//
//			while (it_prod.hasNext()) {
//				try {
//					this.deleteFromDB("bestbuy_goods", "id", it_prod.next());
//				} catch (Exception e) {
//					System.out.println("Fail to clean product info!");
//				}
//
//			}
//			prod_ids.clear();
//
//		}
//
//		//清除list集合order_ids，表名"bestbuy_order"，列名id，值order_ids得遍历结果
//		 
//		public void cleanOrdersFromDB() {
//			Iterator<String> it_order = order_ids.iterator();
//
//			while (it_order.hasNext()) {
//				try {
//					this.deleteFromDB("bestbuy_order", "id", it_order.next());
//				} catch (Exception e) {
//					System.out.println("Fail to clean order info!");
//				}
//
//			}
//			order_ids.clear();
//
//		}
//
//		//清除list集合order_ids，表名"bestbuy_order"，列名id，值order_ids得遍历结果
//		 
//		public void cleanRulesEngine_idsFromDB() {
//			Iterator<String> it_rulesEngine = rulesEngine_ids.iterator();
//			
//			Statement stmt_RulesEngine =null;
//			 
//			while (it_rulesEngine.hasNext()) {
//				try {
//					stmt_RulesEngine = conn.createStatement();
//					String sql = "delete from " + "streaming.customer" + " where " + "id_no" + " like '" + it_rulesEngine.next() + "'";
//					System.out.println("sql==="+sql);
//					int result = stmt_RulesEngine.executeUpdate(sql);
//					System.out.println("result==="+result);
//					if (result == -1) {
//						System.out.println("Clean failed!");
//					}
//				} catch (Exception e) {
//					System.out.println("Fail to clean order info!");
//				}finally{
//					if (stmt_RulesEngine != null){
//						try {
//							stmt_RulesEngine.close();
//						} catch (SQLException e) {
//							e.printStackTrace();
//						}
//					}
//				}
//			}
//			
//			rulesEngine_ids.clear();
//
//		}
//		
//		
//		//从thirdBusi这个map集合中，往huimai.bestbuy_sub_busi这个表添加信息
//		 
//		public void addTestThirdBusiInfoFromDB() {
//			Statement stmt;
//			try {
//				stmt = conn.createStatement();
//
//				String sql = "insert into huimai.bestbuy_sub_busi (busi_id,sub_busi_name,busi_id_third,create_time) "
//						+ "values('999','" + thirdBusi.get("busiName") + "','" + thirdBusi.get("busiId") + "', NOW())";
//				// System.out.println(sql);
//				int result = stmt.executeUpdate(sql);//
//				if (result == -1) {
//					System.out.println("Insert failed!");
//				}
//			} catch (SQLException e) {
//				System.out.println("Insert failed!");
//			}
//
//		}
//
//		//从thirdBusi这个map集合中，在huimai.bestbuy_sub_busi这个表删除信息
//		 
//		public void cleanTestThirdBusiInfoFromDB() {
//			Statement stmt;
//			try {
//				stmt = conn.createStatement();
//
//				String sql = "delete from huimai.bestbuy_sub_busi where " + "sub_busi_name ='"
//						+ thirdBusi.get("busiName") + "' and busi_id_third='" + thirdBusi.get("busiId") + "'";
//				// System.out.println(sql);
//				int result = stmt.executeUpdate(sql);//
//				if (result == -1) {
//					System.out.println("clean busi info failed!");
//				}
//			} catch (SQLException e) {
//				System.out.println("clean busi info failed!");
//			}
//
//		}
//
		
//		
//		//更改
//		
//		public void updateTableValueWithCondition(String table, String setStatement, String whereStatement) {
//			Statement stmt;
//			try {
//				stmt = conn.createStatement();
//
//				String sql = "update "+table+" set "+setStatement+" where "+whereStatement; 
////						System.out.println(sql);
//				int result = stmt.executeUpdate(sql);//
//				if (result == -1) {
//					System.out.println("update table value failed!");
//				}
//			} catch (SQLException e) {
//				System.out.println("update table value failed!");
//			}
//		}
//		
//
//		// 字符串转换成时间类型
//		 
//		public Date convertStrToDate(String dateStr) {
//			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//			Date date = null;
//			try {
//				date = sdf.parse(dateStr);
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//			return date;
//
//		}
//
//		//时间转换成字符串类型
//		public String convertDateToStr(Date date) {
//			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//			String dateStr = null;
//			try {
//				dateStr = sdf.format(date);
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//			return dateStr;
//
//		}
//		
//		public String getContractId(){
//			for(int i=0;i<contractId_dd.size();i++){
//				if(contractId_dd.get(i)!=null){
//					return contractId_dd.get(i);
//				}
//			}
//			return null;
//		}
//	
	//判断字符串是否是空或者是“”
	public boolean StringIsNull(String str){
		if(str==null || "".equals(str)){
			return false;
		}else{
			return true;
		}
	}
	
	//获取errcode或code，或者msg或errmsg，返回去作为用例输出结果
	public String getCode( Response re){
		try {
			String code=JSONObject.fromObject(re.body().asString()).getString("code");
			if(StringIsNull(code)){
				return code;
			}
		} catch (Exception e) {
		}
		
		try {
			String code=JSONObject.fromObject(re.body().asString()).getString("retcode");
			if(StringIsNull(code)){
				return code;
			}
		} catch (Exception e) {
		}
		
		
		try {
			String errcode=JSONObject.fromObject(re.body().asString()).getString("errcode");
			if(StringIsNull(errcode)){
				return errcode;
			}
		} catch (Exception e) {
		}
		return null;
	}
	//获取errcode或code，或者msg或errmsg，返回去作为用例输出结果
	public String getMsg( Response re){
		try {
			String msg=JSONObject.fromObject(re.body().asString()).getString("msg");
			if(StringIsNull(msg)){
				return msg;
			}
		} catch (Exception e) {
		}
		
		try {
			String errmsg=JSONObject.fromObject(re.body().asString()).getString("message");
			if(StringIsNull(errmsg)){
				return errmsg;
			}
		} catch (Exception e) {
		}
		
		try {
			String errmsg=JSONObject.fromObject(re.body().asString()).getString("errmsg");
			if(StringIsNull(errmsg)){
				return errmsg;
			}
		} catch (Exception e) {
		}
		
		return null;
	}

	
	
}
