import request from '@/utils/request';

// 根据工艺路线编码获取工序
export async function getProcessByRoute (data) {
  const res = await request.post(
    '/main/producerouting/taskinstance/page',
    data
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
// 根据生产版本获取工序
export async function getTaskListById (id) {
  const res = await request.get(`/main/produceversion/getTaskListById/${id}`);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
// SAP-查询库存
export async function realTimeStorage (params) {
  const res = await request.get(`/sap/sync/realTimeStorage`, { params });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
// 根据生产版本ID查询bom子项列表
export async function bomSubListByVersionId (params) {
  const res = await request.get(
    `/main/produceversion/resource/bomSubListByVersionId/${params.versionId}/${params.taskInstanceId}`
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
// 工位
export async function factoryworkstation (params) {
  const res = await request.get(`/main/factoryworkstation/page`, { params });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 根据首工序id拿工位
export async function listByFirstTaskId(firstTaskId) {
  const res = await request.get(
    `/main/factoryworkstation/listByFirstTaskId/${firstTaskId}`
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 查询产线
export async function lineByCurrentUser(workCenterId) {
  const res = await request.get(
    `/main/factoryarea/listFactoryLineByWorkCenterId/${workCenterId}`
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 分批任务类型
export async function listAssigneeType() {
  const res = await request.get(`/aps/assign/listAssigneeType`);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 派单任务保存
export async function taskSave(data) {
  const res = await request.post('/aps/assign/assign/save', data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 派单任务撤销
export async function taskRevoked(data) {
  const res = await request.post('/aps/assign/assign/revoked', data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 重置派单任务
export async function resetAssignee(assigneeId) {
  const res = await request.put('/aps/assign/resetAssignee/' + assigneeId);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
// 派单任务
export async function taskAssignment(data) {
  const res = await request.post('/aps/assign/assign', data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 获取派单任务列表
export async function listAssign(params) {
  const res = await request.get(`/aps/assign/listAssign`, { params });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 检查派单确认
export async function checkAssignConfirm(params) {
  const res = await request.get(`/aps/assign/checkAssignConfirm`, { params });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 获取工序的工作中心和班组列表
export async function listByRoutingIds(data) {
  const res = await request.post(`/main/producerouting/taskinstance/listByRoutingIds`, data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

export async function listUserByIds(data) {
  const res = await request.post('/main/team/listUserByIds', data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 根据当前用户查工作中心跟班组
export async function listWorkCenter(firstProduceTaskId) {
  const res = await request.get(
    `/aps/assign/listWorkCenterTeamsByProduceTaskId/${firstProduceTaskId}`
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 查询工序列表 已派单
export async function listByMesWorkOrder(params) {
  const res = await request.get(
    `/aps/assign/listByMesWorkOrder`,{params}
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 下达
export async function releaseWorkOrder(data) {
  const res = await request.post(
    `/aps/workorder/releaseWorkOrder`, data
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 根据工厂查询班次
export async function listByFactoryId(factoryId) {
  const res = await request.get(`main/teamtime/listByFactoryId/${factoryId}`);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}