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

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

/** 创建用户 */
export const updateUser = createAsyncThunk(
  'main/updateUser',
  async ({ params }) => {
    const { data } = await request({
      url: "/auth/user/update",
      data: params,
      method: "POST",
    })
    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 deleteRoles = createAsyncThunk(
  'main/deleteRoles',
  async (id) => {
    const { data } = await request({
      url:'/semantic/role/'+id,
      method: "DELETE",
    })
    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,resolve}) => {
    const { data } = await request({
      url: '/semantic/domain/create',
      data:params,
      method: "POST",
    })
    resolve && resolve(data)
    return data;
  },
);

/** 修改主题域 */
export const updateDomain = createAsyncThunk(
  'main/updateDomain',
  async ({params,resolve}) => {
    const { data } = await request({
      url: '/semantic/domain/update',
      data:params,
      method: "PUT",
    })
    resolve && resolve(data)
    return data;
  },
);

/** 修改主题域 */
export const removeDomain = createAsyncThunk(
  'main/removeDomain',
  async ({id,resolve}) => {
    const { data } = await request({
      url: '/semantic/domain/remove/'+id,
      method: "DELETE",
    })
    resolve && resolve(data)
    return data;
  },
);

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

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

/** 获取主题域下的模型 */
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 updateMetric = createAsyncThunk(
  'main/updateMetric',
  async ({params,resolve}) => {
    const {data} = await request({
      url: `/semantic/metric/updateMetric`,
      method:"PUT",
      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 deleteCondition = createAsyncThunk(
  'main/deleteCondition',
  async ({id,resolve}) => {
    const {data} = await request({
      url: `/semantic/condition/delete/`+id,
      method:"POST",
    })
    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 updateDataSet = createAsyncThunk(
  'main/createDataSet',
  async ({params,resolve}) => {
    const {data} = await request({
      url: `/semantic/dataSet `,
      method:"PUT",
      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 batchUnPublish = createAsyncThunk(
  'main/batchUnPublish',
  async ({params,resolve}) => {
    const {data} = await request({
      url: `/semantic/metric/batchUnPublish`,
      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 ({params,resolve}) => {
    const {data} = await request(
      {
        url: `/semantic/metric/queryTask/`+params?.id,
        data: params,
      }
    )
    resolve && resolve(data)
    return data
  },
);

/** 立即执行调度 */
export const runTask = createAsyncThunk(
  'main/runTask',
  async ({id,resolve}) => {
    const {data} = await request({
      url: `/semantic/metric/runTask/`+id,
      method:"POST",
      data: {
        metricId: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
  },
);

/** 指标收藏 */
export const collectionIndicators = createAsyncThunk(
  'main/createCollectionIndicators',
  async ({params,resolve}) => {
    const {data} = await request({
      url: `/semantic/collect/createCollectionIndicators`,
      method:"POST",
      data:params,
    })
    resolve && resolve(data)
    return data
  },
);

/** 指标取消收藏 */
export const deleteCollectionIndicators = createAsyncThunk(
  'main/createCollectionIndicators',
  async ({id,resolve}) => {
    const {data} = await request({
      url: `/semantic/collect/deleteCollectionIndicators/` +id,
      method:"DELETE",
    })
    resolve && resolve(data)
    return data
  },
);

/** 查询角色下的主题域权限 */
export const getDomainRoleAuthList = createAsyncThunk(
  'main/getDomainRoleAuthList',
  async ({roleId,resolve}) => {
    const {data} = await request({
      url: `/semantic/roleAuth/getDomainRoleAuthList`,
      method:"GET",
      data: {
        roleId
      },
    })
    resolve && resolve(data)
    return data
  },
);

/** 查询权限列表 */
export const getAuthList = createAsyncThunk(
  'main/getAuthList',
  async ({params,resolve}) => {
    const {data} = await request(`/semantic/roleAuth/getAuthList` ,{params})
    resolve && resolve(data)
    return data
  },
);

/** 角色授权 （主题域、指标）*/
export const roleAuthGrant = createAsyncThunk(
  'main/roleAuthGrant',
  async ({params,resolve}) => {
    const {data} = await request({
      url: `/semantic/roleAuth/grant`,
      method:"POST",
      data:params,
    })
    resolve && resolve(data)
    return data
  },
);

/** 角色授权 （主题域、指标）*/
export const roleAuthDelete = createAsyncThunk(
  'main/roleAuthDelete',
  async ({id,resolve}) => {
    const {data} = await request({
      url: `/semantic/roleAuth/delete/`+id,
      method:"DELETE"
    })
    resolve && resolve(data)
    return data
  },
);

/** 指标批量授权 */
export const batchRoleAuth = createAsyncThunk(
  'main/batchRoleAuth',
  async ({params,resolve}) => {
    const {data} = await request({
      url: `/semantic/roleAuth/grant/batch`,
      method:"POST",
      data:params
    })
    resolve && resolve(data)
    return data
  },
);

/** 查询指标版本*/
export const metricVersion = createAsyncThunk(
  'main/metricVersion',
  async ({id,resolve}) => {
    const {data} = await request(`/semantic/metric/version/`+id)
    resolve && resolve(data)
    return data
  },
);

/** 指标持久化配置 */
export const persistenceConfig = createAsyncThunk(
  'main/persistenceConfig',
  async ({params,resolve}) => {
    const {data} = await request({
      url: `/semantic/metric/persistenceConfig`,
      method:"POST",
      data:params
    })
    resolve && resolve(data)
    return data
  },
);

/** 获取持久化配置 */
export const getPersistenceConfig = createAsyncThunk(
  'main/getPersistenceConfig',
  async ({id,resolve}) => {
    const {data} = await request({
      url: '/semantic/metric/persistenceConfig/'+id,
      method:'GET',
    })
    resolve && resolve(data)
    return data
  },
);

/** 获取时间周期 */
export const periodList = createAsyncThunk(
  'main/periodList',
  async ({params,resolve}) => {
    const {data} = await request({
      url: '/semantic/period/list',
      data: params,
    })
    resolve && resolve(data)
    return data
  },
);

/** 获取调用详情 */
export const queryTaskLog = createAsyncThunk(
  'main/queryTaskLog',
  async ({metricId,instanceId,resolve}) => {
    const {data} = await request(`/semantic/metric/queryTaskLog/${metricId}/${instanceId}`)
    resolve && resolve(data)
    return data
  },
);

/** 修改维度 */
export const updateDimension = createAsyncThunk(
  'main/updateDimension',
  async ({params,resolve}) => {
    const {data} = await request({
      url: `/semantic/dimension/updateDimension`,
      method:"POST",
      data:params
    })
    resolve && resolve(data)
    return data
  },
);

/** 修改维度 */
export const createDimension = createAsyncThunk(
  'main/createDimension',
  async ({params,resolve}) => {
    const {data} = await request({
      url: `/semantic/dimension/createDimension`,
      method:"POST",
      data:params
    })
    resolve && resolve(data)
    return data
  },
);

/** 根据指标ID获取BI查询使用的SQL */
export const getMetricSQL = createAsyncThunk(
  'main/getMetricSQL',
  async (id) => {
    const {data} = await request(`/semantic/query/sql/metric/`+id)
    return data
  },
);