package com.weaver.seconddev.interfaces.htjt.util;

import java.util.Iterator;
import java.util.Map;

import com.weaver.common.distribution.genid.IdGenerator;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.verupgrade.conn.CONN_TYPE;
import java.lang.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import weaver.conn.ConnStatementNew.D;
import com.weaver.verupgrade.conn.ConnStatement;
import com.weaver.verupgrade.conn.RecordSet;
import com.weaver.verupgrade.conn.RecordSetDataSource;
import com.weaver.verupgrade.general.BaseBean;
import com.weaver.verupgrade.general.Util;

/**
 */
public class InsertUtil {

	private static final org.slf4j.Logger logger_110ff965 = LoggerFactory.getLogger(InsertUtil.class);

	BaseBean log = new BaseBean();

	public boolean insertold(Map<String, String> mapStr, String table) {
		if (mapStr == null) {
			return false;
		}
		if (mapStr.isEmpty()) {
			return false;
		}
		RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
		String sql_0 = "insert into " + table + "(";
		StringBuffer sql_1 = new StringBuffer();
		String sql_2 = ") values(";
		StringBuffer sql_3 = new StringBuffer();
		String sql_4 = ")";
		Iterator<String> it = mapStr.keySet().iterator();
		while (it.hasNext()) {
			String tmp_1 = it.next();
			// String tmp_1_str = Util.null2String(mapStr.get(tmp_1));
			String tmp_1_str = mapStr.get(tmp_1);
			if (tmp_1_str == null) {
				tmp_1_str = "";
			}
			if (tmp_1_str.length() > 0) {
				sql_1.append(tmp_1);
				sql_1.append(",");
				if (tmp_1_str.contains("##")) {
					sql_3.append(tmp_1_str.replace("##", ""));
					sql_3.append(",");
				} else {
					sql_3.append("'");
					sql_3.append(tmp_1_str);
					sql_3.append("',");
				}
			}
		}
		String now_sql_1 = sql_1.toString();
		if (now_sql_1.lastIndexOf(",") > 0) {
			now_sql_1 = now_sql_1.substring(0, now_sql_1.length() - 1);
		}
		String now_sql_3 = sql_3.toString();
		if (now_sql_3.lastIndexOf(",") > 0) {
			now_sql_3 = now_sql_3.substring(0, now_sql_3.length() - 1);
		}
		String sql = sql_0 + now_sql_1 + sql_2 + now_sql_3 + sql_4;
		logger_110ff965.error(String.valueOf("## sql = " + sql));
		String poolname = CONN_TYPE.workflow.getType();
        /* return false;
 *TODO  检测到insert语句 -> E9插入sql迁移到E10需要特殊处理请参考文档进行调整  https://www.e-cology.com.cn/sp/doc/docShare/1027709301045985331?tenantKey=t7akvdnf84
 TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源*/
      long generate = IdGenerator.generate();
		return rs.executeSql(sql, poolname);
	}

