package com.yunzhupaas.onlinedev.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.yunzhupaas.base.UserInfo;
import com.yunzhupaas.base.entity.ModuleEntity;
import com.yunzhupaas.base.entity.VisualdevShortLinkEntity;
import com.yunzhupaas.base.mapper.FlowFormDataMapper;
import com.yunzhupaas.base.model.ColumnDataModel;
import com.yunzhupaas.base.model.Template6.ColumnListField;
import com.yunzhupaas.base.model.VisualDevJsonModel;
import com.yunzhupaas.base.model.column.ColumnModel;
import com.yunzhupaas.base.model.module.PropertyJsonModel;
import com.yunzhupaas.base.service.DbLinkService;
import com.yunzhupaas.base.service.ModuleService;
import com.yunzhupaas.base.service.SuperServiceImpl;
import com.yunzhupaas.base.service.VisualdevShortLinkService;
import com.yunzhupaas.base.util.FlowFormDataUtil;
import com.yunzhupaas.base.util.FormPublicUtils;
import com.yunzhupaas.database.model.entity.DbLinkEntity;
import com.yunzhupaas.database.model.superQuery.ConditionJsonModel;
import com.yunzhupaas.database.model.superQuery.SuperJsonModel;
import com.yunzhupaas.database.model.superQuery.SuperQueryJsonModel;
import com.yunzhupaas.database.util.ConnUtil;
import com.yunzhupaas.database.util.DynamicDataSourceUtil;
import com.yunzhupaas.emnus.SearchMethodEnum;
import com.yunzhupaas.exception.WorkFlowException;
import com.yunzhupaas.model.OnlineDevData;
import com.yunzhupaas.model.visualJson.*;
import com.yunzhupaas.model.visualJson.analysis.*;
import com.yunzhupaas.onlinedev.entity.VisualdevModelDataEntity;
import com.yunzhupaas.onlinedev.mapper.VisualdevModelDataMapper;
import com.yunzhupaas.onlinedev.model.OnlineDevListModel.VisualColumnSearchVO;
import com.yunzhupaas.onlinedev.model.PaginationModel;
import com.yunzhupaas.onlinedev.service.VisualDevListService;
import com.yunzhupaas.onlinedev.util.onlineDevUtil.*;
import com.yunzhupaas.permission.model.authorize.OnlineDynamicSqlModel;
import com.yunzhupaas.permission.service.AuthorizeService;
import com.yunzhupaas.permissions.PermissionInterfaceImpl;
import com.yunzhupaas.util.*;
import com.yunzhupaas.util.visiual.YunzhupaasKeyConsts;
import com.yunzhupaas.workflow.service.TemplateApi;
import lombok.Cleanup;
import org.apache.commons.collections4.CollectionUtils;
import org.mybatis.dynamic.sql.*;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
import org.mybatis.dynamic.sql.select.SelectModel;
import org.mybatis.dynamic.sql.select.join.EqualTo;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 在线开发列表
 *
 * @author 云筑产品开发平台组
 * @version V3.2.0
 * @copyright 深圳市乐程软件有限公司（http://www.szlecheng.cn）
 * @date 2024/7/28
 */
