/*
 * @Author: Badguy
 * @Date: 2022-04-27 15:54:38
 * @LastEditTime: 2022-05-11 17:58:57
 * @LastEditors: your name
 * @Description: 服务相关数据查询
 * have a nice day!
 */

import { queryMap, myQuery } from '@/api/dataIntegration'

export default {
  data() {
    return {}
  },
  mounted() {
    if (this.serviceID) {
      this.queryAll()
    } else {
      this.$router.push('/systemSettings')
    }
  },
  methods: {
    // 协议 id: 1
    queryProtocolList() {
      return myQuery(queryMap.protocol)
    },
    // 机器/节点 id: 2
    queryNodeList() {
      return myQuery(queryMap.node)
    },
    // 服务 id: 3
    queryServiceList() {
      return myQuery(queryMap.service)
    },
    // 源数据 id: 4
    querySourceList() {
      return myQuery(queryMap.source)
    },
    // 源数据项 id: 5
    querySourceItemList() {
      return myQuery(queryMap.sourceItem)
    },
    // 源数据服务关联 id: 6
    queryServiceAndSourceList() {
      return myQuery(queryMap.serviceAndSource)
    },
    // 目标数据 id: 7
    queryTargetList() {
      return myQuery(queryMap.target)
    },
    // 目标数据项 id: 8
    queryTargetItemList() {
      return myQuery(queryMap.targetItem)
    },
    // 服务输出项关联 id: 9
    queryServiceAndOutputList() {
      return myQuery(queryMap.serviceAndOutput)
    },
    // 服务节点关联 id: 10
    queryServiceAndNodeList() {
      return myQuery(queryMap.serviceAndNode)
    },
    // 按照ID查询服务 id: 11
    queryServiceByID(serviceID) {
      return myQuery(queryMap.serviceByID, serviceID)
    },
    // 按照服务ID查询节点列表 id: 12
    queryNodeListByServiceId(serviceID) {
      return myQuery(queryMap.nodeByServiceID, serviceID)
    },
    // 按照服务ID查询源数据列表 id: 13
    querySourceListByServiceId(serviceID) {
      return myQuery(queryMap.sourceByServiceID, serviceID)
    },
    // 按照服务ID查询目标数据列表 id: 14
    queryTargetListByServiceId(serviceID) {
      return myQuery(queryMap.targetByServiceID, serviceID)
    },
    // 按照目标数据ID查询目标数据项列表 id: 15
    queryTargetItemListByTargetID(targetID) {
      return myQuery(queryMap.targetItemByTargetID, targetID)
    },
    // 按照源数据ID查询源数据项列表 id: 16
    querySourceItemListBySourceID(sourceID) {
      return myQuery(queryMap.sourceItemBySourceID, sourceID)
    },
    // 按照目标数据ID查询目标数据 id: 17
    queryTargetByID(targetID) {
      return myQuery(queryMap.targetByID, targetID)
    },
    // 按照源数据ID查询源数据 id: 18
    querySourceBySourceID(sourceID) {
      return myQuery(queryMap.sourceByID, sourceID)
    },
    // 按照服务ID查询算法计算方式 id: 19
    queryAlgorithmByServiceID(serviceID) {
      return myQuery(queryMap.serviceAlgorithmByServiceID, serviceID)
    },
    // 按照服务ID查询服务输出列表 id: 20
    queryOutputListByServiceId(serviceID) {
      return myQuery(queryMap.outputListByServiceID, serviceID)
    },
    async queryAll(serviceID = this.serviceID) {
      try {
        const [
          [serviceInfo],
          nodeList,
          checkedNodeList,
          serviceAndNodeList,
          protocolList,
          sourceList,
          checkedSourceList,
          serviceAndSourceList,
          outputList,
          targetList
        ] = await Promise.all([
          this.queryServiceByID(serviceID),
          this.queryNodeList(),
          this.queryNodeListByServiceId(serviceID),
          this.queryServiceAndNodeList(),
          this.queryProtocolList(),
          this.querySourceList(),
          this.querySourceListByServiceId(serviceID),
          this.queryServiceAndSourceList(),
          this.queryOutputListByServiceId(serviceID),
          this.queryTargetList()
        ])
        Object.keys(this.serviceForm).forEach(key => {
          this.serviceForm[key] = serviceInfo[key]
        })
        Object.keys(this.inputForm).forEach(key => {
          this.inputForm[key] = serviceInfo[key]
        })
        this.nodeList = nodeList
        this.checkedNodeKeys = checkedNodeList.map(node => node.deployNodeID)
        this.serviceAndNodeList = serviceAndNodeList
        this.protocolList = protocolList
        this.sourceList = sourceList
        this.checkedSourceKeys = checkedSourceList.map(source => source.sourceDataID)
        this.serviceAndSourceList = serviceAndSourceList
        this.outputList = outputList
        this.targetList = targetList
      } catch (error) {
        console.log('错误', error)
      }
    }
  }
}
