package com.ankaibei.workFlow.webadmin.ankaibei.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.ankaibei.workFlow.common.core.annotation.DisableDataFilter;
import com.ankaibei.workFlow.common.core.object.MyPageData;
import com.ankaibei.workFlow.common.core.object.ResponseResult;
import com.ankaibei.workFlow.common.core.object.TokenData;
import com.ankaibei.workFlow.common.core.util.MyPageUtil;
import com.ankaibei.workFlow.common.flow.dao.ProjectInfoFlowMapper;
import com.ankaibei.workFlow.common.flow.service.FlowApiService;
import com.ankaibei.workFlow.common.flow.service.FlowCategoryService;
import com.ankaibei.workFlow.common.flow.service.FlowEntryService;
import com.ankaibei.workFlow.common.flow.service.FlowWorkOrderService;
import com.ankaibei.workFlow.common.flow.util.FlowOperationHelper;
import com.ankaibei.workFlow.common.flow.vo.FlowTaskVo;
import com.ankaibei.workFlow.common.flow.vo.ListRuntimeTaskRequestVo;
import com.ankaibei.workFlow.common.flow.vo.TaskInfoVo;
import com.ankaibei.workFlow.common.online.dto.OnlineFilterDto;
import com.ankaibei.workFlow.common.online.model.OnlineColumn;
import com.ankaibei.workFlow.common.online.model.OnlineDatasource;
import com.ankaibei.workFlow.common.online.model.OnlineDatasourceRelation;
import com.ankaibei.workFlow.common.online.model.OnlineTable;
import com.ankaibei.workFlow.common.online.model.constant.FieldFilterType;
import com.ankaibei.workFlow.common.online.model.constant.RelationType;
import com.ankaibei.workFlow.common.online.service.OnlineOperationService;
import com.ankaibei.workFlow.common.online.util.OnlineOperationHelper;
import com.ankaibei.workFlow.webadmin.ankaibei.api.CostInFoApi;
import com.ankaibei.workFlow.webadmin.ankaibei.enums.CodeEnum;
import com.ankaibei.workFlow.webadmin.ankaibei.utils.FlowTaskStart;
import com.ankaibei.workFlow.webadmin.ankaibei.vo.CostInfoVo;
import com.ankaibei.workFlow.webadmin.ankaibei.vo.ResMsg;
import com.ankaibei.workFlow.webadmin.ankaibei.vo.UserInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lwq
 * @version 1.0
 * @description:费用管理
 * @date 2022/9/26 14:43
 */
@RestController
@RequestMapping("/cost")
public class CostInfoController {

    @Autowired
    private CostInFoApi costInFoApi;
    @Autowired
    private FlowApiService flowApiService;
    @Autowired
    private FlowEntryService flowEntryService;
    @Autowired
    private FlowCategoryService categoryService;
    @Autowired
    private ProjectInfoFlowMapper projectInfoFlowMapper;
    @Autowired
    private FlowOperationHelper flowOperationHelper;
    @Autowired
    private OnlineOperationHelper onlineOperationHelper;
    @Autowired
    private OnlineOperationService onlineOperationService;
    @Autowired
    private FlowTaskStart flowTaskStart;
    @Autowired
    private FlowWorkOrderService flowWorkOrderService;

    /**
     * 查询所有费用信息
     *
     * @param
     * @return ResMsg
     */
    @RequestMapping("/findCostInfoAll")
    public ResMsg findCostInfoAll(@RequestBody CostInfoVo costInfoVo) {

        return costInFoApi.findCostInfoAll(costInfoVo);
    }

    /**
     * 删除
     *
     * @param costInfoVoVos
     * @return ResMsg
     */
    @RequestMapping("/deleteCostInfo")
    public ResMsg deleteCostInfo(@RequestBody List<CostInfoVo> costInfoVoVos) {

        if (costInFoApi.deleteCostInfo(costInfoVoVos)) {
            return new ResMsg(CodeEnum.OK);
        }
        return new ResMsg(CodeEnum.FAIL);
    }

    /**
     * 编辑
     *
     * @param costInfoVo
     * @return ResMsg
     */
    @RequestMapping("/updateCostInfo")
    public ResMsg updateCostInfo(@RequestBody CostInfoVo costInfoVo) {

        return costInFoApi.updateCostInfo(costInfoVo);
    }

