useNodeData.ts 4.5 KB
import { computed, onMounted, ref, unref } from 'vue'
import { doGetNodeBindData, doSaveNodeAct, doSaveNodeAllData, doSaveNodeDataSource, doSaveNodeEvent } from '@/api/node'
import { useParseParams } from '@/core/LoadData'
import type { MxCell } from '@/fitCore/types'
import type { BasicNodeBindType, NodeDataActJsonType, NodeDataDataSourceJsonType, NodeDataEventJsonType, NodeDataType, SaveNodeDataParamsType } from '@/api/node/model'
import { CellAttributeKeyEnum } from '@/enums/cellAttributeEnum'
import { useContentDataStoreWithOut } from '@/store/modules/contentData'
import { useMessage } from '@/hooks/web/useMessage'
import { MessageEnum } from '@/enums/messageEnum'
import type { DeviceItemType } from '@/api/device/model'
import { getDeviceInfo } from '@/api/device'

interface UseNodeDataParamsType {
  cell: MxCell
  immediate?: boolean
}

const getDefaultNodeData = (): Partial<NodeDataType> => {
  return {
    dataSourceJson: {} as any,
    eventJson: {} as any,
    actJson: {} as any,
  }
}

enum ActionType {
  SAVE = 'SAVE',
  GET = 'GET',
}

function removeCellSourceAttribute(cell: MxCell) {
  if (cell.hasAttribute(CellAttributeKeyEnum.COPY_SOURCE))
    cell.getValue()?.removeAttribute?.(CellAttributeKeyEnum.COPY_SOURCE)
}

export function useNodeData({ cell, immediate = true }: UseNodeDataParamsType) {
  const { createMessage } = useMessage()

  const nodeData = ref<NodeDataType>()
  const deviceInfo = ref<DeviceItemType>()

  const contentDataStore = useContentDataStoreWithOut()
  const { configurationId } = useParseParams()

  const getCellID = () => {
    return cell.getId()
  }

  const getSourceCellID = () => {
    const sourceNode = cell.getAttribute(CellAttributeKeyEnum.COPY_SOURCE)
    return sourceNode
  }

  const basicNodeBindData = (type: ActionType): BasicNodeBindType => {
    return {
      id: type === ActionType.GET ? getSourceCellID () || getCellID () : getCellID(),
      contentId: window.DrawApp.currentPage.getId(),
      configurationId,
    }
  }

  const getCellInfo = computed(() => {
    return {
      [CellAttributeKeyEnum.CATEGORY]: cell.getAttribute(CellAttributeKeyEnum.CATEGORY),
      [CellAttributeKeyEnum.COMPONENT_KEY]: cell.getAttribute(CellAttributeKeyEnum.COMPONENT_KEY),
    }
  })

  const getDeviceDetail = async () => {
    if (!unref(nodeData)?.dataSourceJson.deviceId) return
    deviceInfo.value = await getDeviceInfo(unref(nodeData)!.dataSourceJson.deviceId)
  }

  const getNodeAllData = async () => {
    const result = await doGetNodeBindData(basicNodeBindData(ActionType.GET))
    nodeData.value = result || getDefaultNodeData()
    await getDeviceDetail()
    return result
  }

  const saveNodeAllData = async (data: Partial<SaveNodeDataParamsType>) => {
    if (!contentDataStore.hasDesignAuth) {
      createMessage.error(MessageEnum.NO_AUTH)
      throw new Error(MessageEnum.NO_AUTH)
    }

    const result = await doSaveNodeAllData(Object.assign(data, basicNodeBindData(ActionType.SAVE)))
    removeCellSourceAttribute(cell)
    nodeData.value = result || getDefaultNodeData()

    await getDeviceDetail()
    return result
  }

  const saveNodeDataSourceData = async (data: NodeDataDataSourceJsonType) => {
    if (!contentDataStore.hasDesignAuth) {
      createMessage.error(MessageEnum.NO_AUTH)
      throw new Error(MessageEnum.NO_AUTH)
    }

    const result = await doSaveNodeDataSource({ ...basicNodeBindData(ActionType.SAVE), data })
    removeCellSourceAttribute(cell)
    if (unref(nodeData)?.dataSourceJson)
      nodeData.value!.dataSourceJson = result

    return result
  }

  const saveNodeEventData = async (data: NodeDataEventJsonType) => {
    if (!contentDataStore.hasDesignAuth) {
      createMessage.error(MessageEnum.NO_AUTH)
      throw new Error(MessageEnum.NO_AUTH)
    }

    const result = await doSaveNodeEvent({ ...basicNodeBindData(ActionType.SAVE), data })
    removeCellSourceAttribute(cell)
    return result
  }

  const saveNodeActData = async (data: NodeDataActJsonType) => {
    if (!contentDataStore.hasDesignAuth) {
      createMessage.error(MessageEnum.NO_AUTH)
      throw new Error(MessageEnum.NO_AUTH)
    }

    const result = await doSaveNodeAct({ ...basicNodeBindData(ActionType.SAVE), data })
    removeCellSourceAttribute(cell)
    return result
  }

  onMounted(() => {
    immediate && getNodeAllData()
  })

  return {
    getDeviceInfo: computed(() => unref(deviceInfo)),
    getNodeData: computed(() => unref(nodeData)),
    getCellInfo,
    getNodeAllData,
    saveNodeAllData,
    saveNodeEventData,
    saveNodeActData,
    saveNodeDataSourceData,
  }
}