	public boolean insert(Map<String, String> mapStr, String table) {
		if (mapStr == null) {
			return false;
		}
		if (mapStr.isEmpty()) {
			return false;
		}
		String id = "";
		String sql_0 = "insert into " + table + "(";
		StringBuffer sql_1 = new StringBuffer();
		String sql_2 = ") values(";
		StringBuffer sql_3 = new StringBuffer();
		String sql_4 = ")";
		Iterator<String> it = mapStr.keySet().iterator();
		while (it.hasNext()) {
			String tmp_1 = it.next();
			// String tmp_1_str = Util.null2String(mapStr.get(tmp_1));
			String tmp_1_str = mapStr.get(tmp_1);
			if (tmp_1_str == null) {
				tmp_1_str = "";
			}
			if (tmp_1.length() > 0) {
				sql_1.append(tmp_1);
				sql_1.append(",");
				sql_3.append("?");
				sql_3.append(",");
			}
		}
		String now_sql_1 = sql_1.toString();
		if (now_sql_1.lastIndexOf(",") > 0) {
			now_sql_1 = now_sql_1.substring(0, now_sql_1.length() - 1);
		}
		String now_sql_3 = sql_3.toString();
		if (now_sql_3.lastIndexOf(",") > 0) {
			now_sql_3 = now_sql_3.substring(0, now_sql_3.length() - 1);
		}
		String setvalues = "";
		String sql = sql_0 + now_sql_1 + sql_2 + now_sql_3 + sql_4;
		ConnStatement cs = I18nContextUtil.getBean(ConnStatement.class);
		try {
			String poolname_x1 = "";
			// TODO E10请传入数据源名称
			cs.setStatementSql(sql, poolname_x1);
			it = mapStr.keySet().iterator();
			int num = 1;
			while (it.hasNext()) {
				String tmp_1 = it.next();
				// String tmp_1_str = Util.null2String(mapStr.get(tmp_1));
				String tmp_1_str = mapStr.get(tmp_1);
				if (tmp_1_str == null) {
					tmp_1_str = "";
				}
				if (tmp_1.length() < 0) {
					continue;
				}
				setvalues = setvalues + "," + tmp_1_str;
				if ("".equals(tmp_1_str)) {
					cs.setString(num, null);
				} else {
					cs.setString(num, tmp_1_str);
				}
				// log.writeLog("## setvalues = " + setvalues+" num:"+num+" tmp_1:"+tmp_1);
				num++;
			}
			cs.executeUpdate();
			cs.close();
		} catch (Exception e) {
			logger_110ff965.error(String.valueOf(e));
			logger_110ff965.error(String.valueOf("## sql = " + sql));
			logger_110ff965.error(String.valueOf("++++++++++++++++++++++++++++++" + mapStr));
			cs.close();
			return false;
		}
		// return false;
		return true;
	}