    /**
     * 查询费用详情  参数为类型和id
     *
     * @param costInfoVo
     * @return ResMsg
     */
    @RequestMapping("/findCostInfoDetails")
    public ResMsg findCostInfoDetails(@RequestBody CostInfoVo costInfoVo) {

        return costInFoApi.findCostInfoDetails(costInfoVo);
    }

    /**
     * 查询项目审核列表
     *
     * @param
     * @return ResMsg
     */
    @RequestMapping("/findProjectAuditList")
    public ResMsg findProjectAuditList(@RequestBody UserInfoVo userInfoVo) {
        Long id = TokenData.takeFromRequest().getUserId();
        return costInFoApi.findProjectAuditList(id, userInfoVo);
    }


    /**
     * 返回当前用户待办的任务列表。
     * param processDefinitionName 流程定义名 (模糊查询)。
     * param taskName              任务名称 (模糊查询)。
     * param pageParam             分页对象。
     *
     * @return 返回当前用户待办的任务列表。如果指定流程标识，则仅返回该流程的待办任务列表。
     */
    @DisableDataFilter
    @PostMapping("/listRuntimeTask")
    public ResMsg listRuntimeTask(
            @RequestBody ListRuntimeTaskRequestVo listRuntimeTaskRequestVo) {
        String username = TokenData.takeFromRequest().getLoginName();
        MyPageData<Task> pageData = flowApiService.getTaskListByUserName(
                username, listRuntimeTaskRequestVo);
        List<FlowTaskVo> flowTaskVoList = flowApiService.convertToFlowTaskList(pageData.getDataList());
        //加上流程分类名称和关联的表单信息中的项目名称
        List<FlowTaskVo> flowTaskVoListAd = new ArrayList<>();
        for (FlowTaskVo taskVo : flowTaskVoList) {
            String categoryName = categoryService.getById(flowEntryService.getById(taskVo.getEntryId()).getCategoryId()).getName();
            taskVo.setCategoryName(categoryName);
            //拿到任务所在表单中数据字段和对应的值(包含每个申请记录的项目名称和流程的任务变量)
            JSONObject jsonData = viewUserTaskDatasource(taskVo.getProcessInstanceId(), taskVo.getTaskId()).getData();
            Map<String, Object> masterAndOneToOne = (Map<String, Object>) jsonData.get("masterAndOneToOne");
            if (masterAndOneToOne.get("project_id") != null) {
                Integer projectId = Integer.parseInt(masterAndOneToOne.get("project_id").toString());
                taskVo.setProjectId(projectId);
                taskVo.setProjectName(projectInfoFlowMapper.getById(projectId).getProjectName());
            } else if (masterAndOneToOne.get("project_name") != null) {
                Integer projectId = Integer.parseInt(masterAndOneToOne.get("project_name").toString());
                taskVo.setProjectId(projectId);
                taskVo.setProjectName(projectInfoFlowMapper.getById(projectId).getProjectName());
            }
            if (masterAndOneToOne.get("application_fee") != null) {
                taskVo.setApplicationFee(masterAndOneToOne.get("application_fee").toString());
            }
            if (masterAndOneToOne.get("total_money") != null) {
                taskVo.setApplicationFee(masterAndOneToOne.get("total_money").toString());
            }
            if (masterAndOneToOne.get("accommodation_days") != null) {
                taskVo.setAccommodationDays(masterAndOneToOne.get("accommodation_days").toString());
            }
            //取每条费用申请记录中的费用项，费用类型的直接取，工时和新建项目使用流程分类名称即可
            if (masterAndOneToOne.get("cost_type") != null) {
                taskVo.setCostType(masterAndOneToOne.get("cost_type").toString());
            } else {
                taskVo.setCostType(categoryName);
            }
            flowWorkOrderService.getFlowWorkOrderByProcessInstanceId(taskVo.getProcessInstanceId());
            taskVo.setFlowTaskStatus(flowWorkOrderService.getFlowWorkOrderByProcessInstanceId(taskVo.getProcessInstanceId()).getFlowStatus() + "");
            if (StringUtils.isNotBlank(listRuntimeTaskRequestVo.getProjectName())) {
                if (taskVo.getProjectName().contains(listRuntimeTaskRequestVo.getProjectName())) {
                    flowTaskVoListAd.add(taskVo);
                }
            } else {
                flowTaskVoListAd.add(taskVo);
            }
        }
        return ResMsg.newInstance(CodeEnum.OK, MyPageUtil.makeResponseData(flowTaskVoListAd, pageData.getTotalCount()));
    }

