package psdi.app.iface.comm;
import com.dbms.dbcon.DBCon;
import com.dbms.dbcon.QueryResult;
import com.dbms.dbcon.ResultRow;
import psdi.security.ConnectionKey;

import java.io.PrintStream;
import java.lang.reflect.Method;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dbms.dbcon.DBCon;
public class getDataRecord {
    //HashMap hmalrAddproject;
    public getDataRecord()
    {
        //hmalrAddproject = new HashMap();
    }
    //GeneralQuery
    public static JSONObject SearchGeneral(String userid, String sql, String where, int index, int size, String orderby, ConnectionKey connectionKey,String conWhere)
            throws Exception
    {
        if(where == null)
            where = "";
        sql = (where.length() >0)?sql + " " + conWhere + " "+ where:sql;
        sql = (orderby.length() >0)?sql + " order by " + orderby:sql;
        System.out.println("==================execuse SQL="+sql);
        //sql = sql + " where " + where;
        //sql = (new StringBuilder(String.valueOf(sql))).append(" order by ").append(orderby).toString();
        ArrayList dataAll = DBCon.DBSelect(sql,connectionKey);
        if(dataAll == null) return null;
        //ArrayList data = new ArrayList();
        JSONArray data = new JSONArray();//json数组
        index = (index > 0)?index-1:index;
        int start = index * size;
        int end = start + size;
        int i = 0;
        int nloop = dataAll.size();
        System.out.println("start =" + start + ", end =" + end);
        for(int l = nloop; i < l; i++)
        {
            HashMap record = (HashMap)dataAll.get(i);
            JSONObject jsonRecord = new JSONObject();//json数组里面的数据记录
            if(record != null)
            {
                if(start <= i && i < end) {
                    jsonRecord.putAll(record);
                    data.add(jsonRecord);
                }
            }
        }
        //HashMap result = new HashMap();
        JSONObject result = new JSONObject();//返回值
        result.put("result", data);
        result.put("userid", userid);
        int total = nloop;
        //int showcountInt = Integer.parseInt(showcount);
        int totalPage = total % size ==0?total/size:(total/size+1);
        result.put("totalPage",String.valueOf(totalPage));
        return result;
    }
}
