package com.qmth.qrzk.repository.common;

import org.josql.Query;
import org.josql.QueryResults;
import java.util.*;

/**
 * <p>Title: ListSqlUtil.java</p>
 * <p>Description: ListSqlUtil</p>
 * <p>Copyright: Copyright 2005 COSTMA (SHEN ZHEN) INC.</p>
 * <p>Company: COSTMA (SHEN ZHEN) INC.</p>
 * @author <a href="mailto:sunrao@91bi.com">sunrao</a>
 * @version 1.0
 */

public class ListSqlUtil {
    public ListSqlUtil() {
    }

    /**
     * 根据SQL查询对像
     * @param list List
     * @param sql String
     * @return List
     */
    public static List getList(List list, String sql) {
        List list_new = new ArrayList();
        try {
            Query q = new Query();
            q.addFunctionHandler(new SqlFuncUtil());//注入方法
            q.parse(sql);
            QueryResults qr = q.execute(list);
            list_new = qr.getResults();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("---SQL:ERROR----"+sql);
        }
        return list_new;
    }

    public List getGroup(List list,String sql,String keys) {
        List listx= new ArrayList();
        try {
            Query q = new Query();
            q.addFunctionHandler(new SqlFuncUtil());//注入方法
            q.parse(sql);
            QueryResults qr = q.execute(list);
            Map m=qr.getGroupByResults();
            Iterator iter = m.keySet().iterator();

            String[] par=keys.split(",");
            while (iter.hasNext()) {
                List key = (List) iter.next();
                List res = (List) m.get(key);
                List s=(List)res.get(0);
                Map mm=new HashMap();
                for(int i=0;i<par.length;i++){
                    mm.put(par[i],s.get(i));
                }
                listx.add(mm);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listx;
    }

    public static void main(String[] args) {
        ListSqlUtil listsqlutil = new ListSqlUtil();
    }
}
