package com.saga.util;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.zillion.util.log.LogUtil;

import com.saga.entity.PointSet;
import com.saga.entity.Record;
import com.saga.entity.RecordMsg;

/**
 * Mysql工具
 *
 * @author 李井强
 */
public class MySQLUtil {
	public static List<String> select(String SqlContent) {
		List<String> result = new ArrayList<String>();
		try {
			Statement stmt = Constant.conn.createStatement();
			ResultSet rs = stmt.executeQuery(SqlContent);

			while (rs.next()) {
				System.out.println(rs.getString("data"));
			}
			rs.close();
			stmt.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 加载状态-物理结构
	 */
	public static Map<String, String> ps_physical_status(String db, String tb) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 address_1 String 一级地址 主键
		// 3 address_2 String 二级地址 主键
		// 4 address_3 String 三级地址 主键
		// 5 address_4 String 四级地址 主键
		// 6 status String 状态 conn/disconn
		// 7 receivetime String 时间 最近通讯时间
		// 8 detail String 详情
		Map<String, String> dbPhysicalStatus = new HashMap<String, String>();
		try {
			Statement stmt = Constant.conn.createStatement();
			ResultSet rs = stmt.executeQuery("select * from " + db + "." + tb);
			while (rs.next()) {
				String project = rs.getString("project");
				String address_1 = rs.getString("address_1");
				String address_2 = rs.getString("address_2");
				String address_3 = rs.getString("address_3");
				String address_4 = rs.getString("address_4");
				String key = project + ";" + address_1 + ";" + address_2 + ";" + address_3 + ";" + address_4;
				String status = rs.getString("status");
				dbPhysicalStatus.put(key, status);
			}
			rs.close();
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dbPhysicalStatus;
	}

	/**
	 * 加载状态-点位
	 */
	public static Map<String, String> ps_virtual_point_status(String db, String tb) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 meter String 仪表 主键
		// 3 funcid Long 功能号 主键
		// 4 updown String 上/下行 主键
		// 5 status String 状态
		// 6 receivetime String 时间
		// 7 detail String 详情
		Map<String, String> dbPointStatus = new HashMap<String, String>();
		try {
			Statement stmt = Constant.conn.createStatement();
			ResultSet rs = stmt.executeQuery("select * from " + db + "." + tb);
			while (rs.next()) {
				String project = rs.getString("project");
				String meter = rs.getString("meter");
				long funcid = rs.getLong("funcid");
				String updown = rs.getString("updown");
				if ("up".equalsIgnoreCase(updown)) {
					String key = project + "-" + meter + "." + funcid;
					String status = rs.getString("status");
					dbPointStatus.put(key, status);
				}
			}
			rs.close();
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dbPointStatus;
	}

	/**
	 * 加载状态-meter
	 */
	public static Map<String, String> ps_virtual_meter_status(String db, String tb) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 meter String 仪表 主键
		// 3 updown String 上/下行 主键
		// 4 status String 状态
		// 5 receivetime String 时间
		// 6 detail String 详情

		Map<String, String> dbMeterStatus = new HashMap<String, String>();

		try {
			Statement stmt = Constant.conn.createStatement();
			ResultSet rs = stmt.executeQuery("select * from " + db + "." + tb);
			while (rs.next()) {
				String project = rs.getString("project");
				String meter = rs.getString("meter");
				String updown = rs.getString("updown");
				if ("up".equalsIgnoreCase(updown)) {
					String key = project + "-" + meter;
					String status = rs.getString("status");
					dbMeterStatus.put(key, status);
				}
			}
			rs.close();
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dbMeterStatus;
	}

	/**
	 * 加载状态-project
	 */
	public static Map<String, String> ps_virtual_project_status(String db, String tb) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 status String 状态
		// 3 receivetime String 时间
		// 4 detail String 详情

		Map<String, String> dbProjectStatus = new HashMap<String, String>();
		try {
			Statement stmt = Constant.conn.createStatement();
			ResultSet rs = stmt.executeQuery("select * from " + db + "." + tb);
			while (rs.next()) {
				String project = rs.getString("project");
				String key = project;
				String status = rs.getString("status");
				dbProjectStatus.put(key, status);
			}
			rs.close();
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dbProjectStatus;
	}

	/**
	 * 加载状态-配置点表
	 */
	public static void LoadDB2Cache_dy_pointlist(String db, String tb) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 building String 楼号 主键
		// 2 meter String 表号 主键
		// 3 funcid Long 功能号 主键
		// 4 system String 系统
		// 5 source String 数据来源
		// 6 address_1 String 一级地址 采集器网关ID
		// 7 address_2 String 二级地址 总线端口号
		// 8 address_3 String 三级地址 仪表地址
		// 9 address_4 String 四级地址
		// 10 collect_cycle String 采集频率 采集器上传频率
		// 11 data_type String 点位类型 Acc:累积变量；Inst:瞬时变量；Boolv:通断变量；
		// Enumv:状态变量；Step:阶跃变量
		// 12 unit String 单位

		try {
			Statement stmt = Constant.conn.createStatement();
			LogUtil.info("Reload Project Point List:" + "  dy_pointlist Begin...");
			ResultSet rs = stmt.executeQuery("select * from " + db + "." + tb + "");
			while (rs.next()) {
				String project = rs.getString("building");
				String meter = rs.getString("meter");
				long funcid = rs.getLong("funcid");
				String address_1 = rs.getString("address_1");
				String address_2 = rs.getString("address_2");
				String address_3 = rs.getString("address_3");
				String address_4 = rs.getString("address_4");
				String key4 = project + ";" + address_1 + ";" + address_2 + ";" + address_3 + ";" + address_4;
				String key3 = project + ";" + address_1 + ";" + address_2 + ";" + address_3 + ";" + "";
				String key2 = project + ";" + address_1 + ";" + address_2 + ";" + "" + ";" + "";
				String key1 = project + ";" + address_1 + ";" + "" + ";" + "" + ";" + "";

				String value = project + "-" + meter + "." + funcid;
				synchronized (Constant.ps_PhysicalList) {
					if (!Constant.ps_PhysicalList.containsKey(key4)) {
						Constant.ps_PhysicalList.put(key4, new ArrayList<String>());
					}
					if (!Constant.ps_PhysicalList.containsKey(key3)) {
						Constant.ps_PhysicalList.put(key3, new ArrayList<String>());
					}
					if (!Constant.ps_PhysicalList.containsKey(key2)) {
						Constant.ps_PhysicalList.put(key2, new ArrayList<String>());
					}
					if (!Constant.ps_PhysicalList.containsKey(key1)) {
						Constant.ps_PhysicalList.put(key1, new ArrayList<String>());
					}
					if (!Constant.ps_PhysicalList.get(key4).contains(value)) {
						Constant.ps_PhysicalList.get(key4).add(value);
					}
					if (!Constant.ps_PhysicalList.get(key3).contains(value)) {
						Constant.ps_PhysicalList.get(key3).add(value);
					}
					if (!Constant.ps_PhysicalList.get(key2).contains(value)) {
						Constant.ps_PhysicalList.get(key2).add(value);
					}
					if (!Constant.ps_PhysicalList.get(key1).contains(value)) {
						Constant.ps_PhysicalList.get(key1).add(value);
					}
				}

				synchronized (Constant.ps_MeterList) {
					if (!Constant.ps_MeterList.containsKey(project + "-" + meter)) {
						Constant.ps_MeterList.put(project + "-" + meter, new ArrayList<String>());
					}
					if (!Constant.ps_MeterList.get(project + "-" + meter).contains(value)) {
						Constant.ps_MeterList.get(project + "-" + meter).add(value);
					}
				}
				synchronized (Constant.ps_ProjectList) {

					if (!Constant.ps_ProjectList.containsKey(project)) {
						Constant.ps_ProjectList.put(project, new ArrayList<String>());
					}
					if (!Constant.ps_ProjectList.get(project).contains(value)) {
						Constant.ps_ProjectList.get(project).add(value);
					}
				}
				checkStatus(project, meter, funcid, address_1, address_2, address_3, address_4);
			}
			rs.close();
			stmt.close();
			LogUtil.info("Reload Project Point List:" + "  dy_pointlist End...");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 记录状态
	 */
	private static void checkStatus(String project, String meter, long funcid, String address_1, String address_2,
			String address_3, String address_4) {
		// TODO Auto-generated method stub
		if (!Constant.ps_PointStatusUp.containsKey(project + "-" + meter + "." + funcid)) {
			Constant.ps_PointStatusUp.put((project + "-" + meter + "." + funcid), null);
		}
		if (!Constant.ps_MeterStatusUp.containsKey(project + "-" + meter)) {
			Constant.ps_MeterStatusUp.put((project + "-" + meter), null);
		}
		if (!Constant.ps_ProjectStatus.containsKey(project)) {
			Constant.ps_ProjectStatus.put((project), null);
		}
		String key4 = project + ";" + address_1 + ";" + address_2 + ";" + address_3 + ";" + address_4;
		String key3 = project + ";" + address_1 + ";" + address_2 + ";" + address_3 + ";" + "";
		String key2 = project + ";" + address_1 + ";" + address_2 + ";" + "" + ";" + "";
		String key1 = project + ";" + address_1 + ";" + "" + ";" + "" + ";" + "";
		if (!Constant.ps_PhysicalStatus.containsKey(key4)) {
			Constant.ps_PhysicalStatus.put((key4), null);
		}
		if (!Constant.ps_PhysicalStatus.containsKey(key3)) {
			Constant.ps_PhysicalStatus.put((key3), null);
		}
		if (!Constant.ps_PhysicalStatus.containsKey(key2)) {
			Constant.ps_PhysicalStatus.put((key2), null);
		}
		if (!Constant.ps_PhysicalStatus.containsKey(key1)) {
			Constant.ps_PhysicalStatus.put((key1), null);
		}
	}

	/**
	 * 加载状态-物理结构
	 */
	public static void LoadDB2Cache_ps_physical_status(String db, String tb) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 address_1 String 一级地址 主键
		// 3 address_2 String 二级地址 主键
		// 4 address_3 String 三级地址 主键
		// 5 address_4 String 四级地址 主键
		// 6 status String 状态 conn/disconn
		// 7 receivetime String 时间 最近通讯时间
		// 8 detail String 详情
		try {
			Statement stmt = Constant.conn.createStatement();
			LogUtil.info("Reload Project:" + "  ps_physical_status Begin...");
			ResultSet rs = stmt.executeQuery("select * from " + db + "." + tb);
			while (rs.next()) {
				String project = rs.getString("project");
				String address_1 = rs.getString("address_1");
				String address_2 = rs.getString("address_2");
				String address_3 = rs.getString("address_3");
				String address_4 = rs.getString("address_4");
				String key = project + ";" + address_1 + ";" + address_2 + ";" + address_3 + ";" + address_4;
				String receivetime = rs.getString("receivetime");
				Constant.ps_PhysicalStatus.put(key, Constant.parse(receivetime).getTime());
			}
			rs.close();
			stmt.close();
			LogUtil.info("Reload Project:" + "  ps_physical_status End...");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 加载状态-点位
	 */
	public static void LoadDB2Cache_ps_virtual_point_status(String db, String tb) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 meter String 仪表 主键
		// 3 funcid Long 功能号 主键
		// 4 updown String 上/下行 主键
		// 5 status String 状态
		// 6 receivetime String 时间
		// 7 detail String 详情
		Statement stmt = Constant.conn.createStatement();
		LogUtil.info("Reload Project:" + " ps_virtual_point_status Begin...");
		ResultSet rs = stmt.executeQuery("select * from " + db + "." + tb);
		while (rs.next()) {
			String project = rs.getString("project");
			String meter = rs.getString("meter");
			long funcid = rs.getLong("funcid");
			String updown = rs.getString("updown");
			if ("up".equalsIgnoreCase(updown)) {
				String key = project + "-" + meter + "." + funcid;
				String receivetime = rs.getString("receivetime");
				Constant.ps_PointStatusUp.put(key, Constant.parse(receivetime).getTime());
				LogUtil.info("project " + project + " meter " + meter + " funcid " + funcid + " "
						+ System.currentTimeMillis() + " " + Constant.parse(receivetime).getTime());
			}
		}
		rs.close();
		stmt.close();
		LogUtil.info("Reload Project:" + "  ps_virtual_point_status End...");
	}

	/**
	 * 加载状态-meter
	 */
	public static void LoadDB2Cache_ps_virtual_meter_status(String db, String tb) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 meter String 仪表 主键
		// 3 updown String 上/下行 主键
		// 4 status String 状态
		// 5 receivetime String 时间
		// 6 detail String 详情

		try {
			Statement stmt = Constant.conn.createStatement();
			LogUtil.info("Reload Project:" + " ps_virtual_meter_status Begin...");
			ResultSet rs = stmt.executeQuery("select * from " + db + "." + tb);
			while (rs.next()) {
				String project = rs.getString("project");
				String meter = rs.getString("meter");
				String updown = rs.getString("updown");
				if ("up".equalsIgnoreCase(updown)) {
					String key = project + "-" + meter;
					String receivetime = rs.getString("receivetime");
					Constant.ps_MeterStatusUp.put(key, Constant.parse(receivetime).getTime());
					LogUtil.info("project " + project + " meter " + meter + " " + System.currentTimeMillis() + " "
							+ Constant.parse(receivetime).getTime());
				}
			}
			rs.close();
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		LogUtil.info("Reload Project:" + "  ps_virtual_meter_status End...");
	}

	/**
	 * 加载状态-project
	 */
	public static void LoadDB2Cache_ps_virtual_project_status(String db, String tb) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 status String 状态
		// 3 receivetime String 时间
		// 4 detail String 详情
		try {
			Statement stmt = Constant.conn.createStatement();
			LogUtil.info("Reload Project:" + " ps_virtual_project_status Begin...");
			ResultSet rs = stmt.executeQuery("select * from " + db + "." + tb);
			while (rs.next()) {
				String project = rs.getString("project");
				String key = project;
				String receivetime = rs.getString("receivetime");
				Constant.ps_ProjectStatus.put(key, Constant.parse(receivetime).getTime());
				LogUtil.info("project " + project + " " + System.currentTimeMillis() + " "
						+ Constant.parse(receivetime).getTime());
			}
			LogUtil.info("Reload Project:" + "  ps_virtual_project_status End...");
			rs.close();
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 加载状态-配置点表
	 */
	public static Map<String, List<Integer>> GetDBPointList(String db, String tb, String building) throws Exception {

		Map<String, List<Integer>> dbPointList = new HashMap<String, List<Integer>>();
		try {
			Statement stmt = Constant.conn.createStatement();
			ResultSet rs = stmt.executeQuery("select * from " + db + "." + tb + " where building='" + building + "'");
			while (rs.next()) {
				String meter = rs.getString("meter");
				Long f = rs.getLong("funcid");
				Integer funcid = f.intValue();
				if (!dbPointList.containsKey(meter)) {
					dbPointList.put(meter, new ArrayList<Integer>());
				}
				dbPointList.get(meter).add(funcid);
			}
			rs.close();
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dbPointList;
	}

	/**
	 * 加载状态-指令记录
	 */
	public static void Getset(String db, String tb, String building, String meter, long funcid, String query_from,
			String query_to) throws Exception {
		try {
			Statement stmt = Constant.conn.createStatement();
			ResultSet rs = stmt.executeQuery("select * from " + db + "." + tb + " where building='" + building + "'"
					+ " and meter='" + meter + "'" + " and funcid='" + funcid + "'" + " and receivetime>'" + query_from
					+ "'" + " and receivetime<'" + query_to + "'");
			while (rs.next()) {
				String receivetime = rs.getString("receivetime");
				String endtime = rs.getString("endtime");
				Double data = rs.getDouble("data");
				String status = rs.getString("status");
				LogUtil.info(
						"receivetime " + receivetime + " endtime " + endtime + " data:" + data + " status " + status);
			}
			rs.close();
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 加载状态-当前指令
	 */
	public static void Getset_present(String db, String tb, String building, String meter, long funcid)
			throws Exception {
		try {
			Statement stmt = Constant.conn.createStatement();
			ResultSet rs = stmt.executeQuery("select * from " + db + "." + tb + " where building='" + building + "'"
					+ " and meter='" + meter + "'" + " and funcid='" + funcid + "'");
			while (rs.next()) {
				String receivetime = rs.getString("receivetime");
				String endtime = rs.getString("endtime");
				Double data = rs.getDouble("data");
				String status = rs.getString("status");
				LogUtil.info(
						"receivetime " + receivetime + " endtime " + endtime + " data:" + data + " status " + status);
			}
			rs.close();
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// public static void GetOriginal_month(String db, String tb, String
	// building, String meter, long funcid,
	// String query_from, String query_to) throws Exception {
	// try {
	// Connection conn = null;
	// Statement stmt = null;
	// Class.forName("com.mysql.jdbc.Driver");
	// EntityMysql entityMysql = new EntityMysql();
	// entityMysql = (EntityMysql) Constant.DbConnection.get("Db-Mysql");
	// conn = DriverManager.getConnection("jdbc:mysql://" + entityMysql.Ip + ":"
	// + entityMysql.Port + "/",
	// entityMysql.User, entityMysql.Password);
	// stmt = conn.createStatement();
	// ResultSet rs = stmt.executeQuery("select * from " + db + "." + tb + "
	// where building='" + building + "'"
	// + " and meter='" + meter + "'" + " and funcid='" + funcid + "'" + " and
	// receivetime>'" + query_from
	// + "'" + " and receivetime<'" + query_to + "'");
	// while (rs.next()) {
	// String receivetime = rs.getString("receivetime");
	// Double data = rs.getDouble("data");
	// }
	// rs.close();
	// stmt.close();
	// conn.close();
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// }
	//
	// public static void GetOriginal_present(String db, String tb, String
	// building, String meter, long funcid)
	// throws Exception {
	// try {
	// Connection conn = null;
	// Statement stmt = null;
	// Class.forName("com.mysql.jdbc.Driver");
	// EntityMysql entityMysql = new EntityMysql();
	// entityMysql = (EntityMysql) Constant.DbConnection.get("Db-Mysql");
	// conn = DriverManager.getConnection("jdbc:mysql://" + entityMysql.Ip + ":"
	// + entityMysql.Port + "/",
	// entityMysql.User, entityMysql.Password);
	// stmt = conn.createStatement();
	// ResultSet rs = stmt.executeQuery("select * from " + db + "." + tb + "
	// where building='" + building + "'"
	// + " and meter='" + meter + "'" + " and funcid='" + funcid + "'");
	// while (rs.next()) {
	// String receivetime = rs.getString("receivetime");
	// Double data = rs.getDouble("data");
	// LogUtil.info("meter " + meter + " funcid " + funcid + " receivetime " +
	// receivetime + " data:" + data);
	// }
	// rs.close();
	// stmt.close();
	// conn.close();
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// }

	/**
	 * 建库
	 */
	public static void CreateDB(String DB) throws Exception {
		LogUtil.info("CreateDB:" + DB);
		try {
			Statement stmt = Constant.conn.createStatement();
			stmt.execute(
					"CREATE DATABASE IF NOT EXISTS " + DB + " default character set utf8 COLLATE utf8_general_ci;");
			stmt.close();
			Thread.sleep(100L);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 建库
	 */
	public static void CreateUploadDB(String DB) throws Exception {
		if (Constant.connUpload == null) {
			return;
		}
		LogUtil.info("CreateUploadDB:" + DB);
		try {
			Statement stmt = Constant.connUpload.createStatement();
			stmt.execute(
					"CREATE DATABASE IF NOT EXISTS " + DB + " default character set utf8 COLLATE utf8_general_ci;");
			stmt.close();
			Thread.sleep(100L);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 判断表是否存在
	 */
	public static boolean tableExist(String DBName, String tableName) throws Exception {
		boolean result = false;
		try {
			Statement stmt = Constant.conn.createStatement();
			ResultSet rs = stmt.executeQuery("select * from information_schema.TABLES WHERE `TABLE_SCHEMA` = '" + DBName
					+ "' AND `TABLE_NAME` = '" + tableName + "'");
			while (rs.next()) {
				result = true;
				break;
			}
			rs.close();
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 建表-monthdata
	 */
	public static void CreateTable(String DB, String tableName) throws Exception {
		try {
			if (!tableExist(DB, tableName)) {
				Statement stmt = Constant.conn.createStatement();
				stmt.execute("CREATE TABLE IF NOT EXISTS " + DB + "." + tableName
						+ " (`meter` varchar(50) NOT NULL,`funcid` int(11) NOT NULL,`receivetime` datetime NOT NULL,`data` double NOT NULL,PRIMARY KEY (`meter`,`funcid`,`receivetime`),KEY `index_intervaldata` (`meter`,`funcid`,`receivetime`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
				stmt.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 建表-detail
	 */
	public static void CreateTable_detail(String DB, String tableName) throws Exception {
		try {
			if (!tableExist(DB, tableName)) {
				Statement stmt = Constant.conn.createStatement();
				stmt.execute("CREATE TABLE IF NOT EXISTS " + DB + "." + tableName
						+ " (`meter` varchar(50) NOT NULL,`funcid` int(11) NOT NULL,`seq` int(11) NOT NULL,`receivetime` datetime NOT NULL,`logtime` datetime NOT NULL,`data` double NOT NULL,PRIMARY KEY (`meter`,`funcid`,`seq`),KEY `index_intervaldata` (`meter`,`funcid`,`seq`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
				stmt.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 建表-upload
	 */
	public static void CreateTable_upload(String DB, List<String> uploadList) throws Exception {
		if (Constant.connUpload == null) {
			return;
		}
		try {
			for (String tableName : uploadList) {
				// if (!tableExist(DB, tableName)) {
				Statement stmt = Constant.connUpload.createStatement();
				stmt.execute("CREATE TABLE IF NOT EXISTS " + DB + "." + tableName
						+ " (`meter` varchar(50) NOT NULL,`funcid` int(11) NOT NULL,`receivetime` datetime NOT NULL,`data` double NOT NULL,PRIMARY KEY (`meter`,`funcid`,`receivetime`),KEY `index_intervaldata` (`meter`,`funcid`,`receivetime`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
				stmt.close();
				// }
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 建表-present
	 */
	public static void CreateTable_present(String DB, String tableName) throws Exception {
		try {
			if (!tableExist(DB, tableName)) {
				Statement stmt = Constant.conn.createStatement();
				stmt.execute("CREATE TABLE IF NOT EXISTS " + DB + "." + tableName
						+ " (`meter` varchar(50) NOT NULL,`funcid` int(11) NOT NULL,`receivetime` datetime NOT NULL,`data` double NOT NULL,PRIMARY KEY (`meter`,`funcid`),KEY `index_intervaldata` (`meter`,`funcid`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
				stmt.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 建表-指令记录
	 */
	public static void CreateTable_set(String DB, String tableName) throws Exception {
		try {
			if (!tableExist(DB, tableName)) {
				Statement stmt = Constant.conn.createStatement();
				stmt.execute("CREATE TABLE IF NOT EXISTS " + DB + "." + tableName + " (`meter` varchar(50) NOT NULL,"
						+ "`funcid` int(11) NOT NULL," + "`receivetime` datetime NOT NULL," + "`data` double NOT NULL,"
						+ "`status` varchar(50) NOT NULL," + "`endtime` datetime NOT NULL,"
						+ "PRIMARY KEY (`meter`,`funcid`,`receivetime`),"
						+ "KEY `index_intervaldata` (`meter`,`funcid`,`receivetime`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
				stmt.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 建表-当前指令
	 */
	public static void CreateTable_setpresent(String DB, String tableName) throws Exception {
		try {
			if (!tableExist(DB, tableName)) {
				Statement stmt = Constant.conn.createStatement();
				stmt.execute("CREATE TABLE IF NOT EXISTS " + DB + "." + tableName + " (`meter` varchar(50) NOT NULL,"
						+ "`funcid` int(11) NOT NULL," + "`receivetime` datetime NOT NULL," + "`data` double NOT NULL,"
						+ "`status` varchar(50) NOT NULL," + "`endtime` datetime NOT NULL,"
						+ "PRIMARY KEY (`meter`,`funcid`),"
						+ "KEY `index_intervaldata` (`meter`,`funcid`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
				stmt.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 建表-fjd_0_buildingcomputetime
	 */
	public static void CreateTable_fjd_0_buildingcomputetime(String DB, String tableName) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 building String 楼号 主键
		// 2 compute_time String 计算时间
		// 3 first_compute_time String 首次计算时间
		try {
			if (!tableExist(DB, tableName)) {
				Statement stmt = Constant.conn.createStatement();
				stmt.execute("CREATE TABLE IF NOT EXISTS " + DB + "." + tableName + " (`building` varchar(50) NOT NULL,"
						+ "`compute_time` datetime NOT NULL," + "`first_compute_time` datetime NOT NULL,"
						+ "PRIMARY KEY (`building`),"
						+ "KEY `index_intervaldata` (`building`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
				stmt.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 建表-dy_pointlist
	 */
	public static void CreateTable_dy_pointlist(String DB, String tableName) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 building String 楼号 主键
		// 2 meter String 表号 主键
		// 3 funcid Long 功能号 主键
		// 4 system String 系统
		// 5 source String 数据来源
		// 6 address_1 String 一级地址 采集器网关ID
		// 7 ip_port String 一级地址IP_PORT 如无端口，可缺省
		// 8 address_2 String 二级地址 总线端口号
		// 9 address_3 String 三级地址 仪表地址
		// 10 address_4 String 四级地址
		// 11 collect_cycle String 采集频率 采集器上传频率
		// 12 data_type String 点位类型 Acc:累积变量；Inst:瞬时变量；Boolv:通断变量；
		// Enumv:状态变量；Step:阶跃变量
		// 13 unit String 单位
		// 14 remark1 String 备注1 预留
		// 15 remark2 String 备注2 预留
		// 16 remark3 String 备注3 预留
		try {
			if (!tableExist(DB, tableName)) {
				Statement stmt = Constant.conn.createStatement();
				stmt.execute("CREATE TABLE IF NOT EXISTS " + DB + "." + tableName + " (`building` varchar(50) NOT NULL,"
						+ "`meter` varchar(50) NOT NULL," + "`funcid` int(11) NOT NULL," + "`system` varchar(50) ,"
						+ "`source` varchar(50) ," + "`address_1` varchar(50) ," + "`ip_port` varchar(50) ,"
						+ "`address_2` varchar(50) ," + "`address_3` varchar(50) ," + "`address_4` varchar(50) ,"
						+ "`collect_cycle` varchar(50) NOT NULL," + "`data_type` varchar(50) NOT NULL,"
						+ "`unit` varchar(50) ," + "`remark1` varchar(50) ," + "`remark2` varchar(50),"
						+ "`remark3` varchar(50) ," + "PRIMARY KEY (`building`,`meter`,`funcid` ),"
						+ "KEY `index_intervaldata` (`building`,`meter`,`funcid`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
				stmt.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 建表-ps_physical_status
	 */
	public static void CreateTable_ps_physical_status(String DB, String tableName) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 address_1 String 一级地址 主键
		// 3 address_2 String 二级地址 主键
		// 4 address_3 String 三级地址 主键
		// 5 address_4 String 四级地址 主键
		// 6 status String 状态 conn/disconn
		// 7 receivetime String 时间 最近通讯时间
		// 8 detail String 详情
		// 9 manual_inspection String 人工检查记录
		try {
			if (!tableExist(DB, tableName)) {
				Statement stmt = Constant.conn.createStatement();
				stmt.execute("CREATE TABLE IF NOT EXISTS " + DB + "." + tableName + " (`project` varchar(50) NOT NULL,"
						+ "`address_1` varchar(50) NOT NULL," + "`address_2` varchar(50) NOT NULL,"
						+ "`address_3` varchar(50) NOT NULL," + "`address_4` varchar(50) NOT NULL,"
						+ "`status` varchar(50) NOT NULL," + "`receivetime` datetime NOT NULL,"
						+ "`detail` varchar(50) ," + "`manual_inspection` varchar(50) ,"
						+ "PRIMARY KEY (`project`,`address_1`,`address_2`,`address_3`,`address_4` ),"
						+ "KEY `index_intervaldata` (`project`,`address_1`,`address_2`,`address_3`,`address_4` )) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
				stmt.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 建表-ps_physical_log
	 */
	public static void CreateTable_ps_physical_log(String DB, String tableName) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 address_1 String 一级地址 主键
		// 3 address_2 String 二级地址 主键
		// 4 address_3 String 三级地址 主键
		// 5 address_4 String 四级地址 主键
		// 6 receivetime String 时间 主键，触发时间
		// 7 function String 通讯功能 主键，conn/disconn
		// 8 manual_inspection String 人工检查记录
		try {
			if (!tableExist(DB, tableName)) {
				Statement stmt = Constant.conn.createStatement();
				stmt.execute("CREATE TABLE IF NOT EXISTS " + DB + "." + tableName + " (`project` varchar(50) NOT NULL,"
						+ "`address_1` varchar(50) NOT NULL," + "`address_2` varchar(50) NOT NULL,"
						+ "`address_3` varchar(50) NOT NULL," + "`address_4` varchar(50) NOT NULL,"
						+ "`receivetime` datetime NOT NULL," + "`function` varchar(50) NOT NULL,"
						+ "`manual_inspection` varchar(50) ,"
						+ "PRIMARY KEY (`project`,`address_1`,`address_2`,`address_3`,`address_4`,`receivetime`,`function` ),"
						+ "KEY `index_intervaldata` (`project`,`address_1`,`address_2`,`address_3`,`address_4`,`receivetime`,`function` )) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
				stmt.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 建表-ps_virtual_point_status
	 */
	public static void CreateTable_ps_virtual_point_status(String DB, String tableName) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 meter String 仪表 主键
		// 3 funcid Long 功能号 主键
		// 4 updown String 上/下行 主键
		// 5 status String 状态
		// 6 receivetime String 时间
		// 7 detail String 详情
		// 8 manual_inspection String 人工检查记录
		try {
			if (!tableExist(DB, tableName)) {
				Statement stmt = Constant.conn.createStatement();
				stmt.execute("CREATE TABLE IF NOT EXISTS " + DB + "." + tableName + " (`project` varchar(50) NOT NULL,"
						+ "`meter` varchar(50) NOT NULL," + "`funcid` int(11) NOT NULL,"
						+ "`updown` varchar(50) NOT NULL," + "`status` varchar(50) NOT NULL,"
						+ "`receivetime` datetime NOT NULL," + "`detail` varchar(50) ,"
						+ "`manual_inspection` varchar(50)," + "PRIMARY KEY (`project`,`meter`,`funcid`,`updown` ),"
						+ "KEY `index_intervaldata` (`project`,`meter`,`funcid`,`updown` )) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
				stmt.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 建表-ps_virtual_point_log
	 */
	public static void CreateTable_ps_virtual_point_log(String DB, String tableName) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 meter String 仪表 主键
		// 3 funcid Long 功能号 主键
		// 4 updown String 上/下行 主键
		// 5 receivetime String 时间 主键
		// 6 seq Long 报文序号 主键
		// 7 function String 通讯功能 主键
		// 8 feedback String 反馈 processing:rcvd(采集软件已收到)
		// processing:sent(采集软件已发出)
		// processing:finish:finish:+采集节点反馈标识(采集软件已收到反馈)
		// finish:finish:+采集节点反馈标识(采集软件已反馈)
		// 其中采集节点反馈标识，分两级，分别为：
		// 1、success/fail
		// 2、如success，该级空；如fail，则该级描述fail原因
		// 9 manual_inspection String 人工检查记录
		try {
			if (!tableExist(DB, tableName)) {
				Statement stmt = Constant.conn.createStatement();
				stmt.execute("CREATE TABLE IF NOT EXISTS " + DB + "." + tableName + " (`project` varchar(50) NOT NULL,"
						+ "`meter` varchar(50) NOT NULL," + "`funcid` int(11) NOT NULL,"
						+ "`updown` varchar(50) NOT NULL," + "`receivetime` datetime NOT NULL,"
						+ "`seq` int(11) NOT NULL," + "`function` varchar(50) NOT NULL,"
						+ "`feedback` varchar(50) NOT NULL," + "`manual_inspection` varchar(50),"
						+ "PRIMARY KEY (`project`,`meter`,`funcid`,`updown`,`receivetime`,`seq`,`function` ),"
						+ "KEY `index_intervaldata` (`project`,`meter`,`funcid`,`updown`,`receivetime`,`seq`,`function` )) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
				stmt.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 建表-ps_virtual_meter_status
	 */
	public static void CreateTable_ps_virtual_meter_status(String DB, String tableName) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 meter String 仪表 主键
		// 3 updown String 上/下行 主键
		// 4 status String 状态
		// 5 receivetime String 时间
		// 6 detail String 详情
		// 7 manual_inspection String 人工检查记录
		try {
			if (!tableExist(DB, tableName)) {
				Statement stmt = Constant.conn.createStatement();
				stmt.execute("CREATE TABLE IF NOT EXISTS " + DB + "." + tableName + " (`project` varchar(50) NOT NULL,"
						+ "`meter` varchar(50) NOT NULL," + "`updown` varchar(50) NOT NULL,"
						+ "`status` varchar(50) NOT NULL," + "`receivetime` datetime NOT NULL,"
						+ "`detail` varchar(50) ," + "`manual_inspection` varchar(50),"
						+ "PRIMARY KEY (`project`,`meter`,`updown` ),"
						+ "KEY `index_intervaldata` (`project`,`meter`,`updown` )) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
				stmt.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 建表-ps_virtual_meter_log
	 */
	public static void CreateTable_ps_virtual_meter_log(String DB, String tableName) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 meter String 仪表 主键
		// 3 updown String 上/下行 主键
		// 4 receivetime String 时间 主键
		// 5 function String 通讯功能 主键
		// 6 manual_inspection String 人工检查记录
		try {
			if (!tableExist(DB, tableName)) {
				Statement stmt = Constant.conn.createStatement();
				stmt.execute("CREATE TABLE IF NOT EXISTS " + DB + "." + tableName + " (`project` varchar(50) NOT NULL,"
						+ "`meter` varchar(50) NOT NULL," + "`updown` varchar(50) NOT NULL,"
						+ "`receivetime` datetime NOT NULL," + "`function` varchar(50) NOT NULL,"
						+ "`manual_inspection` varchar(50) ,"
						+ "PRIMARY KEY (`project`,`meter`,`updown`,`receivetime`,`function` ),"
						+ "KEY `index_intervaldata` (`project`,`meter`,`updown`,`receivetime`,`function` )) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
				stmt.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 建表-ps_virtual_project_status
	 */
	public static void CreateTable_ps_virtual_project_status(String DB, String tableName) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 status String 状态
		// 3 receivetime String 时间
		// 4 detail String 详情
		// 5 manual_inspection String 人工检查记录
		try {
			if (!tableExist(DB, tableName)) {
				Statement stmt = Constant.conn.createStatement();
				stmt.execute("CREATE TABLE IF NOT EXISTS " + DB + "." + tableName + " (`project` varchar(50) NOT NULL,"
						+ "`status` varchar(50) NOT NULL," + "`receivetime` datetime NOT NULL,"
						+ "`detail` varchar(50)," + "`manual_inspection` varchar(50) ," + "PRIMARY KEY (`project`),"
						+ "KEY `index_intervaldata` (`project`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
				stmt.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 建表-ps_virtual_project_log
	 */
	public static void CreateTable_ps_virtual_project_log(String DB, String tableName) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 receivetime String 时间 主键
		// 3 function String 通讯功能 主键
		// 4 manual_inspection String 人工检查记录
		try {
			if (!tableExist(DB, tableName)) {
				Statement stmt = Constant.conn.createStatement();
				stmt.execute("CREATE TABLE IF NOT EXISTS " + DB + "." + tableName + " (`project` varchar(50) NOT NULL,"
						+ "`receivetime` datetime NOT NULL," + "`function` varchar(50) NOT NULL,"
						+ "`manual_inspection` varchar(50)," + "PRIMARY KEY (`project`,`receivetime`,`function` ),"
						+ "KEY `index_intervaldata` (`project`,`receivetime`,`function` )) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
				stmt.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 写fjd_0_buildingcomputetime
	 */
	public static void Insert_fjd_0_buildingcomputetime(String DB, String building, String compute_time,
			String first_compute_time) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 building String 楼号 主键
		// 2 compute_time String 计算时间
		// 3 first_compute_time String 首次计算时间
		try {
			Statement stmt = Constant.conn.createStatement();
			stmt.execute("replace into " + DB + ".fjd_0_buildingcomputetime"
					+ " (`building`,`compute_time`,`first_compute_time` ) VALUES ('" + building + "','" + compute_time
					+ "','" + first_compute_time + "' )");
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 写dy_pointlist
	 */
	public static void Insert_dy_pointlist(String DB, String building, String meter, long funcid, String system,
			String source, String address_1, String ip_port, String address_2, String address_3, String address_4,
			String collect_cycle, String data_type, String unit) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 building String 楼号 主键
		// 2 meter String 表号 主键
		// 3 funcid Long 功能号 主键
		// 4 system String 系统
		// 5 source String 数据来源
		// 6 address_1 String 一级地址 采集器网关ID
		// 7 ip_port String 一级地址IP_PORT 如无端口，可缺省
		// 8 address_2 String 二级地址 总线端口号
		// 9 address_3 String 三级地址 仪表地址
		// 10 address_4 String 四级地址
		// 11 collect_cycle String 采集频率 采集器上传频率
		// 12 data_type String 点位类型 Acc:累积变量；Inst:瞬时变量；Boolv:通断变量；
		// Enumv:状态变量；Step:阶跃变量
		// 13 unit String 单位
		// 14 remark1 String 备注1 预留
		// 15 remark2 String 备注2 预留
		// 16 remark3 String 备注3 预留
		if (addressNull(address_1, address_2, address_3, address_4)) {
			return;
		}
		try {
			Statement stmt = Constant.conn.createStatement();
			stmt.execute("replace into " + DB + ".dy_pointlist"
					+ " (`building`,`meter`,`funcid`,`system`,`source`,`address_1`,`ip_port`,`address_2`,"
					+ "`address_3`,`address_4`,`collect_cycle`,`data_type`,`unit` ) VALUES ('" + building + "','"
					+ meter + "','" + funcid + "','" + system + "','" + source + "','" + address_1 + "','" + ip_port
					+ "','" + address_2 + "','" + address_3 + "','" + address_4 + "','" + collect_cycle + "','"
					+ data_type + "','" + unit + "' )");
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 判断地址是否为空
	 */
	private static boolean addressNull(String address_1, String address_2, String address_3, String address_4) {
		// TODO Auto-generated method stub
		if ("null".equalsIgnoreCase(address_1) || "null".equalsIgnoreCase(address_2)
				|| "null".equalsIgnoreCase(address_3) || "null".equalsIgnoreCase(address_4)) {
			return true;
		}
		return false;
	}

	/**
	 * 写ps_physical_status
	 */
	public static void Insert_ps_physical_status(String DB, String project, String address_1, String address_2,
			String address_3, String address_4, String status, String receivetime, String detail) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 address_1 String 一级地址 主键
		// 3 address_2 String 二级地址 主键
		// 4 address_3 String 三级地址 主键
		// 5 address_4 String 四级地址 主键
		// 6 status String 状态 conn/disconn
		// 7 receivetime String 时间 最近通讯时间
		// 8 detail String 详情
		// 9 manual_inspection String 人工检查记录
		if (addressNull(address_1, address_2, address_3, address_4)) {
			return;
		}
		try {
			Statement stmt = Constant.conn.createStatement();
			stmt.execute("replace into " + DB + ".ps_physical_status"
					+ " (`project`,`address_1`,`address_2` ,`address_3`,`address_4`,`status`,`receivetime`,`detail`,`manual_inspection`) VALUES ('"
					+ project + "','" + address_1 + "','" + address_2 + "','" + address_3 + "','" + address_4 + "','"
					+ status + "','" + receivetime + "','" + detail + "','" + "' )");
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 写ps_physical_log
	 */
	public static void Insert_ps_physical_log(String DB, String project, String address_1, String address_2,
			String address_3, String address_4, String receivetime, String function) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 address_1 String 一级地址 主键
		// 3 address_2 String 二级地址 主键
		// 4 address_3 String 三级地址 主键
		// 5 address_4 String 四级地址 主键
		// 6 receivetime String 时间 主键，触发时间
		// 7 function String 通讯功能 主键，conn/disconn

		if (addressNull(address_1, address_2, address_3, address_4)) {
			return;
		}
		try {
			Statement stmt = Constant.conn.createStatement();
			stmt.execute("replace into " + DB + ".ps_physical_log"
					+ " (`project`,`address_1`,`address_2` ,`address_3`,`address_4`,`receivetime`,`function`) VALUES ('"
					+ project + "','" + address_1 + "','" + address_2 + "','" + address_3 + "','" + address_4 + "','"
					+ receivetime + "','" + function + "' )");
			stmt.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 写ps_virtual_point_status
	 */
	public static void Insert_ps_virtual_point_status(String DB, String project, String meter, long funcid,
			String updown, String status, String receivetime, String detail) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 meter String 仪表 主键
		// 3 funcid Long 功能号 主键
		// 4 updown String 上/下行 主键
		// 5 status String 状态
		// 6 receivetime String 时间
		// 7 detail String 详情
		// 8 manual_inspection String 人工检查记录
		try {
			Statement stmt = Constant.conn.createStatement();
			stmt.execute("replace into " + DB + ".ps_virtual_point_status"
					+ " (`project`,`meter`,`funcid` ,`updown`,`status`,`receivetime`,`detail`,`manual_inspection`) VALUES ('"
					+ project + "','" + meter + "','" + funcid + "','" + updown + "','" + status + "','" + receivetime
					+ "','" + detail + "','" + "' )");
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 写ps_virtual_point_log
	 */
	public static void Insert_ps_virtual_point_log(String DB, String project, String meter, long funcid, String updown,
			String receivetime, long seq, String function, String feedback) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 meter String 仪表 主键
		// 3 funcid Long 功能号 主键
		// 4 updown String 上/下行 主键
		// 5 receivetime String 时间 主键
		// 6 seq Long 报文序号 主键
		// 7 function String 通讯功能 主键
		// 8 feedback String 反馈 processing:rcvd(采集软件已收到)
		// processing:sent(采集软件已发出)
		// processing:finish:finish:+采集节点反馈标识(采集软件已收到反馈)
		// finish:finish:+采集节点反馈标识(采集软件已反馈)
		// 其中采集节点反馈标识，分两级，分别为：
		// 1、success/fail
		// 2、如success，该级空；如fail，则该级描述fail原因
		try {
			Statement stmt = Constant.conn.createStatement();
			stmt.execute("replace into " + DB + ".ps_virtual_point_log"
					+ " (`project`,`meter`,`funcid` ,`updown`,`receivetime`,`seq`,`function`,`feedback`) VALUES ('"
					+ project + "','" + meter + "','" + funcid + "','" + updown + "','" + receivetime + "','" + seq
					+ "','" + function + "','" + feedback + "' )");
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 写ps_virtual_meter_status
	 */
	public static void Insert_ps_virtual_meter_status(String DB, String project, String meter, String updown,
			String status, String receivetime, String detail) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 meter String 仪表 主键
		// 3 updown String 上/下行 主键
		// 4 status String 状态
		// 5 receivetime String 时间
		// 6 detail String 详情
		// 7 manual_inspection String 人工检查记录
		try {
			Statement stmt = Constant.conn.createStatement();
			stmt.execute("replace into " + DB + ".ps_virtual_meter_status"
					+ " (`project`,`meter`,`updown`,`status`,`receivetime`,`detail`,`manual_inspection`) VALUES ('"
					+ project + "','" + meter + "','" + updown + "','" + status + "','" + receivetime + "','" + detail
					+ "','" + "' )");
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 写ps_virtual_meter_log
	 */
	public static void Insert_ps_virtual_meter_log(String DB, String project, String meter, String updown,
			String receivetime, String function) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 meter String 仪表 主键
		// 3 updown String 上/下行 主键
		// 4 receivetime String 时间 主键
		// 5 function String 通讯功能 主键
		try {
			Statement stmt = Constant.conn.createStatement();
			stmt.execute("replace into " + DB + ".ps_virtual_meter_log"
					+ " (`project`,`meter`,`updown`,`receivetime`,`function`) VALUES ('" + project + "','" + meter
					+ "','" + updown + "','" + receivetime + "','" + function + "' )");
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 写ps_virtual_project_status
	 */
	public static void Insert_ps_virtual_project_status(String DB, String project, String status, String receivetime,
			String detail) throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 status String 状态
		// 3 receivetime String 时间
		// 4 detail String 详情
		// 5 manual_inspection String 人工检查记录
		try {
			Statement stmt = Constant.conn.createStatement();
			stmt.execute("replace into " + DB + ".ps_virtual_project_status"
					+ " (`project`,`status`,`receivetime`,`detail`,`manual_inspection`) VALUES ('" + project + "','"
					+ status + "','" + receivetime + "','" + detail + "','" + "' )");
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 写ps_virtual_project_log
	 */
	public static void Insert_ps_virtual_project_log(String DB, String project, String receivetime, String function)
			throws Exception {
		// 序号 名称 数据类型 释义 备注
		// 1 project String 项目名 主键
		// 2 receivetime String 时间 主键
		// 3 function String 通讯功能 主键
		try {
			Statement stmt = Constant.conn.createStatement();
			stmt.execute(
					"replace into " + DB + ".ps_virtual_project_log" + " (`project`,`receivetime`,`function`) VALUES ('"
							+ project + "','" + receivetime + "','" + function + "' )");
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 写数据
	 */
	public static void BatchInsertUpload(String DB, List<String> tableList, List<Record> dataList) throws Exception {
		if (Constant.connUpload == null) {
			return;
		}
		try {
			Statement stmt = Constant.connUpload.createStatement();
			for (int i = 0; i < dataList.size(); i++) {
				Record data = dataList.get(i);

				List<String> tb_uploads = new ArrayList<String>();
				for (String upload : tableList) {
					tb_uploads.add(upload + "_" + data.buildingSign);
				}
				CreateTable_upload(DB, tb_uploads);
				for (String tb_upload : tb_uploads) {
					stmt.execute("replace into " + DB + "." + tb_upload
							+ " (`meter`,`funcid`,`receivetime`,`data`) VALUES ('" + data.meterSign + "','"
							+ data.funcID + "','" + Constant.format(data.receivetime) + "','" + data.data + "' )");
				}
			}
			stmt.close();
		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.error("BatchInsertUpload.Exception: " + e.toString());
		}
	}

	/**
	 * 写数据
	 */
	public static void BatchInsert(String DB, List<Record> dataList) throws Exception {
		try {
			Statement stmt = Constant.conn.createStatement();
			for (int i = 0; i < dataList.size(); i++) {
				Record data = dataList.get(i);
				String tb_month = Constant.format_month(data.receivetime) + "_" + "monthdata" + "_" + data.buildingSign;
				String tb_present = "presentdata" + "_" + data.buildingSign;
				String tb_detail = "detaildata" + "_" + data.buildingSign;
				// List<String> tb_uploads = new ArrayList<String>();
				// for (String upload : Constant.uploadList) {
				// tb_uploads.add(upload + "_" + data.buildingSign);
				// }
				CreateTable(DB, tb_month);
				// CreateTable_upload(DB, tb_uploads);
				CreateTable_present(DB, tb_present);
				CreateTable_detail(DB, tb_detail);
				stmt.execute("replace into " + DB + "." + tb_month
						+ " (`meter`,`funcid`,`receivetime`,`data`) VALUES ('" + data.meterSign + "','" + data.funcID
						+ "','" + Constant.format(data.receivetime) + "','" + data.data + "' )");
				// for (String tb_upload : tb_uploads) {
				// stmt.execute("replace into " + DB + "." + tb_upload
				// + " (`meter`,`funcid`,`receivetime`,`data`) VALUES ('" +
				// data.meterSign + "','"
				// + data.funcID + "','" + Constant.format(data.receivetime) +
				// "','" + data.data + "' )");
				// }
				stmt.execute("replace into " + DB + "." + tb_present
						+ " (`meter`,`funcid`,`receivetime`,`data`) VALUES ('" + data.meterSign + "','" + data.funcID
						+ "','" + Constant.format(data.receivetime) + "','" + data.data + "' )");
				stmt.execute("replace into " + DB + "." + tb_detail
						+ " (`meter`,`funcid`,`seq`,`receivetime`,`logtime`,`data`) VALUES ('" + data.meterSign + "','"
						+ data.funcID + "','"
						+ Constant.getSeq(data.buildingSign + "-" + data.meterSign + "-" + data.funcID) + "','"
						+ Constant.format(data.receivetime) + "','" + Constant.format(new Date()) + "','" + data.data
						+ "' )");

				Constant.writeDB++;
			}
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			LogUtil.error("BatchInsert.Exception: " + e.toString());
			// Constant.DBConn = false;
		}
	}

	/**
	 * 写Msg数据Month
	 */
	public static void MsgBatchInsertMonth(String DB, List<RecordMsg> dataList) throws Exception {
		try {
			Statement stmt = Constant.conn.createStatement();
			for (int i = 0; i < dataList.size(); i++) {
				RecordMsg data = dataList.get(i);
				String tb_month = "zillion_data_original_str_data";
				stmt.execute("replace into " + DB + "." + tb_month
						+ " (`building`,`meter`,`funcid`,`receivetime`,`data`) VALUES ('" + data.buildingSign + "','"
						+ data.meterSign + "','" + data.funcID + "','" + Constant.format(data.receivetime) + "','"
						+ data.data + "' )");
			}
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			LogUtil.error("MsgBatchInsert.Exception: " + e.toString());
			// Constant.DBConn = false;
		}
	}

	/**
	 * 写Msg数据Present
	 */
	public static void MsgBatchInsertPresent(String DB, List<RecordMsg> dataList) throws Exception {
		try {
			Statement stmt = Constant.conn.createStatement();
			for (int i = 0; i < dataList.size(); i++) {
				RecordMsg data = dataList.get(i);
				String tb_present = "zillion_data_original_str_data_present";

				stmt.execute("replace into " + DB + "." + tb_present
						+ " (`building`,`meter`,`funcid`,`receivetime`,`data`) VALUES ('" + data.buildingSign + "','"
						+ data.meterSign + "','" + data.funcID + "','" + Constant.format(data.receivetime) + "','"
						+ data.data + "' )");
			}
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			LogUtil.error("MsgBatchInsert.Exception: " + e.toString());
			// Constant.DBConn = false;
		}
	}

	// /**
	// * 更新Msg数据Present
	// */
	// public static void MsgBatchUpdatePresent(String DB, List<RecordMsg>
	// dataList) throws Exception {
	// try {
	// Statement stmt = Constant.conn.createStatement();
	// for (int i = 0; i < dataList.size(); i++) {
	// RecordMsg data = dataList.get(i);
	// String tb_present = "zillion_data_original_str_data_present";
	//
	// stmt.execute("update " + DB + "." + tb_present + " set data='" +
	// data.data + "' where meter='"
	// + data.meterSign + "'");
	// }
	// stmt.close();
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// LogUtil.error("MsgBatchInsert.Exception: " + e.toString());
	// // Constant.DBConn = false;
	// }
	// }

	/**
	 * 查询Msg数据Present
	 */
	public static String MsgBatchSelectPresent(String DB, RecordMsg data) throws Exception {
		String result = null;
		try {
			String tb_present = "zillion_data_original_str_data_present";
			Statement stmt = Constant.conn.createStatement();
			ResultSet rs = stmt.executeQuery("select data from " + DB + "." + tb_present + " where meter='"
					+ data.meterSign + "' and funcid='" + data.funcID + "'");
			while (rs.next()) {
				System.out.println(rs.getString("data"));
				result = rs.getString("data");
			}
			rs.close();
			stmt.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			LogUtil.error("MsgBatchInsert.Exception: " + e.toString());
			// Constant.DBConn = false;
		}
		return result;
	}

	/**
	 * 查询Msg数据Present
	 */
	public static String MsgBatchSelectPresentByID(String DB, RecordMsg data) throws Exception {
		String result = null;
		try {
			String tb_present = "zillion_data_original_str_data_present";
			Statement stmt = Constant.conn.createStatement();
			ResultSet rs = stmt.executeQuery(
					"select meter from " + DB + "." + tb_present + " where data like '%" + data.data + "%' ");
			while (rs.next()) {
				System.out.println(rs.getString("meter"));
				result = rs.getString("meter");
			}
			rs.close();
			stmt.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			LogUtil.error("MsgBatchInsert.Exception: " + e.toString());
			// Constant.DBConn = false;
		}
		return result;
	}

	/**
	 * 写Msg数据
	 */
	public static void MsgBatchInsert(String DB, List<RecordMsg> dataList) throws Exception {
		try {
			Statement stmt = Constant.conn.createStatement();
			for (int i = 0; i < dataList.size(); i++) {
				RecordMsg data = dataList.get(i);
				String tb_month = "zillion_data_original_str_data";
				String tb_present = "zillion_data_original_str_data_present";

				stmt.execute("replace into " + DB + "." + tb_month
						+ " (`building`,`meter`,`funcid`,`receivetime`,`data`) VALUES ('" + data.buildingSign + "','"
						+ data.meterSign + "','" + data.funcID + "','" + Constant.format(data.receivetime) + "','"
						+ data.data + "' )");
				stmt.execute("replace into " + DB + "." + tb_present
						+ " (`building`,`meter`,`funcid`,`receivetime`,`data`) VALUES ('" + data.buildingSign + "','"
						+ data.meterSign + "','" + data.funcID + "','" + Constant.format(data.receivetime) + "','"
						+ data.data + "' )");
			}
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			LogUtil.error("MsgBatchInsert.Exception: " + e.toString());
			// Constant.DBConn = false;
		}
	}

	/**
	 * 删Msg数据
	 */
	public static void MsgBatchDelte(String DB, List<RecordMsg> dataList) throws Exception {
		try {
			Statement stmt = Constant.conn.createStatement();
			for (int i = 0; i < dataList.size(); i++) {
				RecordMsg data = dataList.get(i);
				String tb_month = "zillion_data_original_str_data";
				String tb_present = "zillion_data_original_str_data_present";
				stmt.execute("replace into " + DB + "." + tb_month
						+ " (`building`,`meter`,`funcid`,`receivetime`,`data`) VALUES ('" + data.buildingSign + "','"
						+ data.meterSign + "','" + data.funcID + "','" + Constant.format(data.receivetime) + "','"
						+ data.data + "' )");
				stmt.execute("delete from " + DB + "." + tb_present + " where building='" + data.buildingSign
						+ "' AND meter='" + data.meterSign + "' AND funcid='" + data.funcID + "'");
			}
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			LogUtil.error("MsgBatchDelte.Exception: " + e.toString());
			// Constant.DBConn = false;
		}
	}

	/**
	 * 写指令记录-sent
	 */
	public static void BatchInsert_Set1(String DB, PointSet ps) throws Exception {
		try {
			Statement stmt = Constant.conn.createStatement();
			LogUtil.info("insert into " + DB + "." + "originalset&" + "originalset_present ");
			stmt.execute("replace into " + DB + ".originalset"
					+ " (`building`,`meter`,`funcid`,`receivetime`,`data`,`status`) VALUES ('" + ps.buildingSignUP
					+ ps.meterSign + "','" + ps.funcID + "','" + Constant.format(new Date((long) ps.sendTime)) + "','"
					+ ps.dataSet + "','" + "processing:sent" + "' )");
			stmt.execute("replace into " + DB + ".originalset_present"
					+ " (`building`,`meter`,`funcid`,`receivetime`,`data`,`status`) VALUES ('" + ps.buildingSignUP
					+ ps.meterSign + "','" + ps.funcID + "','" + Constant.format(new Date((long) ps.sendTime)) + "','"
					+ ps.dataSet + "','" + "processing:sent" + "' )");
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			LogUtil.error("BatchInsert_Set1.Exception: " + e.toString());
		}
	}

	/**
	 * 写指令记录-其他状态
	 */
	public static void BatchInsert_Set2(String DB, PointSet ps) throws Exception {
		try {
			Statement stmt = Constant.conn.createStatement();
			LogUtil.info("insert into " + DB + "." + "originalset&" + "originalset_present ");
			stmt.execute("replace into " + DB + ".originalset"
					+ " (`building`,`meter`,`funcid`,`receivetime`,`data`,`status`,`endtime`) VALUES ('"
					+ ps.buildingSignUP + ps.meterSign + "','" + ps.funcID + "','"
					+ Constant.format(new Date((long) ps.sendTime)) + "','" + ps.dataSet + "','" + "finish:finish:"
					+ ps.status + "','" + Constant.format(new Date()) + "' )");
			stmt.execute("replace into " + DB + ".originalset_present"
					+ " (`building`,`meter`,`funcid`,`receivetime`,`data`,`status`,`endtime`) VALUES ('"
					+ ps.buildingSignUP + ps.meterSign + "','" + ps.funcID + "','"
					+ Constant.format(new Date((long) ps.sendTime)) + "','" + ps.dataSet + "','" + "finish:finish:"
					+ ps.status + "','" + Constant.format(new Date()) + "' )");
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			LogUtil.error("BatchInsert_Set2.Exception: " + e.toString());
		}
	}

	/**
	 * 写指令记录-timeout
	 */
	public static void BatchInsert_Set3(String DB, PointSet ps) throws Exception {
		try {
			Statement stmt = Constant.conn.createStatement();
			LogUtil.info("insert into " + DB + "." + "originalset&" + "originalset_present ");
			stmt.execute("replace into " + DB + ".originalset"
					+ " (`building`,`meter`,`funcid`,`receivetime`,`data`,`status`,`endtime`) VALUES ('"
					+ ps.buildingSignUP + ps.meterSign + "','" + ps.funcID + "','"
					+ Constant.format(new Date((long) ps.sendTime)) + "','" + ps.dataSet + "','" + "finish:finish:"
					+ "fail:timeout" + "','" + Constant.format(new Date()) + "' )");
			stmt.execute("replace into " + DB + ".originalset_present"
					+ " (`building`,`meter`,`funcid`,`receivetime`,`data`,`status`,`endtime`) VALUES ('"
					+ ps.buildingSignUP + ps.meterSign + "','" + ps.funcID + "','"
					+ Constant.format(new Date((long) ps.sendTime)) + "','" + ps.dataSet + "','" + "finish:finish:"
					+ "fail:timeout" + "','" + Constant.format(new Date()) + "' )");
			stmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			LogUtil.error("BatchInsert_Set3.Exception: " + e.toString());
		}
	}
}
