package weaver.interfaces.zw.utils;

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommonUtil {

    /**
     *
     * @param sql
     * @return
     */
    public static List<Map<String,String>> queryListBySql(String sql){
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        List<Map<String,String>> list = new ArrayList<Map<String,String>>();
        try{
            if(StringUtils.isNotBlank(sql)){
                Map<String,String> map = new HashMap<String,String>();
                rs.executeQuery(sql);
                String[] column = rs.getColumnName();
                while (rs.next()){
                    map = new HashMap<String,String>();
                    for(int i=0;i<column.length;i++){
                        String value = rs.getString(column[i]);
                        map.put(column[i],value);
                    }
                    if(!map.isEmpty() && map.size()>0){
                        list.add(map);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            bb.writeLog("queryListBySql-e:"+e);
        }
        return list;
    }

    /***
     *
     * @param sql
     * @return
     */
    public static Map<String,String> queryMapBySql(String sql){
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        Map<String,String> map = new HashMap<String,String>();
        try{
            if(StringUtils.isNotBlank(sql)){
                rs.executeQuery(sql);
                String[] column = rs.getColumnName();
                if (rs.next()){
                    for(int i=0;i<column.length;i++){
                        String value = rs.getString(column[i]);
                        map.put(column[i],value);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            bb.writeLog("queryMapBySql-e:"+e);
        }
        return map;
    }

    /***
     *
     * @param workflowid
     * @return
     */
    public static String getFormtableByWorkflowId(String workflowid){

        String formtable = "" ;
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        try{
            if(StringUtils.isNotBlank(workflowid)){
                String sql = "select abs(formid) as formid from workflow_base where id= "+workflowid ;
                bb.writeLog("sql:"+sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    String formid = Util.null2String(rs.getString("formid"));
                    bb.writeLog("formid:"+formid);
                    if(StringUtils.isNotBlank(formid)){
                        formtable = "formtable_main_"+formid ;
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            bb.writeLog("getFormtableByWorkflowId:"+e);
        }

        return formtable;

    }

    /**
     *
     * @param requestid
     * @return
     */
    public static String getFormtableByRequestId(String requestid){
        String formtable = "" ;
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        try{
            if(StringUtils.isNotBlank(requestid)){
                String sql =" select abs(formid) from workflow_base t \n" +
                        " inner join workflow_requestbase h on h.workflowid = t.id \n" +
                        " where requestid=  "+requestid ;
                bb.writeLog("sql2:"+sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    String formid = Util.null2String(rs.getString("formid"));
                    if(StringUtils.isNotBlank(formid)){
                        formtable = "formtable_main_"+formid ;
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            bb.writeLog("getFormtableByRequestId:"+e);
        }

        return formtable;
    }
}
