import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { ShopStoreWarehouse, ShopStoreWarehouseParam } from './model';

/**
 * 分页查询仓库
 */
export async function pageShopStoreWarehouse(params: ShopStoreWarehouseParam) {
  const res = await request.get<ApiResult<PageResult<ShopStoreWarehouse>>>(
    '/shop/shop-store-warehouse/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询仓库列表
 */
export async function listShopStoreWarehouse(params?: ShopStoreWarehouseParam) {
  const res = await request.get<ApiResult<ShopStoreWarehouse[]>>(
    '/shop/shop-store-warehouse',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加仓库
 */
export async function addShopStoreWarehouse(data: ShopStoreWarehouse) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-store-warehouse',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改仓库
 */
export async function updateShopStoreWarehouse(data: ShopStoreWarehouse) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-store-warehouse',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除仓库
 */
export async function removeShopStoreWarehouse(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/shop/shop-store-warehouse/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除仓库
 */
export async function removeBatchShopStoreWarehouse(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/shop/shop-store-warehouse/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询仓库
 */
export async function getShopStoreWarehouse(id: number) {
  const res = await request.get<ApiResult<ShopStoreWarehouse>>(
    '/shop/shop-store-warehouse/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
