import qs from 'qs';

import { request } from '@/utils';

import type {
  AccountParams,
  AddInterface,
  AgreementUpdate,
  AIAdvancedParamItem,
  AIOptimizeDetail,
  AIOptimizeDevData,
  AIStartStopDeviceItem,
  AIStartStopDeviceParams,
  AIStartStopOptimizeInfo,
  AlarmEvent,
  AlarmEventHistoryItemData,
  AlarmHistory,
  AlarmHistoryItem,
  AlarmPageParams,
  AlgorithmConfigInfo,
  AllDevicesList,
  AutomaticMatching,
  BatchUpdate,
  ChangeState,
  CharacterPageItem,
  CharacterPageItemData,
  CharacterParams,
  CoolingHistoryDataQuery,
  CoolingHistoryDataResult,
  CoolingRealTimeDataQuery,
  CoolingRealTimeDataResult,
  CoolingStatisticsQuery,
  CoolingStatisticsResult,
  CustomParameters,
  DeviceBindQuery,
  DeviceCop,
  DeviceGroup,
  DeviceGroupItem,
  DeviceGroupListQuery,
  DeviceParamGroup,
  DeviceParams,
  DeviceParamType,
  DevicePermissionsParams,
  DevicesList,
  DevicesListItemData,
  DeviceTypeCount,
  DevStartStopStatus,
  DevWorkHisDataQuery,
  DevWorkHistoryData,
  DevWorkRealTimeData,
  DevWorkRTDQuery,
  DictTypeData,
  DictTypeDataParams,
  ElectricityStatisticsQuery,
  ElectricityStatisticsResult,
  EquipmentDetailsForm,
  EquipmentDetailsItem,
  EquipmentTypeItem,
  EquipmentUpdateForm,
  EventTriggerData,
  GatewayBody,
  GatewayInformation,
  GatewayInterface,
  GatewayLinkProtocolQuery,
  GatewayListItemData,
  GatewayModelInfo,
  GatewayParameterBody,
  GatewayParameterItemData,
  GetListItem,
  GroupingList,
  GroupingListData,
  GroupModuleBatchItem,
  GroupModuleDevData,
  GroupModuleInfo,
  GroupModuleQuery,
  GroupRegions,
  InfoListByOrg,
  InterfaceData,
  InterfaceLsit,
  LimitForm,
  ListEquipmentParametersItemData,
  ListInfo,
  ListInterfaces,
  ListPhysicalInterfaces,
  LoginData,
  loginForm,
  LoginUser,
  MonitoringForm,
  MonitorPointInfo,
  MonitorPointItem,
  NoticePageItemData,
  OperateLogData,
  OperateLogQuery,
  Organization,
  OrganizationInfo,
  OrganizationItem,
  OrganizationListItem,
  OrganizationListItemData,
  OrganizationListParams,
  OutdooForm,
  PageParams,
  ParameterVerification,
  ParamValueListAutomaticQuery,
  PasswordParams,
  PermissionGroupItem,
  PermissionList,
  PointTimeSeriesQuery,
  PostProtocolPage,
  PostProtocolPageItem,
  PostProtocolPageItemData,
  ProtocolBaseInfo,
  ProtocolGatewayListListItem,
  ProtocolItemData,
  ProtocolList,
  ProtocolListData,
  ProtocolListItem,
  ProtocolListQuery,
  ProtocolParamData,
  ProtocolParamInfo,
  ProtocolParamSearchParams,
  ProtocolReset,
  ProtocolStandardParam,
  ProtocolStandardParamData,
  ProtocolStandardParamQuery,
  RegionQuery,
  RegionsPointsItem,
  RolePermissions,
  RolePermissionsItem,
  RolePermissionsParams,
  SerialNumberItem,
  SerialNumberItemData,
  SmartCtrlLogData,
  SmartCtrlLogQuery,
  SubmitDeviceGroup,
  SubmitSorting,
  TempHumidityControlSettings,
  TreeStructure,
  UploadLogo,
  UserPageItem,
  UserPageItemData,
  UserPageParams,
  VerificationAgreement,
  VerificationEquipment,
  WarningItem,
} from '@/types';

/**
 * 获取认证授权服务 url
 */
const apiUaa = (path: string, params?: unknown) => {
  const apiUrl = params ? `${path}?${qs.stringify(params)}` : path;
  return `/api-uaa${apiUrl}`;
};

/**
 * 获取系统服务 url
 */
const apiSys = (path: string, params?: unknown) => {
  const apiUrl = params ? `${path}?${qs.stringify(params)}` : path;
  return `/api-sys${apiUrl}`;
};

/**
 * 获取业务服务 url
 */
