import request from '@/utils/request';
import { download } from '@/utils/file';

// 获取类别实体分页
export async function getBoatList (data) {
  let par = new URLSearchParams(data);
  const res = await request.get(`/main/category/pageSubstance?` + par, {});
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// // 查询实例详情
// export async function getCategoryInfo (id) {
//   const res = await request.get(`/main/category/info/${id}`);
//   if (res.data.code == 0) {
//     return res.data;
//   }
//   return Promise.reject(new Error(res.data.message));
// }

// 获取单个类别实体明细分页
export async function getPageSingle (data) {
  let par = new URLSearchParams(data);
  const res = await request.get(`/main/asset/pageSingleSubstance?` + par, {});
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 获取实体列表分页
export async function getAssetList (data) {
  let par = new URLSearchParams(data);
  const res = await request.get(`/main/asset/page?` + par, {});
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 查询实例详情
export async function getAssetInfo (id) {
  const res = await request.get(`main/asset/getById/${id}`);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
// 保存or更新
export async function saveOrEdit (data) {
  const res = await request.post(`/main/asset/saveOrEdit`, data);
  if (res.data.code == 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.message));
}


// 根据类别统计位置数量
export async function getCount (data) {
  const res = await request.get(`/main/asset/countPosition` , { params: data } );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 导出实体
export async function downloadAsset (params, fileName) {
  const res = await request.post('/main/asset/page/export',  params, {responseType:'blob'} );
  download(res.data, fileName)
}

// 导出类别
export async function downloadCategory (params, fileName) {
  const res = await request.post('/main/category/pageSubstance/export',  params, {responseType:'blob'} );
  download(res.data, fileName)
}