@Service
public class VisualDevListServiceImpl extends SuperServiceImpl<VisualdevModelDataMapper, VisualdevModelDataEntity>
        implements VisualDevListService {

    @Autowired
    private DbLinkService dblinkService;

    @Autowired
    private OnlineSwapDataUtils onlineSwapDataUtils;
    @Autowired
    private FlowFormDataUtil flowFormDataUtil;
    @Autowired
    private FlowFormDataMapper flowFormDataMapper;
    @Autowired
    private VisualdevShortLinkService visualdevShortLinkService;
    @Autowired
    private TemplateApi templateApi;
    @Autowired
    private ModuleService moduleService;
    @Autowired
    private AuthorizeService authorizeService;

    @Override
    public List<Map<String, Object>> getDataList(VisualDevJsonModel visualDevJsonModel, PaginationModel paginationModel)
            throws WorkFlowException {
        List<Map<String, Object>> realList = new ArrayList<>();
        ColumnDataModel columnDataModel = visualDevJsonModel.getColumnData();
        FormDataModel formDataModel = visualDevJsonModel.getFormData();
        List<FieLdsModel> fieLdsModels = JsonUtil.getJsonToList(formDataModel.getFields(), FieLdsModel.class);
        List<TableModel> visualTables = visualDevJsonModel.getVisualTables();
        // 解析所有控件
        RecursionForm recursionForm = new RecursionForm(fieLdsModels, visualTables);
        List<FormAllModel> formAllModel = new ArrayList<>();
        FormCloumnUtil.recursionForm(recursionForm, formAllModel);
        // 当前用户信息
        UserInfo userInfo = UserProvider.getUser();

        // 是否流程
        boolean enableFlow = false;
        // 是否数据管理（有菜单则不是数据管理）
        if (ObjectUtil.isNotEmpty(paginationModel.getMenuId())) {
            // 菜单id
            String menuId = paginationModel.getMenuId();
            if (columnDataModel.getUseColumnPermission()) {
                visualDevJsonModel.setNeedPermission(true);
                List<String> pList = new ArrayList<>();
                Map<String, Object> pMap = PermissionInterfaceImpl.getColumnMap();
                if (pMap.get(menuId) != null) {
                    pList = JsonUtil.getJsonToList(pMap.get(menuId), ColumnModel.class).stream()
                            .map(ColumnModel::getEnCode).collect(Collectors.toList());
                }
                visualDevJsonModel.setPermissionList(pList);
            }
            // 菜单判断是否启用流程-添加流程状态
            ModuleEntity info = moduleService.getInfo(menuId);
            // 流程菜单
            enableFlow = Objects.equals(info.getType(), 9);
            visualDevJsonModel.setEnableFlow(enableFlow);
            if (enableFlow) {
                PropertyJsonModel model = JsonUtil.getJsonToBean(info.getPropertyJson(), PropertyJsonModel.class);
                List<String> flowVersionIds = templateApi.getFlowIdsByTemplateId(model.getModuleId());
                visualDevJsonModel.setFlowId(model.getModuleId());
                visualDevJsonModel.setFlowVersionIds(flowVersionIds);
            }
        }

        // 判断有无表
        if (visualTables.size() > 0) {
            // 封装搜索数据
            OnlineProductSqlUtils.queryList(formAllModel, visualDevJsonModel, paginationModel);
            realList = this.getListWithTable(visualDevJsonModel, paginationModel, userInfo, null);
        }

        if (realList.size() < 1) {
            return realList;
        }
        // 编辑表格(行内编辑)--页签（有getExtraQueryJson）不走行内编辑
        boolean inlineEdit = StringUtil.isNotEmpty(paginationModel.getExtraQueryJson()) ? false
                : columnDataModel.getType() != null && columnDataModel.getType() == 4;

        // 复制父级字段+_id
        realList.forEach(item -> {
            item.put(columnDataModel.getParentField() + "_id", item.get(columnDataModel.getParentField()));
        });
        // 数据转换
        // 递归处理控件
        List<FieLdsModel> fields = new ArrayList<>();
        OnlinePublicUtils.recursionFields(fields, fieLdsModels);
        visualDevJsonModel.setFormListModels(fields);
        realList = onlineSwapDataUtils.getSwapList(realList, fields, visualDevJsonModel.getId(), inlineEdit);

        // 取回传主键
        String pkeyId = visualDevJsonModel.getPkeyId();
        // 树形子字段key
        columnDataModel.setSubField(pkeyId);

        if (enableFlow) {
            onlineSwapDataUtils.getFlowStatus(realList);
        }
        return realList;
    }

    @Override
    public List<Map<String, Object>> getDataListLink(VisualDevJsonModel visualDevJsonModel,
            PaginationModel paginationModel) throws WorkFlowException {
        List<Map<String, Object>> realList;
        VisualdevShortLinkEntity shortLinkEnt = visualdevShortLinkService.getById(visualDevJsonModel.getId());
        List<VisualColumnSearchVO> listCondition = StringUtil.isNotEmpty(shortLinkEnt.getColumnCondition())
                ? JsonUtil.getJsonToList(shortLinkEnt.getColumnCondition(), VisualColumnSearchVO.class)
                : new ArrayList<>();
        List<FieLdsModel> listFields = StringUtil.isNotEmpty(shortLinkEnt.getColumnCondition())
                ? JsonUtil.getJsonToList(shortLinkEnt.getColumnText(), FieLdsModel.class)
                : new ArrayList<>();
        visualDevJsonModel.setFormListModels(listFields);
        visualDevJsonModel.setIsLinkList(true);
        FormDataModel formDataModel = visualDevJsonModel.getFormData();
        List<TableModel> visualTables = visualDevJsonModel.getVisualTables();
        // 当前用户信息
        UserInfo userInfo = UserProvider.getUser();
        List<String> isBetween = new ArrayList() {
            {
                add(YunzhupaasKeyConsts.DATE);
                add(YunzhupaasKeyConsts.TIME);
                add(YunzhupaasKeyConsts.NUM_INPUT);
                add(YunzhupaasKeyConsts.RATE);
                add(YunzhupaasKeyConsts.SLIDER);
            }
        };
        for (VisualColumnSearchVO searchVO : listCondition) {
            String yunzhupaasKey = searchVO.getConfig().getYunzhupaasKey();
            searchVO.setSearchType(isBetween.contains(yunzhupaasKey) ? "3" : "2");
        }
        // 菜单id
        String menuId = paginationModel.getMenuId();
        ColumnDataModel columnDataModel = new ColumnDataModel();
        List<ColumnListField> list = JsonUtil.getJsonToList(shortLinkEnt.getColumnText(), ColumnListField.class);
        columnDataModel.setColumnList(JsonUtil.getListToJsonArray(list).toJSONString());// 查询字段构造
        columnDataModel.setSearchList(JsonUtil.getListToJsonArray(listCondition).toJSONString());
        columnDataModel.setType(1);// 普通列表
        visualDevJsonModel.setColumnData(columnDataModel);
        // 查询
        List<FieLdsModel> fieLdsModels = JsonUtil.getJsonToList(formDataModel.getFields(), FieLdsModel.class);
        RecursionForm recursionForm = new RecursionForm(fieLdsModels, visualTables);
        List<FormAllModel> formAllModel = new ArrayList<>();
        FormCloumnUtil.recursionForm(recursionForm, formAllModel);
        // 封装搜索数据
        OnlineProductSqlUtils.queryList(formAllModel, visualDevJsonModel, paginationModel);
        realList = this.getListWithTable(visualDevJsonModel, paginationModel, userInfo, null);
        if (realList.size() < 1) {
            return realList;
        }
        // 数据转换
        List<FieLdsModel> fields = new ArrayList<>();
        OnlinePublicUtils.recursionFields(fields, fieLdsModels);
        visualDevJsonModel.setFormListModels(fields);
        realList = onlineSwapDataUtils.getSwapList(realList, fields, visualDevJsonModel.getId(), false);
        return realList;
    }

    @Override
    public List<Map<String, Object>> getList(List<Map<String, Object>> noSwapDataList,
            List<VisualColumnSearchVO> searchVOList, PaginationModel paginationModel) {
        if (searchVOList.size() > 0) {
            // 条件查询
            noSwapDataList = OnlineDevListUtils.getNoSwapList(noSwapDataList, searchVOList);
        }
        // 排序
        if (noSwapDataList.size() > 0) {
            if (StringUtil.isNotEmpty(paginationModel.getSidx())) {
                // 排序处理
                noSwapDataList.sort((o1, o2) -> {
                    Map<String, Object> i1 = o1;
                    Map<String, Object> i2 = o2;
                    String s1 = String.valueOf(i1.get(paginationModel.getSidx()));
                    String s2 = String.valueOf(i2.get(paginationModel.getSidx()));
                    if ("desc".equalsIgnoreCase(paginationModel.getSort())) {
                        return s2.compareTo(s1);
                    } else {
                        return s1.compareTo(s2);
                    }
                });
            }

            long total = noSwapDataList.size();

            // 数据分页
            noSwapDataList = PageUtil.getListPage((int) paginationModel.getCurrentPage(),
                    (int) paginationModel.getPageSize(), noSwapDataList);
            noSwapDataList = paginationModel.setData(noSwapDataList, total);
        }
        return noSwapDataList;
    }

    @Override
    public List<Map<String, Object>> getWithoutTableData(String modelId) {
        QueryWrapper<VisualdevModelDataEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(VisualdevModelDataEntity::getVisualDevId, modelId);
        List<VisualdevModelDataEntity> list = this.list(queryWrapper);
        List<Map<String, Object>> dataVoList = list.parallelStream().map(t -> {
            Map<String, Object> dataMap = JsonUtil.stringToMap(t.getData());
            dataMap.put("id", t.getId());
            return dataMap;
        }).collect(Collectors.toList());
        return dataVoList;
    }

    @Override
    public List<Map<String, Object>> getListWithTable(VisualDevJsonModel visualDevJsonModel,
            PaginationModel paginationModel, UserInfo userInfo, List<String> columnPropList) {
        ColumnDataModel columnDataModel = visualDevJsonModel.getColumnData();
        List<Map<String, Object>> dataList = new ArrayList<>();
        // 数据过滤
        SuperJsonModel ruleQuery = visualDevJsonModel.getRuleQuery();
        // 高级搜索
        SuperJsonModel superQuery = visualDevJsonModel.getSuperQuery();
        // 列表搜索
        SuperJsonModel query = visualDevJsonModel.getQuery();
        // 数据过滤
        List<SuperJsonModel> authorizeListAll = visualDevJsonModel.getAuthorize();
        // 关键词
        SuperJsonModel keyQuery = visualDevJsonModel.getKeyQuery();
        // 列表搜索
        SuperJsonModel extraQuery = visualDevJsonModel.getExtraQuery();
        Boolean logicalDelete = visualDevJsonModel.getFormData().getLogicalDelete();

        // 数据源
        DbLinkEntity linkEntity = dblinkService.getInfo(visualDevJsonModel.getDbLinkId());
        boolean userStanding = authorizeService.getUserCurrentStanding(userInfo.getUserId(), 2);
        try {
            DynamicDataSourceUtil.switchToDataSource(linkEntity);
            @Cleanup
            Connection connection = ConnUtil.getConnOrDefault(linkEntity);
            String databaseProductName = connection.getMetaData().getDatabaseProductName().trim();
            List<TableModel> tableModelList = visualDevJsonModel.getVisualTables();
            // 主表
            TableModel mainTable = tableModelList.stream().filter(t -> t.getTypeId().equals("1")).findFirst()
                    .orElse(null);

            List<ColumnListField> modelList = JsonUtil.getJsonToList(columnDataModel.getColumnList(),
                    ColumnListField.class);

            FormDataModel formData = visualDevJsonModel.getFormData();
            List<FieLdsModel> jsonToList = JsonUtil.getJsonToList(formData.getFields(), FieLdsModel.class);
            // 递归处理控件
            List<FieLdsModel> allFieLds = new ArrayList<>();
            OnlinePublicUtils.recursionFields(allFieLds, jsonToList);
            Map<String, String> tableFieldAndTableName = new HashMap<>(8);
            Map<String, String> tableNameAndTableField = new HashMap<>(8);
            allFieLds.stream().filter(f -> f.getConfig().getYunzhupaasKey().equals(YunzhupaasKeyConsts.CHILD_TABLE))
                    .forEach(f -> {
                        tableFieldAndTableName.put(f.getVModel(), f.getConfig().getTableName());
                        tableNameAndTableField.put(f.getConfig().getTableName(), f.getVModel());
                    });

            // 取出所有有用到的表-进行关联过滤
            List<String> allTableName = OnlinePublicUtils.getAllTableName(modelList, new ArrayList() {
                {
                    add(ruleQuery);
                    add(superQuery);
                    add(query);
                    addAll(authorizeListAll);
                }
            }, tableFieldAndTableName);

            // 列表中区别子表正则
            String reg = "^[yunzhupaas_]\\S*_yunzhupaas\\S*";
            String pkeyId = flowFormDataUtil.getKey(mainTable, databaseProductName);
            visualDevJsonModel.setPkeyId(pkeyId);

            // 所有字段
            List<String> collect = new ArrayList<>();
            if (columnPropList != null) {
                collect.addAll(columnPropList);
            } else {
                collect.addAll(modelList.stream().map(mode -> mode.getProp()).collect(Collectors.toList()));
            }
            if (OnlineDevData.COLUMNTYPE_FIVE.equals(columnDataModel.getType())
                    && !collect.contains(columnDataModel.getParentField())) {
                collect.add(columnDataModel.getParentField());
            }
            // 列表权限字段移出
            if (visualDevJsonModel.getNeedPermission()) {
                List<String> permissionList = visualDevJsonModel.getPermissionList() == null ? Collections.EMPTY_LIST
                        : visualDevJsonModel.getPermissionList();
                List<String> newCollect = new ArrayList<>();
                for (String key : collect) {
                    if (!permissionList.contains(key)) {
                        newCollect.add(key);
                    }
                }
                collect.removeAll(newCollect);
            }
            // 添加排序字段
            if (StringUtil.isNotEmpty(paginationModel.getSidx())) {
                String[] split = paginationModel.getSidx().split(",");
                for (String s : split) {
                    String filedName = "";
                    if (s.startsWith("-")) {
                        filedName = s.substring(1);
                    } else {
                        filedName = s;
                    }
                    if (!collect.contains(filedName)) {
                        collect.add(filedName);
                    }
                }
            }

            List<OnlineDynamicSqlModel> sqlModelList = new ArrayList<>();
            // 根据表字段创建sqltable
            for (TableModel model : tableModelList) {
                OnlineDynamicSqlModel sqlModel = new OnlineDynamicSqlModel();
                sqlModel.setSqlTable(SqlTable.of(model.getTable()));
                sqlModel.setTableName(model.getTable());
                if (model.getTypeId().equals("1")) {
                    sqlModel.setMain(true);
                } else {
                    sqlModel.setForeign(model.getTableField());
                    sqlModel.setRelationKey(model.getRelationField());
                    sqlModel.setMain(false);
                }
                sqlModelList.add(sqlModel);
            }

            OnlineProductSqlUtils.getColumnListSql(sqlModelList, visualDevJsonModel, collect, linkEntity);
            // 主表
            OnlineDynamicSqlModel mainSqlModel = sqlModelList.stream().filter(OnlineDynamicSqlModel::isMain).findFirst()
                    .orElse(null);
            // 非主表
            List<OnlineDynamicSqlModel> dycList = sqlModelList.stream().filter(dyc -> !dyc.isMain())
                    .collect(Collectors.toList());
            List<BasicColumn> sqlColumns = new ArrayList<>();
            Map<String, String> aliasMap = new HashMap<>();
            boolean isOracle = databaseProductName.equalsIgnoreCase("oracle");
            boolean isDm = databaseProductName.equalsIgnoreCase("DM DBMS");

            for (OnlineDynamicSqlModel dynamicSqlModel : sqlModelList) {
                List<BasicColumn> basicColumns = Optional.ofNullable(dynamicSqlModel.getColumns())
                        .orElse(new ArrayList<>());
                // 达梦或者oracle 别名太长转换-底下有方法进行还原
                if (isOracle || isDm) {
                    for (int i = 0; i < basicColumns.size(); i++) {
                        BasicColumn item = basicColumns.get(i);
                        String alias = item.alias().orElse(null);
                        if (StringUtil.isNotEmpty(alias)) {
                            String aliasNewName = "A" + RandomUtil.uuId();
                            aliasMap.put(aliasNewName, alias);
                            basicColumns.set(i, item.as(aliasNewName));
                        }
                    }
                }
                sqlColumns.addAll(basicColumns);
            }
            QueryExpressionDSL<SelectModel> from = SqlBuilder.select(sqlColumns).from(mainSqlModel.getSqlTable());
            QueryExpressionDSL<SelectModel> subFrom = SqlBuilder.select(mainSqlModel.getSqlTable().column(pkeyId))
                    .from(mainSqlModel.getSqlTable());

            // 构造table和table下字段的分组
            Map<String, List<String>> tableFieldGroup = new HashMap<>(8);
            allFieLds.forEach(f -> {
                tableFieldGroup.computeIfAbsent(f.getConfig().getTableName(), k -> new ArrayList<>()).add(
                        "table".equals(f.getConfig().getType()) ? f.getConfig().getTableName() : f.getVModel());
            });
            Map<String, SqlTable> subSqlTableMap = new HashMap<>();
            if (dycList.size() > 0) {
                for (OnlineDynamicSqlModel sqlModel : dycList) {
                    if (!allTableName.contains(sqlModel.getTableName()))
                        continue;
                    String relationKey = sqlModel.getRelationKey();

                    boolean relationKeyTypeString = false;
                    for (TableModel item : tableModelList) {
                        if (item.getTable().equals(sqlModel.getTableName())) {
                            String foreign = sqlModel.getForeign();
                            TableFields thisfield = item.getFields().stream().filter(t -> t.getField().equals(foreign))
                                    .findFirst().orElse(null);
                            if (thisfield != null) {
                                relationKeyTypeString = thisfield.getDataType().toLowerCase().contains("varchar");
                            }
                        }
                    }
                    // postgresql自增 int和varchar无法对比-添加以下判断
                    if (Objects.equals(formData.getPrimaryKeyPolicy(), 2)
                            && "PostgreSQL".equalsIgnoreCase(databaseProductName) && relationKeyTypeString) {
                        relationKey += "::varchar";
                    }
                    // 移除子表的外联--用于查询主副表字段数据
                    if (!tableNameAndTableField.containsKey(sqlModel.getTableName())) {
                        from.leftJoin(sqlModel.getSqlTable())
                                .on(sqlModel.getSqlTable().column(sqlModel.getForeign()),
                                        new EqualTo(mainSqlModel.getSqlTable().column(relationKey)));
                    }

                    // 用于查询拼接各条件之后的主表id列表（和统计数量）
                    subFrom.leftJoin(sqlModel.getSqlTable())
                            .on(sqlModel.getSqlTable().column(sqlModel.getForeign()),
                                    new EqualTo(mainSqlModel.getSqlTable().column(relationKey)));
                    String tableName = sqlModel.getTableName();
                    List<String> fieldList = tableFieldGroup.get(tableName);
                    if (fieldList != null) {
                        fieldList.forEach(fieldKey -> {
                            subSqlTableMap.put(fieldKey, sqlModel.getSqlTable());
                        });
                    }
                }
            }

            QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder where = from.where();
            QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder subWhere = subFrom.where();

            // 逻辑删除不展示
            if (logicalDelete) {
                subWhere.and(mainSqlModel.getSqlTable().column(TableFeildsEnum.DELETEMARK.getField()),
                        SqlBuilder.isNull());
            }
            // 是否流程菜单
            List<String> flowVersionIds = visualDevJsonModel.getFlowVersionIds();
            if (visualDevJsonModel.isEnableFlow() && flowVersionIds.size() > 0) {
                subWhere.and(mainSqlModel.getSqlTable().column(TableFeildsEnum.FLOWID.getField()),
                        SqlBuilder.isIn(flowVersionIds));
            } else {
                subWhere.and(mainSqlModel.getSqlTable().column(TableFeildsEnum.FLOWID.getField()), SqlBuilder.isNull());
            }
            // 页签查询
            OnlineProductSqlUtils.getSuperSql(subWhere, extraQuery, sqlModelList, databaseProductName, null, false);

            // 查询条件sql
            OnlineProductSqlUtils.getSuperSql(subWhere, query, sqlModelList, databaseProductName, null, false);

            // 高级查询
            OnlineProductSqlUtils.getSuperSql(subWhere, superQuery, sqlModelList, databaseProductName, null, false);

            // 数据过滤
            OnlineProductSqlUtils.getSuperSql(subWhere, ruleQuery, sqlModelList, databaseProductName, null, false);

            // 关键词搜索
            OnlineProductSqlUtils.getSuperSql(subWhere, keyQuery, sqlModelList, databaseProductName, null, false);

            // 数据权限
            if (columnDataModel.getUseDataPermission() != null && columnDataModel.getUseDataPermission()) {
                if (!userInfo.getIsAdministrator() && !userStanding) {
                    if (authorizeListAll.size() == 0) {
                        return new ArrayList<>();
                    }
                    OnlineProductSqlUtils.getSuperSql(subWhere, authorizeListAll, sqlModelList, databaseProductName,
                            null);
                }
            }

            // 统计语句
            BasicColumn countColumn;
            if (dycList.size() > 0) {
                countColumn = SqlBuilder.countDistinct(mainSqlModel.getSqlTable().column(pkeyId));
            } else {
                countColumn = SqlBuilder.count(mainSqlModel.getSqlTable().column(pkeyId));
            }
            QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder countSelect = SqlBuilder
                    .select(countColumn)
                    .from(subWhere, "tmp")
                    .where();
            SelectStatementProvider renderCount = countSelect.build().render(RenderingStrategies.MYBATIS3);
            long count = flowFormDataMapper.count(renderCount);
            if (count == 0) {
                paginationModel.setTotal(0);
                return new ArrayList<>();
            }

            // 排序
            if (StringUtil.isNotEmpty(paginationModel.getSidx())) {
                String[] split = paginationModel.getSidx().split(",");
                List<SortSpecification> sidxList = new ArrayList<>();
                for (String sidx : split) {
                    // 目前只支持主表排序
                    if (sidx.toLowerCase().contains("_yunzhupaas_")
                            || sidx.toLowerCase().contains(YunzhupaasKeyConsts.CHILD_TABLE_PREFIX)) {
                        continue;
                    }
                    SortSpecification sortSpecification;
                    if (sidx.startsWith("-")) {
                        sortSpecification = SqlBuilder.sortColumn(mainTable.getTable(),
                                SqlTable.of(mainTable.getTable()).column(sidx.substring(1))).descending();
                    } else {
                        sortSpecification = SqlBuilder.sortColumn(mainTable.getTable(),
                                SqlTable.of(mainTable.getTable()).column(sidx));
                    }
                    sidxList.add(sortSpecification);
                }
                where.orderBy(sidxList);
            } else {
                where.orderBy(
                        SqlBuilder.sortColumn(mainTable.getTable(), SqlTable.of(mainTable.getTable()).column(pkeyId))
                                .descending());
            }
            // 假分页----1导出全部 0导出当前页 null---列表分页 树形和分组不需要分页
            boolean hasPage = (paginationModel.getDataType() == null || "0".equals(paginationModel.getDataType()))
                    && !Objects.equals(columnDataModel.getType(), 5) && !Objects.equals(columnDataModel.getType(), 3);
            if (hasPage) {
                PageHelper.startPage((int) paginationModel.getCurrentPage(), (int) paginationModel.getPageSize(),
                        false);
            }

            // 查询主副表数据
            where.and(SqlTable.of(mainTable.getTable()).column(pkeyId), SqlBuilder.isIn(subWhere));
            SelectStatementProvider render = where.build().render(RenderingStrategies.MYBATIS3);
            dataList = flowFormDataMapper.selectManyMappedRows(render);

            String finalPkeyId = pkeyId;
            List<Object> idStringList = dataList.stream().map(m -> m.get(finalPkeyId)).distinct()
                    .collect(Collectors.toList());
            if (idStringList.size() > 0) {
                // 处理子表
                List<String> tableFields = collect.stream()
                        .filter(c -> c.toLowerCase().contains(YunzhupaasKeyConsts.CHILD_TABLE_PREFIX))
                        .collect(Collectors.toList());
                List<TableModel> childTableModels = tableModelList.stream().filter(t -> t.getTypeId().equals("0"))
                        .collect(Collectors.toList());
                Map<String, List<String>> tableMap = tableFields.stream()
                        .collect(Collectors.groupingBy(t -> t.substring(0, t.lastIndexOf("-"))));
                for (TableModel tableModel : childTableModels) {
                    String table = tableModel.getTable();
                    if (!allTableName.contains(table))
                        continue;
                    String tableField = tableNameAndTableField.get(table);

                    String childPrimKey = flowFormDataUtil.getKey(tableModel, databaseProductName);
                    String fogID = tableModel.getTableField();
                    String mainPrimKey = tableModel.getRelationField();
                    // 外键值集合（通过主表关联键查询）
                    List<Object> fogValueList = dataList.stream().map(m -> m.get(mainPrimKey)).distinct()
                            .collect(Collectors.toList());
                    // 外键是字符串 - 转换主表数据string
                    TableFields fogIdField = tableModel.getFields().stream().filter(t -> t.getField().equals(fogID))
                            .findFirst().orElse(null);
                    boolean fogIdTypeString = Objects.nonNull(fogIdField)
                            ? fogIdField.getDataType().toLowerCase().contains("varchar")
                            : false;
                    if (fogIdTypeString) {
                        idStringList = idStringList.stream().map(t -> t.toString()).collect(Collectors.toList());
                    }

                    List<String> childFields = tableMap.get(tableField);
                    if (childFields != null) {
                        OnlineDynamicSqlModel onlineDynamicSqlModel = sqlModelList.stream()
                                .filter(s -> s.getTableName().equalsIgnoreCase(table)).findFirst().orElse(null);
                        SqlTable childSqlTable = onlineDynamicSqlModel.getSqlTable();
                        List<BasicColumn> childSqlColumns = new ArrayList<>();
                        for (String c : childFields) {
                            String childF = c.substring(c.lastIndexOf("-") + 1);
                            SqlColumn<Object> column = childSqlTable.column(childF);
                            childSqlColumns.add(column);
                        }
                        childSqlColumns.add(childSqlTable.column(childPrimKey));
                        childSqlColumns.add(childSqlTable.column(fogID));
                        // 查子表数据字段，不去重
                        QueryExpressionDSL<SelectModel> childFrom = SqlBuilder.select(childSqlColumns)
                                .from(mainSqlModel.getSqlTable());
                        if (dycList.size() > 0) {
                            for (OnlineDynamicSqlModel sqlModel : dycList) {
                                String relationKey = sqlModel.getRelationKey();
                                boolean relationKeyTypeString = false;
                                for (TableModel item : tableModelList) {
                                    if (item.getTable().equals(sqlModel.getTableName())) {
                                        String foreign = sqlModel.getForeign();
                                        TableFields thisfield = item.getFields().stream()
                                                .filter(t -> t.getField().equals(foreign)).findFirst().orElse(null);
                                        if (thisfield != null) {
                                            relationKeyTypeString = thisfield.getDataType().toLowerCase()
                                                    .contains("varchar");
                                        }
                                    }
                                }
                                // postgresql自增 int和varchar无法对比-添加以下判断
                                if (Objects.equals(formData.getPrimaryKeyPolicy(), 2)
                                        && "PostgreSQL".equalsIgnoreCase(databaseProductName)
                                        && relationKeyTypeString) {
                                    relationKey += "::varchar";
                                }
                                // 用于查询拼接各条件之后的主表id列表（和统计数量）
                                childFrom.leftJoin(sqlModel.getSqlTable())
                                        .on(sqlModel.getSqlTable().column(sqlModel.getForeign()),
                                                new EqualTo(mainSqlModel.getSqlTable().column(relationKey)));
                            }
                        }
                        QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder childWhere = childFrom.where();

                        if (fogValueList.size() > 1000) {
                            List<List<Object>> lists = Lists.partition(fogValueList, 1000);
                            List<AndOrCriteriaGroup> groupListAll = new ArrayList<>();
                            for (List<Object> item : lists) {
                                groupListAll.add(SqlBuilder.or(childSqlTable.column(fogID), SqlBuilder.isIn(item)));
                            }
                            groupListAll.remove(0);
                            childWhere.and(childSqlTable.column(fogID), SqlBuilder.isIn(lists.get(0)), groupListAll);
                        } else {
                            childWhere.and(childSqlTable.column(fogID), SqlBuilder.isIn(fogValueList));
                        }

                        // 逻辑删除不展示
                        if (logicalDelete) {
                            childWhere.and(childSqlTable.column(TableFeildsEnum.DELETEMARK.getField()),
                                    SqlBuilder.isNull());
                        }
                        // 查询条件sql
                        OnlineProductSqlUtils.getSuperSql(childWhere, query, sqlModelList, databaseProductName, null,
                                false);
                        // 高级查询
                        OnlineProductSqlUtils.getSuperSql(childWhere, superQuery, sqlModelList, databaseProductName,
                                null, false);
                        // 数据过滤
                        OnlineProductSqlUtils.getSuperSql(childWhere, ruleQuery, sqlModelList, databaseProductName,
                                null, false);
                        // 关键词搜索
                        OnlineProductSqlUtils.getSuperSql(childWhere, keyQuery, sqlModelList, databaseProductName, null,
                                false);
                        // 数据权限
                        if (columnDataModel.getUseDataPermission() != null && columnDataModel.getUseDataPermission()) {
                            if (!userInfo.getIsAdministrator() && !userStanding) {
                                OnlineProductSqlUtils.getSuperSql(childWhere, authorizeListAll, sqlModelList,
                                        databaseProductName, null);
                            }
                        }
                        SelectStatementProvider childRender = childWhere.build().render(RenderingStrategies.MYBATIS3);
                        List<Map<String, Object>> mapList = flowFormDataMapper.selectManyMappedRows(childRender);
                        // 连表去重，distinct去不掉
                        List<Object> dictinctKey = new ArrayList<>();
                        List<Map<String, Object>> newList = new ArrayList<>();
                        for (Map<String, Object> item : mapList) {
                            Object o = item.get(childPrimKey);
                            if (!dictinctKey.contains(o)) {
                                newList.add(item);
                                dictinctKey.add(o);
                            }
                            item.put(FlowFormConstant.ID, o);
                        }
                        Map<String, List<Map<String, Object>>> idMap = newList.stream()
                                .collect(Collectors.groupingBy(m -> m.get(fogID).toString()));

                        for (Map<String, Object> m : dataList) {
                            if (ObjectUtil.isNotEmpty(m.get(mainPrimKey))) {
                                String s = m.get(mainPrimKey).toString();
                                Map<String, Object> valueMap = new HashMap<>();
                                valueMap.put(tableField, idMap.get(s));
                                m.putAll(valueMap);
                            }
                        }
                    }
                }
            } else {
                return new ArrayList<>();
            }

            // 添加id属性
            dataList = FormPublicUtils.addIdToList(dataList, pkeyId);

            // 别名key还原
            setAliasKey(dataList, aliasMap);

            paginationModel.setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DynamicDataSourceUtil.clearSwitchDataSource();
        }
        return dataList;
    }

    private List<ConditionJsonModel> superQueryList(List<SuperQueryJsonModel> queryJsonModels,
            Map<String, String> tableFieldAndTableName, String mainTable) {
        List<ConditionJsonModel> superQueryList = new ArrayList<>();
        for (SuperQueryJsonModel jsonModel : queryJsonModels) {
            List<FieLdsModel> conditionList = JsonUtil.getJsonToList(jsonModel.getGroups(), FieLdsModel.class);
            for (FieLdsModel fieLdsModel : conditionList) {
                ConditionJsonModel sup = JsonUtil.getJsonToBean(fieLdsModel, ConditionJsonModel.class);
                sup.setTableName(fieLdsModel.getConfig().getRelationTable());
                String field = sup.getField();
                String mastKey = fieLdsModel.getConfig().getYunzhupaasKey();
                if (field.toLowerCase().contains(YunzhupaasKeyConsts.CHILD_TABLE_PREFIX)) {
                    String substring = field.substring(field.lastIndexOf("-") + 1);
                    String tableField = field.substring(0, field.indexOf("-"));
                    sup.setField(substring);
                    sup.setTableName(tableFieldAndTableName.get(tableField));
                } else if (StringUtil.isEmpty(sup.getTableName())) {
                    sup.setTableName(mainTable);
                }
                if (mastKey.equals(YunzhupaasKeyConsts.CHECKBOX) || mastKey.equals(YunzhupaasKeyConsts.ADDRESS)
                        || mastKey.equals(YunzhupaasKeyConsts.CASCADER)
                        || mastKey.equals(YunzhupaasKeyConsts.COMSELECT)) {
                    fieLdsModel.setMultiple(true);
                }
                if (fieLdsModel.getMultiple() && StringUtil.isEmpty(sup.getFieldValue())) {
                    sup.setFieldValue("[]");
                }
                superQueryList.add(sup);
            }
        }
        return superQueryList;
    }

    @Override
    public List<Map<String, Object>> getRelationFormList(VisualDevJsonModel visualDevJsonModel,
            PaginationModel paginationModel) {
        FormDataModel formData = visualDevJsonModel.getFormData();
        Boolean logicalDelete = visualDevJsonModel.getFormData().getLogicalDelete();
        List<String> collect = StringUtil.isNotEmpty(paginationModel.getColumnOptions())
                ? Arrays.asList(paginationModel.getColumnOptions().split(","))
                : new ArrayList<>();
        List<FieLdsModel> fieLdsModels = JsonUtil.getJsonToList(formData.getFields(), FieLdsModel.class);

        List<FieLdsModel> mainFieldModelList = new ArrayList<>();

        List<Map<String, Object>> noSwapDataList = new ArrayList<>();
        List<VisualColumnSearchVO> searchVOList = new ArrayList<>();
        // 列表中区别子表正则
        String reg = "^[yunzhupaas_]\\S*_yunzhupaas\\S*";
        // 查询的关键字
        String keyword = paginationModel.getKeyword();
        // 判断有无表
        if (visualDevJsonModel.getVisualTables().size() > 0) {
            try {
                List<TableModel> tableModelList = JsonUtil.getJsonToList(visualDevJsonModel.getVisualTables(),
                        TableModel.class);

                OnlinePublicUtils.recursionFields(mainFieldModelList, fieLdsModels);

                // 主表
                TableModel mainTable = tableModelList.stream().filter(t -> t.getTypeId().equals("1")).findFirst()
                        .orElse(null);

                DbLinkEntity linkEntity = dblinkService.getInfo(visualDevJsonModel.getDbLinkId());

                List<OnlineDynamicSqlModel> sqlModelList = new ArrayList<>();
                // 根据表字段创建sqltable
                for (TableModel model : tableModelList) {
                    OnlineDynamicSqlModel sqlModel = new OnlineDynamicSqlModel();
                    sqlModel.setSqlTable(SqlTable.of(model.getTable()));
                    sqlModel.setTableName(model.getTable());
                    if (model.getTypeId().equals("1")) {
                        sqlModel.setMain(true);
                    } else {
                        sqlModel.setForeign(model.getTableField());
                        sqlModel.setRelationKey(model.getRelationField());
                        sqlModel.setMain(false);
                    }
                    sqlModelList.add(sqlModel);
                }

                // 判断是否分页
                Boolean isPage = paginationModel.getPageSize() > 500 ? false : true;
                // 获取表单主表副表全字段
                List<String> allFields = new ArrayList<>();
                for (FieLdsModel item : mainFieldModelList) {
                    if (StringUtil.isNotEmpty(item.getVModel())
                            && !item.getVModel().toLowerCase().startsWith(YunzhupaasKeyConsts.CHILD_TABLE_PREFIX)) {
                        allFields.add(item.getVModel());
                    }
                }
                OnlineProductSqlUtils.getColumnListSql(sqlModelList, visualDevJsonModel, allFields, linkEntity);

                // 数据过滤
                List<TableModel> visualTables = visualDevJsonModel.getVisualTables();
                RecursionForm recursionForm = new RecursionForm(fieLdsModels, visualTables);
                List<FormAllModel> formAllModel = new ArrayList<>();
                FormCloumnUtil.recursionForm(recursionForm, formAllModel);
                OnlineProductSqlUtils.queryList(formAllModel, visualDevJsonModel, paginationModel);
                SuperJsonModel ruleQuery = visualDevJsonModel.getRuleQuery();
                ArrayList listquery = new ArrayList() {
                    {
                        add(ruleQuery);
                    }
                };

                DynamicDataSourceUtil.switchToDataSource(linkEntity);
                @Cleanup
                Connection connection = ConnUtil.getConnOrDefault(linkEntity);
                String databaseProductName = connection.getMetaData().getDatabaseProductName().trim();
                // 主表
                OnlineDynamicSqlModel mainSqlModel = sqlModelList.stream().filter(OnlineDynamicSqlModel::isMain)
                        .findFirst().orElse(null);

                String pkeyId = flowFormDataUtil.getKey(mainTable, databaseProductName);
                visualDevJsonModel.setPkeyId(pkeyId);

                Map<String, String> tableFieldAndTableName = new HashMap<>(8);
                Map<String, String> tableNameAndTableField = new HashMap<>(8);
                mainFieldModelList.stream()
                        .filter(f -> f.getConfig().getYunzhupaasKey().equals(YunzhupaasKeyConsts.CHILD_TABLE))
                        .forEach(f -> {
                            tableFieldAndTableName.put(f.getVModel(), f.getConfig().getTableName());
                            tableNameAndTableField.put(f.getConfig().getTableName(), f.getVModel());
                        });

                // 非主表
                List<OnlineDynamicSqlModel> dycList = sqlModelList.stream().filter(dyc -> !dyc.isMain())
                        .collect(Collectors.toList());
                List<BasicColumn> sqlColumns = new ArrayList<>();
                for (OnlineDynamicSqlModel dynamicSqlModel : sqlModelList) {
                    if (tableNameAndTableField.containsKey(dynamicSqlModel.getTableName()))
                        continue;
                    List<BasicColumn> basicColumns = Optional.ofNullable(dynamicSqlModel.getColumns())
                            .orElse(new ArrayList<>());
                    sqlColumns.addAll(basicColumns);
                }
                QueryExpressionDSL<SelectModel> from = SqlBuilder.select(sqlColumns).from(mainSqlModel.getSqlTable());
                QueryExpressionDSL<SelectModel> childFrom = SqlBuilder.select(mainSqlModel.getSqlTable().column(pkeyId))
                        .from(mainSqlModel.getSqlTable());

                SuperJsonModel keyWordQuery = new SuperJsonModel();
                if (Objects.equals(paginationModel.getQueryType(), 0) && StringUtil.isNotEmpty(keyword)) {
                    List<FieLdsModel> keywordFields = mainFieldModelList.stream().filter(t -> t.getConfig() != null
                            && YunzhupaasKeyConsts.getKeyWordList().contains(t.getConfig().getYunzhupaasKey()))
                            .collect(Collectors.toList());
                    List<FieLdsModel> keywordList = new ArrayList<>();
                    for (FieLdsModel fieLdsModel : keywordFields) {
                        fieLdsModel.setFieldValue(keyword);
                        fieLdsModel.setSymbol(SearchMethodEnum.Like.getSymbol());
                        fieLdsModel.setId(fieLdsModel.getVModel());
                        OnlineProductSqlUtils.tabelName(fieLdsModel, formAllModel);
                        keywordList.add(fieLdsModel);
                    }
                    SuperQueryJsonModel queryJsonModel = new SuperQueryJsonModel();
                    queryJsonModel.setLogic(SearchMethodEnum.Or.getSymbol());
                    queryJsonModel.setGroups(keywordList);
                    keyWordQuery.setConditionList(new ArrayList() {
                        {
                            add(queryJsonModel);
                        }
                    });
                    listquery.add(keyWordQuery);
                }
                // 取出所有有用到的表-进行关联过滤
                List<String> allTableName = OnlinePublicUtils.getAllTableName(new ArrayList<>(), listquery,
                        tableFieldAndTableName);

                if (dycList.size() > 0) {
                    for (OnlineDynamicSqlModel sqlModel : dycList) {
                        if (tableNameAndTableField.containsKey(sqlModel.getTableName())
                                && !allTableName.contains(sqlModel.getTableName()))
                            continue;
                        // 过滤数据连接所有表
                        childFrom.leftJoin(sqlModel.getSqlTable()).on(
                                sqlModel.getSqlTable().column(sqlModel.getForeign()),
                                new EqualTo(mainSqlModel.getSqlTable().column(sqlModel.getRelationKey())));
                        // 结果数据不连子表
                        if (!tableNameAndTableField.containsKey(sqlModel.getTableName())) {
                            from.leftJoin(sqlModel.getSqlTable()).on(
                                    sqlModel.getSqlTable().column(sqlModel.getForeign()),
                                    new EqualTo(mainSqlModel.getSqlTable().column(sqlModel.getRelationKey())));
                        }
                    }
                }
                QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder childWhere = childFrom.where();

                OnlineProductSqlUtils.getSuperSql(childWhere, ruleQuery, sqlModelList, databaseProductName, null,
                        false);

                if (Objects.equals(paginationModel.getQueryType(), 0) && StringUtil.isNotEmpty(keyword)) {
                    OnlineProductSqlUtils.getSuperSql(childWhere, keyWordQuery, sqlModelList, databaseProductName, null,
                            false);
                }

                // 逻辑删除不展示
                if (logicalDelete) {
                    childWhere.and(mainSqlModel.getSqlTable().column(TableFeildsEnum.DELETEMARK.getField()),
                            SqlBuilder.isNull());
                }

                QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder whereRes = from.where();
                whereRes.and(SqlTable.of(mainTable.getTable()).column(pkeyId), SqlBuilder.isIn(childWhere));

                // 排序
                if (StringUtil.isNotEmpty(paginationModel.getSidx())) {
                    String[] split = paginationModel.getSidx().split(",");
                    List<SortSpecification> sidxList = new ArrayList<>();
                    for (String sidx : split) {
                        // 目前只支持主表排序
                        if (sidx.toLowerCase().contains("_yunzhupaas_")
                                || sidx.toLowerCase().contains(YunzhupaasKeyConsts.CHILD_TABLE_PREFIX)) {
                            continue;
                        }
                        SortSpecification sortSpecification;
                        if (sidx.startsWith("-")) {
                            sortSpecification = SqlBuilder.sortColumn(mainTable.getTable(),
                                    SqlTable.of(mainTable.getTable()).column(sidx.substring(1))).descending();
                        } else {
                            sortSpecification = SqlBuilder.sortColumn(mainTable.getTable(),
                                    SqlTable.of(mainTable.getTable()).column(sidx));
                        }
                        sidxList.add(sortSpecification);
                    }
                    whereRes.orderBy(sidxList);
                } else {
                    whereRes.orderBy(SqlBuilder
                            .sortColumn(mainTable.getTable(), SqlTable.of(mainTable.getTable()).column(pkeyId))
                            .descending());
                }
                SelectStatementProvider renderRes = whereRes.build().render(RenderingStrategies.MYBATIS3);

                // 真分页，查询类型：0-简易查询（单行，多行，数字，下拉补全），1-全部字段
                if (Objects.equals(paginationModel.getQueryType(), 0)) {
                    QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder countSelect = SqlBuilder
                            .select(SqlBuilder.countDistinct(mainSqlModel.getSqlTable().column(pkeyId)))
                            .from(mainSqlModel.getSqlTable())
                            .where()
                            .and(SqlTable.of(mainTable.getTable()).column(pkeyId), SqlBuilder.isIn(childWhere));
                    SelectStatementProvider renderCount = countSelect.build().render(RenderingStrategies.MYBATIS3);
                    long count = flowFormDataMapper.count(renderCount);
                    paginationModel.setTotal(count);
                    if (count == 0) {
                        return new ArrayList<>();
                    }
                    if (isPage) {
                        PageHelper.startPage((int) paginationModel.getCurrentPage(),
                                (int) paginationModel.getPageSize(), false);
                    }
                }

                // 查询结果
                List<Map<String, Object>> dataList = flowFormDataMapper.selectManyMappedRows(renderRes);
                noSwapDataList = dataList.stream().map(data -> {
                    data.put(FlowFormConstant.ID, data.get(pkeyId));
                    return data;
                }).collect(Collectors.toList());

                // 第二种 有关键字不分页
                if (Objects.equals(paginationModel.getQueryType(), 1) && StringUtil.isNotEmpty(keyword)) {
                    for (FieLdsModel fieldsModel : mainFieldModelList) {
                        if (fieldsModel.getVModel() != null) {
                            boolean b = collect.stream().anyMatch(c -> fieldsModel.getVModel().equalsIgnoreCase(c));
                            // 组装为查询条件
                            if (b) {
                                VisualColumnSearchVO vo = new VisualColumnSearchVO();
                                vo.setSearchType("2");
                                vo.setVModel(fieldsModel.getVModel());
                                vo.setValue(keyword);
                                vo.setConfig(fieldsModel.getConfig());
                                Boolean multiple = fieldsModel.getMultiple();
                                vo.setMultiple(multiple);
                                searchVOList.add(vo);
                            }
                        }
                    }
                    noSwapDataList = onlineSwapDataUtils.getSwapList(noSwapDataList, mainFieldModelList,
                            visualDevJsonModel.getId(), false);

                    noSwapDataList = RelationFormUtils.getRelationListByKeyword(noSwapDataList, searchVOList);
                } else {
                    noSwapDataList = onlineSwapDataUtils.getSwapList(noSwapDataList, mainFieldModelList,
                            visualDevJsonModel.getId(), false);
                }
                // 假分页
                if (Objects.equals(paginationModel.getQueryType(), 1) && isPage) {
                    if (CollectionUtils.isNotEmpty(noSwapDataList)) {
                        paginationModel.setTotal(noSwapDataList.size());
                        List<List<Map<String, Object>>> partition = Lists.partition(noSwapDataList,
                                (int) paginationModel.getPageSize());
                        int i = (int) paginationModel.getCurrentPage() - 1;
                        noSwapDataList = partition.size() > i ? partition.get(i) : Collections.emptyList();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                DynamicDataSourceUtil.clearSwitchDataSource();
            }
        }
        if (noSwapDataList.size() < 1) {
            return new ArrayList<>();
        }
        return noSwapDataList;
    }

    @Override
    public List<Map<String, Object>> getListWithTableList(VisualDevJsonModel visualDevJsonModel,
            PaginationModel paginationModel, UserInfo userInfo) {
        FormDataModel formDataModel = visualDevJsonModel.getFormData();
        List<TableModel> visualTables = visualDevJsonModel.getVisualTables();
        if (ObjectUtil.isEmpty(visualDevJsonModel.getColumnData())) {
            ColumnDataModel columnDataModel = new ColumnDataModel();
            columnDataModel.setColumnList("[]");
            visualDevJsonModel.setColumnData(columnDataModel);
        }
        List<FieLdsModel> fieLdsModels = JsonUtil.getJsonToList(formDataModel.getFields(), FieLdsModel.class);
        RecursionForm recursionForm = new RecursionForm(fieLdsModels, visualTables);
        List<FormAllModel> formAllModel = new ArrayList<>();
        FormCloumnUtil.recursionForm(recursionForm, formAllModel);
        List<String> columnPropList = new ArrayList<>();
        for (FormAllModel model : formAllModel) {
            if (FormEnum.mast.getMessage().equals(model.getYunzhupaasKey())) {
                FieLdsModel fieLdsModel = model.getFormColumnModel().getFieLdsModel();
                if (ObjectUtil.isNotEmpty(fieLdsModel.getVModel())) {
                    columnPropList.add(fieLdsModel.getVModel());
                }
            }
            if (FormEnum.mastTable.getMessage().equals(model.getYunzhupaasKey())) {
                FieLdsModel fieLdsModel = model.getFormMastTableModel().getMastTable().getFieLdsModel();
                if (ObjectUtil.isNotEmpty(fieLdsModel.getVModel())) {
                    columnPropList.add(fieLdsModel.getVModel());
                }
            }
            if (FormEnum.table.getMessage().equals(model.getYunzhupaasKey())) {
                FormColumnTableModel childList = model.getChildList();
                List<FormColumnModel> childListAll = childList.getChildList();
                for (FormColumnModel childModel : childListAll) {
                    FieLdsModel fieLdsModel = childModel.getFieLdsModel();
                    if (ObjectUtil.isNotEmpty(fieLdsModel.getVModel())) {
                        columnPropList.add(childList.getTableModel() + "-" + fieLdsModel.getVModel());
                    }
                }
            }
        }
        // OnlineProductSqlUtils.queryList(formAllModel, visualDevJsonModel,
        // paginationModel);
        List<Map<String, Object>> dataList = getListWithTable(visualDevJsonModel, paginationModel, userInfo,
                columnPropList);
        List<FieLdsModel> fields = new ArrayList<>();
        OnlinePublicUtils.recursionFields(fields, fieLdsModels);
        visualDevJsonModel.setFormListModels(fields);
        dataList = onlineSwapDataUtils.getSwapList(dataList, fields, visualDevJsonModel.getId(), false);
        return dataList;
    }

    /**
     * 达梦或者oracle 别名太长转换-别名还原
     *
     * @param
     * @return
     * @copyright 深圳市乐程软件有限公司
     * @date 2023/3/16
     */
    private void setAliasKey(List<Map<String, Object>> dataList, Map<String, String> aliasMap) {
        if (dataList.size() > 0 && aliasMap.size() > 0) {
            for (int i = 0; i < dataList.size(); i++) {
                Map<String, Object> objMap = dataList.get(i);
                Set<String> aliasKey = aliasMap.keySet();
                Map<String, Object> newObj = new HashMap<>();
                for (String key : objMap.keySet()) {
                    Object value = objMap.get(key);
                    String oldKey = aliasMap.get(key);
                    if (aliasKey.contains(key)) {
                        newObj.put(oldKey, value);
                    } else {
                        newObj.put(key, value);
                    }
                }
                dataList.remove(i);
                dataList.add(i, newObj);
            }
        }
    }

    /**
     * 获取id，及主表的排序字段列表(用于查询排序)
     *
     * @param mainSqlModel
     * @param sidxs
     * @param pkeyId
     * @return
     */
    public List<BasicColumn> orderByColumns(OnlineDynamicSqlModel mainSqlModel, String sidxs, String pkeyId) {
        List<BasicColumn> distinctColumns = new ArrayList<>();
        distinctColumns.add(mainSqlModel.getSqlTable().column(pkeyId));
        if (StringUtil.isNotEmpty(sidxs)) {
            String[] split = sidxs.split(",");
            for (String sidx : split) {
                // 目前只支持主表排序
                if (sidx.toLowerCase().contains("_yunzhupaas_")
                        || sidx.toLowerCase().contains(YunzhupaasKeyConsts.CHILD_TABLE_PREFIX)
                        || sidx.toLowerCase().equals(pkeyId)) {
                    continue;
                }
                SqlColumn<Object> column = mainSqlModel.getSqlTable().column(sidx);
                if (sidx.startsWith("-")) {
                    column = mainSqlModel.getSqlTable().column(sidx.substring(1));
                }
                distinctColumns.add(column);
            }
        }
        return distinctColumns;
    }
}