	public String insertBackId(Map<String, String> mapStr, String table, String removefield) {
		if (mapStr == null) {
			return "";
		}
		if (mapStr.isEmpty()) {
			return "";
		}
		String id = "";
		String sql_0 = "insert into " + table + "(";
		StringBuffer sql_1 = new StringBuffer();
		String sql_2 = ") values(";
		StringBuffer sql_3 = new StringBuffer();
		String sql_4 = ")";
		Iterator<String> it = mapStr.keySet().iterator();
		while (it.hasNext()) {
			String tmp_1 = it.next();
			// String tmp_1_str = Util.null2String(mapStr.get(tmp_1));
			String tmp_1_str = mapStr.get(tmp_1);
			if (tmp_1_str == null) {
				tmp_1_str = "";
			}
			if (tmp_1.length() > 0) {
				sql_1.append(tmp_1);
				sql_1.append(",");
				sql_3.append("?");
				sql_3.append(",");
			}
		}
		String now_sql_1 = sql_1.toString();
		if (now_sql_1.lastIndexOf(",") > 0) {
			now_sql_1 = now_sql_1.substring(0, now_sql_1.length() - 1);
		}
		String now_sql_3 = sql_3.toString();
		if (now_sql_3.lastIndexOf(",") > 0) {
			now_sql_3 = now_sql_3.substring(0, now_sql_3.length() - 1);
		}
		String setvalues = "";
		String sql = sql_0 + now_sql_1 + sql_2 + now_sql_3 + sql_4;
		ConnStatement cs = I18nContextUtil.getBean(ConnStatement.class);
		try {
			String poolname_rb = "";
			// TODO E10请传入数据源名称
			cs.setStatementSql(sql, poolname_rb);
			it = mapStr.keySet().iterator();
			int num = 1;
			while (it.hasNext()) {
				String tmp_1 = it.next();
				// String tmp_1_str = Util.null2String(mapStr.get(tmp_1));
				String tmp_1_str = mapStr.get(tmp_1);
				if (tmp_1_str == null) {
					tmp_1_str = "";
				}
				if (tmp_1.length() < 0) {
					continue;
				}
				setvalues = setvalues + "," + tmp_1_str;
				if ("".equals(tmp_1_str)) {
					cs.setString(num, null);
				} else {
					cs.setString(num, tmp_1_str);
				}
				// log.writeLog("## setvalues = " + setvalues+" num:"+num+" tmp_1:"+tmp_1);
				num++;
			}
			cs.executeUpdate();
			StringBuffer sqlquery = new StringBuffer();
			sqlquery.append("select id from " + table + " where ");
			it = mapStr.keySet().iterator();
			while (it.hasNext()) {
				String tmp_1 = it.next();
				if (("," + removefield + ",").indexOf("," + tmp_1 + ",") >= 0) {
					continue;
				}
				// String tmp_1_str = Util.null2String(mapStr.get(tmp_1));
				if (tmp_1.length() > 0) {
					sqlquery.append(tmp_1);
					String tmp_1_str = mapStr.get(tmp_1);
					if ("".equals(tmp_1_str)) {
						sqlquery.append(" is null and ");
					} else {
						sqlquery.append("=? and ");
					}
				}
			}
			String querysql = sqlquery.toString();
			if (querysql.lastIndexOf("and ") > 0) {
				querysql = querysql.substring(0, querysql.length() - 4);
			}
			String poolname_po = "";
			// TODO E10请传入数据源名称
			cs.setStatementSql(querysql, poolname_po);
			it = mapStr.keySet().iterator();
			num = 1;
			while (it.hasNext()) {
				String tmp_1 = it.next();
				if (("," + removefield + ",").indexOf("," + tmp_1 + ",") >= 0) {
					continue;
				}
				// String tmp_1_str = Util.null2String(mapStr.get(tmp_1));
				String tmp_1_str = mapStr.get(tmp_1);
				if (tmp_1_str == null) {
					tmp_1_str = "";
				}
				if (tmp_1.length() < 0) {
					continue;
				}
				if ("".equals(tmp_1_str)) {
					continue;
				}
				setvalues = setvalues + "," + tmp_1_str;
				if ("".equals(tmp_1_str)) {
					cs.setString(num, null);
				} else {
					cs.setString(num, tmp_1_str);
				}
				// log.writeLog("## setvalues = " + setvalues+" num:"+num+" tmp_1:"+tmp_1);
				num++;
			}
			// log.writeLog("InsertBackquery sqlquery="+querysql);
			cs.executeQuery();
			if (cs.next()) {
				id = cs.getString("id");
			}
			cs.close();
		} catch (Exception e) {
			logger_110ff965.error(String.valueOf(e));
			logger_110ff965.error(String.valueOf("## sql = " + sql));
			logger_110ff965.error(String.valueOf("++++++++++++++++++++++++++++++" + mapStr));
			cs.close();
			return "";
		}
		// return false;
		return id;
	}

	public boolean update(Map<String, String> mapStr, String table, String uqField, String uqVal) {
		if (mapStr == null || mapStr.isEmpty()) {
			return false;
		}
		if (uqField == null || "".equals(uqField)) {
			return false;
		}
		if (uqVal == null || "".equals(uqVal)) {
			return false;
		}
		RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
		StringBuffer buff = new StringBuffer();
		buff.append("update ");
		buff.append(table);
		buff.append(" set ");
		Iterator<String> it = mapStr.keySet().iterator();
		String flag = "";
		while (it.hasNext()) {
			String tmp_1 = it.next();
			String tmp_1_str = mapStr.get(tmp_1);
			if (tmp_1_str == null) {
				tmp_1_str = "";
			}
			if (tmp_1_str.length() > 0) {
				buff.append(flag);
				buff.append(tmp_1);
				buff.append("=");
				if (tmp_1_str.contains("##")) {
					buff.append(tmp_1_str.replace("##", ""));
				} else {
					buff.append("'");
					buff.append(tmp_1_str);
					buff.append("'");
				}
				flag = ",";
			}
		}
		buff.append(" where ");
		buff.append(uqField);
		buff.append("='");
		buff.append(uqVal);
		buff.append("' and superid is null");
		String sql = buff.toString();
		String poolname_1l = CONN_TYPE.workflow.getType();
		// log.writeLog("## sql = " + sql);
		// System.out.println("## sql = " + sql);
        /* return false;
 TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源*/
		return rs.executeSql(sql, poolname_1l);
	}

