Files
kim.dev.6789 b7f8db7d08 复制项目
2026-01-14 22:35:45 +08:00

117 lines
6.5 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright © 2023 OpenIM open source community. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package chat
import (
"context"
"time"
"github.com/openimsdk/tools/db/pagination"
)
// BalanceRecordType 余额变动类型
const (
BalanceRecordTypeRecharge = 1 // 充值
BalanceRecordTypeWithdraw = 2 // 提现/提款
BalanceRecordTypeConsume = 3 // 消费
BalanceRecordTypeRefund = 4 // 退款
BalanceRecordTypeReward = 5 // 奖励
BalanceRecordTypeAdminRecharge = 6 // 后台充值
BalanceRecordTypeSendRedPacket = 7 // 发红包(减少余额)
BalanceRecordTypeGrabRedPacket = 8 // 抢红包(增加余额)
BalanceRecordTypeOther = 99 // 其他
)
// WithdrawAccountType 提现账号类型
const (
WithdrawAccountTypeAlipay = 1 // 支付宝
WithdrawAccountTypeWeChat = 2 // 微信
WithdrawAccountTypeBankCard = 3 // 银行卡
)
// RealNameAuth 实名认证信息
type RealNameAuth struct {
IDCard string `bson:"id_card"` // 身份证号
IDCardPhotoFront string `bson:"id_card_photo_front"` // 身份证正面照片URL
IDCardPhotoBack string `bson:"id_card_photo_back"` // 身份证反面照片URL
Name string `bson:"name"` // 真实姓名
AuditStatus int32 `bson:"audit_status"` // 审核状态0-未审核1-审核通过2-审核拒绝
}
// Wallet 钱包模型
type Wallet struct {
UserID string `bson:"user_id"` // 用户ID
Balance int64 `bson:"balance"` // 用户余额(单位:分)
PaymentPassword string `bson:"payment_password"` // 支付密码(加密存储)
WithdrawAccount string `bson:"withdraw_account"` // 提现账号
WithdrawAccountType int32 `bson:"withdraw_account_type"` // 提现账号类型1-支付宝2-微信3-银行卡
RealNameAuth RealNameAuth `bson:"real_name_auth"` // 实名认证信息
WithdrawReceiveAccount string `bson:"withdraw_receive_account"` // 提现收款账号
CreateTime time.Time `bson:"create_time"` // 创建时间
UpdateTime time.Time `bson:"update_time"` // 更新时间
}
func (Wallet) TableName() string {
return "wallets"
}
type WalletInterface interface {
Create(ctx context.Context, wallets ...*Wallet) error
Take(ctx context.Context, userID string) (*Wallet, error)
Find(ctx context.Context, userIDs []string) ([]*Wallet, error)
Update(ctx context.Context, userID string, data map[string]any) error
UpdateBalance(ctx context.Context, userID string, balance int64) error
IncrementBalance(ctx context.Context, userID string, amount int64) (beforeBalance int64, afterBalance int64, err error) // 原子更新余额,返回更新前后的余额
UpdatePaymentPassword(ctx context.Context, userID string, paymentPassword string) error
UpdateWithdrawAccount(ctx context.Context, userID string, withdrawAccount string) error // 更新提款账号(兼容旧接口)
UpdateWithdrawAccountWithType(ctx context.Context, userID string, withdrawAccount string, accountType int32) error // 更新提款账号(带类型)
UpdateRealNameAuth(ctx context.Context, userID string, realNameAuth RealNameAuth) error // 更新实名认证信息
Delete(ctx context.Context, userIDs []string) error
Page(ctx context.Context, pagination pagination.Pagination) (int64, []*Wallet, error)
PageByRealNameAuthAuditStatus(ctx context.Context, auditStatus int32, userID string, pagination pagination.Pagination) (int64, []*Wallet, error) // 按实名认证审核状态分页查询支持用户ID搜索
SearchByRealNameAuth(ctx context.Context, realNameKeyword string, idCardKeyword string) ([]string, error) // 按实名认证信息搜索钱包返回userIDs
}
// WalletBalanceRecord 钱包余额记录
type WalletBalanceRecord struct {
ID string `bson:"_id"` // 记录ID
UserID string `bson:"user_id"` // 用户ID
Amount int64 `bson:"amount"` // 变动金额(单位:分,正数表示增加,负数表示减少)
Type int32 `bson:"type"` // 变动类型1-充值2-提现/提款3-消费4-退款5-奖励6-后台充值7-发红包8-抢红包99-其他
BeforeBalance int64 `bson:"before_balance"` // 变动前余额(单位:分)
AfterBalance int64 `bson:"after_balance"` // 变动后余额(单位:分)
OrderID string `bson:"order_id"` // 关联订单ID可选
TransactionID string `bson:"transaction_id"` // 交易ID可选
RedPacketID string `bson:"red_packet_id"` // 红包ID用于发红包和抢红包记录关联可选
Remark string `bson:"remark"` // 备注
CreateTime time.Time `bson:"create_time"` // 创建时间
}
func (WalletBalanceRecord) TableName() string {
return "wallet_balance_records"
}
type WalletBalanceRecordInterface interface {
Create(ctx context.Context, records ...*WalletBalanceRecord) error
Take(ctx context.Context, recordID string) (*WalletBalanceRecord, error)
FindByUserID(ctx context.Context, userID string, pagination pagination.Pagination) (int64, []*WalletBalanceRecord, error)
FindByUserIDAndType(ctx context.Context, userID string, recordType int32, pagination pagination.Pagination) (int64, []*WalletBalanceRecord, error)
FindByOrderID(ctx context.Context, orderID string) (*WalletBalanceRecord, error)
FindByTransactionID(ctx context.Context, transactionID string) (*WalletBalanceRecord, error)
FindByRedPacketID(ctx context.Context, redPacketID string) ([]*WalletBalanceRecord, error)
GetUserBalanceHistory(ctx context.Context, userID string, startTime, endTime *time.Time, pagination pagination.Pagination) (int64, []*WalletBalanceRecord, error)
CountByUserID(ctx context.Context, userID string) (int64, error)
}