import HelperUtil from "../../utils/HelperUtil";
import {PORT_KEY, PORT_DOWNLOAD_FILE_NAME} from "../../utils/constantCollection";
import PortListAPI from "../../api/restApis/PortListAPI";

// 静态方法
let staticMethods = {
  // 接入业务的条件查询 2664
  conditionQueryPortList: function (conditionSet){
    return PortListAPI.putConditionQueryPortList(conditionSet).then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue,PORT_KEY);
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 接入业务的删除 2682
  batchDeletePortList: function (portKeyList) {
    return PortListAPI.deleteDeletePortList(portKeyList).then(result => {
      if (result.data.result) {
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 获取全部端口信息
  getAllPortList: function () {
    return PortListAPI.getAllPortList().then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue,PORT_KEY);
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 导入端口文件
  uploadPortListFile: function (fileFormData) {
    return PortListAPI.postUploadPortListFile(fileFormData).then(result => {
      if (result.data.result) {
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 下载端口文件
  downloadPortListFile: function (conditionSet) {
    return PortListAPI.putDownloadPortListFile(conditionSet).then(result => {
      return HelperUtil.downFile(result,PORT_DOWNLOAD_FILE_NAME);
    });
  },

  // 创建端口
  addPort: function (port) {
    return PortListAPI.postAddPort(port).then(result => {
      if (result.data.result) {
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 修改端口
  modifyPort: function (port) {
    return PortListAPI.putModifyPort(port).then(result => {
      if (result.data.result) {
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  }
};

function PortListService (websocketService = null) {

  return Object.freeze(Object.assign(
    {
      // object methods comes here
    },
    staticMethods
  ))
}
// inject static methods
Object.assign(PortListService, staticMethods);
export default PortListService
