import request from '@/utils/request';

// 保存/修改编码信息
export async function saveNew (data) {
  const res = await request.post('/main/codemanage/saveNew', data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 获取编码列表
export async function getCodeList (data) {
  let par = new URLSearchParams(data);
  const res = await request.get(`/main/codemanage/page?` + par, {});
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除编码
 */
export async function removeCodeInfo (params) {
  const res = await request.post('/main/codemanage/delete', params);
  if (res.data.code == 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

// 获取编码详情
export async function getCodeDetail (id) {
  const res = await request.get(`/main/codemanage/getById/` + id, {});
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
// 生成编码
export async function getCode (code) {
  const res = await request.get(`/main/codemanage/getCode/` + code, {});
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 检查编码是否存在
 * @param code 编码
 */
export async function checkCode (code) {
  const res = await request.get('/main/codemanage/getByCode/' + code);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