    /**
     * 返回当前用户待办的任务列表。(费用报销分类)
     * param processDefinitionName 流程定义名 (模糊查询)。
     * param taskName              任务名称 (模糊查询)。
     * param pageParam             分页对象。
     *
     * @return 返回当前用户待办的任务列表。如果指定流程标识，则仅返回该流程的待办任务列表。
     */
    @DisableDataFilter
    @PostMapping("/listRuntimeTaskByExpense")
    public ResMsg listRuntimeTaskByExpense(
            @RequestBody ListRuntimeTaskRequestVo listRuntimeTaskRequestVo) {
        String username = TokenData.takeFromRequest().getLoginName();
        MyPageData<Task> pageData = flowApiService.getTaskListByUserName(
                username, listRuntimeTaskRequestVo);
        List<FlowTaskVo> flowTaskVoList = flowApiService.convertToFlowTaskList(pageData.getDataList());
        List<FlowTaskVo> flowTaskVoListByExpense = new ArrayList<>();
        for (FlowTaskVo taskVo : flowTaskVoList) {
            String categoryName = categoryService.getById(flowEntryService.getById(taskVo.getEntryId()).getCategoryId()).getName();
            taskVo.setCategoryName(categoryName);
            if (categoryName.equals("费用报销")) {
                //拿到任务所在表单中数据字段和对应的值(包含每个申请记录的项目名称和流程的任务变量)
                JSONObject jsonData = viewUserTaskDatasource(taskVo.getProcessInstanceId(), taskVo.getTaskId()).getData();
                Map<String, Object> masterAndOneToOne = (Map<String, Object>) jsonData.get("masterAndOneToOne");
                if (masterAndOneToOne.get("project_name") != null) {
                    Integer projectId = Integer.parseInt(masterAndOneToOne.get("project_name").toString());
                    taskVo.setProjectName(projectInfoFlowMapper.getById(projectId).getProjectName());
                }
                if (masterAndOneToOne.get("application_fee") != null) {
                    taskVo.setApplicationFee(masterAndOneToOne.get("application_fee").toString());
                }
                if (masterAndOneToOne.get("total_money") != null) {
                    taskVo.setApplicationFee(masterAndOneToOne.get("total_money").toString());
                }
                if (masterAndOneToOne.get("accommodation_days") != null) {
                    taskVo.setAccommodationDays(masterAndOneToOne.get("accommodation_days").toString());
                }
                //每个审批项在各自记录表中的主键id
                if (masterAndOneToOne.get("id") != null) {
                    taskVo.setApplyDataId(Integer.parseInt(masterAndOneToOne.get("id").toString()));
                }
                if (masterAndOneToOne.get("communication_id") != null) {
                    taskVo.setApplyDataId(Integer.parseInt(masterAndOneToOne.get("communication_id").toString()));
                }
                if (masterAndOneToOne.get("purchase_id") != null) {
                    taskVo.setApplyDataId(Integer.parseInt(masterAndOneToOne.get("purchase_id").toString()));
                }
                if (masterAndOneToOne.get("cost_type") != null) {
                    taskVo.setCostType(masterAndOneToOne.get("cost_type").toString());
                }
                flowWorkOrderService.getFlowWorkOrderByProcessInstanceId(taskVo.getProcessInstanceId());
                taskVo.setFlowTaskStatus(flowWorkOrderService.getFlowWorkOrderByProcessInstanceId(taskVo.getProcessInstanceId()).getFlowStatus() + "");
                if (StringUtils.isNotBlank(listRuntimeTaskRequestVo.getProjectName())) {
                    if (taskVo.getProjectName().contains(listRuntimeTaskRequestVo.getProjectName())) {
                        flowTaskVoListByExpense.add(taskVo);
                    }
                } else {
                    flowTaskVoListByExpense.add(taskVo);
                }
            }
        }
        return ResMsg.newInstance(CodeEnum.OK, MyPageUtil.makeResponseData(flowTaskVoListByExpense, Long.valueOf(flowTaskVoListByExpense.size())));
    }


    /**
     * 返回当前用户待办的任务列表。(员工工时分类)
     * param processDefinitionName 流程定义名 (模糊查询)。
     * param taskName              任务名称 (模糊查询)。
     * param pageParam             分页对象。
     *
     * @return 返回当前用户待办的任务列表。如果指定流程标识，则仅返回该流程的待办任务列表。
     */

