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

import cn.com.capcloud.api.action.service.util.TextUtil;
import cn.com.capcloud.ccui.core.common.json.JackJSON;
import cn.com.capcloud.ccui.core.common.json.JackJSONArray;
import cn.com.capcloud.ccui.core.common.json.JackJSONObject;
import cn.com.capcloud.ccui.core.common.utils.StringUtil;
import cn.com.capcloud.core.cache.KHCRedisCache;
import cn.com.capcloud.core.noentitydb.BaseDao;
import cn.com.capcloud.core.noentitydb.DBCheck;
import cn.com.capcloud.core.system.user.User;
import cn.com.capcloud.core.system.user.UserService;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class DataSourceService {

    @Autowired
    private ApiService apiService;
    @Autowired
    private UserService userService;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private KHCRedisCache khcRedisCache;
    @Autowired
    private DictService dictService;

    /**
     * id 优先级最高，其次是dsCode
     */
    public static Map<String, Object> findDataSource(String uuid,String dsCode, JdbcTemplate jdbcTemplate) {
        String sql="SELECT * FROM tbl_data_source WHERE STATUS =0 ";
        if(uuid!=null){
            sql+=" AND uuid = '"+uuid+"'";
        }else if(dsCode!=null){
            sql+=" AND ds_code = '"+dsCode+"'";
        }
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        if(list.size()==0){
            return null;
        }else{
            return list.get(0);
        }
    }

    public String getDataSourceUuid(String jsonStr, JackJSONObject retJson, JackJSONObject dataJson) {
        String dsCode=apiService.getJsonValueByKey(jsonStr,retJson,"dsCode");
        if("-1".equals(dsCode)){
            return "-1";
        }else{
            Map<String,Object> map=findDataSourceByCode(dsCode);
            if(map==null){
                retJson.putAll(JackJSONObject.fromObject(apiService.getReturnDataByCode("7040")));
                retJson.put("message",retJson.get("message")+" dsCode："+dsCode);
                return "-1";
            }else{
                return String.valueOf(map.get("uuid"));
            }
        }
    }

    //附件上传使用
    public String getDataSourceUuid(JackJSONObject retJson, HttpServletRequest request) {
        String dsCode=apiService.getRequestValueByKey(request,retJson,"dsCode");
        if("-1".equals(dsCode)){
            return "-1";
        }else{
            Map<String,Object> map=findDataSourceByCode(dsCode);
            if(map==null){
                retJson.putAll(JackJSONObject.fromObject(apiService.getReturnDataByCode("7040")));
                retJson.put("message",retJson.get("message")+" dsCode："+dsCode);
                return "-1";
            }else{
                return String.valueOf(map.get("uuid"));
            }
        }
    }

    public Map<String, Object> findDataSourceByUuid(String uuid) {
        return findDataSource(uuid,null,jdbcTemplate);
    }

    public Map<String, Object> findDataSourceByCode(String dsCode) {
        return findDataSource(null,dsCode,jdbcTemplate);
    }
    public Map<String, Object> findDataSourceByJsonStr(String jsonStr) {
        JackJSONObject jsonObject = JackJSONObject.fromObject(jsonStr);
        String dsCode=String.valueOf(jsonObject.get("dsCode"));
        Map<String,Object> map=findDataSourceByCode(dsCode);
        return map;
    }
    public Map<String, Object> findDataSourceByJsonStr(String jsonStr, JackJSONObject retJson, JackJSONObject dataJson) {
        JackJSONObject jsonObject = JackJSONObject.fromObject(jsonStr);
        String dsCode=String.valueOf(jsonObject.get("dsCode"));
        Map<String,Object> map=findDataSourceByCode(dsCode);
        if(map==null){
            retJson.putAll(JackJSONObject.fromObject(apiService.getReturnDataByCode("7040")));
            retJson.put("message",retJson.get("message")+" dsCode："+dsCode);
        }
        return map;
    }

    public String getDataSourceUuid(String jsonStr) {//内部使用
        JackJSONObject jsonObject = JackJSONObject.fromObject(jsonStr);
        String dsCode=String.valueOf(jsonObject.get("dsCode"));
        Map<String,Object> map=findDataSourceByCode(dsCode);
        return String.valueOf(map.get("uuid"));
    }

    public String getDataSourceInfo(String jsonStr, String uuid, JackJSONObject retJson, JackJSONObject dataJson) {
        JackJSONObject dsJson=new JackJSONObject();
        dsJson.put("ds_pk_id",findDataSourceByUuid(uuid).get("ds_pk_id"));
        dataJson.put("ds",dsJson);
        return "0";
    }

    public List<Map<String, Object>> getQueryInfo(String jsonStr) {//内部使用
        JackJSONObject jsonObject = JackJSONObject.fromObject(jsonStr);
        String uuid=getDataSourceUuid(jsonStr);
        String queryCode=String.valueOf(jsonObject.get("queryCode"));
        Map<String, Object> map=getQuery(uuid,queryCode);
        String pageParentUuid=String.valueOf(map.get("uuid"));
        String sql="SELECT * FROM tbl_data_query_info WHERE STATUS=0 AND page_parent_uuid='"+pageParentUuid+"' ORDER BY field_sort ASC ";
        List<Map<String, Object>> dataList = jdbcTemplate.queryForList(sql);
        return dataList;
    }
    public Map<String, Object> getForm(String jsonStr) {
        JackJSONObject jsonObject = JackJSONObject.fromObject(jsonStr);
        String uuid=getDataSourceUuid(jsonStr);
        String formCode=String.valueOf(jsonObject.get("formCode"));
        Map<String, Object> map=getForm(uuid,formCode);
        return map;
    }
    public Map<String, Object> getForm(String uuid,String code) {
        String sql="SELECT * FROM tbl_data_form WHERE STATUS=0 AND page_parent_uuid='"+uuid+"' AND form_code='"+code+"'";
        List<Map<String, Object>> list=jdbcTemplate.queryForList(sql);
        if(list.size()>0){
            return list.get(0);
        }else{
            return new HashMap<>();
        }
    }
    public Map<String, Object> getForm(String uuid,String code,JackJSONObject retJson) {
        String sql="SELECT * FROM tbl_data_form WHERE STATUS=0 AND page_parent_uuid='"+uuid+"' AND form_code='"+code+"'";
        List<Map<String, Object>> list=jdbcTemplate.queryForList(sql);
        if(list.size()>0){
            return list.get(0);
        }else{
            retJson.putAll(JackJSONObject.fromObject(apiService.getReturnDataByCode("7040")));
            retJson.put("message",retJson.get("message")+"formCode："+code);
            return new HashMap<>();
        }
    }
    public Map<String, Object> getQuery(String jsonStr) {
        JackJSONObject jsonObject = JackJSONObject.fromObject(jsonStr);
        String uuid=getDataSourceUuid(jsonStr);
        String queryCode=String.valueOf(jsonObject.get("queryCode"));
        Map<String, Object> map=getQuery(uuid,queryCode);
        return map;
    }
    public Map<String, Object> getQuery(String uuid,String code) {
        String sql="SELECT * FROM tbl_data_query WHERE STATUS=0 AND page_parent_uuid='"+uuid+"' AND query_code='"+code+"'";
        List<Map<String, Object>> list=jdbcTemplate.queryForList(sql);
        if(list.size()>0){
            return list.get(0);
        }else{
            return new HashMap<>();
        }
    }
    public Map<String, Object> getQuery(String uuid,String code,JackJSONObject retJson) {
        String sql="SELECT * FROM tbl_data_query WHERE STATUS=0 AND page_parent_uuid='"+uuid+"' AND query_code='"+code+"'";
        List<Map<String, Object>> list=jdbcTemplate.queryForList(sql);
        if(list.size()>0){
            return list.get(0);
        }else{
            retJson.putAll(JackJSONObject.fromObject(apiService.getReturnDataByCode("7040")));
            retJson.put("message",retJson.get("message")+"queryCode："+code);
            return new HashMap<>();
        }
    }
    public String getQuery(String jsonStr, String uuid, JackJSONObject retJson, JackJSONObject dataJson) {
        JackJSONObject jsonObject = JackJSONObject.fromObject(jsonStr);
        String queryCode=String.valueOf(jsonObject.get("queryCode"));
        if(StringUtil.isEmpty(queryCode)){
            return "0";//formCode允许为空 继续
        }else{
            Map<String, Object> map=getQuery(uuid,queryCode);
            if(map.size()==0){
                retJson.putAll(JackJSONObject.fromObject(apiService.getReturnDataByCode("7040")));
                retJson.put("message",retJson.get("message")+"queryCode："+queryCode);
                return "-1";
            }else{
                return "0";
            }
        }
    }

    public String getFormInfo(String jsonStr, String uuid, JackJSONObject retJson, JackJSONObject dataJson,HttpServletRequest request) {
        JackJSONObject jsonObject = JackJSONObject.fromObject(jsonStr);
        JackJSONObject formJson = new JackJSONObject();
        String formCode=String.valueOf(jsonObject.get("formCode"));
        if(StringUtil.isEmpty(formCode)){
            return "0";//formCode允许为空 继续往下执行
        }else{
            Map<String, Object> formMap=getForm(uuid,formCode);
            if(formMap.size()==0){
                retJson.putAll(JackJSONObject.fromObject(apiService.getReturnDataByCode("7040")));
                retJson.put("message",retJson.get("message")+"formCode："+formCode);
                return "-1";
            }else{
                String formUuid=String.valueOf(formMap.get("uuid"));
                String formVersion=String.valueOf(jsonObject.get("formVersion"));
                if(StringUtil.isEmpty(formVersion)){// 入参没有传此参数则取出表单组件绑定的。
                    formVersion=String.valueOf(formMap.get("form_version"));
                }
                //clearValue说明：关于是使用默认的clearValue还是使用自定义四的clearValue（自定义的则需要先绑定扩展js，然后再扩展js里写clearValue函数即可）
                List<Map<String, Object>> dataList;
                if(StringUtil.isNotEmpty(formVersion)){
                    dataList = khcRedisCache.getFormInfoByUuidAndVersion(formUuid,formVersion);
                }else{
                    dataList = khcRedisCache.getFormInfoByUuid(formUuid);
                }
                List<Map<String, Object>> formData = new ArrayList<>();
                User user=userService.getUser(request);
                for (int i = 0; i < dataList.size(); i++) {
                    Map<String,Object> map= new HashMap<>();
                    Map<String,Object> dataMap= dataList.get(i);
                    Object dictCode = dataMap.get("dict_code");
                    String code;
                    if (dictCode instanceof HashMap) {
                        code=String.valueOf(((HashMap<?, ?>) dictCode).get("code"));
                    }else{
                        code=String.valueOf(dataMap.get("dict_code"));
                    }
                    if(StringUtil.isNotEmpty(code)){
                        dataMap.put("dict_code",dictService.getAllDictInfoByCode(code,user));
                    }
                    map.put("data",dataMap);
                    map.put("name",dataList.get(i).get("name"));
                    map.put("show_type",dataList.get(i).get("show_type"));
                    formData.add(map);
                }
                //查询按钮
                formJson.put("formInfo",formMap);
                formJson.put("formButton",getButtonFilter(khcRedisCache.getButtonByParentUUID(String.valueOf(formMap.get("uuid")),""),request));
                formJson.put("formData",formData);
                dataJson.put("form",formJson);
                return "0";
            }
        }
    }
    public List<Map<String, Object>> getFormInfo(String jsonStr) {//内部使用
        JackJSONObject jsonObject = JackJSONObject.fromObject(jsonStr);
        String uuid=getDataSourceUuid(jsonStr);
        String formCode=String.valueOf(jsonObject.get("formCode"));
        Map<String, Object> map=getForm(uuid,formCode);
        String pageParentUuid=String.valueOf(map.get("uuid"));
        String sql="SELECT * FROM tbl_data_form_info WHERE STATUS=0 AND page_parent_uuid='"+pageParentUuid+"' ORDER BY field_sort ASC ";
        List<Map<String, Object>> dataList = jdbcTemplate.queryForList(sql);
        return dataList;
    }
    public List<Map<String, Object>> getFormInfoByUuid(String pageParentUuid) {//内部使用
        String sql="SELECT * FROM tbl_data_form_info WHERE STATUS=0 AND page_parent_uuid='"+pageParentUuid+"' ORDER BY field_sort ASC ";
        List<Map<String, Object>> dataList = jdbcTemplate.queryForList(sql);
        return dataList;
    }
    public String getQueryInfo(String jsonStr, String uuid, JackJSONObject retJson, JackJSONObject dataJson, HttpServletRequest request) {
        JackJSONObject jsonObject = JackJSONObject.fromObject(jsonStr);
        JackJSONObject queryJson = new JackJSONObject();
        String queryCode=String.valueOf(jsonObject.get("queryCode"));
        if(StringUtil.isEmpty(queryCode)){
            return "0";//formCode允许为空 继续
        }else{
            Map<String, Object> queryMap=getQuery(uuid,queryCode);
            if(queryMap.size()==0){
                retJson.putAll(JackJSONObject.fromObject(apiService.getReturnDataByCode("7040")));
                retJson.put("message",retJson.get("message")+"queryCode："+queryCode);
                return "-1";
            }else{
                String queryUuid=String.valueOf(queryMap.get("uuid"));
                List<Map<String, Object>> dataList = khcRedisCache.getQueryInfoByUuid(queryUuid);
                List<Map<String, Object>> queryData = new ArrayList<>();
                User user=userService.getUser(request);
                for (int i = 0; i < dataList.size(); i++) {
                    Map<String,Object> map= new HashMap<>();
                    Map<String,Object> dataMap= dataList.get(i);
                    Object dictCode = dataMap.get("dict_code");
                    String code;
                    if (dictCode instanceof HashMap) {
                        code=String.valueOf(((HashMap<?, ?>) dictCode).get("code"));
                    }else{
                        code=String.valueOf(dataMap.get("dict_code"));
                    }
                    if(StringUtil.isNotEmpty(code)){
                        dataMap.put("dict_code",dictService.getAllDictInfoByCode(code,user));
                    }
                    map.put("data",dataMap);
                    map.put("name",dataList.get(i).get("name"));
                    map.put("show_type",dataList.get(i).get("show_type"));
                    queryData.add(map);
                }
                //查询按钮
                queryMap.put("rowButtonArray", getButtonFilter(khcRedisCache.getButtonByParentUUID(String.valueOf(queryMap.get("uuid"))," and button_type='rowButton'"),request));
                queryMap.put("tableButtonArray", getButtonFilter(khcRedisCache.getButtonByParentUUID(String.valueOf(queryMap.get("uuid"))," and button_type='tableButton'"),request));
                queryMap.put("queryButtonArray", getButtonFilter(khcRedisCache.getButtonByParentUUID(String.valueOf(queryMap.get("uuid"))," and button_type='queryButton'"),request));
                String sql="select * from tbl_data_query_simple_card where status=0 and simple_caard_status=0 and page_parent_uuid='"+queryMap.get("uuid")+"'";
                queryMap.put("querySimpleCardArray", JackJSON.parseObject(TextUtil.nullToStrFilter(jdbcTemplate.queryForList(sql)), ArrayList.class));
                queryJson.put("queryInfo",queryMap);
                queryJson.put("queryData",queryData);
                dataJson.put("query",queryJson);
                return "0";
            }
        }
    }
    //如果没有按钮角色权限则过滤掉。
    public List<Map<String, Object>> getButtonFilter(List<Map<String, Object>> buttons,HttpServletRequest request){
        List<Map<String, Object>> filterButtons=new ArrayList<>();
        if(buttons.size()>0){
            User user=userService.getUser(request);
            for (int i = 0; i < buttons.size(); i++) {
                String roleStr=String.valueOf(buttons.get(i).get("button_role"));
                if(roleStr.equals("")){//空则不控制按钮权限
                    filterButtons.add(buttons.get(i));
                }else{
                    String[] roleArr=roleStr.split(",");
                    for (int j = 0; j < roleArr.length; j++) {
                        if(!roleArr[j].equals("")&&userService.isExistsRole(roleArr[j], user.getId())) {
                            filterButtons.add(buttons.get(i));
                            break;
                        }
                    }
                }
            }
        }
        return filterButtons;
    }

    public String getDsTableName(Map<String,Object> dsMap,Map<String,Object> queryMap,User user){
        String isRewriteTableName=String.valueOf(queryMap.get("is_rewrite_table_name"));
        String tableName=String.valueOf(dsMap.get("ds_table_name"));
        if(isRewriteTableName.equals("Y")){//查询组件重写表名SQL
            tableName=String.valueOf(queryMap.get("rewrite_table_name"));
            tableName=tableName.replaceAll("&nbsp;"," ");
            tableName=tableName.replaceAll("&quot;","\'");
            tableName=tableName.replace("${ds_table_name}",String.valueOf(dsMap.get("ds_table_name")));
            if(user!=null){
                tableName=tableName.replace("${user_id}",String.valueOf(user.getId()));
            }
            return tableName;
        }
        isRewriteTableName=String.valueOf(dsMap.get("is_rewrite_table_name"));
        if(isRewriteTableName.equals("Y")){//数据源重写表名SQL
            tableName=String.valueOf(dsMap.get("rewrite_table_name"));
            tableName=tableName.replaceAll("&nbsp;"," ");
            tableName=tableName.replaceAll("&quot;","\'");
            tableName=tableName.replace("${ds_table_name}",String.valueOf(dsMap.get("ds_table_name")));
            if(user!=null){
                tableName=tableName.replace("${user_id}",String.valueOf(user.getId()));
            }
            return tableName;
        }
        return tableName;//都不重写返回数据源配置的表名

    }

    /**
     * 目前有两个验证
     * 1，表单配置is_unique=Y 验证数据是否唯一
     * 2，验证值的长度是否超过最大长度，超过则返回相应提示信息
     */
    public String saveBeforeValidate(String  jsonStr, JackJSONArray saveDataArray, JackJSONObject retJson, HttpServletRequest request) {
        Map<String,Object> dsMap=findDataSourceByJsonStr(jsonStr);
        String tableName=String.valueOf(dsMap.get("ds_table_name"));
        List<Map<String,Object>> mapList= BaseDao.getAllCommentsByTableName(tableName, jdbcTemplate);
        Map<String,String> typeMap=new HashMap<String,String>();
        Map<String,Integer> widthMap=new HashMap<String,Integer>();
        List<Object> valueList=new ArrayList<Object>();
        String fieldKey="Field";
        String fieldType="Type";
        if(DBCheck.isKingbase()){
            fieldKey="column_name";
            fieldType="data_type";
        }
        for(int k=0;k<mapList.size();k++) {
            Map<String,Object> field=mapList.get(k);
            String fieldName=String.valueOf(field.get(fieldKey));
            typeMap.put(fieldName, String.valueOf(field.get(fieldType)));
            valueList.add(fieldName);
            if(DBCheck.isKingbase()){
                if(field.get(fieldType).equals("varchar")) {
                    String width=field.get("character_maximum_length").toString();
                    if(DBCheck.getCachedJpaDatabase().equals("mysql")){//使用Kingbase 兼容MySQL
                        // 定义正则表达式，匹配括号内的数字
                        Pattern pattern = Pattern.compile("\\((\\d+)\\s*char\\)");
                        Matcher matcher = pattern.matcher(width);
                        if(matcher.find()){
                            String numberStr = matcher.group(1); // 提取第一个捕获组（数字部分）
                            widthMap.put(fieldName, Integer.parseInt(numberStr));
                        }else{
                            widthMap.put(fieldName, 500);
                        }
                    }else{//使用Kingbase 兼容Oracle
                        widthMap.put(fieldName, Integer.parseInt(width));
                    }
                }
            }else{
                if(field.get(fieldType).toString().indexOf("(")>0) {
                    String width=field.get(fieldType).toString();
                    width=width.substring(width.indexOf("(")+1,width.indexOf(")"));
                    widthMap.put(fieldName, Integer.parseInt(width));
                }
            }

        }
        List<Map<String,Object>> listFormInfo=getFormInfo(jsonStr);
        for (int i = 0; i < saveDataArray.size(); i++) {
            JackJSONObject saveData=saveDataArray.getJackJSONObject(i);
            for (int j = 0; j < listFormInfo.size(); j++) {
                Map<String,Object> map=listFormInfo.get(j);
                String isUnique=String.valueOf(map.get("is_unique"));
                String name=String.valueOf(map.get("name"));
                String value=String.valueOf(saveData.get(name));
                if(isUnique.equals("Y")){
                    String dsPkId=String.valueOf(dsMap.get("ds_pk_id"));
                    String pId=String.valueOf(saveData.get(dsPkId));
                    String pageParentUUID=String.valueOf(saveData.get("page_parent_uuid"));
                    String whereSql="";
                    if(StringUtil.isNotEmpty(pId)){
                        whereSql+=" AND "+dsPkId+"!='"+pId+"'";
                    }
                    if(StringUtil.isNotEmpty(pageParentUUID)){
                        whereSql+=" AND page_parent_uuid='"+pageParentUUID+"'";
                    }
                    String sql="SELECT COUNT(1) FROM "+tableName+" WHERE STATUS=0 AND "+name+"='"+value+"' "+whereSql;
                    Integer count=this.jdbcTemplate.queryForObject(sql,Integer.class);
                    if(count>0){
                        retJson.putAll(JackJSONObject.fromObject(apiService.getReturnDataByCode("5023")));
                        retJson.put("message",map.get("label_name")+"已存在："+value);
                        return "-1";
                    }
                }
                try {
                    Integer len=widthMap.get(name);
                    if((!value.equals("null"))&&value.length()>len){
                        retJson.putAll(JackJSONObject.fromObject(apiService.getReturnDataByCode("5022")));
                        retJson.put("message",map.get("label_name")+"："+value+"  超过最大长度："+widthMap.get(name));
                        return "-1";
                    }
                } catch (Exception e) {
                    // text 多行文本 不校验长度
                }
            }
        }
        return "0";
    }

    public String getOrderBySql(String jsonStr, JackJSONObject retJson) {
        JackJSONObject jsonObject = JackJSONObject.fromObject(jsonStr).getJackJSONObject("queryParam");
        try {
            jsonObject.size();
        } catch (Exception e) {
            jsonObject=new JackJSONObject();
        }
        Map<String,Object> dsMap=findDataSourceByJsonStr(jsonStr);
        Map<String,Object> queryMap=getQuery(jsonStr);
        String dsPkId = String.valueOf(dsMap.get("ds_pk_id"));
        String defaultSortType = "desc";
        String sortField = String.valueOf(jsonObject.get("sortField"));
        String sortType = String.valueOf(jsonObject.get("sortType"));
        if (StringUtil.isEmpty(sortField)) {
            sortField=String.valueOf(queryMap.get("default_sort_field"));
            if(StringUtil.isEmpty(sortField)){
                sortField = dsPkId;
            }
        }
        if (StringUtil.isEmpty(sortType)) {
            sortType=String.valueOf(queryMap.get("default_sort_type"));
            if(StringUtil.isEmpty(sortType)){
                sortType = defaultSortType;
            }
        }
        List<Map<String,Object>> fieldList=getQueryInfo(jsonStr);
        Map<String,Object> map=new HashMap<>();
        for (int i = 0; i < fieldList.size(); i++) {
            String fieldName=String.valueOf(fieldList.get(i).get("name"));
            map.put(fieldName,fieldList.get(i));//map里存一份用fieldInfo,fieldName为key
        }
        Map<String,Object> fieldInfo= (Map<String, Object>) map.get(sortField);
        if(fieldInfo!=null){
            String orderBySql = " order by " + sortField + " " + sortType;
            return orderBySql;
        }else{
            retJson.put("status","no_code");
            retJson.put("message","SQL拼接异常，原因是查询组件配置的排序字段不存在（"+sortField+"）");
            return "-1";
        }

    }

    public void getLimitPage(String jsonStr,Map<String,Object> pageMap) {
        JackJSONObject jsonObject = JackJSONObject.fromObject(jsonStr).getJackJSONObject("queryParam");
        try {
            jsonObject.size();
        } catch (Exception e) {
            jsonObject=new JackJSONObject();
        }
        Integer defaultPage = 0;
        Integer defaultPageSize = 10;
        String page = String.valueOf(jsonObject.get("page"));
        String pageSize = String.valueOf(jsonObject.get("pageSize"));
        if (StringUtil.isEmpty(page)) {
            pageMap.put("page",defaultPage);
        }else{
            pageMap.put("page",Integer.valueOf(page));
        }
        if (StringUtil.isEmpty(pageSize)) {
            pageMap.put("pageSize",defaultPageSize);
        }else{
            pageMap.put("pageSize",Integer.valueOf(pageSize));
        }
    }

    public Map<String, Object> resolveQuery(String jsonStr, JackJSONObject retJson, JackJSONObject dataJson) {
        String uuid = getDataSourceUuid(jsonStr, retJson, dataJson);
        if ("-1".equals(uuid)) {
            return null;
        }
        String queryCode = apiService.getJsonValueByKey(jsonStr, retJson, "queryCode");
        if ("-1".equals(queryCode)) {
            return null;
        }
        Map<String, Object> queryMap = getQuery(uuid, queryCode, retJson);
        if (queryMap == null || queryMap.isEmpty()) {
            return null;
        }
        return queryMap;
    }

    public Map<String, Object> resolveForm(String jsonStr, JackJSONObject retJson, JackJSONObject dataJson) {
        String uuid = getDataSourceUuid(jsonStr, retJson, dataJson);
        if ("-1".equals(uuid)) {
            return null;
        }
        String queryCode = apiService.getJsonValueByKey(jsonStr, retJson, "formCode");
        if ("-1".equals(queryCode)) {
            return null;
        }
        Map<String, Object> queryMap = getForm(uuid, queryCode, retJson);
        if (queryMap == null || queryMap.isEmpty()) {
            return null;
        }
        return queryMap;
    }

}

