package com.weaver.seconddev.interfaces.swfa;

import java.util.*;
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 com.weaver.verupgrade.conn.RecordSet;
import com.weaver.verupgrade.general.BaseBean;
import com.weaver.verupgrade.general.Util;
import org.springframework.stereotype.Component;

/**
 * 字段名称工具类
 *
 * @ClassName: BillFieldUtil
 * @Description: TODO
 * @author ocean
 * @date 2013-6-20 下午6:40:46
 */
@Component
public class BillFieldUtil {

	private static final org.slf4j.Logger logger_6c507bbc = LoggerFactory.getLogger(BillFieldUtil.class);

	/**
	 * 方法描述 : 获取字段的ID 创建者：ocean 项目名称： ecologyTest 类名： CwUtil.java 版本： v1.0 创建时间：
	 * 2013-9-14 下午3:05:53
	 *
	 *            流程id
	 * @param num
	 *            明细表
	 * @return Map
	 */

//	public static Map getFieldId(int formid, String num) {
//
//		logger_6c507bbc.error("BillFieldUtil getFieldId start formid "+ formid);
//		logger_6c507bbc.error("num "+ num);
//		formid = Math.abs(formid);
//		String sql = "";
//		if ("0".equals(num)) {
//			sql = "select b.id,fieldname,detailtable from workflow_billfield b ,workflow_base a where b.billid=-" + formid + " and a.formid=b.billid and (detailtable='' or detailtable is null)";
//		} else {
//			sql = "select b.id,fieldname,detailtable from workflow_billfield b ,workflow_base a where b.billid=-" + formid + " and a.formid=b.billid and detailtable='formtable_main_" + formid + "_dt" + num + "'";
//		}
//
//		logger_6c507bbc.error("sql "+ sql);
//		RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
//		String poolname = CONN_TYPE.workflow.getType();
//		// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
//		rs.executeSql(sql, poolname);
//		Map array = new HashMap();
//		while (rs.next()) {
//			logger_6c507bbc.error("fieldname "+ rs.getString("fieldname"));
//			array.put(Util.null2String(rs.getString("fieldname")).toLowerCase(), Util.null2String(rs.getString("id")));
//		}
//		logger_6c507bbc.error("array "+ array);
//		return array;
//	}

	/**
	 * @Title: getlabelId
	 * @Description: TODO
	 * @param @param name 数据库字段名称 小写
	 * @param @param formid 表单ID
	 * @param @param ismain 是否主表 0:主表 1：明细表
	 * @param @param num 明细表序号
	 * @param @return
	 * @return String
	 * @throws
	 */

//	public static String getlabelId(String name, int formid, String ismain, String num) {
//
//		logger_6c507bbc.error("BillFieldUtil getlabelId start  ");
//		logger_6c507bbc.error("name-----------  " + name);
//		logger_6c507bbc.error("formid-----------  " + formid);
//		logger_6c507bbc.error("ismain-----------  " + ismain);
//		logger_6c507bbc.error("num-----------  " + num);
//		String id = "";
//		String sql = "";
//		formid = formid * -1;
//		if ("0".equals(ismain)) {
//			sql = "select id,fieldname,detailtable from workflow_billfield where billid=-" + formid + " and (detailtable='' or detailtable is null) and lower(fieldname)='" + name + "'";
//		} else {
//			sql = "select id,fieldname,detailtable from workflow_billfield where billid=-" + formid + " and detailtable='formtable_main_" + formid + "_dt" + num + "' and lower(fieldname)='" + name + "'";
//		}
//		// System.out.println(sql);
//		logger_6c507bbc.error("sql-----------  " + sql);
//		RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
//		String poolname_a1 = CONN_TYPE.workflow.getType();
//		// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
//		rs.executeSql(sql, poolname_a1);
//		if (rs.next()) {
//			id = Util.null2String(rs.getString("id"));
//		}
//		logger_6c507bbc.error("id-----------  " + id);
//		return id;
//	}

