package weaver.interfaces.htsc.comInfo;

import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

public class PropBean {

	public String getPropName(String pkey)
	{
		if("".equals(pkey)){
			return "";
		}
		BaseBean bb = new BaseBean();
		String active = Util.null2String(bb.getPropValue("htsc_prop","active"));
		if(active ==""){
			return "";
		}else{
			return getPropNameByDb(pkey,active);
		}
	}

	private String getPropNameByDb(String pkey,String active){
		String pvalue = "";
		try{
			RecordSet rs = new RecordSet();
			String sql = " SELECT DEVVALUE,TESTVALUE,PRODVALUE FROM UF_PROP WHERE PKEY = ?";

			rs.executeQuery(sql,new Object[]{pkey});
			if(rs.next()){
				pvalue = Util.null2String(rs.getString(active+"VALUE"));
			}
		}catch (Exception e){

		}
		return pvalue;
	}

	/***
	 *
	 * @return
	 */
	public JSONObject getModeInfoByUUID(String pkey)
	{
		JSONObject jsonObject = new JSONObject();
		try{
			RecordSet rs = new RecordSet();
			String sql =" select t.id,t.modename,t.formid,t.seccategory,k.tablename\n" +
						" from modeinfo t\n" +
						" inner join workflow_bill k on k.id = t.formid\n" +
						" where cubeuuid=?";
			rs.executeQuery(sql,new Object[]{pkey});
			if(rs.next()) {
				jsonObject.put("modeid", Util.null2String(rs.getString("id")));
				jsonObject.put("modename", Util.null2String(rs.getString("modename")));
				jsonObject.put("formid", Util.null2String(rs.getString("formid")));
				jsonObject.put("seccategory", Util.null2String(rs.getString("seccategory")));
				jsonObject.put("tablename", Util.null2String(rs.getString("tablename")));
			}
		}catch (Exception e){

		}
		return jsonObject;
	}


	/***
	 *
	 * @param formid
	 * @param fieldname
	 * @return
	 */
	public String getFieldId(String formid,String fieldname)
	{
		if("".equals(formid) || "".equals(fieldname)){
			return "";
		}
		String fieldid = "";
		try{
			RecordSet rs = new RecordSet();
			String sql =" select id from workflow_billfield\n" +
					" where billid=?\n" +
					" and fieldname=?";
			rs.executeQuery(sql,new Object[]{formid,fieldname});
			if(rs.next()) {
				fieldid = Util.null2String(rs.getString("id"));
			}
		}catch (Exception e){

		}
		return fieldid;
	}

	/***
	 *
	 * @param workflowid
	 * @return
	 */
	public String getWorkflowUnNumberByid(String workflowid)
	{
		if("".equals(workflowid) || "".equals(workflowid)){
			return "";
		}
		String unNumber = "";
		try{
			RecordSet rs = new RecordSet();
			String sql =" select unnumber from workflow_base where id= ?";
			rs.executeQuery(sql,new Object[]{workflowid});
			if(rs.next()) {
				unNumber = Util.null2String(rs.getString("unnumber"));
			}
		}catch (Exception e){

		}
		return unNumber;
	}

	/***
	 *
	 * @param unNumber
	 * @return
	 */
	public String getWorkflowIdsByUnNumber(String unNumber)
	{
		if("".equals(unNumber) || "".equals(unNumber)){
			return "";
		}
		String workflowids = "";
		try{
			RecordSet rs = new RecordSet();
			String sql =" select id from workflow_base where unnumber = ?";
			rs.executeQuery(sql,new Object[]{unNumber});
			while (rs.next()) {
				String workflowid = Util.null2String(rs.getString("id"));
				workflowids += workflowids == "" ? workflowid : ","+workflowid ;
			}
		}catch (Exception e){

		}
		return workflowids;
	}


	/***
	 *
	 * @param unNumber
	 * @return
	 */
	public String getActiveWorkflowIdByUnNumber(String unNumber)
	{
		if("".equals(unNumber) || "".equals(unNumber)){
			return "";
		}
		String workflowids = "";
		try{
			RecordSet rs = new RecordSet();
			String sql =" select id from workflow_base where unnumber = ? and isvalid=1";
			rs.executeQuery(sql,new Object[]{unNumber});
			if (rs.next()) {
				workflowids = Util.null2String(rs.getString("id"));
			}
		}catch (Exception e){

		}
		return workflowids;
	}


	/***
	 *
	 * @param nodeid
	 * @return
	 */
	public String getNodeUnNumberByid(String nodeid)
	{
		if("".equals(nodeid) || "".equals(nodeid)){
			return "";
		}
		String unNumber = "";
		try{
			RecordSet rs = new RecordSet();
			String sql =" select unnumber from workflow_nodebase where id = ?";
			rs.executeQuery(sql,new Object[]{nodeid});
			if(rs.next()) {
				unNumber = Util.null2String(rs.getString("unnumber"));
			}
		}catch (Exception e){

		}
		return unNumber;
	}


	/***
	 * 方法废弃
	 * 请使用
	 * {@link PropBean#getActionNodeIdByUnNumber}
	 * 获取活动版本nodeId
	 * @param unNumber
	 * @return
	 */
	@Deprecated
	public String getNodeIdsByUnNumber(String unNumber)
	{
		return getActionNodeIdByUnNumber(unNumber);
//		if("".equals(unNumber) || "".equals(unNumber)){
////			return "";
////		}
////		String nodeids = "";
////		try{
////			RecordSet rs = new RecordSet();
////			String sql =" select id from workflow_nodebase where unnumber = ?";
////			rs.executeQuery(sql,new Object[]{unNumber});
////			while(rs.next()) {
////				String nodeid = Util.null2String(rs.getString("id"));
////				nodeids += nodeids=="" ? nodeid : ","+nodeid ;
////			}
////		}catch (Exception e){
////
////		}
////		return nodeids;
	}

	/***
	 *
	 * @param unNumber
	 * @return
	 */
	public String getActionNodeIdByUnNumber(String unNumber)
	{
		if("".equals(unNumber) || "".equals(unNumber)){
			return "";
		}
		String nodeids = "";
		try{
			RecordSet rs = new RecordSet();
			String sql =" select t.id,t.nodename \n" +
						" from workflow_nodebase t \n" +
						" inner join workflow_flownode k on k.nodeid = t.id\n" +
						" inner join workflow_base l on l.id = k.workflowid\n" +
						" where l.isvalid=1 \n" +
						" and t.unnumber=? ";

			rs.executeQuery(sql,new Object[]{unNumber});
			while(rs.next()) {
				String nodeid = Util.null2String(rs.getString("id"));
				nodeids += nodeids=="" ? nodeid : ","+nodeid ;
			}
		}catch (Exception e){

		}
		return nodeids;
	}



}
