package com.engine.htsc.doc.util;

import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;

/**
 * 转换工具类
 */
public class ConvertUtil {

    /**
     * 根据requestid获取流程主表单数据所在的表名
     * @param requestId
     * @return
     */
    public static String getMainTableName(String requestId) {
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        //根据requestid获取流程主表单所在的表名
        rs.executeQuery("select t2.tablename from workflow_requestbase t0,workflow_base t1,workflow_bill t2 where t0.workflowid = t1.id " +
                "and t1.formid = t2.id and  t0.requestid = ?",new Object[]{requestId});
        String tablename = "";
        if(rs.next()){
            tablename = rs.getString("tablename");
            bb.writeLog("工具类 ConvertUtil--getMainTableName requestid:"+requestId+"对应的流程表单的表名为:"+tablename);
        }
        return tablename;
    }

    /**
     * 根据requestId获取流程图英文名
     * @param requestId
     * @return
     */
    public static String getUnNumber(String requestId) {
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        //根据requestid获取流程图定义英文名
        rs.executeQuery("select t1.unnumber from workflow_requestbase t0,workflow_base t1,workflow_bill t2 where t0.workflowid = t1.id " +
                "and t1.formid = t2.id and  t0.requestid = ?",new Object[]{requestId});
        String unnumber = "";
        if(rs.next()){
            unnumber = rs.getString("unnumber");
            bb.writeLog("工具类 ConvertUtil--getUnNumber requestid:"+requestId+"对应的流程图英文名的表名为:"+unnumber);
        }
        return unnumber;
    }

    /**
     * 根据流程实例id和表单filedid返回实际展示的中文
     * @param requestId
     * @param filedId
     * @return
     */
    public static String getNameByRequestIdAndFiledId(String requestId, String filedId) {
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        String tablename = getMainTableName(requestId);

        if(StringUtils.isNotEmpty(tablename)) {
            String sql = " select t6.selectname from workflow_base t , workflow_bill t2, workflow_billfield t3,workflow_requestbase t4," +
                    tablename + " t5 " +
                    " ,workflow_selectitem t6 " +
                    " where t2.id = t3.billid and t.formid = t2.id " +
                    " and t4.workflowid = t.id and t4.requestid = t5.requestid and t6.fieldid = t3.id and t6.selectvalue = t5." + filedId +
                    " and t4.requestid = '" + requestId + "' and t3.fieldname = '" + filedId + "' ";
            bb.writeLog("工具类 ConvertUtil--getNameByRequestIdAndFiledId requestid:"+requestId+"对应的查询sql:"+sql);
            rs.executeQuery(sql);
            if(rs.next()) {
                return rs.getString("selectname");
            }
        }
        return "";
    }

    /**
     * 根据流程实例id和filedid 返回该字段的值
     * @param requestId
     * @param filedId
     * @return
     */
    public static String getValueByRequestIdAndFiledId(String requestId, String filedId) {
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        String tablename = getMainTableName(requestId);
        String sql = " select t." + filedId +" from "+ tablename+ " t where t.requestid = "+requestId;
        bb.writeLog("工具类 ConvertUtil--getValueByRequestIdAndFiledId requestid:"+requestId+"对应的查询sql:"+sql);
        rs.executeQuery(sql);
        if(rs.next()) {
            return rs.getString(filedId);
        }
        return "";
    }


}
