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

/**
 * 分页查询配送员
 */
export async function pageShopStoreRider(params: ShopStoreRiderParam) {
  const res = await request.get<ApiResult<PageResult<ShopStoreRider>>>(
    '/shop/shop-store-rider/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询配送员列表
 */
export async function listShopStoreRider(params?: ShopStoreRiderParam) {
  const res = await request.get<ApiResult<ShopStoreRider[]>>(
    '/shop/shop-store-rider',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加配送员
 */
export async function addShopStoreRider(data: ShopStoreRider) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-store-rider',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改配送员
 */
export async function updateShopStoreRider(data: ShopStoreRider) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-store-rider',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除配送员
 */
export async function removeShopStoreRider(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-store-rider/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

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

/**
 * 根据id查询配送员
 */
export async function getShopStoreRider(id: number) {
  const res = await request.get<ApiResult<ShopStoreRider>>(
    '/shop/shop-store-rider/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
