package com.steering.pss.plnsaleord.base;

import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.tools.EntityTool;
import net.sf.json.JSONObject;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 生产计划公共类
 * 
 * @author peng
 * 
 */
public class PlnHelper {
	/**
	 * 查询实体类
	 * 
	 * @param con
	 *            连接字符串
	 * @param params
	 *            参数数组(按顺序)
	 * @param sql
	 *            SQL ID
	 * @param supSql
	 *            补充SQL
	 * @param cls
	 *            实体类
	 * @return
	 * @throws SQLException
	 */
	public static <T> T getEntityData(Connection con, String[] params,
			String sqlId, String supSql, Class<T> cls) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql(sqlId);
		sql = sql + "  " + supSql;
		PreparedStatement ps = con.prepareStatement(sql);
		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++) {
				ps.setObject(i + 1, params[i]);
			}
		}
		ResultSet rs = ps.executeQuery();
		T t = EntityTool.getEntityByRs(rs, cls);
		rs.close();
		ps.close();
		return t;
	}

	/**
	 * 查询实体类
	 * 
	 * @param con
	 *            连接字符串
	 * @param params
	 *            参数数组(按顺序)
	 * @param sql
	 *            SQL ID
	 * @param cls
	 *            实体类
	 * @return
	 * @throws SQLException
	 */
	public static <T> T getEntityData(Connection con, String[] params,
			String sqlId, Class<T> cls) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql(sqlId);
		PreparedStatement ps = con.prepareStatement(sql);
		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++) {
				ps.setObject(i + 1, params[i]);
			}
		}
		ResultSet rs = ps.executeQuery();
		T t = EntityTool.getEntityByRs(rs, cls);
		rs.close();
		ps.close();
		return t;
	}

	/**
	 * 查询实体类集合
	 * 
	 * @param con
	 *            连接字符串
	 * @param params
	 *            参数数组(按顺序)
	 * @param sql
	 *            SQLID
	 * @param supSql
	 *            补充SQL
	 * @param cls
	 *            实体类
	 * @return
	 * @throws SQLException
	 */
	public static <T> List<T> getEntityDatas(Connection con, String[] params,
			String sqlId, String supSql, Class<T> cls) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql(sqlId);
		sql = sql + "  " + supSql;
		PreparedStatement ps = con.prepareStatement(sql);
		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++) {
				ps.setObject(i + 1, params[i]);
			}
		}
		ResultSet rs = ps.executeQuery();
		List<T> t = EntityTool.getEntitysByRs(rs, cls);
		rs.close();
		ps.close();
		return t;
	}

	/**
	 * 新增实体类
	 *
	 * @param con
	 * @param sqlId
	 *            SQL ID
	 * @param obj
	 *            实体类
	 * @throws SQLException
	 */
	public static void addEntityData(Connection con, String sqlId, Object obj)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql(sqlId);
		sql = EntityTool.fillSqlByEntity(sql, obj);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 查询实体类集合
	 *
	 * @param con
	 *            连接字符串
	 * @param params
	 *            参数数组(按顺序)
	 * @param sql
	 *            SQLID
	 * @param cls
	 *            实体类
	 * @return
	 * @throws SQLException
	 */
	public static <T> List<T> getEntityDatas(Connection con, String[] params,
											 String sqlId, Class<T> cls) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql(sqlId);
		PreparedStatement ps = con.prepareStatement(sql);
		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++) {
				ps.setObject(i + 1, params[i]);
			}
		}
		ResultSet rs = ps.executeQuery();
		List<T> t = EntityTool.getEntitysByRs(rs, cls);
		rs.close();
		ps.close();
		return t;
	}

	/**
	 * 删除数据
	 * 
	 * @param con
	 * @param sqlId
	 * @param supSql
	 * @param params
	 * @throws SQLException
	 */
	public static void deleteOrUpdate(Connection con, String sqlId,
			String supSql, Object[] params) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql(sqlId);
		sql = sql + "  " + supSql;
		PreparedStatement ps = con.prepareStatement(sql);
		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++) {
				ps.setObject(i + 1, params[i]);
			}
		}
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除数据
	 * 
	 * @param con
	 * @param sqlId
	 * @param params
	 * @throws SQLException
	 */
	public static void deleteOrUpdate(Connection con, String sqlId,
			Object[] params) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql(sqlId);
		PreparedStatement ps = con.prepareStatement(sql);
		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++) {
				ps.setObject(i + 1, params[i]);
			}
		}
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 根据实体类修改或者删除
	 * 
	 * @param con
	 * @param sqlId
	 * @param obj
	 * @throws SQLException
	 */
	public static void deleteOrUpdateByEntity(Connection con, String sqlId,
			Object obj) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql(sqlId);
		sql = EntityTool.fillSqlByEntity(sql, obj);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * json转化为实体类
	 * 
	 * @param c
	 *            实体类类型
	 * @param jsonStr
	 *            字符串
	 * @return T
	 */
	public static <T> T jsonToEntity(Class<?> c, String jsonStr) {
		if (jsonStr.equals(""))
			return null;
		JSONObject obj = JSONObject.fromObject(jsonStr);
		@SuppressWarnings("unchecked")
		T t = (T) JSONObject.toBean(obj, c);
		return t;
	}

	/**
	 * json集合转化实体类集合
	 * 
	 * @param c
	 *            实体类类型
	 * @param jsonList
	 *            字符串集合
	 * @return <T>
	 */
	public static <T> List<T> jsonsToEntityList(Class<?> c,
			List<String> jsonList) {
		if (jsonList == null || jsonList.size() == 0)
			return null;
		List<T> list = new ArrayList<T>();
		for (String str : jsonList) {
			T t = PlnHelper.jsonToEntity(c, str);
			list.add(t);
		}
		return list;
	}

	/**
	 * 查询数组
	 * 
	 * @param con
	 * @param sqlId
	 *            SQLID
	 * @param supSql
	 *            补充sql
	 * @param inParams
	 *            传入参数
	 * @param outParams
	 *            要查询的字段(数据库的字段)
	 * @return String[]
	 * @throws SQLException
	 */
	public static String[] getArray(Connection con, String sqlId,
			String supSql, String[] inParams, String[] outParams)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql(sqlId);
		sql = sql + "  " + supSql;
		PreparedStatement ps = con.prepareStatement(sql);
		if (inParams != null && inParams.length > 0) {
			for (int i = 0; i < inParams.length; i++) {
				ps.setObject(i + 1, inParams[i]);
			}
		}
		String[] arr = null;
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			arr = new String[outParams.length];
			for (int i = 0; i < outParams.length; i++) {
				arr[i] = rs.getString(outParams[i]);
			}
		}
		rs.close();
		ps.close();
		return arr;
	}

	/**
	 * 查询数组
	 * 
	 * @param con
	 * @param sqlId
	 *            SQLID
	 * @param supSql
	 *            补充sql
	 * @param inParams
	 *            传入参数
	 * @param outParams
	 *            要查询的字段(数据库的字段)
	 * @return String[]
	 * @throws SQLException
	 */
	public static String[] getArray(Connection con, String sqlId,
			String[] inParams, String[] outParams) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql(sqlId);
		PreparedStatement ps = con.prepareStatement(sql);
		if (inParams != null && inParams.length > 0) {
			for (int i = 0; i < inParams.length; i++) {
				ps.setObject(i + 1, inParams[i]);
			}
		}
		String[] arr = null;
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			arr = new String[outParams.length];
			for (int i = 0; i < outParams.length; i++) {
				arr[i] = rs.getString(outParams[i]);
			}
		}
		rs.close();
		ps.close();
		return arr;
	}

	/**
	 * 查询单个字段
	 * 
	 * @param con
	 * @param sqlId
	 * @param supSql
	 * @param inParams
	 * @param outParam
	 * @return
	 * @throws SQLException
	 */
	public static String getParam(Connection con, String sqlId, String supSql,
			String[] inParams, String outParam) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql(sqlId);
		sql = sql + "  " + supSql;
		PreparedStatement ps = con.prepareStatement(sql);
		if (inParams != null && inParams.length > 0) {
			for (int i = 0; i < inParams.length; i++) {
				ps.setObject(i + 1, inParams[i]);
			}
		}
		String str = "";
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			str = rs.getString(outParam);
		}
		rs.close();
		ps.close();
		return str;
	}

	/**
	 * 查询单个字段
	 * 
	 * @param con
	 * @param sqlId
	 * @param inParams
	 * @param outParam
	 * @return
	 * @throws SQLException
	 */
	public static String getParam(Connection con, String sqlId,
			String[] inParams, String outParam) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql(sqlId);
		PreparedStatement ps = con.prepareStatement(sql);
		if (inParams != null && inParams.length > 0) {
			for (int i = 0; i < inParams.length; i++) {
				ps.setObject(i + 1, inParams[i]);
			}
		}
		String str = "";
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			str = rs.getString(outParam);
		}
		rs.close();
		ps.close();
		return str;
	}

	/**
	 * 查询单个字段集合
	 * 
	 * @param con
	 * @param sqlId
	 * @param supSql
	 * @param inParams
	 * @param outParam
	 * @return
	 * @throws SQLException
	 */
	public static List<String> getParams(Connection con, String sqlId,
			String supSql, String[] inParams, String outParam)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql(sqlId);
		sql = sql + "  " + supSql;
		PreparedStatement ps = con.prepareStatement(sql);
		if (inParams != null && inParams.length > 0) {
			for (int i = 0; i < inParams.length; i++) {
				ps.setObject(i + 1, inParams[i]);
			}
		}
		List<String> list = new ArrayList<String>();
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			list.add(rs.getString(outParam));
		}
		rs.close();
		ps.close();
		return list;
	}

	/**
	 * 查询单个字段集合
	 * 
	 * @param con
	 * @param sqlId
	 * @param inParams
	 * @param outParam
	 * @return
	 * @throws SQLException
	 */
	public static List<String> getParams(Connection con, String sqlId,
			String[] inParams, String outParam) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql(sqlId);
		PreparedStatement ps = con.prepareStatement(sql);
		if (inParams != null && inParams.length > 0) {
			for (int i = 0; i < inParams.length; i++) {
				ps.setObject(i + 1, inParams[i]);
			}
		}
		List<String> list = new ArrayList<String>();
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			list.add(rs.getString(outParam));
		}
		rs.close();
		ps.close();
		return list;
	}

	/**
	 * 查询数组集合集合
	 * 
	 * @param con
	 * @param sqlId
	 *            SQLID
	 * @param supSql
	 *            补充sql
	 * @param inParams
	 *            传入参数
	 * @param outParams
	 *            要查询的字段(数据库的字段)
	 * @return String[]
	 * @throws SQLException
	 */
	public static List<String[]> getArrays(Connection con, String sqlId,
			String supSql, String[] inParams, String[] outParams)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql(sqlId);
		sql = sql + "  " + supSql;
		PreparedStatement ps = con.prepareStatement(sql);
		if (inParams != null && inParams.length > 0) {
			for (int i = 0; i < inParams.length; i++) {
				ps.setObject(i + 1, inParams[i]);
			}
		}
		List<String[]> list = new ArrayList<String[]>();
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			String[] arr = new String[outParams.length];
			for (int i = 0; i < outParams.length; i++) {
				arr[i] = rs.getString(outParams[i]);
			}
			list.add(arr);
		}
		rs.close();
		ps.close();
		return list;
	}

	/**
	 * 查询数组集合集合
	 * 
	 * @param con
	 * @param sqlId
	 *            SQLID
	 * @param inParams
	 *            传入参数
	 * @param outParams
	 *            要查询的字段(数据库的字段)
	 * @return String[]
	 * @throws SQLException
	 */
	public static List<String[]> getArrays(Connection con, String sqlId,
			String[] inParams, String[] outParams) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql(sqlId);
		PreparedStatement ps = con.prepareStatement(sql);
		if (inParams != null && inParams.length > 0) {
			for (int i = 0; i < inParams.length; i++) {
				ps.setObject(i + 1, inParams[i]);
			}
		}
		List<String[]> list = new ArrayList<String[]>();
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			String[] arr = new String[outParams.length];
			for (int i = 0; i < outParams.length; i++) {
				arr[i] = rs.getString(outParams[i]);
			}
			list.add(arr);
		}
		rs.close();
		ps.close();
		return list;
	}

	/**
	 * 批量执行删除或者修改
	 * 
	 * @param con
	 * @param sqlId
	 * @param supSql
	 * @param paramsList
	 * @throws SQLException
	 */
	public static void batchDeleteOrUpdate(Connection con, String sqlId,
			String supSql, List<Object[]> paramsList) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql(sqlId);
		sql += " " + supSql;
		PreparedStatement ps = con.prepareStatement(sql);
		if (paramsList == null || paramsList.size() == 0) {
			ps.executeUpdate();
			ps.close();
			return;
		}
		int batch = 1000;
		int count = 0;
		for (int i = 0; i < paramsList.size(); i++) {
			Object[] params = paramsList.get(i);
			if (params != null && params.length > 0) {
				for (int j = 0; j < params.length; j++) {
					ps.setObject(j + 1, params[j]);
				}
			}
			ps.addBatch();
			if (count++ % batch == 0) {
				ps.executeBatch();
			}
		}
		ps.executeBatch();
		ps.close();
	}

	/**
	 * 批量执行删除或者修改
	 * 
	 * @param con
	 * @param sqlId
	 * @param supSql
	 * @param paramsList
	 * @throws SQLException
	 */
	public static void batchDeleteOrUpdate(Connection con, String sqlId,
			List<Object[]> paramsList) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql(sqlId);
		PreparedStatement ps = con.prepareStatement(sql);
		if (paramsList == null || paramsList.size() == 0) {
			ps.executeUpdate();
			ps.close();
			return;
		}
		int batch = 1000;
		int count = 0;
		for (int i = 0; i < paramsList.size(); i++) {
			Object[] params = paramsList.get(i);
			if (params != null && params.length > 0) {
				for (int j = 0; j < params.length; j++) {
					ps.setObject(j + 1, params[j]);
				}
			}
			ps.addBatch();
			if (count++ % batch == 0) {
				ps.executeBatch();
			}
		}
		ps.executeBatch();
		ps.close();
	}

}