	/**
	 * 选择框 显示名
	 *
	 * @param val
	 * @param id
	 * @return
	 */
	public static String getselectName(String val, String id) {
		logger_6c507bbc.error("BillFieldUtil getselectName start  ");
		String name = "";
		if (val.equals("")) {
			return "";
		}
		RecordSet recordSet = I18nContextUtil.getBean(RecordSet.class);
		String poolname_n1 = CONN_TYPE.workflow.getType();
		// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
		//TODO custom 已处理 int类型转string
		String sql = "select name,field_id from e10_core_business.dbo.field_option as  workflow_selectItem where field_id ='"+id+"'  and value_key= '" +val+"'";
		logger_6c507bbc.error("sql-----------  " + sql);
		recordSet.executeSql(sql, poolname_n1);
		if (recordSet.next()) {
			name = recordSet.getString("name");
		}
		logger_6c507bbc.error("name-----------  " + name);
		return name;
	}

	/**
	 * 通过数据库字段名获取选择框 显示名
	 *
	 * @return
	 */

	public static String getselectNameByCol(String fieldname, String workflowid, String selectvalue) {
		logger_6c507bbc.error("BillFieldUtil getselectNameByCol start  ");
		logger_6c507bbc.error("fieldname-----------  " + fieldname);
		logger_6c507bbc.error("workflowid-----------  " + workflowid);
		logger_6c507bbc.error("selectvalue-----------  " + selectvalue);
		String name = "";
		RecordSet recordSet = I18nContextUtil.getBean(RecordSet.class);
		String poolname_jl = CONN_TYPE.workflow.getType();
		// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
//		recordSet.executeSql("select c.name from wfp_base a " + "INNER JOIN form_field b on a.formid=b.form_id " + "INNER JOIN field_option c on b.id=c.field_id " + "where b.fieldname='" + fieldname + "' and a.id=" + workflowid + " and c.selectvalue=" + selectvalue, poolname_jl);
		String sql = " select a.id, b.relatekey ,c.title , c.data_key ,d.name ,d.value_key  from e10_core_business.dbo.wfp_base  a   " +
				" left join e10_core_business.dbo.wfp_relateform b on a.id = b.workflowid  " +
				" left join e10_core_business.dbo.form_field c on b.relatekey = c.form_id " +
				" left join e10_core_business.dbo.field_option d on c.id = d.field_id  " +
				" where a.id =  '"+workflowid+"' and c.data_key = '"+fieldname+"' and value_key = '"+selectvalue+"'";
		logger_6c507bbc.error("sql-----------  " + sql);

		recordSet.executeSql(sql, poolname_jl);

		if (recordSet.next()) {
			name = recordSet.getString("name");
		}
		logger_6c507bbc.error("name-----------  " + name);
		return name;
	}

	/**
	 * 选择框 options
	 *
	 * @param val
	 * @param id
	 * @return
	 */
//	public static String getselectOptionStr(String val, String id) {
//		String str = "<option value=''></option>";
//		RecordSet recordSet = I18nContextUtil.getBean(RecordSet.class);
//		String poolname_fi = CONN_TYPE.workflow.getType();
//		// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
//		//TODO custom 已处理 int类型转string
//		recordSet.executeSql("select name,field_id,value_key from e10_core_business.dbo.field_option as  workflow_selectItem  where field_id = '" + id + "' order by value_key", poolname_fi);
//		while (recordSet.next()) {
//			String name = Util.null2String(recordSet.getString("name"));
//			String value = Util.null2String(recordSet.getString("value_key"));
//			if (value.equals(val)) {
//				str += "<option value='" + value + "' selected>" + name + "</option>";
//			} else {
//				str += "<option value='" + value + "'>" + name + "</option>";
//			}
//		}
//		return str;
//	}

