import { createAsyncThunk } from '@reduxjs/toolkit';
import {request} from "../../../../utils/request";
import {listTools} from "../../../../utils/listTools";

/** 查询用户列表 */
export const getUsers = createAsyncThunk(
  'main/getUsers',
  async ({ params }) => {
    const { data } = await request({
      url: '/auth/user/getUserList',
      data: params,
      method: "GET",
    })
    return data;
  },
);

/** 创建用户 */
export const createUser = createAsyncThunk(
  'main/createUser',
  async ({ params }) => {
    const { data } = await request({
      url: "/auth/user/create",
      data: params,
      method: "POST",
    })
    return data;
  },
);

/** 查询角色列表 */
export const getRoles = createAsyncThunk(
  'main/getRoles',
  async ({ params }) => {
    const { data } = await request('/semantic/role',{params})
    return data;
  },
);

/** 创建用户 */
export const saveOrUpdateRole = createAsyncThunk(
  'main/createRole',
  async ({ params, resolve }) => {
    const { data } = await request({
      url: "/semantic/role/saveOrUpdate",
      data: params,
      method: "POST",
    })
    resolve()
    return data;
  },
);

/** 查询数据源 */
export const getDBSource = createAsyncThunk(
  'main/getDBSource',
  async () => {
    const { data } = await request({
      url: '/semantic/database/getDatabaseList',
      method: "GET",
    })
    return data;
  },
);

/** 查询数据源下的数据库信息 */
export const getDBList = createAsyncThunk(
  'main/getDBList',
  async ({sourceId}) => {
    const { data } = await request({
      url: '/semantic/database/getDbNames/'+sourceId,
      method: "GET",
    })
    return data;
  },
);

/** 查询数据库中的表 */
export const getTableList = createAsyncThunk(
  'main/getTableList',
  async ({sourceId, db}) => {
    const { data } = await request({
      url: `/semantic/database/getTables/${sourceId}/${db}`,
      method: "GET",
    })
    return data;
  },
);

/** 查询表中的列 */
export const getTableColumns = createAsyncThunk(
  'main/getTableColumns',
  async ({sourceId, db,tableName}) => {
    const { data } = await request({
      url: `/semantic/database/getColumns/${sourceId}/${db}/${tableName}`,
      method: "GET",
    })
    return data;
  },
);

/** 创建主题域 */
export const createDomain = createAsyncThunk(
  'main/createDomain',
  async ({params}) => {
    const { data } = await request({
      url: '/semantic/domain/create',
      data:params,
      method: "POST",
    })
    return data;
  },
);

/** 获取主题域 */
export const getDomains = createAsyncThunk(
  'main/getDomains',
  async () => {
    const {data} = await request({
      url: '/semantic/domain/list'
    })
    return listTools({array: data})
  },
);

/** 获取主题域下的模型 */
export const getModals = createAsyncThunk(
  'main/getModals',
  async (domainId) => {
    const {data} = await request({
      url: '/semantic/model/getModelList/'+ domainId,
      method: "GET",
    })
    return data.map(item=>({
      ...item,
      ...item?.metricsCount,
      label:item.name,
      value:item.id,
      createMethod:data?.modelDetail?.sqlQuery?"SQL自定义":"快速创建",
    }))
  },
);

/** 获取主题域下的模型 */
export const getAllModals = createAsyncThunk(
  'main/getAllModals',
  async ({domainId}) => {
    const {data} = await request({
      url: '/semantic/model/getAllModels/'+ domainId,
      method: "GET",
    })
    return data.map(item=>({
      ...item,
      ...item.metricsCount,
      label:item.name,
      value:item.id,
      createMethod:data?.modelDetail?.sqlQuery?"SQL自定义":"快速创建",
    }))
  },
);

/** 创建模型 */
export const createModal = createAsyncThunk(
  'main/createModal',
  async ({params, resolve}) => {
    const {data} = await request({
      url: '/semantic/model/createModel',
      data:params,
      method: "POST",
    })
    resolve && resolve(data)
    return data
  },
);


