/*
 * @Author: Badguy
 * @Date: 2022-04-29 09:17:55
 * @LastEditTime: 2022-05-12 16:46:54
 * @LastEditors: your name
 * @Description: 服务相关操作
 * have a nice day!
 */

import { GeneralDataReception } from '@/api/dataIntegration'

const operateMap = {
  create: 1,
  update: 2,
  delete: 3
}

export default {
  methods: {
    async updateService() {
      const params = {
        serviceID: this.serviceID,
        ...this.serviceForm,
        ...this.inputForm,
        operate: 2
      }
      try {
        const result = await GeneralDataReception({
          serviceId: 3,
          dataContent: JSON.stringify(params)
        })
        if (Number(result.code === 0)) {
          this.$message.success(result.message ?? '成功')
        } else {
          this.$message.error(result.message ?? '失败')
        }
      } catch (error) {
        console.log('错误', error)
      }
    },
    async updateServiceNode(operate, deployNodeID) {
      const params = {
        operate: operateMap[operate],
        serviceID: this.serviceID
      }
      if (operate === 'create') {
        params.deployNodeID = deployNodeID
      } else {
        const { DeployID } = this.serviceAndNodeList.find(
          element => element.serviceID === this.serviceID && element.deployNodeID === deployNodeID
        )
        params.DeployID = DeployID
      }
      try {
        const result = await GeneralDataReception({
          serviceId: 10,
          dataContent: JSON.stringify(params)
        })
        if (Number(result.code) === 0) {
          return result.message
        } else {
          return Promise.reject(result.message)
        }
      } catch (error) {
        console.log('错误', error)
      }
    },
    async updateServiceSource(operate, sourceDataID) {
      const params = {
        operate: operateMap[operate],
        serviceID: this.serviceID
      }
      if (operate === 'create') {
        params.sourceDataID = sourceDataID
      } else {
        const { serviceInputSourceDataID } = this.serviceAndSourceList.find(
          element => element.serviceID === this.serviceID && element.sourceDataID === sourceDataID
        )
        params.serviceInputSourceDataID = serviceInputSourceDataID
      }
      try {
        const result = await GeneralDataReception({
          serviceId: 6,
          dataContent: JSON.stringify(params)
        })
        if (Number(result.code) === 0) {
          return result.message
        } else {
          return Promise.reject(result.message)
        }
      } catch (error) {
        console.log('错误', error)
      }
    },
    async updateServiceOutput(operate, outputTarget) {
      const { dataOutputConfig, outputCondition, protocolID, serviceOutputID, targetdataID } = outputTarget
      const params = {
        operate: operateMap[operate]
      }
      if (operate !== 'create') {
        Object.assign(params, {
          serviceOutputID
        })
      }
      if (operate !== 'delete') {
        Object.assign(params, {
          protocolID,
          dataOutputConfig,
          targetdataID,
          outputCondition,
          serviceID: this.serviceID
        })
      }
      try {
        const result = await GeneralDataReception({
          serviceId: 9,
          dataContent: JSON.stringify(params)
        })
        if (Number(result.code) === 0) {
          return result.message
        } else {
          return Promise.reject(result.message)
        }
      } catch (error) {
        console.log('错误', error)
      }
    }
  }
}
