import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { UserBalanceLog, UserBalanceLogParam } from './model';
import {SERVER_API_URL} from "@/utils/server";

/**
 * 分页查询余额明细
 */
export async function pageUserBalanceLog(params: UserBalanceLogParam) {
  const res = await request.get<ApiResult<PageResult<UserBalanceLog>>>(
    SERVER_API_URL + '/sys/user-balance-log/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询余额明细列表
 */
export async function listUserBalanceLog(params?: UserBalanceLogParam) {
  const res = await request.get<ApiResult<UserBalanceLog[]>>(
    SERVER_API_URL + '/sys/user-balance-log',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询余额明细
 */
export async function getUserBalanceLog(id: number) {
  const res = await request.get<ApiResult<UserBalanceLog>>(
    SERVER_API_URL + '/sys/user-balance-log/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加余额明细
 */
export async function addUserBalanceLog(data: UserBalanceLog) {
  const res = await request.post<ApiResult<unknown>>(
    SERVER_API_URL + '/sys/user-balance-log',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改余额明细
 */
export async function updateUserBalanceLog(data: UserBalanceLog) {
  const res = await request.put<ApiResult<unknown>>(
    SERVER_API_URL + '/sys/user-balance-log',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除余额明细
 */
export async function removeUserBalanceLog(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    SERVER_API_URL + '/sys/user-balance-log/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除余额明细
 */
export async function removeUserBalanceLogs(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    SERVER_API_URL + '/sys/user-balance-log/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}
