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 Connection conn1 = null;
	
	public static Statement stmt1 = null;
	public static Connection conn2 = null;
	public static Statement stmt2 = null;
//	
	public static Map<String,Object> map=new XMLread().getSystem();// 读取XML配置文件
	public abstract HashMap<String, Object> setPublicParameter(HashMap<String, Object> data);
	public abstract HashMap<String, Object> setDynamicParameter(HashMap<String, Object> data);

//	
//	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 sqlFromDB_template(String sql) throws Exception {
					ResultSet rs = stmt1.executeQuery(sql);
					return rs;

				}
				
				//查询表名tableName的条件是condition的结果集
				public ResultSet sqlFromDB_package(String sql) throws Exception {
					ResultSet rs = stmt2.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");//订单表
		

	}
//
//		//按照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!");
//					}
//			}
//		}

	//判断字符串是否是空或者是“”
	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 retcode=JSONObject.fromObject(re.body().asString()).getString("retcode");
			if(StringIsNull(retcode)){
				return retcode;
			}
		} 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 message=JSONObject.fromObject(re.body().asString()).getString("message");
			if(StringIsNull(message)){
				return message;
			}
		} catch (Exception e) {
		}
		
		try {
			String errmsg=JSONObject.fromObject(re.body().asString()).getString("errmsg");
			if(StringIsNull(errmsg)){
				return errmsg;
			}
		} catch (Exception e) {
		}
		
		return null;
	}

	
	
}
