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

/**
 * 分页查询物流公司
 */
export async function pageShopExpress(params: ShopExpressParam) {
  const res = await request.get<ApiResult<PageResult<ShopExpress>>>(
    '/shop/shop-express/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询物流公司列表
 */
export async function listShopExpress(params?: ShopExpressParam) {
  const res = await request.get<ApiResult<ShopExpress[]>>(
    '/shop/shop-express',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加物流公司
 */
export async function addShopExpress(data: ShopExpress) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-express',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改物流公司
 */
export async function updateShopExpress(data: ShopExpress) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-express',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除物流公司
 */
export async function removeShopExpress(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-express/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除物流公司
 */
export async function removeBatchShopExpress(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-express/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询物流公司
 */
export async function getShopExpress(id: number) {
  const res = await request.get<ApiResult<ShopExpress>>(
    '/shop/shop-express/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