/** 更新模型 */
export const updateModal = createAsyncThunk(
  'main/updateModal',
  async ({params, resolve}) => {
    const {data} = await request({
      url: '/semantic/model/updateModel',
      data:params,
      method: "PUT",
    })
    resolve && resolve(data)
    return data
  },
);

/** 删除模型 */
export const deleteModal = createAsyncThunk(
  'main/deleteModal',
  async ({id,resolve}) => {
    const {data} = await request({
      url: '/semantic/model/deleteModel/'+id,
      method: "DELETE",
    })
    resolve && resolve(data)
    return data
  },
);

/** 获取模型详情 */
export const getModalDetail = createAsyncThunk(
  'main/getModalDetail',
  async ({id,resolve}) => {
    const {data} = await request({
      url: '/semantic/model/getModel/'+ id,
      method: "GET",
    })
    resolve && resolve(data)
    return data
  },
);

/** 获取模型详情 */
export const getModalInfo = createAsyncThunk(
  'main/getModalDetail',
  async ({id,resolve}) => {
    const {data} = await request({
      url: '/semantic/model/getModelInfo/'+ id,
      method: "GET",
    })
    resolve && resolve(data)
    return data
  },
);

/** 获取模型下的所有指标 */
export const getMetricList = createAsyncThunk(
  'main/getMetricList',
  async ({id,resolve}) => {
    const {data} = await request(`/semantic/metric/getMetricList/`+ id)
    resolve && resolve(data)
    return data
  },
);

/** 根据参数获取指标列表 */
export const getIndexByConditions = createAsyncThunk(
  'main/getIndexByConditions',
  async ({params,resolve}) => {
    const {data} = await request({
      url: '/semantic/metric/queryMetric',
      data:params,
      method: "POST",
    })
    resolve && resolve(data)
    return data
  },
);

/** 根据参数获取指标列表 */
export const getIndexDetail = createAsyncThunk(
  'main/getIndexDetail',
  async ({id,resolve}) => {
    const {data} = await request({
      url: `/semantic/metric/getMetric/${id}`,
    })
    resolve && resolve(data)
    return data
  },
);

/** 根据参数获取指标列表 */
export const createMetric = createAsyncThunk(
  'main/createMetric',
  async ({params,resolve}) => {
    const {data} = await request({
      url: `/semantic/metric/createMetric`,
      method:"POST",
      data:params,
    })
    resolve && resolve(data)
    return data
  },
);

/** 根据参数获取指标列表 */
export const createCondition = createAsyncThunk(
  'main/createCondition',
  async ({params,resolve}) => {
    const {data} = await request({
      url: `/semantic/condition`,
      method:"POST",
      data:params,
    })
    resolve && resolve(data)
    return data
  },
);

/** 根据参数获取指标列表 */
export const getConditions = createAsyncThunk(
  'main/getConditions',
  async ({params}) => {
    const {data} = await request(
      `/semantic/condition/list` ,
      {
        params:{
          status:1,
          ...params
        }
      }
    )
    return data
  },
);

/** 保存数据集 */
export const createDataSet = createAsyncThunk(
  'main/createDataSet',
  async ({params,resolve}) => {
    const {data} = await request({
      url: `/semantic/dataSet `,
      method:"POST",
      data:params,
    })
    resolve && resolve(data)
    return data
  },
);

/** 获取数据集详情 */
export const getDataSet = createAsyncThunk(
  'main/getDataSet',
  async ({id,resolve}) => {
    const {data} = await request(`/semantic/dataSet/`+id )
    resolve && resolve(data)
    return data
  },
);

/** 获取数据集详情 */
export const deleteDataSet = createAsyncThunk(
  'main/deleteDataSet',
  async ({id,resolve}) => {
    const {data} = await request({
      url: `/semantic/dataSet/`+id,
      method:"DELETE",
    })
    resolve && resolve(data)
    return data
  },
);