	/**
	 * 判断节点是否为创建节点
	 *
	 * @param nodeid
	 * @return
	 */
//	public static boolean judgeNodeStart(String nodeid) {
//		boolean falg = false;
//		String isstart = "0";
//		RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
//		String poolname_y9 = CONN_TYPE.workflow.getType();
//		// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
//		//TODO custom 已处理 int类型转string
//		rs.executeSql("select * from wfp_node as  workflow_nodebase where id='" + nodeid+"'", poolname_y9);
//		if (rs.next()) {
//			isstart = rs.getString("isstart");
//		}
//		if ("1".equals(isstart)) {
//			falg = true;
//		}
//		return falg;
//	}

	/**
	 * 获取节点名称
	 * @param nodeid
	 * @return
	 */

//	public String getNodename(String nodeid) {
//		String nodename = "";
//		RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
//		String poolname_v8 = CONN_TYPE.workflow.getType();
//		// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
//		//TODO custom 已处理 int类型转string
//		rs.executeSql("select id,nodename from wfp_node as  workflow_nodebase where id='" + nodeid +"'", poolname_v8);
//		if (rs.next()) {
//			nodename = rs.getString("nodename");
//		}
//		return nodename;
//	}

	/**
	 * 获取计量单位内码
	 * @param unitid
	 * @return
	 */
	public String getUnitno(String unitid) {
		logger_6c507bbc.error("BillFieldUtil getUnitno start  ");
		logger_6c507bbc.error("unitid-----------  " + unitid);
		String unitno = "";
		RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
		String poolname_rg = CONN_TYPE.workflow.getType();
		// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
		//TODO custom 已处理 int类型转string
		String sql = "select id,dwnm from e10_common.dbo.formtable_main_286 where id='" + unitid+"'";
		logger_6c507bbc.error("sql-----------  " + sql);
		boolean b1 = rs.executeSql(sql, poolname_rg);
		logger_6c507bbc.error("b1-----------  " + b1);
		if (rs.next()) {
			unitno = rs.getString("dwnm");
		}
		logger_6c507bbc.error("unitno-----------  " + unitno);
		return unitno;
	}

	/**
	 * 获取工号
	 * @param hrid
	 * @return
	 */
	public String getWorkcode(String hrid) {
		logger_6c507bbc.error("BillFieldUtil getWorkcode start  ");
		logger_6c507bbc.error("hrid-----------  " + hrid);
		String code = "";
		RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
		String poolname_vp = CONN_TYPE.workflow.getType();
		// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源

		String sql = "select id,job_num from eteams.dbo.employee as  HrmResource where id = " + hrid;
		logger_6c507bbc.error("sql-----------  " + sql);
		rs.executeSql(sql, poolname_vp);
		if (rs.next()) {
			code = Util.null2String(rs.getString("job_num"));
		}
		logger_6c507bbc.error("code-----------  " + code);
		return code;
	}

	/**
	 * 获取身份证号
	 * @param hrid
	 * @return
	 */
	public String getCredentialsNumber(String hrid) {
		logger_6c507bbc.error("BillFieldUtil getCredentialsNumber start  ");
		logger_6c507bbc.error("hrid-----------  " + hrid);
		String code = "";
		RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
		String poolname_2s = CONN_TYPE.workflow.getType();
		// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
		String sql = "SELECT HrmResource.id_no AS certificatenum " +
				"FROM ( " +
				" SELECT employee.*, hr_userinfo.id_no " +
				" FROM eteams.dbo.employee " +
				" LEFT JOIN e10_other_business.dbo.hr_userinfo " +
				" ON employee.id = hr_userinfo.[user] " +
				" AND employee.delete_type = 0 " +
				" AND employee.tenant_key = 't6kj9c07jr' " +
				" AND hr_userinfo.delete_type = 0 " +
				" AND hr_userinfo.tenant_key = 't6kj9c07jr' " +
				") HrmResource " +
				" WHERE HrmResource.id = '" + hrid +
				"' AND HrmResource.delete_type = 0 " +
				" AND HrmResource.tenant_key = 't6kj9c07jr' " ;

		logger_6c507bbc.error("sql-----------  " + sql);
		boolean b1 = rs.executeSql(sql, poolname_2s);
		logger_6c507bbc.error("getCredentialsNumber flag-----------  " + b1);

		if (rs.next()) {
			code = Util.null2String(rs.getString("certificatenum"));
		}
		logger_6c507bbc.error("code-----------  " + code);
		return code;
	}

