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

export interface UserBalance {
  /** 主键ID */
  userBalanceId: number;
  /** 企业微信id */
  corpid: string;
  /** openid */
  openid: string;
  /** 汇邦云用户ID */
  ab98UserId: number;
  /** 企业用户id */
  qyUserId: number;
  /** 用户余额（单位：分） */
  balance: number;
  /** 用户余额（单位：分） */
  useBalance: number;
  /** 用户余额（单位：分） */
  balanceLimit: number;
  /** 创建者ID */
  creatorId: number;
  /** 更新者ID */
  updaterId: number;
}

export interface UserBalanceListParams {
  pageNum?: number;
  pageSize?: number;
  corpid?: string;
  openid?: string;
  ab98UserId?: number;
  qyUserId?: number;
}

export interface UserBalanceListResponse {
  rows: UserBalance[];
  total: number;
}

// 新增用户余额请求参数
export interface AddUserBalanceCommand {
  /** 企业微信ID */
  corpid: string;
  /** 微信openid */
  openid?: string;
  /** 汇邦云用户ID */
  ab98UserId: number;
  /** 企业用户ID */
  qyUserId?: number;
  /** 用户余额(分) */
  balance?: number;
  /** 可用余额(分) */
  useBalance?: number;
  /** 余额限制(分) */
  balanceLimit?: number;
}

// 修改用户余额请求参数
export interface UpdateUserBalanceCommand extends AddUserBalanceCommand { }

// 余额增减操作请求参数
export interface ChangeBalanceCommand {
  /** 企业微信ID */
  corpid: string;
  /** 汇邦云用户ID */
  ab98UserId: number;
  /** 金额(分) */
  amount: number;
}

// 根据企业微信ID和汇邦云用户ID查询参数
export interface QueryByCorpidAb98Params {
  corpid: string;
  ab98UserId: number;
}

// 根据openid查询参数
export interface QueryByOpenidParams {
  openid: string;
}

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

// 用户余额相关API
export const getUserBalanceListApi = (params: UserBalanceListParams) => {
  return http.request<ResponseData<PageDTO<UserBalance>>>("get", "/ab98/balance", { params });
};

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

export const getUserBalanceByCorpidAb98Api = (params: QueryByCorpidAb98Params) => {
  return http.request<ResponseData<UserBalance>>("get", "/ab98/balance/byCorpidAb98", { params });
};

export const getUserBalanceByOpenidApi = (params: QueryByOpenidParams) => {
  return http.request<ResponseData<UserBalance>>("get", "/ab98/balance/byOpenid", { params });
};

export const addUserBalanceApi = (data: AddUserBalanceCommand) => {
  return http.request<ResponseData<UserBalance>>("post", "/ab98/balance", { data });
};

export const updateUserBalanceApi = (id: number, data: UpdateUserBalanceCommand) => {
  return http.request<ResponseData<UserBalance>>("put", `/ab98/balance/${id}`, { data });
};

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

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

export const addUserBalanceAmountApi = (data: ChangeBalanceCommand) => {
  return http.request<ResponseData<string>>("post", "/ab98/balance/addBalance", { data });
};

export const subtractUserBalanceAmountApi = (data: ChangeBalanceCommand) => {
  return http.request<ResponseData<string>>("post", "/ab98/balance/subtractBalance", { data });
};

export const insertOrUpdateUserBalanceApi = (data: AddUserBalanceCommand) => {
  return http.request<ResponseData<UserBalance>>("post", "/ab98/balance/insertOrUpdate", { data });
};