import request from '@/utils/request';

/**
 * 列表
 */
export async function getList(data) {
  const res = await request.post(
    '/mes/please_entrust_management/beEntrustedPage',
    data
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 申请入库
export async function warehouseEntry(params) {
  const res = await request.get(`/mes/be_entrusted_management/warehouseEntry`, {
    params
  });
  if (res.data.code == 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.message));
}

//修改
export async function update(data) {
  const res = await request.post('mes/be_entrusted_management/update', data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

//创建更新销售订单
export async function createOrUpdate(data) {
  const res = await request.post(`/aps/salesorder/createOrUpdate`, data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 获取订单详情
export async function getOrderDetail(code) {
  const res = await request.get(`/aps/salesorder/getByCode/${code}`);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// aps生产计划获取产品的多个bom版本
export async function bomListByPlan(params) {
  const res = await request.get(`/main/bomCategory/bomListByPlan`, { params });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 根据bom 获取工艺路线
export async function bomRoutingList(id) {
  const res = await request.get(`/main/bomCategory/bomRoutingList/${id}`);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

export async function getFactoryList(params) {
  const res = await request.get(`/main/factoryarea/getFactoryList`, { params });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 根据产品查询bom类型
export async function listBomType(params) {
  const res = await request.get(`/main/bomCategory/listBomType`, { params });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 根据计划id查询物料信息
export async function findMaterialInfoSalesorder(params) {
  const res = await request.get(
    '/aps/salesorder/findMaterialInfoBySalesOrderId',
    {
      params
    }
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 根据产品ID查询物料信息
export async function findMaterialInfoByCategory(params) {
  const res = await request.get(
    '/aps/salesorder/findMaterialInfoByCategoryId',
    {
      params
    }
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 根据产品查询BOM分类
export async function bomVersionList(params) {
  const res = await request.get(`/main/bomCategory/versionList`, { params });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 根据产品ID查询物料信息
export async function getProductKitting(params) {
  const res = await request.get('/aps/productionplan/getProductKitting', {
    params
  });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 齐套性检查
export async function orderHomogeneityInspect(data) {
  const res = await request.post(
    `/aps/salesorder/orderHomogeneityInspect`,
    data
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

export async function getStockList(params) {
  const res = await request.get('/wms/outindetailtwo/page', { params });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

export async function getCurrentList(data) {
  const res = await request.post('/eom/purchaseorder/getZtDetail', data);
  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('/aps/batchingplan/save', data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

//确认收货
export async function confirmReceipt(data) {
  const res = await request.post(
    `/mes/please_entrust_management/receiveDetail`,
    data
  );
  if (res.data.code == 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.message));
}

//创建生产订单
export async function createProductionOrder(data) {
  const res = await request.post(`/aps/workorder/save`, data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

//驳回收货
export async function rejectGoods(data) {
  const res = await request.post(
    `/mes/please_entrust_management/returnDetail`,
    data
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
