package cn.com.capcloud.api.action.service.util;

import cn.com.capcloud.ccui.core.common.json.JackJSONObject;
import cn.com.capcloud.ccui.core.common.utils.StringUtil;
import cn.com.capcloud.core.noentitydb.DBCheck;
import cn.com.capcloud.core.system.user.User;
import org.springframework.jdbc.core.JdbcTemplate;

import java.lang.reflect.Method;
import java.util.*;

/**
 * @Author LiaoJiaHui
 * @Create 2026/04/27
 */
public class DictUtil {

    /**
     * @param dictCode 内部字典编码
     */
    public static List<Map<String, Object>> getDictInfoByDictCodeToList(String dictCode, JdbcTemplate jdbcTemplate) {
        String sqlChar= DBCheck.getIdentifierQuoteChar();
        String sql="SELECT dict_key as "+sqlChar+"key"+sqlChar+",dict_value as "+sqlChar+"value"+sqlChar+" FROM  tbl_data_dict_info  \n" +
                "WHERE STATUS=0 AND page_parent_uuid=\n" +
                "(SELECT uuid FROM tbl_data_dict WHERE STATUS=0 AND dict_code='"+dictCode+"') \n" +
                "AND dict_status=0 order by dict_sort asc ";
        List<Map<String, Object>> queryDictData = jdbcTemplate.queryForList(sql);
        return queryDictData;
    }
    /**
     * @param dictCode 外部字典编码
     */
    public static List<Map<String, Object>> getOutDictInfoByDictCodeToList(String dictCode, JdbcTemplate jdbcTemplate, User user) {
        String sql=getOutDictWhereSql(dictCode,jdbcTemplate,user);
        List<Map<String, Object>> returnQueryDictData=new ArrayList<>();
        if(sql!=null){
            List<Map<String, Object>> queryDictData=jdbcTemplate.queryForList(sql);
            //将key类型转为字符串
            for (int i = 0; i < queryDictData.size(); i++) {
                Map<String,Object> map=new HashMap<>();
                String key=String.valueOf(queryDictData.get(i).get("key"));
                String value=String.valueOf(queryDictData.get(i).get("value"));
                map.put("key",key);
                map.put("value",value);
                returnQueryDictData.add(map);
            }
        }
        return returnQueryDictData;
    }
    public static List<Map<String, Object>> getOutDictInfoByDictCodeToList(String dictCode, JdbcTemplate jdbcTemplate) {
        return getOutDictInfoByDictCodeToList(dictCode,jdbcTemplate,null);
    }
    /**
     * @param dictCode 第三方字典编码
     */
    public static List<Map<String, Object>> getTPDictInfoByDictCodeToList(String dictCode, JdbcTemplate jdbcTemplate) {
        List<Map<String, Object>> returnQueryDictData= null;
        try {
            String querySql="select * from tbl_data_dict_tp where tp_dict_code='"+dictCode+"'";
            List<Map<String, Object>> dictList=jdbcTemplate.queryForList(querySql);
            String myActionClass=String.valueOf(dictList.get(0).get("my_action_class"));
            String methodName=String.valueOf(dictList.get(0).get("method_name"));
            String whereJson=String.valueOf(dictList.get(0).get("where_json"));
            Class actionClass = Class.forName(myActionClass);
            Object myActionClassInstance=actionClass.newInstance();
            Method method = actionClass.getMethod(methodName,String.class);
            Object retObj=method.invoke(myActionClassInstance,whereJson);
            List<Map<String, Object>> queryDictData= (List<Map<String, Object>>) retObj;
            returnQueryDictData = new ArrayList<>();
            //将key类型转为字符串
            for (int i = 0; i < queryDictData.size(); i++) {
                Map<String,Object> map=new HashMap<>();
                String key=String.valueOf(queryDictData.get(i).get("key"));
                String value=String.valueOf(queryDictData.get(i).get("value"));
                map.put("key",key);
                map.put("value",value);
                returnQueryDictData.add(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnQueryDictData;
    }
    /**
     * @param treeCode tree字典编码
     */
    public static List<Map<String, Object>> getTreeInfoByTreeCodeToList(String treeCode,String pId,User user, JdbcTemplate jdbcTemplate) {
        String queryTreeInfo="SELECT * FROM tbl_data_tree WHERE status=0 and tree_code='"+treeCode+"'";
        List<Map<String, Object>> treeList=jdbcTemplate.queryForList(queryTreeInfo);
        if(treeList.size()==0){
            return new ArrayList<>();
        }
        Map<String, Object> treeInfo=treeList.get(0);
        if(StringUtil.isEmpty(pId)){
            pId=String.valueOf(treeInfo.get("t_first_id_value"));
        }
        String whereSql="";
        String fromSql="";
        if(StringUtil.isNotEmpty(pId)){
            if(DBCheck.isH2()){

            }else{
                String queryFunctionSql="SHOW FUNCTION STATUS WHERE NAME='"+treeCode+"' AND DB='"+ DBCheck.getDatabaseName()+"'";
                if(DBCheck.isKingbase()){
                    queryFunctionSql="SELECT proname, proargtypes FROM pg_proc WHERE proname = '"+treeCode+"';";
                }
                if(jdbcTemplate.queryForList(queryFunctionSql).size()==0){
                    initTreeFunction(treeInfo,jdbcTemplate);
                }
                fromSql+=",(SELECT "+treeCode+"('"+pId+"') ids)t";
                whereSql+=" AND (FIND_IN_SET("+treeInfo.get("t_p_id")+",ids) OR "+treeInfo.get("t_id")+"='"+pId+"')";
            }
        }
        String name=String.valueOf(treeInfo.get("t_name"));
        String[] strArray=name.split(",");
        if(strArray.length>=2){
            String split=String.valueOf(treeInfo.get("split"));
            name="CONCAT(";
            for (int i = 0; i < strArray.length; i++) {
                name+=strArray[i]+",";
                name+="'"+split+"',";
            }
            name=name.substring(0,name.length()-(4+split.length()));//4是：,'', #所以减4，还需减去分割符的长度，也就是4+分隔符的长度
            name+=")";
        }
        String tWhereJsonStr=String.valueOf(treeInfo.get("t_where_json"));
        if(StringUtil.isNotEmpty(tWhereJsonStr)){
            JackJSONObject json= JackJSONObject.fromObject(tWhereJsonStr);
            for(Object key:json.keySet()) {
                String value=String.valueOf(json.get(key));
                if((value.equals("${org_id}")||value.equals("${user_id}"))&&user==null){
                    continue;
                }
                whereSql+=" and "+key+"='"+json.get(key)+"'";
            }
            whereSql+=" order by "+treeInfo.get("t_default_sort_field") +" "+treeInfo.get("t_default_sort_type");
        }
        if(user!=null){
            whereSql=whereSql.replace("${org_id}",""+user.getOrgId());
            whereSql=whereSql.replace("${user_id}",""+user.getId());
        }
        String sqlChar= DBCheck.getIdentifierQuoteChar();
        String queryTreeSql="SELECT " +sqlChar+
                treeInfo.get("t_id")+sqlChar+" as id,"+sqlChar+
                // H2 要求 AS 别名中的大写字母需用双引号包裹（如 "pId"），否则会转为小写。
                treeInfo.get("t_p_id")+sqlChar+" as "+sqlChar+"pId"+sqlChar+"," +sqlChar+
                name+sqlChar+" as name," +sqlChar+
                treeInfo.get("t_level")+sqlChar+" as level " +
                " FROM "+treeInfo.get("t_table_name") +fromSql+
                " WHERE 1=1 " +whereSql;
        List<Map<String, Object>> queryDictData = jdbcTemplate.queryForList(queryTreeSql);
        return queryDictData;
    }
    public static void initTreeFunction(Map<String,Object> treeInfo,JdbcTemplate jdbcTemplate){
        String treeCode=String.valueOf(treeInfo.get("tree_code"));
        String tId=String.valueOf(treeInfo.get("t_id"));
        String tPId=String.valueOf(treeInfo.get("t_p_id"));
        String tTableName=String.valueOf(treeInfo.get("t_table_name"));
        //kingbase 兼容oracle模式的语法不同
        if(DBCheck.isKingbase()&&DBCheck.getCachedJpaDatabase().equals("oracle")){
            String dropSql="DROP FUNCTION IF EXISTS "+treeCode+"(integer)";
            jdbcTemplate.execute(dropSql);
            String initSql="CREATE OR REPLACE FUNCTION "+treeCode+"(p_id TEXT)\n" +
                    "RETURNS VARCHAR AS $$\n" +
                    "WITH RECURSIVE tree_"+tTableName+" AS (\n" +
                    "    SELECT "+tId+"\n" +
                    "    FROM "+tTableName+"\n" +
                    "    WHERE "+tId+" = p_id\n" +
                    "    UNION ALL\n" +
                    "    SELECT m."+tId+"\n" +
                    "    FROM "+tTableName+" m\n" +
                    "    JOIN tree_"+tTableName+" mt ON m."+tPId+" = mt."+tId+"\n" +
                    ")\n" +
                    "SELECT ',' || STRING_AGG("+tId+"::VARCHAR, ',') \n" +
                    "FROM tree_"+tTableName+";\n" +
                    "$$ LANGUAGE SQL;" ;
            jdbcTemplate.execute(initSql);
            return;
        }
        //kingbase 兼容MySQL模式的正常执行下面的即可
        String dropSql="DROP FUNCTION IF EXISTS "+treeCode+" \n";
        jdbcTemplate.execute(dropSql);
        String initSql="CREATE FUNCTION "+treeCode+"(in_id TEXT) RETURNS TEXT \n" +
                "BEGIN \n" +
                " DECLARE ids TEXT DEFAULT ''; \n" +
                " DECLARE tempids TEXT; \n" +
                " \n" +
                " SET tempids = in_id; \n" +
                " WHILE tempids IS NOT NULL DO \n" +
                "  SET ids = CONCAT_WS(',',ids,tempids); \n" +
                "  SELECT GROUP_CONCAT("+tId+") INTO tempids FROM "+tTableName+" WHERE FIND_IN_SET("+tPId+",tempids)>0;  \n" +
                " END WHILE; \n" +
                " RETURN ids; \n" +
                "END  \n" ;
        jdbcTemplate.execute(initSql);
    }
    /**
     * 查询外部字典
     * isReversal 是false dict_key做为key
     * isReversal 是true dict_value做为key
     */
    public static Map<String, Object> getOutDictInfoByDictCodeToMap(String dictCode, Boolean isReversal, JdbcTemplate jdbcTemplate) {
        List<Map<String, Object>> queryDictData = jdbcTemplate.queryForList(getOutDictWhereSql(dictCode,jdbcTemplate,null));
        Map<String, Object> dictMap=new HashMap<>();
        for(Map<String,Object> map : queryDictData){
            Iterator<String> it=  map.keySet().iterator();
            Object dictKey="";
            Object dictValue="";
            while(it.hasNext()){
                String key = it.next();
                if(key.equals("key")){
                    dictKey=map.get(key);
                }else if(key.equals("value")){
                    dictValue= map.get(key);
                }
            }
            if(isReversal){
                dictMap.put(dictValue+"",dictKey);
            }else{
                dictMap.put(dictKey+"",dictValue);
            }
        }
        return dictMap;
    }
    /**
     * isReversal 是false dict_key做为key
     * isReversal 是true dict_value做为key
     */
    public static Map<String, Object> getDictInfoByDictCodeToMap(String dictCode, Boolean isReversal, JdbcTemplate jdbcTemplate) {
        String sql="SELECT dict_key as 'key',dict_value as 'value' FROM  tbl_data_dict_info  \n" +
                "WHERE STATUS=0 AND page_parent_uuid=\n" +
                "(SELECT uuid FROM tbl_data_dict WHERE STATUS=0 AND dict_code='"+dictCode+"') \n" +
                "AND dict_status=0";
        List<Map<String, Object>> queryDictData = jdbcTemplate.queryForList(sql);
        Map<String, Object> dictMap=new HashMap<>();
        for(Map<String,Object> map : queryDictData){
            Iterator<String> it=  map.keySet().iterator();
            Object dictKey="";
            Object dictValue="";
            while(it.hasNext()){
                String key = it.next();
                if(key.equals("key")){
                    dictKey=map.get(key);
                }else if(key.equals("value")){
                    dictValue= map.get(key);
                }
            }
            if(isReversal){
                dictMap.put(dictValue+"",dictKey);
            }else{
                dictMap.put(dictKey+"",dictValue);
            }
        }
        return dictMap;
    }
    /**
     * 展示选中名称
     * isReversal 是false dict_key做为key
     * isReversal 是true dict_value做为key
     */
    public static Map<String, Object> getTreeDictInfoByDictCodeToMap(String dictCode, Boolean isReversal,User user, JdbcTemplate jdbcTemplate) {
        String sql="SELECT * FROM tbl_data_tree WHERE status=0 and tree_code='"+dictCode+"'";
        List<Map<String, Object>> treeList=jdbcTemplate.queryForList(sql);
        Map<String, Object> treeInfo=treeList.get(0);
        String name=String.valueOf(treeInfo.get("t_name"));
        String[] strArray=name.split(",");
        if(strArray.length>=2){
            String split=String.valueOf(treeInfo.get("split"));
            name="CONCAT(";
            for (int i = 0; i < strArray.length; i++) {
                name+=strArray[i]+",";
                name+="'"+split+"',";
            }
            name=name.substring(0,name.length()-(4+split.length()));//4是：,'', #所以减4，还需减去分割符的长度，也就是4+分隔符的长度
            name+=")";
        }
        String queryTreeSql="SELECT " +
                treeInfo.get("t_id")+" as 'key',"+
                name+" as 'value'" +
                " FROM "+treeInfo.get("t_table_name");
        String tWhereJsonStr=String.valueOf(treeInfo.get("t_where_json"));
        queryTreeSql+=" where 1=1 ";
        if(StringUtil.isNotEmpty(tWhereJsonStr)){
            JackJSONObject json= JackJSONObject.fromObject(tWhereJsonStr);
            for(Object key:json.keySet()) {
                String value=String.valueOf(json.get(key));
                if((value.equals("${org_id}")||value.equals("${user_id}"))&&user==null){
                    continue;
                }
                queryTreeSql+=" and "+key+"='"+json.get(key)+"'";
            }
            queryTreeSql+=" order by "+treeInfo.get("t_default_sort_field") +" "+treeInfo.get("t_default_sort_type");
        }
        if(user!=null){
            queryTreeSql=queryTreeSql.replace("${org_id}",""+user.getOrgId());
            queryTreeSql=queryTreeSql.replace("${user_id}",""+user.getId());
        }
        List<Map<String, Object>> queryDictData = jdbcTemplate.queryForList(queryTreeSql);
        Map<String, Object> dictMap=new HashMap<>();
        for(Map<String,Object> map : queryDictData){
            Iterator<String> it=  map.keySet().iterator();
            Object dictKey="";
            Object dictValue="";
            while(it.hasNext()){
                String key = it.next();
                if(key.equals("key")){
                    dictKey=map.get(key);
                }else if(key.equals("value")){
                    dictValue= map.get(key);
                }
            }
            if(isReversal){
                dictMap.put(dictValue+"",dictKey);
            }else{
                dictMap.put(dictKey+"",dictValue);
            }
        }
        return dictMap;
    }
    /**
     * 展示完整名称
     * isReversal 是false id做为key
     * isReversal 是true value做为key
     */
    public static Map<String, Object> getTreeDictInfoByDictCodeToFullMap(String dictCode, Boolean isReversal, JdbcTemplate jdbcTemplate) {
        return getTreeDictInfoByDictCodeToFullMap(dictCode,isReversal,null,jdbcTemplate,"");
    }
    public static Map<String, Object> getTreeDictInfoByDictCodeToFullMap(String dictCode, Boolean isReversal,User user, JdbcTemplate jdbcTemplate,String treeShowFullSplit) {
        String sql="SELECT * FROM tbl_data_tree WHERE status=0 and tree_code='"+dictCode+"'";
        List<Map<String, Object>> treeList=jdbcTemplate.queryForList(sql);
        Map<String, Object> treeInfo=treeList.get(0);
        String name=String.valueOf(treeInfo.get("t_name"));
        String[] strArray=name.split(",");
        if(strArray.length>=2){
            String split=String.valueOf(treeInfo.get("split"));
            name="CONCAT(";
            for (int i = 0; i < strArray.length; i++) {
                name+=strArray[i]+",";
                name+="'"+split+"',";
            }
            name=name.substring(0,name.length()-(4+split.length()));//4是：,'', #所以减4，还需减去分割符的长度，也就是4+分隔符的长度
            name+=")";
        }
        String queryTreeSql="SELECT " +
                treeInfo.get("t_id")+" as id,"+
                treeInfo.get("t_p_id")+" as pId," +
                name+" as name," +
                treeInfo.get("t_level")+" as level " +
                " FROM "+treeInfo.get("t_table_name") ;
        String tWhereJsonStr=String.valueOf(treeInfo.get("t_where_json"));
        queryTreeSql+=" where 1=1 ";
        if(StringUtil.isNotEmpty(tWhereJsonStr)){
            JackJSONObject json=JackJSONObject.fromObject(tWhereJsonStr);
            for(Object key:json.keySet()) {
                String value=String.valueOf(json.get(key));
                if((value.equals("${org_id}")||value.equals("${user_id}"))&&user==null){
                    continue;
                }
                queryTreeSql+=" and "+key+"='"+json.get(key)+"'";
            }
            queryTreeSql+=" order by "+treeInfo.get("t_default_sort_field") +" "+treeInfo.get("t_default_sort_type");
        }
        if(user!=null){
            queryTreeSql=queryTreeSql.replace("${org_id}",""+user.getOrgId());
            queryTreeSql=queryTreeSql.replace("${user_id}",""+user.getId());
        }
        List<Map<String, Object>> queryDictData = jdbcTemplate.queryForList(queryTreeSql);
        Map<String, Map<String,Object>> idMap=new HashMap<>();
        for (int i = 0; i < queryDictData.size(); i++) {
            idMap.put(queryDictData.get(i).get("id")+"",queryDictData.get(i));
        }
        Map<String, Object> dictMap=new HashMap<>();
        for (int i = 0; i < queryDictData.size(); i++) {
            String level=String.valueOf(queryDictData.get(i).get("level"));
            if(!level.equals("0")){
                String id=String.valueOf(queryDictData.get(i).get("id"));
                String showValue=getTreeParentName(idMap,id,"",treeShowFullSplit);
                if(isReversal){
                    dictMap.put(showValue,id);
                }else{
                    dictMap.put(id,showValue);
                }
            }
        }
        return dictMap;
    }
    public static String getTreeParentName(Map<String,Map<String,Object>> idMap,String id,String showValue,String treeShowFullSplit){
        String name=String.valueOf(idMap.get(id).get("name"));
        String level=String.valueOf(idMap.get(id).get("level"));
        if(level.equals("1")){
            if(showValue.equals("")){
                return name+showValue;
            }else{
                return name+treeShowFullSplit+showValue;
            }
        }else{
            String pId=String.valueOf(idMap.get(id).get("pId"));
            if(showValue.equals("")){
                return getTreeParentName(idMap,pId,name+showValue,treeShowFullSplit);
            }else{
                return getTreeParentName(idMap,pId,name+treeShowFullSplit+showValue,treeShowFullSplit);
            }
        }
    }
    /**
     * 将一些字典值转换，数据库存储的是字典需要转换一下。
     */
    public static List<Map<String,Object>> changeFormDict(List<Map<String,Object>> fieldList, List<Map<String,Object>> dataList, JdbcTemplate jdbcTemplate) {
        for (int i = 0; i < fieldList.size(); i++) {//这个循环是提前将dict字典查询出来，避免每次循环都查询数据库
            Map<String,Object> field=fieldList.get(i);
            String code=String.valueOf(field.get("dict_code"));
            if(StringUtil.isNotEmpty(code)){
                if(code.indexOf("tree_")==0){//tree字典
                    String treeShowValueType=String.valueOf(field.get("tree_show_value_type"));
                    Map<String,Object> dictMap;
                    if(treeShowValueType.equals("full")){
                        String treeShowFullSplit=String.valueOf(field.get("tree_show_full_split"));
                        dictMap=getTreeDictInfoByDictCodeToFullMap(code,false,null,jdbcTemplate,treeShowFullSplit);
                    }else{
                        dictMap=getTreeDictInfoByDictCodeToMap(code,false,null,jdbcTemplate);
                    }
                    fieldList.get(i).put("dictMap",dictMap);
                }else if(code.indexOf("o_")==0){//外部字典不放缓存。
                    Map<String,Object> dictMap=getOutDictInfoByDictCodeToMap(code,false,jdbcTemplate);
                    fieldList.get(i).put("dictMap",dictMap);
                }else{//内部字典
                    Map<String,Object> dictMap=getDictInfoByDictCodeToMap(code,false,jdbcTemplate);
                    fieldList.get(i).put("dictMap",dictMap);
                }
            }
        }
        for (int i = 0; i < dataList.size(); i++) {
            Map<String,Object> dataMap=dataList.get(i);
            for (int j = 0; j < fieldList.size(); j++) {
                Map<String,Object> field=fieldList.get(j);
                String code=String.valueOf(field.get("dict_code"));
                String showType=String.valueOf(field.get("show_type"));
                String dictValueSplit=String.valueOf(field.get("dict_value_split"));
                String name=String.valueOf(field.get("name"));
                String value=String.valueOf(dataList.get(i).get(name));
                if(StringUtil.isNotEmpty(code)&&StringUtil.isNotEmpty(value)){
                    Map<String,Object> dictMap= (Map<String, Object>) field.get("dictMap");
                    String newValue="";
                    if(showType.equals("checkbox")||showType.equals("selectCheckbox")||showType.equals("treeCheckBox")){
                        if(dictValueSplit.equals("null")||dictValueSplit.length()==0){
                            dictValueSplit=",";
                        }
                        String[] checkboxArray=String.valueOf(dataMap.get(name)).split(",");
                        for (int k = 0; k < checkboxArray.length; k++) {
                            if(checkboxArray[k].equals("")){
                                continue;
                            }
                            newValue+=dictMap.get(checkboxArray[k])+" "+dictValueSplit+" ";
                        }
                        newValue=newValue.substring(0,newValue.length()-3);
                        dataList.get(i).put(name,newValue);
                    }else{
                        newValue=String.valueOf(dictMap.get(dataMap.get(name)));
                        dataList.get(i).put(name,newValue);
                    }
                }
            }
        }
        return dataList;
    }

    public static String getOutDictWhereSql(String oDictCode,JdbcTemplate jdbcTemplate, User user){
        String queryDictInfo="SELECT * FROM tbl_data_dict_out WHERE o_dict_code='"+oDictCode+"'";
        List<Map<String, Object>> treeList=jdbcTemplate.queryForList(queryDictInfo);
        String whereSql=null;
        if(treeList.size()>0){
            Map<String, Object> dictInfo=treeList.get(0);
            String dictView=String.valueOf(dictInfo.get("o_dict_view"));
            String split=String.valueOf(dictInfo.get("split"));
            if(dictView.contains(",")){
                String[] arr = dictView.split(",");
                dictView="CONCAT(";
                for (int i = 0; i < arr.length; i++) {
                    dictView+=arr[i]+",'"+split+"',";
                }
                dictView=dictView.substring(0,dictView.length()-(4+split.length()));
                dictView+=")";
            }
            if(DBCheck.isKingbase()||DBCheck.isH2()){
                whereSql="SELECT "+dictInfo.get("o_dict_option")+" AS \"key\","+dictView+" AS \"value\" FROM "+dictInfo.get("o_dict_table");
            }else{
                whereSql="SELECT "+dictInfo.get("o_dict_option")+" AS 'key',"+dictView+" AS 'value' FROM "+dictInfo.get("o_dict_table");
            }

            String oWhereJsonStr=String.valueOf(dictInfo.get("o_where_json"));
            String oWhereSql=String.valueOf(dictInfo.get("o_where_sql"));
            whereSql+=" where 1=1 ";
            if(StringUtil.isNotEmpty(oWhereSql)){
                oWhereSql=oWhereSql.replaceAll("&nbsp;"," ");
                oWhereSql=oWhereSql.replaceAll("&quot;","'");
                whereSql+=oWhereSql;
            }
            if(StringUtil.isNotEmpty(oWhereJsonStr)){
                JackJSONObject json=JackJSONObject.fromObject(oWhereJsonStr);
                for(Object key:json.keySet()) {
                    String value=String.valueOf(json.get(key));
                    if((value.equals("${org_id}")||value.equals("${user_id}"))&&user==null){
                        continue;
                    }
                    whereSql+=" and "+key+"='"+json.get(key)+"'";
                }
                whereSql+=" order by "+dictInfo.get("o_default_sort_field") +" "+dictInfo.get("o_default_sort_type");
            }
            if(user!=null){
                whereSql=whereSql.replace("${org_id}",""+user.getOrgId());
                whereSql=whereSql.replace("${user_id}",""+user.getId());
            }
            whereSql=whereSql.replace("${database_id}",DBCheck.getDatabaseId());
        }
        return whereSql;
    }

}