	public boolean updateGen(Map<String, String> mapStr, String table, String uqField, String uqVal) {
		if (mapStr == null || mapStr.isEmpty()) {
			return false;
		}
		if (uqField == null || "".equals(uqField)) {
			return false;
		}
		if (uqVal == null || "".equals(uqVal)) {
			return false;
		}
		StringBuffer buff = new StringBuffer();
		buff.append("update ");
		buff.append(table);
		buff.append(" set ");
		Iterator<String> it = mapStr.keySet().iterator();
		String flag = "";
		while (it.hasNext()) {
			String tmp_1 = it.next();
			String tmp_1_str = mapStr.get(tmp_1);
			if (tmp_1_str == null) {
				tmp_1_str = "";
			}
			buff.append(flag);
			buff.append(tmp_1);
			buff.append("=");
			if ("".equals(tmp_1_str)) {
				buff.append("null");
			} else {
				buff.append("?");
			}
			flag = ",";
		}
		buff.append(" where ");
		buff.append(uqField);
		buff.append("='");
		buff.append(uqVal);
		buff.append("' ");
		String sql = buff.toString();
		// log.writeLog("## sql = " + sql);
		ConnStatement cs = I18nContextUtil.getBean(ConnStatement.class);
		try {
			String poolname_gx = "";
			// TODO E10请传入数据源名称
			cs.setStatementSql(sql, poolname_gx);
			it = mapStr.keySet().iterator();
			int num = 1;
			while (it.hasNext()) {
				String tmp_1 = it.next();
				// String tmp_1_str = Util.null2String(mapStr.get(tmp_1));
				String tmp_1_str = mapStr.get(tmp_1);
				if (tmp_1_str == null) {
					tmp_1_str = "";
				}
				if ("".equals(tmp_1_str)) {
				} else {
					cs.setString(num, tmp_1_str);
					num++;
				}
			}
			cs.executeUpdate();
			cs.close();
		} catch (Exception e) {
			logger_110ff965.error(String.valueOf(e));
			logger_110ff965.error(String.valueOf("## sql = " + sql));
			logger_110ff965.error(String.valueOf("++++++++++++++++++++++++++++++" + mapStr));
			cs.close();
			return false;
		}
		// boolean re = rs.executeSql(sql);
		// if("formtable_main_73_dt1".equals(table)){
		//
		// sql
		// ="update formtable_main_73_dt1 set yhje = (select price*ActualNumbers from formtable_main_73_dt1 where val_1='"+uqVal+"') ,Money =(select price*ActualNumbers from formtable_main_73_dt1 where val_1='"+uqVal+"') where val_1='"+uqVal+"'";
		// rs.executeSql(sql);
		// }
		return true;
	}

