import request from '@/utils/request';

/**
 * 分页
 */
export async function getPage(params) {
  const res = await request.post('/mes/workorder/page', params);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
/**
 * 新增
 */
export async function save(data) {
  const res = await request.post('/mes/workorder/save', data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
/**
 * 修改
 */
export async function update(data) {
  const res = await request.put('/mes/workorder/update', data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 批量完结
export async function batchCompletion(params) {
  const res = await request.post('/mes/workorder/batchCompletion', params);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 取消完结
export async function cancelCompletion(id) {
  const res = await request.get(`/mes/workorder/cancelCompletion/${id}`);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 领料保存
export async function batchSave(data) {
  const res = await request.post(`/mes/pickorder/batchSave`, data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
// 工单详情
export async function getById(id) {
  const res = await request.get(`/mes/workorder/getById/${id}`);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 拆分
export async function splitWork(params) {
  const res = await request.post(`/mes/workorder/splitBatch`, params);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
// 报工
export async function report(params) {
  const res = await request.post(`/mes/workreport/report`, params);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
// 报工 统计数据
export async function reportCount(params) {
  const res = await request.get(`/mes/workreport/count`, { params });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

export async function workorderproductsequencePage(params) {
  // 发送 GET 请求获取数据
  const res = await request.get(`/mes/workorderproductsequence/page`, {
    params
  });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 详情页-tab列表
export async function reportPage(data) {
  const res = await request.post('/mes/workreport/page', data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
// 打印详情页-tab列表
export async function printWorkOrderInfo(workOrderId) {
  const res = await request.get(
    `/mes/workreport/printWorkOrderInfo/${workOrderId}`
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 获取详情页数据
export async function getInfoById(id) {
  const res = await request.get(`/mes/workreport/getById/${id}`);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 冲销接口
export async function writeOffWork(data) {
  const res = await request.post(`/mes/workreport/writeOff`, data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
//包装要求
export async function getPackageList(code) {
  const res = await request.get(`/aps/workorder/getPackageList/${code}`);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
//根据工序ID查询设备信息
export async function getDeviceByTaskId(params) {
  const res = await request.get(
    `/main/producetask/getDeviceByTaskId/${params.taskId}/${params.rootCategoryLevelId}`
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 获取槽数
export async function getSoltNum(id) {
  const res = await request.get(`/main/categorypallet/getByCategoryId/${id}`);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 打印前查询
export async function queryPrint(data) {
  const res = await request.post(`/mes/workorder/queryPrint`, data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
// 修改优先级
export async function updatePriority(data) {
  const res = await request.post(`/mes/workorder/updatePriority`, data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 根据任务实例id查询工序id
export async function getTaskIdByInstanceId(taskInstanceId) {
  const res = await request.get(
    `/main/producetask/getTaskIdByInstanceId/${taskInstanceId}`
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 工单暂停
export async function updateStatusPause(params) {
  const res = await request.put(
    `/mes/workorder/updateStatusPause`,
    params,
    true
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 工单终止
export async function updateStatusTerminate(params) {
  const res = await request.put(
    `/mes/workorder/updateStatusTerminate`,
    params,
    true
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
