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

/**
 * 分页查询用户记录表
 */
export async function pageShopUser(params: ShopUserParam) {
  const res = await request.get<ApiResult<PageResult<ShopUser>>>(
    '/shop/shop-user/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

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

/**
 * 添加用户记录表
 */
export async function addShopUser(data: ShopUser) {
  const res = await request.post<ApiResult<unknown>>(
    'http://127.0.0.1:9200/api/shop/shop-user',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

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

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

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

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