const apiBiz = (path: string, params?: unknown) => {
  const apiUrl = params ? `${path}?${qs.stringify(params, { arrayFormat: 'repeat' })}` : path;
  return `/api-biz${apiUrl}`;
};

// ----- 认证授权服务 -----

// 登录和注销

export const loginUser = async (params: LoginUser) => {
  const data = await request<loginForm>(apiUaa('/oauth/token', params), {
    method: 'POST',
    headers: {
      Authorization: 'Basic ' + btoa('unimat:unimat'),
    },
  });
  return data;
};

export const logoutUser = async () => {
  await request(apiUaa('/oauth/remove/token'));
};

export const refreshUser = async () => {
  await request(apiUaa('/oauth/token'));
};

// ----- 系统服务 -----

// 字典类型表
export const getDictTypeData = async (params: DictTypeDataParams) => {
  const data = await request<DictTypeData[]>(apiSys('/sysDictType/typeAndData', params));
  return data;
};

// 菜单权限表
export const getSubPermList = async (id: number) => {
  const data = await request<TreeStructure[]>(apiSys(`/sysPermission/subPermList/${id}`));
  return data;
};

export const getPermissionCheckTree = async (roleId: number) => {
  const data = await request<TreeStructure[]>(apiSys(`/sysPermission/permissionCheckTree/${roleId}`));
  return data;
};

export const getCurrentUser = async () => {
  const data = await request<TreeStructure[]>(apiSys('/sysPermission/currentUser'));
  return data;
};

export const getPermIdsByCurrentUser = async () => {
  const data = await request<number[]>(apiSys('/sysPermission/PermIdsByCurrentUser'));
  return data;
};

