package weaver.interfaces.htsc.GlobatTools;

import com.engine.integration.util.StringUtils;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;

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

public class DBTools {

    public static List getsqltolist(String sql, Object...value){
        RecordSet rs=new RecordSet();
        List<String> list=new ArrayList<>();

        String where="";
        List<Map<String,Object>> listreturn=new ArrayList<>();

        if("".equals(value) ||value.length<=0){
            rs.execute(sql);
        }else{
            rs.executeQuery(sql,value);
        }

        while(rs.next()){
            Map<String,Object> map=new HashMap<>();
            String[] colnums=rs.getColumnName();
            for (String colnumvalue:colnums ) {
                map.put(colnumvalue.toLowerCase(),rs.getString(colnumvalue));
            }
            listreturn.add(map);

        }
        return listreturn;
    }
    /*
     * @title getsqltomap
     * @remark 备注信息
     * @author xf
     * @Params：
       * @param: sql
     * @param: value
     * @updateTime 2021/9/17 9:49
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     **/
    public static Map<String,Object> getsqltomap(String sql, Object...value){
        RecordSet rs=new RecordSet();
        Map<String,Object> map=new HashMap<>();

        if(value==null ||"".equals(value)){
            rs.execute(sql);
        }else{
            rs.executeQuery(sql,value);
        }

        if(rs.next()){
            String[] colnums=rs.getColumnName();
            for (String colnumvalue:colnums ) {
                map.put(colnumvalue.toLowerCase(),rs.getString(colnumvalue));
            }

        }
        return map;
    }

    public static Map<String,Object> othersqltomap(String otherkey, String sql){
        if(StringUtils.isBlank(otherkey) || StringUtils.isBlank(sql)){
            return null;
        }
        RecordSetDataSource rs=new RecordSetDataSource(otherkey);
        Map<String,Object> map=new HashMap<>();
        rs.execute(sql);
        if(rs.next()){
            String[] colnums=rs.getColumnName();
            for (String colnumvalue:colnums ) {
                map.put(colnumvalue.toLowerCase(),rs.getString(colnumvalue));
            }
        }
        return map;
    }

}