    @DisableDataFilter
    @PostMapping("/listRuntimeTaskByManHour")
    public ResMsg listRuntimeTaskByManHour(
            @RequestBody ListRuntimeTaskRequestVo listRuntimeTaskRequestVo) {
        String username = TokenData.takeFromRequest().getLoginName();
        MyPageData<Task> pageData = flowApiService.getTaskListByUserName(
                username, listRuntimeTaskRequestVo);
        List<FlowTaskVo> flowTaskVoList = flowApiService.convertToFlowTaskList(pageData.getDataList());
        List<FlowTaskVo> flowTaskVoListByManHour = new ArrayList<>();
        for (FlowTaskVo taskVo : flowTaskVoList) {
            String categoryName = categoryService.getById(flowEntryService.getById(taskVo.getEntryId()).getCategoryId()).getName();
            taskVo.setCategoryName(categoryName);
            if (categoryName.equals("人员工时")) {
                //拿到任务所在表单中数据字段和对应的值(包含每个申请记录的项目名称和流程的任务变量)
                JSONObject jsonData = viewUserTaskDatasource(taskVo.getProcessInstanceId(), taskVo.getTaskId()).getData();
                Map<String, Object> masterAndOneToOne = (Map<String, Object>) jsonData.get("masterAndOneToOne");
                if (masterAndOneToOne.get("project_name") != null) {
                    Integer projectId = Integer.parseInt(masterAndOneToOne.get("project_name").toString());
                    taskVo.setProjectId(projectId);
                    taskVo.setProjectName(projectInfoFlowMapper.getById(projectId).getProjectName());
                }
                if (masterAndOneToOne.get("man_id") != null) {
                    taskVo.setApplyDataId(Integer.parseInt(masterAndOneToOne.get("man_id").toString()));
                }
                taskVo.setCostType(categoryName);
                flowWorkOrderService.getFlowWorkOrderByProcessInstanceId(taskVo.getProcessInstanceId());
                taskVo.setFlowTaskStatus(flowWorkOrderService.getFlowWorkOrderByProcessInstanceId(taskVo.getProcessInstanceId()).getFlowStatus() + "");
                if (StringUtils.isNotBlank(listRuntimeTaskRequestVo.getProjectName())) {
                    if (taskVo.getProjectName().contains(listRuntimeTaskRequestVo.getProjectName())) {
                        flowTaskVoListByManHour.add(taskVo);
                    }
                } else {
                    flowTaskVoListByManHour.add(taskVo);
                }
            }
        }
        return ResMsg.newInstance(CodeEnum.OK, MyPageUtil.makeResponseData(flowTaskVoListByManHour, Long.valueOf(flowTaskVoListByManHour.size())));
    }

    /**
     * 返回当前用户待办的任务列表。(操作项目信息分类)
     * param processDefinitionName 流程定义名 (模糊查询)。
     * param taskName              任务名称 (模糊查询)。
     * param pageParam             分页对象。
     *
     * @return 返回当前用户待办的任务列表。如果指定流程标识，则仅返回该流程的待办任务列表。
     */
    @DisableDataFilter
    @PostMapping("/listRuntimeTaskByProjectAdd")
    public ResMsg listRuntimeTaskByProjectAdd(
            @RequestBody ListRuntimeTaskRequestVo listRuntimeTaskRequestVo) {
        String username = TokenData.takeFromRequest().getLoginName();
        MyPageData<Task> pageData = flowApiService.getTaskListByUserName(
                username, listRuntimeTaskRequestVo);
        List<FlowTaskVo> flowTaskVoList = flowApiService.convertToFlowTaskList(pageData.getDataList());
        List<FlowTaskVo> flowTaskVoListByProjectAdd = new ArrayList<>();
        for (FlowTaskVo taskVo : flowTaskVoList) {
            String categoryName = categoryService.getById(flowEntryService.getById(taskVo.getEntryId()).getCategoryId()).getName();
            taskVo.setCategoryName(categoryName);
            if (categoryName.equals("新建项目")) {
                //拿到任务所在表单中数据字段和对应的值(包含每个申请记录的项目名称和流程的任务变量)
                JSONObject jsonData = viewUserTaskDatasource(taskVo.getProcessInstanceId(), taskVo.getTaskId()).getData();
                Map<String, Object> masterAndOneToOne = (Map<String, Object>) jsonData.get("masterAndOneToOne");
                if (masterAndOneToOne.get("project_id") != null) {
                    Integer projectId = Integer.parseInt(masterAndOneToOne.get("project_id").toString());
                    taskVo.setProjectId(projectId);
                    taskVo.setProjectName(projectInfoFlowMapper.getById(projectId).getProjectName());
                }
                taskVo.setCostType(categoryName);
                flowWorkOrderService.getFlowWorkOrderByProcessInstanceId(taskVo.getProcessInstanceId());
                taskVo.setFlowTaskStatus(flowWorkOrderService.getFlowWorkOrderByProcessInstanceId(taskVo.getProcessInstanceId()).getFlowStatus() + "");
                if (StringUtils.isNotBlank(listRuntimeTaskRequestVo.getProjectName())) {
                    if (taskVo.getProjectName().contains(listRuntimeTaskRequestVo.getProjectName())) {
                        flowTaskVoListByProjectAdd.add(taskVo);
                    }
                } else {
                    flowTaskVoListByProjectAdd.add(taskVo);
                }
            }
        }
        return ResMsg.newInstance(CodeEnum.OK, MyPageUtil.makeResponseData(flowTaskVoListByProjectAdd, Long.valueOf(flowTaskVoListByProjectAdd.size())));
    }