	/**
	 * 获取部门名称
	 * @param dpid
	 * @return
	 */
	public String getDepartmentname(String dpid) {
		logger_6c507bbc.error("BillFieldUtil getDepartmentname start  ");
		logger_6c507bbc.error("dpid-----------  " + dpid);
		String dpname = "";
		RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
		String poolname_p1 = CONN_TYPE.workflow.getType();
		// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
		String sql = "select id,fullname from eteams.dbo.department as  HrmDepartment where id = " + dpid;
		logger_6c507bbc.error("sql-----------  " + sql);
		boolean b1 = rs.executeSql(sql, poolname_p1);
		logger_6c507bbc.error("getDepartmentname-----------  " + b1);

		if (rs.next()) {
			dpname = Util.null2String(rs.getString("fullname"));
		}
		logger_6c507bbc.error("dpname-----------  " + dpname);
		return dpname;
	}

	/**
	 * 获取分部名称
	 * @param spid
	 * @return
	 */
	public String getSubcompanyname(String spid) {
		logger_6c507bbc.error("BillFieldUtil getSubcompanyname start  ");
		logger_6c507bbc.error("spid-----------  " + spid);
		String spname = "";
		RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
		String poolname_tg = CONN_TYPE.workflow.getType();
		// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
		String sql ="SELECT id ,  name  " +
				"FROM eteams.dbo.department HrmSubCompany " +
				"WHERE  id = '"+spid+"' and HrmSubCompany.virtualid = 1 " +
				" AND HrmSubCompany.type = 'subcompany' " +
				" AND HrmSubCompany.delete_type = 0 " +
				" AND HrmSubCompany.tenant_key = 't6kj9c07jr'";

		logger_6c507bbc.error("sql-----------  " + sql);
		boolean b1 = rs.executeSql(sql, poolname_tg);
		logger_6c507bbc.error("getSubcompanyname-----------  " + b1);
		if (rs.next()) {
			spname = Util.null2String(rs.getString("name"));
		}
		logger_6c507bbc.error("spname-----------  " + spname);
		return spname;
	}

	/**
	 * 获取流程tablename
	 * @param wfid
	 * @return
	 */

//	public String getTablename(String wfid) {
//		logger_6c507bbc.error("BillFieldUtil getSubcompanyname start  ");
//		logger_6c507bbc.error("wfid-----------  " + wfid);
//		String tbname = "";
//		String fmid = "";
//		RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
//		String poolname_tw = CONN_TYPE.workflow.getType();
//		// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
//		rs.executeSql("select formid from wfp_base as  workflow_base where id = " + wfid, poolname_tw);
//		if (rs.next()) {
//			fmid = Util.null2String(rs.getString("formid"));
//			tbname = "formtable_main_" + fmid.replace("-", "");
//		}
//		logger_6c507bbc.error("tbname-----------  " + tbname);
//		return tbname;
//	}

