/*
 * @Author: Badguy
 * @Date: 2022-04-29 09:17:55
 * @LastEditTime: 2022-06-11 20:56:26
 * @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,
        event: 2
      }
      if (!params.serviceOutputID) {
        params.serviceOutputID = null
      }
      if (!params.dataSourceID) {
        params.dataSourceID = null
      }
      try {
        const result = await GeneralDataReception({
          serviceId: SERVICE_ID.sysServiceAddId,
          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 (event, deployObject) {
      const params = {
        event,
        serviceID: this.serviceID
      }
      if (event === 1) {
        Object.assign(params, {
          deployNodeID: deployObject.deployNodeID
        })
      } else {
        Object.assign(params, {
          DeployID: deployObject.servicedeploynodeID
        })
      }
      try {
        const result = await GeneralDataReception({
          serviceId: SERVICE_ID.sysServiceEditBoxId,
          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)
      }
    }
  }
}
