package weaver.interfaces.htsc.util;

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

public class HT_SelectField {


    /***
     *
     * @param workflowid
     * @param fieldname
     * @param detailtable
     * @return
     */
    public String getbillFieldByWorkflowid(String workflowid,String fieldname,String detailtable){
        RecordSet rs = new RecordSet();
        String fieldid = "" ;
        System.out.println("workflowid:"+workflowid);
        System.out.println("fieldname:"+fieldname);

        if(!"".equals(workflowid) && !"".equals(fieldname)){

            String formid = "" ;
            String billid = "" ;
            String sql ="  select h.formid as billid,abs(h.formid) as formid from workflow_base h where h.id= " + workflowid ;
            System.out.println("sql:"+sql);
            rs.execute(sql);
            if(rs.next()){
                formid = Util.null2String(rs.getString("formid"));
                billid = Util.null2String(rs.getString("billid"));
            }

            System.out.println("formid:"+formid);
            System.out.println("billid:"+billid);

            String where  = "" ;
            if(!"".equals(detailtable) && !"".equals(formid)){
                where = " and detailtable='formtable_main_"+formid+"_"+detailtable+"' " ;
            }

            String fieldSql = " select id from workflow_billfield where billid="+billid+" and fieldname='"+fieldname+"' " + where ;
            rs.execute(fieldSql);
            System.out.println("fieldSql:"+fieldSql);
            if(rs.next()){
                fieldid = Util.null2String(rs.getString("id"));
            }
        }
        return fieldid;
    }

    /***
     *
     * @param billid
     * @param fieldname
     * @param detailtable
     * @return
     */
    public String getbillFieldByBillid(String billid,String fieldname,String detailtable){


        RecordSet rs = new RecordSet();
        String fieldid = "" ;
        if(!"".equals(billid) && !"".equals(fieldname)){


            String tablename = "" ;
            String sql ="  select tablename from workflow_bill where id= " + billid ;
            rs.execute(sql);
            if(rs.next()){
                tablename = "formtable_main_"+ Util.null2String(rs.getString("tablename"));
            }

            String where  = "" ;
            if(!"".equals(detailtable) && !"".equals(tablename)){
                where = " and detailtable='"+tablename+"_"+detailtable+"' " ;
            }

            String fieldSql = " select id from workflow_billfield where billid="+billid+" and fieldname='"+fieldname+"' " + where ;
            rs.execute(fieldSql);
            if(rs.next()){
                fieldid = Util.null2String(rs.getString("id"));
            }
        }
        return fieldid;
    }


    /***
     *
     * @param tablename
     * @param fieldname
     * @param detailtable
     * @return
     */
    public String getbillField(String tablename,String fieldname,String detailtable)
    {
        String where  = "" ;
        if(!"".equals(detailtable)){
            where = " and detailtable='"+tablename+"_"+detailtable+"' " ;
        }
        RecordSet rs = new RecordSet();
        String fieldid = "" ;
        if(!"".equals(tablename) && !"".equals(fieldname)){
            String fieldSql = " select b.id from workflow_bill a,workflow_billfield b \n" +
                              " where a.id=b.billid and a.tablename='"+tablename+"' and b.fieldname='"+fieldname+"' "+where;
            rs.execute(fieldSql);
            if(rs.next()){
                fieldid = Util.null2String(rs.getString("id"));
            }
        }
        return fieldid;
    }


    /***
     *
     * @param requestid
     * @param fieldname
     * @param detailtable
     * @return
     */
    public String getbillFieldByRequestid(String requestid,String fieldname,String detailtable){
        RecordSet rs = new RecordSet();
        String fieldid = "" ;
        if(!"".equals(requestid) && !"".equals(fieldname)){
            String formid = "" ;
            String billid = "" ;
            String sql ="  select h.formid as billid,abs(h.formid) as formid\n" +
                    "  from workflow_base h\n" +
                    "  inner join workflow_requestbase l on l.workflowid = h.id\n" +
                    "  where l.requestid= " + requestid ;
            rs.execute(sql);
            if(rs.next()){
                formid = Util.null2String(rs.getString("formid"));
                billid = Util.null2String(rs.getString("billid"));
            }
            String where  = "" ;
            if(!"".equals(detailtable) && !"".equals(formid)){
                where = " and detailtable='formtable_main_"+formid+"_"+detailtable+"' " ;
            }
            String fieldSql = " select id from workflow_billfield where billid="+billid+" and fieldname='"+fieldname+"' " + where ;
            rs.execute(fieldSql);
            if(rs.next()){
                fieldid = Util.null2String(rs.getString("id"));
            }
        }
        return fieldid;
    }


    /***
     *
     * @param fieldid
     * @param value
     * @return
     */
    public String getSelectItemName(String fieldid,String value){
        RecordSet rs = new RecordSet();
        String selectname = "" ;
        try{
            String sql = " select selectname from workflow_selectitem where fieldid="+fieldid+" and selectvalue= '"+value+"'";
            rs.execute(sql);
            if(rs.next()){
                selectname = Util.null2String(rs.getString("selectname"));
            }
        }catch (Exception e){

        }
        return selectname;

    }


    /***
     *
     * @param fieldid
     * @param name
     * @return
     */
    public String getSelectItemValue(String fieldid,String name){
        RecordSet rs = new RecordSet();
        String selectvalue = "" ;
        try{
            String sql = " select selectvalue from workflow_selectitem where fieldid="+fieldid+" and selectname='"+name+"'";
            System.out.println("sql:"+sql);
            rs.execute(sql);
            if(rs.next()){
                selectvalue = Util.null2String(rs.getString("selectvalue"));
            }
        }catch (Exception e){

        }
        return selectvalue;

    }
}