	/**
	 * 把出差人员放在一起
	 * @param type 调用类名
	 * @param employeeid
	 * @param list
	 * @return
	 */
	public String getEmployeeId(String type, String employeeid, List<String> list) {
		String returnStr = "";
		BaseBean basebean = new BaseBean();
		// 把申请人加在第一个
		list.add(0, employeeid);
		// 去除重复人员
		list = removeDuplicate(list);
		// 计数
		int count = 0;
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				String gh = list.get(i);
				if (count < 20) {
					// 只取前面20个
					if (gh != null && !"".equals(gh)) {
						if (gh.length() == 10) {
							returnStr = returnStr + "," + gh;
							count++;
						}
					}
				} else {
					logger_6c507bbc.error(String.valueOf(type + "出差人员" + gh + "超过20个 ，未加入"));
				}
			}
		}
		if (returnStr != null && !"".equals(returnStr)) {
			returnStr = returnStr.replaceFirst(",", "");
		}
		logger_6c507bbc.error(String.valueOf(type + " employeeid:" + returnStr));
		return returnStr;
	}

	/**
	 * 删除ArrayList中重复元素
	 * @param list
	 */
	private static List<String> removeDuplicate(List<String> list) {
		Set<String> set = new HashSet<String>();
		List<String> newList = new ArrayList<String>();
		for (Iterator<String> iter = list.iterator(); iter.hasNext(); ) {
			String element = (String) iter.next();
			if (set.add(element)) {
				newList.add(element);
			}
		}
		return newList;
	}

	/**
	 * 根据工号获取ID
	 * @param workcode
	 * @param map 所有人员 工号和ID的map
	 * @return
	 */
//	public Map<String, String> getIdsByWorkcode(String workcode, Map<String, String> map) {
//		String ids = "";
//		Map<String, String> returnMap = new HashMap<String, String>();
//		if (workcode != null && !workcode.equals("")) {
//			// 传入的工号为空
//			String[] array = workcode.split(",");
//			for (int i = 0; i < array.length; i++) {
//				if (array[i] != null) {
//					array[i] = array[i].trim();
//				}
//				String userid = map.get(array[i]);
//				if (userid == null || userid.equals("")) {
//					returnMap.put("msg", "工号" + array[i] + "不存在...");
//					return returnMap;
//				} else {
//					ids += "," + userid;
//				}
//			}
//			if (ids != null) {
//				ids = ids.replaceFirst(",", "");
//			}
//			returnMap.put("msg", "ok");
//			returnMap.put("data", ids);
//		}
//		return returnMap;
//	}

	/**
	 * 根据工号获取ID
	 * @param workcode
	 * @return
	 */
//	public Map<String, String> getIdsByWorkcode(String workcode) {
//		String ids = "";
//		Map<String, String> returnMap = new HashMap<String, String>();
//		if (workcode != null && !workcode.equals("")) {
//			// 传入的工号为空
//			String[] array = workcode.split(",");
//			for (int i = 0; i < array.length; i++) {
//				if (array[i] != null) {
//					array[i] = array[i].trim();
//				}
//				RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
//				String poolname_xj = CONN_TYPE.workflow.getType();
//				// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
//				rs.executeSql("select id,job_num from employee as  HrmResource where job_num = '" + array[i] + "'", poolname_xj);
//				// id
//				String userid = "";
//				while (rs.next()) {
//					userid = Util.null2String(rs.getString("id"));
//				}
//				if (userid == null || userid.equals("")) {
//					returnMap.put("msg", "工号" + array[i] + "不存在...");
//					return returnMap;
//				} else {
//					ids += "," + userid;
//				}
//			}
//			if (ids != null) {
//				ids = ids.replaceFirst(",", "");
//			}
//			returnMap.put("msg", "ok");
//			returnMap.put("data", ids);
//		}
//		return returnMap;
//	}

	/**
	 * 获取姓名
	 * @param hrid
	 * @return
	 */
	public String getName(String hrid) {
		String code = "";
		RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
		String poolname_s2 = CONN_TYPE.workflow.getType();
		// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
		logger_6c507bbc.error("getName --" + hrid);
		String sql = "select id,username from eteams.dbo.employee as  HrmResource where id = " + hrid;

		logger_6c507bbc.error("sql-----------  " + sql);
		boolean b1 = rs.executeSql(sql, poolname_s2);
		logger_6c507bbc.error("getName -----------  " + b1);

		if (rs.next()) {
			code = Util.null2String(rs.getString("username"));
		}
		logger_6c507bbc.error("code --" + code);
		return code;
	}
}