// 角色和菜单关联表
export const addGrantRolePermissions = async (params: RolePermissionsParams) => {
  await request(apiSys('/sysRolePermission/grantRolePermissions'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

// 角色和组织关联表
export const getFindRolesByOrgIds = async (params: number[]) => {
  const data = await request<CharacterPageItem[]>(apiSys('/sysRoleOrg/findRolesByOrgIds'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
  return data;
};

// 角色信息表
export const addRolePermissions = async (params: RolePermissions) => {
  await request(apiSys('/sysRole/addRolePermissions'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const getRolePermissions = async (id: number) => {
  const data = await request<RolePermissionsItem>(apiSys(`/sysRole/getRolePermissions/${id}`), {
    method: 'POST',
  });
  return data;
};

// 角色信息表

export const addCharacter = async (params: CharacterParams) => {
  await request(apiSys('/sysRole/add'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const updateCharacter = async (params: CharacterParams) => {
  await request(apiSys('/sysRole/update'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const getCharacterPageList = async (params: PageParams) => {
  const data = await request<CharacterPageItemData>(apiSys('/sysRole/getPageList'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
  return data;
};

export const deleteCharacter = async (id: number) => {
  await request(apiSys(`/sysRole/delete/${id}`), {
    method: 'POST',
  });
};

export const addUpdateRolePermissions = async (params: RolePermissions) => {
  await request(apiSys('/sysRole/updateRolePermissions'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

// 组织表
export const addOrganization = async (params: OrganizationItem) => {
  const data = await request<number>(apiSys('/sysOrg/add'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
  return data;
};

export const updateOrganization = async (params: OrganizationItem) => {
  await request<number>(apiSys('/sysOrg/update'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const addUploadLogo = async (file: Blob) => {
  const formData = new FormData();
  formData.append('file', file);
  const data = await request<UploadLogo>(apiSys('/sysOrg/uploadLogo'), {
    method: 'POST',
    body: formData,
  });
  return data;
};

export const getSubOrgsByToken = async () => {
  const data = await request<Organization[]>(apiSys('/sysOrg/subOrgsByToken'));
  return data;
};

export const getOrganizationList = async (params: OrganizationListParams) => {
  const data = await request<OrganizationListItemData>(apiSys('/sysOrg/getPageList'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
  return data;
};

export const getOrganizationAllList = async (params?: string) => {
  const data = await request<OrganizationListItem[]>(apiSys('/sysOrg/getList'), {
    method: 'POST',
    body: JSON.stringify({ orgName: params }),
  });
  return data;
};

export const getDownloadLogo = async (fileName: string) => {
  const data = await request(apiSys(`/sysOrg/downloadLogo/${fileName}`), {
    method: 'POST',
  });
  return data;
};

// 菜单权限表
export const getOrgInfo = async (id: number) => {
  const data = await request<OrganizationInfo>(apiSys(`/sysOrg/info/${id}`));
  return data;
};

// 用户信息表
export const addAccount = async (params: AccountParams) => {
  await request(apiSys('/sysUser/add'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const updateAccount = async (params: AccountParams) => {
  await request(apiSys('/sysUser/update'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const batchDeleteAccount = async (params: number[]) => {
  await request(apiSys('/sysUser/batchDelete/'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const getUserPageList = async (params: UserPageParams) => {
  const data = await request<UserPageItemData>(apiSys('/sysUser/getPageList'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
  return data;
};

export const getNeedFirstWizard = async () => {
  const data = await request<boolean>(apiSys('/sysUser/needFirstWizard'));
  return data;
};

export const addRevisePassword = async (params: PasswordParams) => {
  await request(apiSys('/sysUser/currentUser/password'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const getLoginData = async () => {
  const data = await request<LoginData>(apiSys('/sysUser/loginData'));
  return data;
};

// 内部用户表
export const getOrgUsers = async () => {
  const data = await request<UserPageItem[]>(apiSys('/inner/sysUser/orgUsers'));
  return data;
};

// ----- 业务服务 -----

// 报警事件

export const addAlarmEvent = async (params: AlarmEvent) => {
  await request(apiBiz('/alarmEvent/add'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const alarmEventChangeState = async (params: ChangeState) => {
  await request(apiBiz('/alarmEvent/changeState'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const deleteAlarmEvent = async (id: number) => {
  await request(apiBiz(`/alarmEvent/delete/${id}`), {
    method: 'POST',
  });
};

export const getAlarmEventGetPageList = async (params: AlarmPageParams) => {
  const data = await request<EventTriggerData>(apiBiz('/alarmEvent/getPageList'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
  return data;
};

export const getAlarmEventInfo = async (id: number) => {
  const data = await request<AlarmEvent>(apiBiz(`/alarmEvent/info/${id}`));
  return data;
};

export const updateAlarmEvent = async (params: AlarmEvent) => {
  await request(apiBiz('/alarmEvent/update'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

// 报警历史表

export const getAlarmHistoryList = async (params: AlarmHistory) => {
  const data = await request<AlarmEventHistoryItemData>(apiBiz('/alarmHistory/getPageList'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
  return data;
};

// 算法配置

export const getAlgorithmConfigInfo = async (devGroupId: number) => {
  const data = await request<AlgorithmConfigInfo>(apiBiz(`/algorithmConfig/info/byGroup/${devGroupId}`));
  return data;
};

export const addAlgorithmConfigUpdate = async (params: Partial<AlgorithmConfigInfo>) => {
  await request(apiBiz('/algorithmConfig/update'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

// 环境监控-监测点预警配置

export const addBatchMonitorPointAlarm = async (params: WarningItem[]) => {
  await request(apiBiz('/monitorPointAlarm/addBatch'), {
    method: 'POST',
    body: JSON.stringify({ monitorPointAlarms: params }),
  });
};

export const getMonitorPointAlarm = async (pointId: number) => {
  const data = await request<MonitorPointItem[]>(apiBiz(`/monitorPointAlarm/point/${pointId}`));
  return data;
};

export const updateBatchMonitorPointAlarm = async (params: WarningItem[]) => {
  await request(apiBiz('/monitorPointAlarm/updateBatch'), {
    method: 'POST',
    body: JSON.stringify({ monitorPointAlarms: params }),
  });
};

export const getMonitorPointAlarmHistory = async (devGroupId: number) => {
  const data = await request<AlarmHistoryItem[]>(apiBiz(`/monitorPointAlarm/history/${devGroupId}`));
  return data;
};

// 环境监控-区域

export const addRegion = async (params: RegionQuery) => {
  await request(apiBiz('/region/add'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const regionUpdate = async (params: RegionQuery) => {
  await request(apiBiz('/region/update'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const regionDelete = async (id: number) => {
  await request(apiBiz(`/region/delete/${id}`), {
    method: 'POST',
  });
};

export const getGroupRegions = async (devGroupId: number) => {
  const data = await request<GroupRegions[]>(apiBiz(`/region/groupRegions/${devGroupId}`), {
    method: 'POST',
  });
  return data;
};

export const getRegionsPointsData = async (devGroupId: number) => {
  const data = await request<RegionsPointsItem[]>(apiBiz(`/region/pointsData/${devGroupId}`), {
    method: 'POST',
  });
  return data;
};

export const getRegionsPointsValue = async (devGroupId: number, params: { endTime: string }) => {
  const data = await request<RegionsPointsItem[]>(apiBiz(`/region/pointsValue/${devGroupId}`), {
    method: 'POST',
    body: JSON.stringify(params),
  });
  return data;
};

// 环境监控-监测点

export const addMonitorPoint = async (params: MonitoringForm) => {
  await request(apiBiz('/monitorPoint/add'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const deleteMonitorPoint = async (id: number) => {
  await request(apiBiz(`/monitorPoint/delete/${id}`), {
    method: 'POST',
  });
};

export const updateMonitorPoint = async (params: MonitoringForm) => {
  await request(apiBiz('/monitorPoint/update'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const updateRegionMonitorPoint = async (params: OutdooForm) => {
  await request(apiBiz('/region/monitorPoint/update'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const getMonitorPointInfo = async (id: number) => {
  const data = await request<MonitorPointInfo>(apiBiz(`/monitorPoint/info/${id}`));
  return data;
};

export const updateLimits = async (params: LimitForm) => {
  await request(apiBiz('/monitorPoint/updateLimits'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const getPointTimeSeries = async (params: PointTimeSeriesQuery, pointId: number) => {
  const data = await request<TempHumidityControlSettings>(apiBiz(`/monitorPoint/pointTimeSeries/${pointId}`), {
    method: 'POST',
    body: JSON.stringify(params),
  });
  return data;
};

// 设备组列表

export const getPageList = async () => {
  const data = await request<DeviceGroupItem[]>(apiBiz('/deviceGroup/getList'), {
    method: 'POST',
  });
  return data;
};

export const addDeviceGroup = async (params: SubmitDeviceGroup) => {
  await request(apiBiz('/deviceGroup/add'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const addDeviceGroupUpdate = async (params: SubmitDeviceGroup) => {
  await request(apiBiz('/deviceGroup/update'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const deleteDeviceGroup = async (id: number) => {
  await request(apiBiz(`/deviceGroup/delete/${id}`), {
    method: 'POST',
  });
};

export const addDevicePermissions = async (params: DevicePermissionsParams) => {
  await request(apiBiz('/deviceGroup/update/role'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const getAllGroupList = async (params: PermissionList) => {
  const data = await request<PermissionGroupItem[]>(apiBiz('/deviceGroup/getAllList'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
  return data;
};

// 设备列表

export const deviceAdd = async (equipmentInformationForm: EquipmentDetailsForm) => {
  const data = await request<number>(apiBiz('/device/add'), {
    method: 'POST',
    body: JSON.stringify(equipmentInformationForm),
  });
  return data;
};

export const getDeviceGroupList = async (params: DeviceGroupListQuery) => {
  const data = await request<DeviceGroup[]>(apiBiz('/deviceGroup/getDeviceGroupList'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
  return data;
};

export const addDeviceBind = async (params: DeviceBindQuery) => {
  await request(apiBiz('/device/bind'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const deviceDeletion = async (ids: number[]) => {
  await request(apiBiz('/device/delete/batch'), {
    method: 'POST',
    body: JSON.stringify(ids),
  });
};

export const equipmentDetails = async (id: number) => {
  const data = await request<EquipmentDetailsItem>(apiBiz(`/device/getBasicInfo/${id}`), {
    method: 'POST',
  });
  return data;
};

export const queryDevicesList = async (devicesList: DevicesList) => {
  const data = await request<DevicesListItemData>(apiBiz('/device/getPageList'), {
    method: 'POST',
    body: JSON.stringify(devicesList),
  });
  return data;
};

export const noPaginationDevicesList = async () => {
  const data = await request<AllDevicesList[]>(apiBiz('/device/getList'), {
    method: 'POST',
  });
  return data;
};

export const getDeviceListSimple = async (deviceId: number) => {
  const data = await request<DeviceParams[]>(apiBiz(`/device/listSimple/${deviceId}`));
  return data;
};

export const getDeviceParams = async (deviceId: number, isProcessData?: boolean) => {
  const data = await request<DeviceParamGroup[]>(apiBiz('/device/protocolParamVerify'), {
    method: 'POST',
    body: JSON.stringify({
      deviceId,
      isProcessData,
    }),
  });

  return data;
};

export const getDeviceListOptions = async (deviceId: number) => {
  const data = await request<DeviceParamType[]>(apiBiz(`/device/listOptions/${deviceId}`));
  return data;
};

export const updateDeviceCop = async (params: DeviceCop[]) => {
  await request(apiBiz('/device/update'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

// 设备工况

export const getDevWorkTypeCount = async (deviceGroupId: number, deviceTypes: number[]) => {
  const data = await request<DeviceTypeCount[]>(apiBiz('/deviceCondition/getTypeCount'), {
    method: 'POST',
    body: JSON.stringify({
      deviceGroupId,
      deviceTypes,
    }),
  });

  return data;
};

export const getDevWorkRealTimeData = async (params: DevWorkRTDQuery) => {
  const data = await request<DevWorkRealTimeData[]>(apiBiz('/deviceCondition/getRTDList'), {
    method: 'POST',
    body: JSON.stringify(params),
  });

  return data;
};

export const getDevWorkHistoryData = async (params: DevWorkHisDataQuery) => {
  const data = await request<DevWorkHistoryData[]>(apiBiz('/deviceCondition/getHisList'), {
    method: 'POST',
    body: JSON.stringify(params),
  });

  return data;
};

// 设备网关

export const deviceGatewayUpdate = async (equipmentUpdateForm: EquipmentUpdateForm[]) => {
  await request(apiBiz('/deviceGateway/update'), {
    method: 'POST',
    body: JSON.stringify(equipmentUpdateForm),
  });
};

// 设备协议参数验证

export const postParameterVerification = async (deviceId: number) => {
  const data = await request<ParameterVerification[]>(apiBiz('/protocolParamVerify/list'), {
    method: 'POST',
    body: JSON.stringify({
      deviceId,
    }),
  });
  return data;
};

// 设备参数分组

export const equipmentParametersList = async (groupingList: GroupingList[]) => {
  await request(apiBiz('/deviceParamGroup/update/batch'), {
    method: 'POST',
    body: JSON.stringify(groupingList),
  });
};

export const queryEquipmentParametersList = async (deviceId: number) => {
  const data = await request<GroupingListData[]>(apiBiz('/deviceParamGroup/getList'), {
    method: 'POST',
    body: JSON.stringify({ deviceId }),
  });
  return data;
};

// 设备参数关系表

export const submitSortingList = async (submitSorting: SubmitSorting[]) => {
  await request(apiBiz('/deviceParamGroupRel/update/batch'), {
    method: 'POST',
    body: JSON.stringify(submitSorting),
  });
};

//  设备自定义参数表

export const submitCustomParameters = async (customParameters: CustomParameters[]) => {
  await request(apiBiz('/protocolParamCustomDev/update/batch'), {
    method: 'POST',
    body: JSON.stringify(customParameters),
  });
};

// 网关基本信息

export const addGateway = async () => {
  await request(apiBiz('/gateway/add'));
};

export const gatewayDelete = async (id: number) => {
  await request(apiBiz(`/gateway/delete/${id}`), {
    method: 'POST',
  });
};

export const getAllGatewayList = async () => {
  const data = await request<SerialNumberItem[]>(apiBiz('/gateway/getGtwLinkList'), {
    method: 'POST',
  });
  return data;
};

export const validateGatewayInfo = async (registerGatewayForm: VerificationEquipment) => {
  const data = await request<GatewayInformation>(apiBiz('/gateway/validate', registerGatewayForm), {
    method: 'POST',
  });
  return data;
};

export const gatewayList = async (gatewayBody: GatewayBody) => {
  const data = await request<GatewayListItemData>(apiBiz('/gateway/getPageList'), {
    method: 'POST',
    body: JSON.stringify(gatewayBody),
  });
  return data;
};

// 网关通讯

export const gatewayLinkAdd = async (addInterface: AddInterface) => {
  await request(apiBiz('/gatewayLink/add'), {
    method: 'POST',
    body: JSON.stringify(addInterface),
  });
};

export const addGatewayLinkBatchUpdate = async (params: BatchUpdate[]) => {
  await request(apiBiz('/gatewayLink/batchUpdate'), {
    method: 'POST',
    body: JSON.stringify({ gatewayLinks: params }),
  });
};

export const gatewayLinkDelete = async (id: number) => {
  await request(apiBiz(`/gatewayLink/delete/${id}`), {
    method: 'POST',
  });
};

export const gatewayLinkGetList = async (gatewayId: number) => {
  const data = await request<InterfaceData[]>(apiBiz(`/gatewayLink/getList/${gatewayId}`), {
    method: 'POST',
  });
  return data;
};

export const gatewayLinkUpdate = async (agreementUpdate: AgreementUpdate) => {
  await request(apiBiz('/gatewayLink/update'), {
    method: 'POST',
    body: JSON.stringify(agreementUpdate),
  });
};

export const getGatewayLinks = async (gatewayId: number) => {
  const data = await request<InterfaceLsit[]>(apiBiz(`/gatewayLink/getGatewayLinks/${gatewayId}`));
  return data;
};

// 网关型号

export const getGatewayModelInfo = async (id: number) => {
  const data = await request<GatewayModelInfo>(apiBiz(`/gatewayModel/info/${id}`));
  return data;
};

export const getGatewayModelInterfaces = async (id: number) => {
  const data = await request<GatewayInterface[]>(apiBiz(`/gatewayModel/interfaces/${id}`), {
    method: 'POST',
  });
  return data;
};

// 网关物理接口协议

export const obtainListPhysicalInterfaces = async (id: number) => {
  const data = await request<ListPhysicalInterfaces[]>(apiBiz(`/gatewayInterfaceProtocol/getProtocols/${id}`));
  return data;
};

// 网关型号物理接口

export const obtainListInterfaces = async (id: number) => {
  const data = await request<ListInterfaces[]>(apiBiz(`/gatewayPhysicalInterface/getInterfaces/${id}`));
  return data;
};

// 网关接口协议

export const getParamValueListAutomatic = async (params: ParamValueListAutomaticQuery) => {
  const data = await request<AutomaticMatching>(apiBiz('/gatewayLinkProtocol/getParamValueListAutomatic'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
  return data;
};

export const getAllGatewayLinkProtocol = async <T>(params: GatewayLinkProtocolQuery) => {
  const data = await request<T>(apiBiz('/gatewayLinkProtocol/getLinkParams'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
  return data;
};

export const gatewayLinkProtocolList = async <T>(protocolListItem: ProtocolListItem) => {
  const data = await request<T>(apiBiz('/gatewayLinkProtocol/getParamPageList'), {
    method: 'POST',
    body: JSON.stringify(protocolListItem),
  });
  return data;
};

export const gatewayLinkProtocolGatewayList = async (protocolGatewayListListItem: ProtocolGatewayListListItem) => {
  const data = await request<ListEquipmentParametersItemData>(
    apiBiz('/gatewayLinkProtocol/getParamPageListByGateway'),
    {
      method: 'POST',
      body: JSON.stringify(protocolGatewayListListItem),
    },
  );
  return data;
};

export const gatewayLinkList = async (id: number) => {
  const data = await request<VerificationAgreement[]>(apiBiz(`/gatewayLinkProtocol/getProtocolInfoList/${id}`), {
    method: 'POST',
  });
  return data;
};

export const gatewayLinkProtocolReset = async (protocolReset: ProtocolReset) => {
  await request(apiBiz('/gatewayLinkProtocol/reset'), {
    method: 'POST',
    body: JSON.stringify(protocolReset),
  });
};

export const postProtocolCandidatesList = async (protocolList: ProtocolList, gatewayId: number) => {
  const data = await request<ProtocolItemData>(apiBiz(`/gatewayLinkProtocol/getProtocolCandidates/${gatewayId}`), {
    method: 'POST',
    body: JSON.stringify(protocolList),
  });
  return data;
};

export const gatewayParameterList = async (gatewayParameterBody: GatewayParameterBody) => {
  const data = await request<GatewayParameterItemData>(apiBiz('/gatewayLinkProtocol/getParamValuePageList'), {
    method: 'POST',
    body: JSON.stringify(gatewayParameterBody),
  });
  return data;
};

// 组织网关

export const orgGatewayRegister = async (gatewayId: number) => {
  await request(apiBiz('/orgGateway/register'), {
    method: 'POST',
    body: JSON.stringify({ gatewayId }),
  });
};

export const orgGatewayUnregister = async (gatewayId: number) => {
  await request(apiBiz(`/orgGateway/unregister/${gatewayId}`), {
    method: 'POST',
  });
};

export const orgGatewaySerialNumber = async (protocolList: ProtocolList) => {
  const data = await request<SerialNumberItemData>(apiBiz('/orgGateway/getGatewayPageList'), {
    method: 'POST',
    body: JSON.stringify(protocolList),
  });
  return data;
};

// 能耗分析

export const getElectricityDataStatistics = async (params: ElectricityStatisticsQuery) => {
  const data = await request<ElectricityStatisticsResult>(
    apiBiz('/deviceElectricityDataChange/getElectricityDataStatistics'),
    {
      method: 'POST',
      body: JSON.stringify(params),
    },
  );

  return data;
};

export const downloadElectricityHisData = async (params: ElectricityStatisticsQuery) => {
  const blob = await request<Blob>(apiBiz('/deviceElectricityDataChange/download', params));
  return blob;
};

// 能效分析

export const getCoolingRealTimeData = async (params: CoolingRealTimeDataQuery) => {
  const data = await request<CoolingRealTimeDataResult>(apiBiz('/deviceCoolingData/rtd'), {
    method: 'POST',
    body: JSON.stringify(params),
  });

  return data;
};

export const getCoolingHistoryData = async (params: CoolingHistoryDataQuery) => {
  const data = await request<CoolingHistoryDataResult>(apiBiz('/deviceCoolingData/his'), {
    method: 'POST',
    body: JSON.stringify(params),
  });

  return data;
};

export const getCoolingDataStatistics = async (params: CoolingStatisticsQuery) => {
  const data = await request<CoolingStatisticsResult>(apiBiz('/deviceCoolingDataChange/getCoolingDataStatistics'), {
    method: 'POST',
    body: JSON.stringify(params),
  });

  return data;
};

export const downloadCoolingHisData = async (params: CoolingStatisticsQuery) => {
  const blob = await request<Blob>(apiBiz('/deviceCoolingDataChange/download', params));
  return blob;
};

// 平台标准协议参数

export const getProtocolStandardParam = async (paramId: number) => {
  const data = await request<ProtocolStandardParam>(apiBiz(`/platformProtocolInfo/info/${paramId}`));
  return data;
};

export const getProtocolStandardParamList = async (params: ProtocolStandardParamQuery) => {
  const data = await request<ProtocolStandardParamData>(apiBiz('/platformProtocolInfo/getPageList'), {
    method: 'POST',
    body: JSON.stringify(params),
  });

  return data;
};

// 协议参数

export const addProtocolParam = async (params: Partial<ProtocolParamInfo>) => {
  await request(apiBiz('/protocolParamInfo/add'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const updateProtocolParam = async (params: Partial<ProtocolParamInfo>) => {
  await request(apiBiz('/protocolParamInfo/update'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const getProtocolParamList = async (params: ProtocolParamSearchParams) => {
  const data = await request<ProtocolParamData>(apiBiz('/protocolParamInfo/getPageList'), {
    method: 'POST',
    body: JSON.stringify(params),
  });

  return data;
};

export const batchDeleleProtocolParam = async (params: number[]) => {
  await request(apiBiz('/protocolParamInfo/batchDeleleByIds'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

// 协议基本信息

export const addProtocolBaseInfo = async (params: Partial<ProtocolBaseInfo>) => {
  const data = await request<number>(apiBiz('/protocolBaseInfo/add'), {
    method: 'POST',
    body: JSON.stringify(params),
  });

  return data;
};

export const addTempProtocol = async (params: Partial<ProtocolBaseInfo>) => {
  const data = await request<number>(apiBiz('/protocolBaseInfo/addTemp'), {
    method: 'POST',
    body: JSON.stringify(params),
  });

  return data;
};

export const downloadProtocolTemplate = async (fileName: string) => {
  const blob = await request<Blob>(apiBiz(`/protocolBaseInfo/downloadTemplate/${fileName}`));
  return blob;
};

export const uploadUserProtocol = async (protocolType: string, file: Blob) => {
  const formData = new FormData();
  formData.append('file', file);

  const data = await request<Pick<ProtocolBaseInfo, 'id' | 'protocolName' | 'protocolType'>>(
    apiBiz('/protocolBaseInfo/uploadUserTemplate', { protocolType }),
    {
      method: 'POST',
      body: formData,
    },
  );

  return data;
};

export const reUploadUserProtocol = async (protocolType: string, id: number, file: Blob) => {
  const formData = new FormData();
  formData.append('file', file);

  const data = await request<Pick<ProtocolBaseInfo, 'id' | 'protocolName' | 'protocolType'>>(
    apiBiz('/protocolBaseInfo/resetConfiguration', { protocolType, id }),
    {
      method: 'POST',
      body: formData,
    },
  );

  return data;
};

export const downloadUserProtocol = async (id: number) => {
  const blob = await request<Blob>(apiBiz('/protocolBaseInfo/downloadUserProtocol', { id }));
  return blob;
};

/**
 * @deprecated
 */
export const getUploadProtocol = async (protocolType: string, protocolName: string) => {
  const data = await request<ProtocolBaseInfo>(
    apiBiz('/protocolBaseInfo/findUploadProtocolBaseInfo', {
      protocolType,
      protocolName,
    }),
  );

  return data;
};

export const getProtocolBaseInfo = async (id: number) => {
  const data = await request<ProtocolBaseInfo>(apiBiz(`/protocolBaseInfo/info/${id}`));
  return data;
};

export const updateProtocolBaseInfo = async (params: Partial<ProtocolBaseInfo>) => {
  await request(apiBiz('/protocolBaseInfo/update'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const deleteProtocolBaseInfo = async (id: number) => {
  await request(apiBiz(`/protocolBaseInfo/delete/${id}`), {
    method: 'POST',
  });
};

export const completeProtocolConfig = async (id: number) => {
  await request(apiBiz(`/protocolBaseInfo/completeConfiguration/${id}`), {
    method: 'POST',
  });
};

export const getProtocolList = async (params: ProtocolListQuery) => {
  const data = await request<ProtocolListData>(apiBiz('/protocolBaseInfo/getPageList'), {
    method: 'POST',
    body: JSON.stringify(params),
  });

  return data;
};

// 协议参数表

export const postProtocolPageList = async (postProtocolPage: PostProtocolPage) => {
  const data = await request<PostProtocolPageItemData>(apiBiz('/protocolParamInfo/page/list'), {
    method: 'POST',
    body: JSON.stringify(postProtocolPage),
  });
  return data;
};

export const postProtocolGetList = async (getListItem: GetListItem) => {
  const data = await request<PostProtocolPageItem[]>(apiBiz('/protocolParamInfo/getList'), {
    method: 'POST',
    body: JSON.stringify(getListItem),
  });
  return data;
};

// 全局参数

export const groupList = async (listInfo: ListInfo) => {
  const data = await request<EquipmentTypeItem[]>(apiBiz('/deviceGlobalData/getList'), {
    method: 'POST',
    body: JSON.stringify(listInfo),
  });
  return data;
};

// 站内通知
export const getUnreadNotifications = async () => {
  const data = await request<number>(apiBiz('/stationNotify/leftSize'), {
    method: 'POST',
  });
  return data;
};

export const getNoticePageList = async (params: PageParams) => {
  const data = await request<NoticePageItemData>(apiBiz('/stationNotify/getPageList'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
  return data;
};

// 组态

export const getGroupModuleInfo = async (params: GroupModuleQuery) => {
  const data = await request<GroupModuleInfo>(apiBiz('/moduleInfo/info'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
  return data;
};

// 组织与设备限制
export const getInfoListByOrgId = async (orgId: number) => {
  const data = await request<InfoListByOrg[]>(apiBiz(`/orgDeviceLimit/infoListByOrgId/${orgId}`));
  return data;
};

export const updateGroupModuleInfo = async (params: Partial<GroupModuleInfo>) => {
  await request(apiBiz('/moduleInfo/update'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const getGroupModuleDevData = async (deviceId: number, deviceType: number, valveType?: number) => {
  const data = await request<GroupModuleDevData>(apiBiz('/moduleInfo/info/device/detail'), {
    method: 'POST',
    body: JSON.stringify({
      deviceId,
      deviceType,
      valveType,
    }),
  });

  return data;
};

export const updateGroupModuleDevData = async (deviceId: number, deviceParamCode: string, value: number | string) => {
  await request(apiBiz('/moduleInfo/update/status'), {
    method: 'POST',
    body: JSON.stringify({
      deviceId,
      deviceParamCode,
      value,
    }),
  });
};

export const getDevStartStopStatus = async (deviceId: number, deviceType: number, valveType?: number) => {
  const data = await request<DevStartStopStatus>(apiBiz('/moduleInfo/info/device/determine'), {
    method: 'POST',
    body: JSON.stringify({
      deviceId,
      deviceType,
      valveType,
    }),
  });

  return data;
};

export const getGroupModuleDevStatus = async (groupId: number) => {
  const data = await request<GroupModuleBatchItem[]>(apiBiz('/moduleInfo/info/batch/status'), {
    method: 'POST',
    body: JSON.stringify({
      groupId,
    }),
  });

  return data;
};

// AI智能启停和寻优，高级设置

export const getAIStartStopData = async (groupId: number, startType: number) => {
  const data = await request<AIStartStopDeviceItem[]>(apiBiz('/moduleInfoAi/info/startType'), {
    method: 'POST',
    body: JSON.stringify({
      groupId,
      startType,
    }),
  });

  return data;
};

export const updateAIStartStopData = async (params: AIStartStopDeviceParams) => {
  await request(apiBiz('/moduleInfoAi/update/startType'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const getAIOptimizeDevData = async (groupId: number) => {
  const data = await request<AIOptimizeDevData>(apiBiz(`/moduleInfoAi/get/info/${groupId}`));
  return data;
};

export const getAIOptimizeDetail = async (groupId: number, deviceType: number) => {
  const data = await request<AIOptimizeDetail>(apiBiz('/moduleInfoAi/infoDetail'), {
    method: 'POST',
    body: JSON.stringify({
      groupId,
      deviceType,
    }),
  });

  return data;
};

export const updateAIStartStopOptimize = async (params: AIStartStopOptimizeInfo) => {
  await request(apiBiz('/moduleInfoAi/update'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const updateAIOptimizeSetting = async (params: AIOptimizeDetail) => {
  await request(apiBiz('/moduleInfoAi/update/infoDetail'), {
    method: 'POST',
    body: JSON.stringify(params),
  });
};

export const getAIAdvancedSetting = async (groupId: number) => {
  const data = await request<AIAdvancedParamItem[]>(apiBiz('/moduleInfoAi/info/advancedSetting'), {
    method: 'POST',
    body: JSON.stringify({
      groupId,
    }),
  });

  return data;
};

// 智控日志

export const getSmartCtrlLog = async (params: SmartCtrlLogQuery) => {
  const data = await request<SmartCtrlLogData>(apiBiz('/algLogInfo/getPageList'), {
    method: 'POST',
    body: JSON.stringify(params),
  });

  return data;
};

// 操作日志

export const getOperateLog = async (params: OperateLogQuery) => {
  const data = await request<OperateLogData>(apiBiz('/controlLogInfo/getPageList'), {
    method: 'POST',
    body: JSON.stringify(params),
  });

  return data;
};