/** 获取所有数据集列表 */
export const getAllDataset = createAsyncThunk(
  'main/getAllDataset',
  async ({params}) => {
    const {data} = await request(`/semantic/dataSet/getAllDataSetList` , {params})
    return data
  },
);


/** 查询数据集结果 */
export const queryDataSet = createAsyncThunk(
  'main/queryDataSet',
  async ({params,resolve}) => {
    const {data} = await request({
      url: `/semantic/query/dataSet`,
      method:"POST",
      data:params,
    })
    resolve && resolve(data)
    return data
  },
);

/** 根据指标查询结果 */
export const queryMetric = createAsyncThunk(
  'main/queryMetric',
  async ({params,resolve}) => {
    const {data} = await request({
      url: `/semantic/query/metric`,
      method:"POST",
      data:params,
    })
    resolve && resolve(data)
    return data
  },
);

/** 发布指标 */
export const publishMetric = createAsyncThunk(
  'main/publishMetric',
  async ({params,resolve}) => {
    const {data} = await request({
      url: `/semantic/metric/batchPublish`,
      method:"POST",
      data:params,
    })
    resolve && resolve(data)
    return data
  },
);

/** 发布指标 */
export const deleteMetric = createAsyncThunk(
  'main/publishMetric',
  async ({id,resolve}) => {
    const {data} = await request({
      url: `/semantic/metric/deleteMetric/`+id,
      method:"DELETE",
    })
    resolve && resolve(data)
    return data
  },
);


/** 获取维度列表 */
export const getDimensions = createAsyncThunk(
  'main/getDimensions',
  async ({id}) => {
    const {data} = await request(`/semantic/dimension/getDimensionList/`+id)
    return data
  },
);

/** 查询指标*/
export const queryDimensions = createAsyncThunk(
  'main/queryDimensions',
  async ({params,resolve}) => {
    const {data} = await request({
      url: `/semantic/dimension/queryDimension`,
      method:"POST",
      data:params,
    })
    resolve && resolve(data)
    return data
  },
);

/** 删除维度 */
export const deleteDimensions = createAsyncThunk(
  'main/deleteDimensions',
  async ({id,resolve}) => {
    const {data} = await request({
      url:`/semantic/dimension/deleteDimension/`+id,
      method:"DELETE",
    })
    resolve && resolve(data)
    return data
  },
);

/** 获取指标血缘*/
export const getMetricLineage = createAsyncThunk(
  'main/getMetricLineage',
  async ({id,resolve}) => {
    const {data} = await request(`/semantic/metric/getMetricLineage/`+id)
    resolve && resolve(data)
    return data
  },
);

/** 查询指标的调度记录*/
export const queryTask = createAsyncThunk(
  'main/queryTask',
  async ({id,resolve}) => {
    const {data} = await request(`/semantic/metric/queryTask/`+id)
    resolve && resolve(data)
    return data
  },
);

/** 批量发布 */
export const batchPublish = createAsyncThunk(
  'main/batchPublish',
  async ({params,resolve}) => {
    const {data} = await request({
      url: `/semantic/metric/batchPublish`,
      method:"POST",
      data:params,
    })
    resolve && resolve(data)
    return data
  },
);

/** 数据源测试连接 */
export const testConnect = createAsyncThunk(
  'main/testConnect',
  async ({params,resolve}) => {
    const {data} = await request({
      url: `/semantic/database/testConnect`,
      method:"POST",
      data:params,
    })
    resolve && resolve(data)
    return data
  },
);

/** 创建或更新数据源 */
export const createOrUpdateDatabase = createAsyncThunk(
  'main/createOrUpdateDatabase',
  async ({params,resolve}) => {
    const {data} = await request({
      url: `/semantic/database/createOrUpdateDatabase`,
      method:"POST",
      data:params,
    })
    resolve && resolve(data)
    return data
  },
);

/** SQL执行 */
export const executeSql = createAsyncThunk(
  'main/executeSql',
  async ({params,resolve}) => {
    const {data} = await request({
      url: `/semantic/database/executeSql`,
      method:"POST",
      data:params,
    })
    resolve && resolve(data)
    return data
  },
);