	public boolean updateGenold(Map<String, String> mapStr, String table, String uqField, String uqVal) {
		if (mapStr == null || mapStr.isEmpty()) {
			return false;
		}
		if (uqField == null || "".equals(uqField)) {
			return false;
		}
		if (uqVal == null || "".equals(uqVal)) {
			return false;
		}
		RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
		StringBuffer buff = new StringBuffer();
		buff.append("update ");
		buff.append(table);
		buff.append(" set ");
		Iterator<String> it = mapStr.keySet().iterator();
		String flag = "";
		while (it.hasNext()) {
			String tmp_1 = it.next();
			String tmp_1_str = mapStr.get(tmp_1);
			if (tmp_1_str == null) {
				tmp_1_str = "";
			}
			buff.append(flag);
			buff.append(tmp_1);
			buff.append("=");
			if (tmp_1_str.contains("##")) {
				buff.append(tmp_1_str.replace("##", ""));
			} else {
				if ("".equals(tmp_1_str)) {
					buff.append("null");
				} else {
					buff.append("'");
					buff.append(tmp_1_str);
					buff.append("'");
				}
			}
			flag = ",";
		}
		buff.append(" where ");
		buff.append(uqField);
		buff.append("='");
		buff.append(uqVal);
		buff.append("' ");
		String sql = buff.toString();
		logger_110ff965.error(String.valueOf("## sql = " + sql));
		String poolname_4e = CONN_TYPE.workflow.getType();
		// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
		boolean re = rs.executeSql(sql, poolname_4e);
		// if("formtable_main_73_dt1".equals(table)){
		//
		// sql
		// ="update formtable_main_73_dt1 set yhje = (select price*ActualNumbers from formtable_main_73_dt1 where val_1='"+uqVal+"') ,Money =(select price*ActualNumbers from formtable_main_73_dt1 where val_1='"+uqVal+"') where val_1='"+uqVal+"'";
		// rs.executeSql(sql);
		// }
		return re;
	}

	public boolean insert(Map<String, String> mapStr, String table, RecordSetDataSource rsx) {
		if (mapStr == null) {
			return false;
		}
		if (mapStr.isEmpty()) {
			return false;
		}
		String sql_0 = "insert into " + table + "(";
		StringBuffer sql_1 = new StringBuffer();
		String sql_2 = ") values(";
		StringBuffer sql_3 = new StringBuffer();
		String sql_4 = ")";
		Iterator<String> it = mapStr.keySet().iterator();
		while (it.hasNext()) {
			String tmp_1 = it.next();
			String tmp_1_str = Util.null2String(mapStr.get(tmp_1));
			if (tmp_1_str.length() > 0) {
				sql_1.append(tmp_1);
				sql_1.append(",");
				if (tmp_1_str.contains("##")) {
					sql_3.append(tmp_1_str.replace("##", ""));
					sql_3.append(",");
				} else {
					sql_3.append("'");
					sql_3.append(tmp_1_str);
					sql_3.append("',");
				}
			}
		}
		String now_sql_1 = sql_1.toString();
		if (now_sql_1.lastIndexOf(",") > 0) {
			now_sql_1 = now_sql_1.substring(0, now_sql_1.length() - 1);
		}
		String now_sql_3 = sql_3.toString();
		if (now_sql_3.lastIndexOf(",") > 0) {
			now_sql_3 = now_sql_3.substring(0, now_sql_3.length() - 1);
		}
		String sql = sql_0 + now_sql_1 + sql_2 + now_sql_3 + sql_4;
		String poolname_1p = "";
		// log.writeLog("## sql = " + sql);
        /* TODO  检测到insert语句 -> E9插入sql迁移到E10需要特殊处理请参考文档进行调整  https://www.e-cology.com.cn/sp/doc/docShare/1027709301045985331?tenantKey=t7akvdnf84
 *TODO E10必须指定poolname数据源*/
      long generate = IdGenerator.generate();
		return rsx.executeSql(sql, poolname_1p);
	}

	public String getModeId(String tableName) {
		RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
		String formid = "";
		String modeid = "";
		String sql = "select id from form as  workflow_bill where tablename='" + tableName + "'";
		String poolname_5n = CONN_TYPE.workflow.getType();
		// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
		rs.executeSql(sql, poolname_5n);
		if (rs.next()) {
			formid = Util.null2String(rs.getString("id"));
		}
		sql = "select id from ebdf_obj as  modeinfo where  formid=" + formid;
		String poolname_zg = CONN_TYPE.workflow.getType();
		// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
		rs.executeSql(sql, poolname_zg);
		if (rs.next()) {
			modeid = Util.null2String(rs.getString("id"));
		}
		return modeid;
	}
}
