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

/**
 * 分页查询分销商推荐关系表
 */
export async function pageShopDealerReferee(params: ShopDealerRefereeParam) {
  const res = await request.get<ApiResult<PageResult<ShopDealerReferee>>>(
    '/shop/shop-dealer-referee/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询分销商推荐关系表列表
 */
export async function listShopDealerReferee(params?: ShopDealerRefereeParam) {
  const res = await request.get<ApiResult<ShopDealerReferee[]>>(
    '/shop/shop-dealer-referee',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加分销商推荐关系表
 */
export async function addShopDealerReferee(data: ShopDealerReferee) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-dealer-referee',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改分销商推荐关系表
 */
export async function updateShopDealerReferee(data: ShopDealerReferee) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-dealer-referee',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除分销商推荐关系表
 */
export async function removeShopDealerReferee(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-dealer-referee/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除分销商推荐关系表
 */
export async function removeBatchShopDealerReferee(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-dealer-referee/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询分销商推荐关系表
 */
export async function getShopDealerReferee(id: number) {
  const res = await request.get<ApiResult<ShopDealerReferee>>(
    '/shop/shop-dealer-referee/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
