package weaver.interfaces.htsc.util;

import weaver.conn.RecordSet;
import weaver.general.Util;

public class HT_WorkflowInfo {

    public String getWorkflowidByRequestId(String requestid){
        RecordSet rs = new RecordSet();
        String workflowid = "" ;
        if(!"".equals(requestid) ){
            String sql ="  select workflowid from workflow_requestbase where requestid=" + requestid ;
            rs.execute(sql);
            if(rs.next()){
                workflowid = Util.null2String(rs.getString("workflowid"));
            }
        }
        return workflowid;
    }


    public String getWorkflowUnNumberByRequestId(String requestid){
        RecordSet rs = new RecordSet();
        String unnumber = "" ;
        if(!"".equals(requestid) ){
            String sql =" select l.unnumber from workflow_base l\n" +
                        " inner join workflow_requestbase t on t.workflowid = l.id\n" +
                        " where t.requestid=" + requestid ;
            rs.execute(sql);
            if(rs.next()){
                unnumber = Util.null2String(rs.getString("unnumber"));
            }
        }
        return unnumber;
    }

    /***
     *
     * @param workflowid
     * @return
     */
    public String getWorkflowUnNumberByWorkflowid(String workflowid){
        RecordSet rs = new RecordSet();
        String unnumber = "" ;
        if(!"".equals(workflowid) ){
            String sql ="  select unnumber from workflow_base where id=" + workflowid ;
            rs.execute(sql);
            if(rs.next()){
                unnumber = Util.null2String(rs.getString("unnumber"));
            }
        }
        return unnumber;
    }

    /***
     *
     * @param workflowid
     * @return
     */
    public String getFormTableByWorkflowId(String workflowid){

        RecordSet rs = new RecordSet();
        String tablename = "" ;
        if(!"".equals(workflowid)){
            String sql ="  select abs(formid) as formid from workflow_base where id=" + workflowid ;
            rs.execute(sql);
            if(rs.next()){
                tablename = "formtable_main_"+Util.null2String(rs.getString("formid"));
            }
        }
        return tablename;
    }


    /***
     *
     * @param requestid
     * @return
     */
    public String getFormTableByRequestid(String requestid)
    {

        RecordSet rs = new RecordSet();
        String tablename = "" ;
        if(!"".equals(requestid)){
            String fieldSql =   " select abs(k.formid) as formid \n" +
                                "  from workflow_requestbase h\n" +
                                "  inner join workflow_base k  on h.workflowid = k.id\n" +
                                "  where requestid="+requestid;
            rs.execute(fieldSql);
            if(rs.next()){
                tablename = "formtable_main_" + Util.null2String(rs.getString("formid"));
            }
        }
        return tablename;
    }


    /***
     *
     * @param requestid
     * @return
     */
    public String getRequestnameByRequestid(String requestid)
    {
        RecordSet rs = new RecordSet();
        String requestname = "" ;
        if(!"".equals(requestid))
        {
            String fieldSql =   " select requestname,requestnamenew from workflow_requestbase where requestid = "+requestid;
            rs.execute(fieldSql);
            if(rs.next()){
                requestname =  Util.null2String(rs.getString("requestnamenew"));
            }
        }
        return requestname;
    }


    /***
     *
     * @param requestid
     * @return
     */
    public String getMainidByRequestid(String requestid)
    {
        RecordSet rs = new RecordSet();
        String mainid = "" ;
        if(!"".equals(requestid))
        {
            String tablename = getFormTableByRequestid(requestid);
            String sql = " select id from "+tablename+" where requestid="+requestid ;
            rs.execute(sql);
            if(rs.next()){
                mainid = Util.null2String(rs.getString("id"));
            }
        }
        return mainid;
    }
}
