import request from '@/utils/request';

/**
 * 分页查询生产版本
 */
export async function versionPage(params) {
  const res = await request.get(`/main/produceversion/page`, { params });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
/**
 * 班组列表
 */
export async function teamPage(params) {
  const res = await request.get(`/main/team/page`, { params });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 根据工作中心查班组

export async function listByWorkCenterId(workCenterId) {
  const res = await request.get(
    `/main/team/listByWorkCenterId/${workCenterId}`
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 根据班组ids查所有人信息
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 listByWorkCenter(workCenterId) {
  const res = await request.get(
    `/main/producetask/listByWorkCenterId/${workCenterId}`
  );
  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));
}

// 根据任务实例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 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));
}

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

//获取系统参数
export async function parameterGetByCode(data) {
  const res = await request.post('/sys/parameter/getByCode', data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 批量派单提交
export async function batchReleaseWorkOrder(data) {
  const res = await request.post('/aps/workorder/batchReleaseWorkOrder', data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