    /**
     * 返回当前用户待办的任务列表。(添加任务分类)
     * param processDefinitionName 流程定义名 (模糊查询)。
     * param taskName              任务名称 (模糊查询)。
     * param pageParam             分页对象。
     *
     * @return 返回当前用户待办的任务列表。如果指定流程标识，则仅返回该流程的待办任务列表。
     */
    @DisableDataFilter
    @PostMapping("/listRuntimeTaskByProjectTaskAdd")
    public ResMsg listRuntimeTaskByProjectTaskAdd(
            @RequestBody ListRuntimeTaskRequestVo listRuntimeTaskRequestVo) {
        String username = TokenData.takeFromRequest().getLoginName();
        MyPageData<Task> pageData = flowApiService.getTaskListByUserName(
                username, listRuntimeTaskRequestVo);
        List<FlowTaskVo> flowTaskVoList = flowApiService.convertToFlowTaskList(pageData.getDataList());
        List<FlowTaskVo> flowTaskVoListByProjectAdd = new ArrayList<>();
        for (FlowTaskVo taskVo : flowTaskVoList) {
            String categoryName = categoryService.getById(flowEntryService.getById(taskVo.getEntryId()).getCategoryId()).getName();
            taskVo.setCategoryName(categoryName);
            if (categoryName.equals("新建任务")) {
                //拿到任务所在表单中数据字段和对应的值(包含每个申请记录的项目名称和流程的任务变量)
                JSONObject jsonData = viewUserTaskDatasource(taskVo.getProcessInstanceId(), taskVo.getTaskId()).getData();
                Map<String, Object> masterAndOneToOne = (Map<String, Object>) jsonData.get("masterAndOneToOne");
                if (masterAndOneToOne.get("project_id") != null) {
                    Integer projectId = Integer.parseInt(masterAndOneToOne.get("project_id").toString());
                    taskVo.setProjectId(projectId);
                    taskVo.setProjectName(projectInfoFlowMapper.getById(projectId).getProjectName());
                }
                if (masterAndOneToOne.get("task_id") != null) {
                    taskVo.setApplyDataId(Integer.parseInt(masterAndOneToOne.get("task_id").toString()));
                }
                taskVo.setCostType(categoryName);
                flowWorkOrderService.getFlowWorkOrderByProcessInstanceId(taskVo.getProcessInstanceId());
                taskVo.setFlowTaskStatus(flowWorkOrderService.getFlowWorkOrderByProcessInstanceId(taskVo.getProcessInstanceId()).getFlowStatus() + "");
                if (StringUtils.isNotBlank(listRuntimeTaskRequestVo.getProjectName())) {
                    if (taskVo.getProjectName().contains(listRuntimeTaskRequestVo.getProjectName())) {
                        flowTaskVoListByProjectAdd.add(taskVo);
                    }
                } else {
                    flowTaskVoListByProjectAdd.add(taskVo);
                }
            }
        }
        return ResMsg.newInstance(CodeEnum.OK, MyPageUtil.makeResponseData(flowTaskVoListByProjectAdd, Long.valueOf(flowTaskVoListByProjectAdd.size())));
    }

