import { http } from "@/utils/http";

export interface UserBalanceLog {
  /** 主键ID */
  logId: number;
  /** 用户余额ID */
  userBalanceId: number;
  /** 变更类型(1-消费,2-审批归还,3-系统调整) */
  changeType: number;
  /** 变更金额(分) */
  changeAmount: number;
  /** 变更前已用余额(分) */
  useBalanceBefore: number;
  /** 变更后已用余额(分) */
  useBalanceAfter: number;
  /** 关联订单ID */
  orderId?: number;
  /** 关联审批ID */
  approvalId?: number;
  /** 关联订单商品ID */
  orderGoodsId?: number;
  /** 创建者ID */
  creatorId: number;
  /** 创建时间 */
  createTime: string;
  /** 更新者ID */
  updaterId: number;
  /** 更新时间 */
  updateTime: string;
  /** 删除标志 */
  deleted: boolean;
}

export interface UserBalanceLogListParams {
  pageNum?: number;
  pageSize?: number;
  userBalanceId?: number;
  changeType?: number;
  orderId?: number;
  approvalId?: number;
  orderGoodsId?: number;
  creatorId?: number;
}

// 新增用户余额变更日志请求参数
export interface AddUserBalanceLogCommand {
  /** 用户余额ID */
  userBalanceId: number;
  /** 变更类型(1-消费,2-审批归还,3-系统调整) */
  changeType: number;
  /** 变更金额(分) */
  changeAmount: number;
  /** 变更前已用余额(分) */
  useBalanceBefore: number;
  /** 变更后已用余额(分) */
  useBalanceAfter: number;
  /** 关联订单ID */
  orderId?: number;
  /** 关联审批ID */
  approvalId?: number;
  /** 关联订单商品ID */
  orderGoodsId?: number;
}

// 修改用户余额变更日志请求参数
export interface UpdateUserBalanceLogCommand extends AddUserBalanceLogCommand {
  /** 主键ID */
  logId: number;
}

// 根据用户余额ID查询参数
export interface QueryByUserBalanceIdParams {
  userBalanceId: number;
}

// 根据订单ID查询参数
export interface QueryByOrderIdParams {
  orderId: number;
}

// 根据审批ID查询参数
export interface QueryByApprovalIdParams {
  approvalId: number;
}

// 根据订单商品ID查询参数
export interface QueryByOrderGoodsIdParams {
  orderGoodsId: number;
}

// 根据变更类型查询参数
export interface QueryByChangeTypeParams {
  changeType: number;
}

// 批量删除参数
export interface BatchDeleteParams {
  ids: number[];
}

// 用户余额变更日志相关API
export const getUserBalanceLogListApi = (params: UserBalanceLogListParams) => {
  return http.request<ResponseData<PageDTO<UserBalanceLog>>>("get", "/ab98/balance-log", { params });
};

export const getUserBalanceLogDetailApi = (id: number) => {
  return http.request<ResponseData<UserBalanceLog>>("get", `/ab98/balance-log/${id}`);
};

export const getUserBalanceLogByUserBalanceIdApi = (userBalanceId: number) => {
  return http.request<ResponseData<UserBalanceLog[]>>("get", `/ab98/balance-log/byUserBalanceId/${userBalanceId}`);
};

export const getUserBalanceLogByOrderIdApi = (orderId: number) => {
  return http.request<ResponseData<UserBalanceLog[]>>("get", `/ab98/balance-log/byOrderId/${orderId}`);
};

export const getUserBalanceLogByApprovalIdApi = (approvalId: number) => {
  return http.request<ResponseData<UserBalanceLog[]>>("get", `/ab98/balance-log/byApprovalId/${approvalId}`);
};

export const getUserBalanceLogByOrderGoodsIdApi = (orderGoodsId: number) => {
  return http.request<ResponseData<UserBalanceLog[]>>("get", `/ab98/balance-log/byOrderGoodsId/${orderGoodsId}`);
};

export const getUserBalanceLogByChangeTypeApi = (changeType: number) => {
  return http.request<ResponseData<UserBalanceLog[]>>("get", `/ab98/balance-log/byChangeType/${changeType}`);
};

export const addUserBalanceLogApi = (data: AddUserBalanceLogCommand) => {
  return http.request<ResponseData<UserBalanceLog>>("post", "/ab98/balance-log", { data });
};

export const updateUserBalanceLogApi = (id: number, data: UpdateUserBalanceLogCommand) => {
  return http.request<ResponseData<UserBalanceLog>>("put", `/ab98/balance-log/${id}`, { data });
};

export const deleteUserBalanceLogApi = (id: number) => {
  return http.request<ResponseData<void>>("delete", `/ab98/balance-log/${id}`);
};

export const batchDeleteUserBalanceLogApi = (ids: number[]) => {
  return http.request<ResponseData<void>>("delete", `/ab98/balance-log/batch/${ids.join(',')}`);
};