package com.stdproject.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Map;

/**
 * 动态数据服务接口
 * 提供对数据源表数据的增删改查操作
 */
public interface IDynamicDataService {

    /**
     * 向指定数据源的表中添加数据
     *
     * @param datasourceId 数据源ID
     * @param tableData    表数据JSON字符串，格式：
     *                     {
     *                       "tableName": "user",
     *                       "data": [
     *                         {
     *                           "fieldName": "id",
     *                           "fieldType": "varchar",
     *                           "IsPrimaryKey": true,
     *                           "fieldValue": "0001"
     *                         },
     *                         {
     *                           "fieldName": "name",
     *                           "fieldType": "varchar",
     *                           "fieldValue": "张三"
     *                         }
     *                       ]
     *                     }
     * @return 是否添加成功
     * @throws Exception 操作异常
     */
    boolean addTableData(Long datasourceId, String tableData) throws Exception;

    /**
     * 根据主键查询表数据
     *
     * @param datasourceId 数据源ID
     * @param condition    查询条件JSON字符串，格式：
     *                     {
     *                       "tableName": "user",
     *                       "key": [
     *                         {
     *                           "fieldName": "id",
     *                           "fieldValue": "0001"
     *                         }
     *                       ]
     *                     }
     * @return 查询到的数据Map，如果没有找到则返回null
     * @throws Exception 操作异常
     */
    Map<String, Object> getTableDataByPk(Long datasourceId, String condition) throws Exception;

    /**
     * 更新表数据
     *
     * @param datasourceId 数据源ID
     * @param tableData    更新数据JSON字符串，格式：
     *                     {
     *                       "tableName": "user",
     *                       "key": [
     *                         {
     *                           "fieldName": "id",
     *                           "fieldValue": "0001"
     *                         }
     *                       ],
     *                       "data": [
     *                         {
     *                           "fieldName": "name",
     *                           "fieldType": "varchar",
     *                           "fieldValue": "李四"
     *                         }
     *                       ]
     *                     }
     * @return 是否更新成功
     * @throws Exception 操作异常
     */
    boolean updateTableData(Long datasourceId, String tableData) throws Exception;

    /**
     * 删除表数据
     *
     * @param datasourceId 数据源ID
     * @param condition    删除条件JSON字符串，格式：
     *                     {
     *                       "tableName": "user",
     *                       "key": [
     *                         {
     *                           "fieldName": "id",
     *                           "fieldValue": "0001"
     *                         }
     *                       ]
     *                     }
     * @return 是否删除成功
     * @throws Exception 操作异常
     */
    boolean deleteTableData(Long datasourceId, String condition) throws Exception;

    /**
     * 分页查询表数据
     *
     * @param datasourceId 数据源ID
     * @param condition    查询条件JSON字符串，格式：
     *                     {
     *                       "tableName": "user",
     *                       "pageNum": 1,
     *                       "pageSize": 10,
     *                       "conditions": [
     *                         {
     *                           "field": "name",
     *                           "operator": "like",
     *                           "value": "张"
     *                         },
     *                         {
     *                           "field": "age",
     *                           "operator": ">",
     *                           "value": 18
     *                         }
     *                       ]
     *                     }
     * @return 分页查询结果
     * @throws Exception 操作异常
     */
    Page<Map<String, Object>> queryTableDataPaged(Long datasourceId, String condition) throws Exception;
}