    /**
     * 获取当前流程实例的详情数据。包括主表数据、一对一从表数据、一对多从表数据列表等。
     * 该接口无需数据权限过滤，因此用DisableDataFilter注解标注。如果当前系统没有支持数据权限过滤，该注解不会有任何影响。
     *
     * @param processInstanceId 当前运行时的流程实例Id。
     * @param taskId            流程任务Id。
     * @return 当前流程实例的详情数据。
     */
    public ResMsg<JSONObject> viewUserTaskDatasource(
            String processInstanceId, String taskId) {
        String errorMessage;
        // 验证流程任务的合法性。
        Task task = flowApiService.getProcessInstanceActiveTask(processInstanceId, taskId);
        ProcessInstance instance = flowApiService.getProcessInstance(processInstanceId);
        // 如果业务主数据为空，则直接返回。
        if (StrUtil.isBlank(instance.getBusinessKey())) {
            return ResMsg.newInstance(CodeEnum.OK);
        }
        ResponseResult<TaskInfoVo> taskInfoResult = flowOperationHelper.verifyAndGetRuntimeTaskInfo(task);
        if (!taskInfoResult.isSuccess()) {
            return ResMsg.newInstance(CodeEnum.FAIL, taskInfoResult);
        }
        TaskInfoVo taskInfo = taskInfoResult.getData();
        // 验证在线表单及其关联数据源的合法性。
        ResponseResult<OnlineDatasource> datasourceResult = flowTaskStart.verifyAndGetOnlineDatasource(taskInfo.getFormId());
        if (!datasourceResult.isSuccess()) {
            return ResMsg.newInstance(CodeEnum.FAIL, datasourceResult);
        }

        //datasource中columnMap为提交的申请的各个字段
        OnlineDatasource datasource = datasourceResult.getData();
        ResponseResult<List<OnlineDatasourceRelation>> relationListResult =
                onlineOperationHelper.verifyAndGetRelationList(datasource.getDatasourceId(), null);
        if (!relationListResult.isSuccess()) {
            return ResMsg.newInstance(CodeEnum.FAIL, relationListResult);
        }
        JSONObject jsonData = this.buildUserTaskData(
                instance.getBusinessKey(), datasource.getMasterTable(), relationListResult.getData());
        return ResMsg.newInstance(CodeEnum.OK, jsonData);
    }


    private JSONObject buildUserTaskData(
            String businessKey, OnlineTable masterTable, List<OnlineDatasourceRelation> relationList) {
        JSONObject jsonData = new JSONObject();
        List<OnlineDatasourceRelation> oneToOneRelationList = relationList.stream()
                .filter(r -> r.getRelationType().equals(RelationType.ONE_TO_ONE)).collect(Collectors.toList());
        Map<String, Object> result =
                onlineOperationService.getMasterData(masterTable, oneToOneRelationList, relationList, businessKey);
        if (MapUtil.isEmpty(result)) {
            return jsonData;
        }
        jsonData.put("masterAndOneToOne", result);
        List<OnlineDatasourceRelation> oneToManyRelationList = relationList.stream()
                .filter(r -> r.getRelationType().equals(RelationType.ONE_TO_MANY)).collect(Collectors.toList());
        if (CollUtil.isEmpty(oneToManyRelationList)) {
            return jsonData;
        }
        JSONObject oneToManyJsonData = new JSONObject();
        jsonData.put("oneToMany", oneToManyJsonData);
        for (OnlineDatasourceRelation relation : oneToManyRelationList) {
            OnlineFilterDto filterDto = new OnlineFilterDto();
            filterDto.setTableName(relation.getSlaveTable().getTableName());
            OnlineColumn slaveColumn = relation.getSlaveTable().getColumnMap().get(relation.getSlaveColumnId());
            filterDto.setColumnName(slaveColumn.getColumnName());
            filterDto.setFilterType(FieldFilterType.EQUAL_FILTER);
            OnlineColumn masterColumn = masterTable.getColumnMap().get(relation.getMasterColumnId());
            Object columnValue = result.get(masterColumn.getColumnName());
            filterDto.setColumnValue(columnValue);
            List<Map<String, Object>> slaveResultList =
                    onlineOperationService.getSlaveDataList(relation, CollUtil.newLinkedList(filterDto), null);
            if (CollUtil.isNotEmpty(slaveResultList)) {
                oneToManyJsonData.put(relation.getVariableName(), slaveResultList);
            }
        }
        return jsonData;
    }


}
