import request from '@/utils/request';

/**
 * 列表
 */
export async function getList(data) {
  const res = await request.post('/aps/productionplan/page', 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/productionplan/save', data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
/**
 * 基本信息
 */
export async function getById(id) {
  console.log(id);
  const res = await request.get(`/aps/productionplan/getById/` + id);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
/**
 * 发布信息返显
 */
export async function getReleaseInfoById(id) {
  const res = await request.get(`/aps/productionplan/getReleaseInfoById/${id}`);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
/**
 * 详情
 */
export async function getProductPlanDetail(id) {
  const res = await request.get(
    `/aps/productionplan/getProductPlanDetail/${id}`
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
/**
 * 根据计划id获取物料信息
 */
export async function getMaterialinfo(planId) {
  const res = await request.get(`/aps/materialinfo/listByPlanId`, {
    params: { planId }
  });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
/**
 * 删除
 */
export async function del(id) {
  const res = await request.delete(`/aps/productionplan/delete/${id}`);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
/**
 * 删除
 */
export async function factoryDelete(id) {
  const res = await request.delete(`/aps/productionplan/delete/${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(`/aps/productionplan/splitBatch`, params);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 计划合批
export async function mergeBatch(params) {
  const res = await request.post(`/aps/productionplan/mergeBatch`, params);
  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(`/aps/productionplan/updatePriority`, data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
/**
 * 生产详情列表
 */
export async function queryTaskDetail(data) {
  const res = await request.get(
    '/aps/quality_details_work_order/queryPlanId/' + data
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 计划分解（获取产品BOM）

export async function getBom(params) {
  const res = await request.get(`/main/bomCategory/getBom`, { params });
  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(`/aps/productionplan/batchSave`, data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 计划分解 （保存并发布）
export async function issueProductionPlan(data) {
  const res = await request.post(
    `/aps/productionplan/issueProductionPlan`,
    data
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 齐套性检查
export async function homogeneityInspect(data) {
  const res = await request.post(
    `/aps/productionplan/homogeneityInspect`,
    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 homogeneityInspectMerge(data) {
  const res = await request.post(
    `/aps/productionplan/homogeneityInspectMerge`,
    data
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

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

// PBOM 齐套性检查
export async function pbomHomogeneityInspect(data) {
  const res = await request.post(
    `/aps/productionplan/pbomHomogeneityInspect`,
    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 getPlanStatistics(params) {
  const res = await request.get('/aps/productionplan/getPlanStatistics', {
    params
  });
  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));
}

// 根据产品查询BOM分类
export async function findBomCategoryByCategoryIdV2(bomId, produceType) {
  const res = await request.get(
    `/aps/productionplan/findBomCategoryByCategoryId/v2/${bomId}?produceType=${produceType}`
  );
  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));
}
// 根据产品查询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));
}

// 根据产品查询BOM分类
export async function findBomCategoryByCategoryId(
  categoryId,
  produceType = ''
) {
  const res = await request.get(
    `/aps/productionplan/findBomCategoryByCategoryId/${categoryId}?produceType=${produceType}`
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

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

// 根据产品查询BOM分类
export async function findBomSalesorderCategoryIdV2(bomId, produceType) {
  const res = await request.get(
    `/aps/salesorder/findBomCategoryByCategoryId/v2/${bomId}?produceType=${produceType}`
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 根据产品查询BOM分类
export async function findBomSalesorderCategoryId(categoryId, produceType) {
  const res = await request.get(
    `/aps/salesorder/findBomCategoryByCategoryId/${categoryId}?produceType=${produceType}`
  );
  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));
}

// 根据产品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 planIsReview(data) {
  const res = await request.post(
    `/aps/productionplan/releaseSaveByApproval`,
    data
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
