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

/**
 * 分页查询分销商用户记录表
 */
export async function pageShopDealerUser(params: ShopDealerUserParam) {
  // 使用新的request方法，它会自动处理错误并返回完整的ApiResult
  const res = await request.get<ApiResult<PageResult<ShopDealerUser>>>(
    '/shop/shop-dealer-user/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询分销商用户记录表列表
 */
export async function listShopDealerUser(params?: ShopDealerUserParam) {
  const res = await request.get<ApiResult<ShopDealerUser[]>>(
    '/shop/shop-dealer-user',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加分销商用户记录表
 */
export async function addShopDealerUser(data: ShopDealerUser) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-dealer-user',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改分销商用户记录表
 */
export async function updateShopDealerUser(data: ShopDealerUser) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-dealer-user',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除分销商用户记录表
 */
export async function removeShopDealerUser(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-dealer-user/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

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

/**
 * 根据userId查询分销商用户记录表
 */
export async function getShopDealerUser(userId: number) {
  const res = await request.get<ApiResult<ShopDealerUser>>(
    '/shop/shop-dealer-user/' + userId
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
