import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { ShopUserReferee, ShopUserRefereeParam } from './model';
import type {ShopDealerReferee} from "@/api/shop/shopDealerReferee/model";

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

/**
 * 查询用户推荐关系表列表
 */
export async function listShopUserReferee(params?: ShopUserRefereeParam) {
  const res = await request.get<ApiResult<ShopUserReferee[]>>(
    '/shop/shop-user-referee',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

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

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

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

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

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


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