import request from '@/config/axios'

// 保养工单 VO
export interface IotMainWorkOrderVO {
  id: number // 主键id
  planId: number // 保养计划id
  planSerialNumber: string // 保养计划编号
  deptId: number // 组织id
  deptName: string // 组织名称
  orderNumber: string // 工单号
  name: string // 工单名称 (吐哈-C14-保养计划)
  type: number // 工单类型(1计划生成  2临时新建)
  responsiblePerson: string // 负责人id 多个以逗号分隔
  responsiblePersonName: string // 负责人id 多个以逗号分隔
  cost: number // 保养费用
  result: number // 保养结果(1待执行 2已执行)
  otherCost: number // 其他费用
  laborCost: number // 人工费用
  outsourcingFlag: number // 是否委外 0否  1是
  actualStartTime: Date // 实际保养开始时间
  actualEndTime: Date // 实际保养结束时间
  remark: string // 备注
  delayReason: string // 延时原因
  status: number // 状态 0启用  1停用
  processInstanceId: string // 流程实例id
  auditStatus: number // 审批状态 未提交、审批中、审批通过、审批不通过、已取消
  applicant: string // 申请人id
}

// 保养工单 API
export const IotMainWorkOrderApi = {
  // 查询保养工单分页
  getIotMainWorkOrderPage: async (params: any) => {
    return await request.get({ url: `/pms/iot-main-work-order/page`, params })
  },

  // 获得保养工单分页 按照保养项的最近保养排序
  sortedMainWorkOrderPage: async (params: any) => {
    return await request.get({ url: `/pms/iot-main-work-order/sortedMainWorkOrderPage`, params })
  },

  // 以设备为维度统计所有保养明细中最近的保养数据
  deviceMainDistances: async (params: any) => {
    return await request.get({ url: `/pms/iot-main-work-order/deviceMainDistances`, params })
  },

  // 以设备为维度统计所有保养明细中最近的保养数据 保养项分组
  maintenanceSearch: async (params: any) => {
    return await request.get({ url: `/pms/iot-main-work-order/maintenanceSearch`, params })
  },

  getDeviceIotMainWorkOrderPage: async (params: any) => {
    return await request.get({ url: `/pms/iot-main-work-order/deviceOrderPage`, params })
  },
  // 根据保养结果统计工单数量
  allWorkOrderCountByResult: async () => {
    return await request.get({ url: `/pms/iot-main-work-order/allWorkOrderCountByResult` })
  },

  // 查询保养工单详情
  getIotMainWorkOrder: async (id: number) => {
    return await request.get({ url: `/pms/iot-main-work-order/get?id=` + id })
  },

  getDeviceIotWorkOrder: async (orderId: number, deviceId: number) => {
    return await request.get({
      url:
        `/pms/iot-main-work-order/deviceWorkOrder?workOrderId=` + orderId + '&deviceId=' + deviceId
    })
  },

  // 新增保养工单
  createIotMainWorkOrder: async (data: IotMainWorkOrderVO) => {
    return await request.post({ url: `/pms/iot-main-work-order/create`, data })
  },

  // 修改保养工单
  updateIotMainWorkOrder: async (data: IotMainWorkOrderVO) => {
    return await request.put({ url: `/pms/iot-main-work-order/update`, data })
  },

  // 填报保养工单
  fillWorkOrder: async (data: any) => {
    return await request.put({ url: `/pms/iot-main-work-order/fillWorkOrder`, data })
  },

  // 保存保养工单附件
  hiWorkOrderAttachment: async (data: Pick<IotMainWorkOrderVO, 'id'> & { attachments: any[] }) => {
    return await request.put({ url: `/pms/iot-main-work-order/hiWorkOrderAttachment`, data })
  },

  // 修改保养工单
  modifyWorkOrder: async (data: any) => {
    return await request.put({ url: `/pms/iot-main-work-order/modifyWorkOrder`, data })
  },

  // 手工新增保养工单
  addWorkOrder: async (data: any) => {
    return await request.put({ url: `/pms/iot-main-work-order/addWorkOrder`, data })
  },

  // 删除保养工单
  deleteIotMainWorkOrder: async (id: number) => {
    return await request.delete({ url: `/pms/iot-main-work-order/delete?id=` + id })
  },

  // 导出保养工单 Excel
  exportIotMainWorkOrder: async (params) => {
    return await request.download({ url: `/pms/iot-main-work-order/export-excel`, params })
  },

  exportIotMainWorkOrderIndex: async (params) => {
    return await request.download({
      url: `/pms/iot-main-work-order/exportMaintenanceOrders`,
      params
    })
  }
}
