387 lines
14 KiB
Go
387 lines
14 KiB
Go
|
|
package data
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"context"
|
|||
|
|
"fmt"
|
|||
|
|
"time"
|
|||
|
|
|
|||
|
|
"sandc/app/eonline/internal/biz"
|
|||
|
|
"sandc/pkg/marshal"
|
|||
|
|
"sandc/pkg/utils/stime"
|
|||
|
|
|
|||
|
|
"gorm.io/gorm"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// PayoutUser 提现用户信息
|
|||
|
|
type PayoutUser struct {
|
|||
|
|
Id uint `gorm:"column:id" db:"id" json:"id" form:"id"` // 用户ID
|
|||
|
|
Platform string `gorm:"column:platform" db:"platform" json:"platform" form:"platform"` // 平台
|
|||
|
|
Ip string `gorm:"column:ip" db:"ip" json:"ip" form:"ip"` // ip地址
|
|||
|
|
Country string `gorm:"column:country" db:"country" json:"country" form:"country"` // 国家
|
|||
|
|
DeviceId string `gorm:"column:device_id" db:"device_id" json:"device_id" form:"device_id"` // 设备ID
|
|||
|
|
Version string `gorm:"column:version" db:"version" json:"version" form:"version"` // 版本号
|
|||
|
|
Uuid string `gorm:"column:uuid" db:"uuid" json:"uuid" form:"uuid"` // 用户唯一ID,根据ip, country,device_id生成
|
|||
|
|
LoginDays uint `gorm:"column:login_days" db:"login_days" json:"login_days" form:"login_days"` // 登录天数
|
|||
|
|
CreatedAt time.Time `gorm:"column:created_at" db:"created_at" json:"created_at" form:"created_at"` // 创建时间
|
|||
|
|
UpdatedAt time.Time `gorm:"column:updated_at" db:"updated_at" json:"updated_at" form:"updated_at"` // 更新时间
|
|||
|
|
ClientData string `gorm:"column:clientData" db:"clientData" json:"clientData" form:"clientData"` // 客户端数据
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// TableName sets the insert table name for this struct type
|
|||
|
|
func (p *PayoutUser) TableName() string {
|
|||
|
|
return "payout_user"
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 提现记录
|
|||
|
|
type PayoutRecord struct {
|
|||
|
|
Id uint `gorm:"column:id" db:"id" json:"id" form:"id"` // 提现记录ID
|
|||
|
|
PayoutId string `gorm:"column:payout_id" db:"payout_id" json:"payout_id" form:"payout_id"` // 支付第三方id
|
|||
|
|
RecordNo string `gorm:"column:record_no" db:"record_no" json:"record_no" form:"record_no"` // 提现唯一编码
|
|||
|
|
Channel string `gorm:"column:channel" db:"channel" json:"channel" form:"channel"` // 支付渠道
|
|||
|
|
Uuid string `gorm:"column:uuid" db:"uuid" json:"uuid" form:"uuid"` // 提现用户唯一编码
|
|||
|
|
Account string `gorm:"column:account" db:"account" json:"account" form:"account"` // 提现账号
|
|||
|
|
ItemId uint `gorm:"column:item_id" db:"item_id" json:"item_id" form:"item_id"` // 提现的item对应id
|
|||
|
|
Amount float64 `gorm:"column:amount" db:"amount" json:"amount" form:"amount"` // 提现金额
|
|||
|
|
Currency string `gorm:"column:currency" db:"currency" json:"currency" form:"currency"` // 货币单位
|
|||
|
|
Status uint8 `gorm:"column:status" db:"status" json:"status" form:"status"` // 提现状态 1:提现中,2:提现成功,3:提现失败
|
|||
|
|
Ecpm uint8 `gorm:"column:ecpm" db:"ecpm" json:"ecpm" form:"ecpm"` // ecpm等级
|
|||
|
|
Version string `gorm:"column:version" db:"version" json:"version" form:"version"` // 版本号
|
|||
|
|
CreatedAt time.Time `gorm:"column:created_at" db:"created_at" json:"created_at" form:"created_at"` // 创建时间
|
|||
|
|
UpdatedAt time.Time `gorm:"column:updated_at" db:"updated_at" json:"updated_at" form:"updated_at"` // 更新时间
|
|||
|
|
Fail string `gorm:"column:fail" db:"fail" json:"fail" form:"fail"` // 提现失败原因
|
|||
|
|
Email string `gorm:"column:email" db:"email" json:"email" form:"email"` // 邮箱
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// TableName sets the insert table name for this struct type
|
|||
|
|
func (p *PayoutRecord) TableName() string {
|
|||
|
|
return "payout_record"
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 分红记录
|
|||
|
|
type BonusRecord struct {
|
|||
|
|
Id uint `gorm:"column:id" db:"id" json:"id" form:"id"` // 年月日时间ID
|
|||
|
|
Dau uint `gorm:"column:dau" db:"dau" json:"dau" form:"dau"` // 原始dau
|
|||
|
|
Pass uint `gorm:"column:pass" db:"pass" json:"pass" form:"pass"` // 通关总份额
|
|||
|
|
Coin uint `gorm:"column:coin" db:"coin" json:"coin" form:"coin"` // 分红每一份额美元数/万分比
|
|||
|
|
Bonus uint `gorm:"column:bonus" db:"bonus" json:"bonus" form:"bonus"` // 服务器分红总额美分
|
|||
|
|
BonusCur uint `gorm:"column:bonus_cur" db:"bonus_cur" json:"bonus_cur" form:"bonus_cur"` // 服务器实际分红总额美分
|
|||
|
|
CreatedAt time.Time `gorm:"column:created_at" db:"created_at" json:"created_at" form:"created_at"` // 创建时间
|
|||
|
|
UpdatedAt time.Time `gorm:"column:updated_at" db:"updated_at" json:"updated_at" form:"updated_at"` // 更新时间
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// TableName sets the insert table name for this struct type
|
|||
|
|
func (p *BonusRecord) TableName() string {
|
|||
|
|
return "bonus_record"
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// SearchPayoutUser 查询提现用户信息
|
|||
|
|
func (r *eonlineRepo) SearchPayoutUser(ctx context.Context, opts *biz.SearchPayoutUserReq) (*biz.ListPayoutUser, error) {
|
|||
|
|
tx := r.data.DB(ctx).Model(&PayoutUser{})
|
|||
|
|
sortBy := "id desc"
|
|||
|
|
if opts.SortBy != "" {
|
|||
|
|
sortBy = opts.SortBy
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if opts.SelectFields != "" {
|
|||
|
|
tx.Select(opts.SelectFields)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if opts.StartTime.Unix() > 0 {
|
|||
|
|
tx.Where("start_time <= ?", stime.FormatTimeString(opts.StartTime))
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if opts.EndTime.Unix() > 0 {
|
|||
|
|
tx.Where("end_time >= ?", stime.FormatTimeString(opts.EndTime))
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if opts.Uuid != "" {
|
|||
|
|
tx.Where("name = ?", opts.Uuid)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// count
|
|||
|
|
var count int64
|
|||
|
|
if !opts.DisablePaging {
|
|||
|
|
if err := tx.Count(&count).Error; err != nil {
|
|||
|
|
return nil, fmt.Errorf("count search user: %w", err)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if page := opts.Page; page != nil && !opts.DisablePaging {
|
|||
|
|
tx.Limit(int(page.Limit())).Offset(int(page.Offset()))
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var list []PayoutUser
|
|||
|
|
if err := tx.Order(sortBy).Find(&list).Error; err != nil {
|
|||
|
|
return nil, fmt.Errorf("find: %w", err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if opts.DisablePaging {
|
|||
|
|
count = int64(len(list))
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
rv := make([]*biz.PayoutUser, 0)
|
|||
|
|
for _, item := range list {
|
|||
|
|
mitem, err := r.unmarshalPayoutUserFromDB(item)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, fmt.Errorf("unmarshalPayoutUserFromDB err: %w", err)
|
|||
|
|
}
|
|||
|
|
rv = append(rv, mitem)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return &biz.ListPayoutUser{
|
|||
|
|
Items: rv,
|
|||
|
|
Total: int32(count),
|
|||
|
|
}, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// unmarshalPayoutUserFromDB 数据库数据转换为业务数据
|
|||
|
|
func (r *eonlineRepo) unmarshalPayoutUserFromDB(s PayoutUser) (*biz.PayoutUser, error) {
|
|||
|
|
l := &biz.PayoutUser{}
|
|||
|
|
err := marshal.ConvertStructToOtherType(s, l)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, err
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 单独处理时间
|
|||
|
|
l.CreatedAt = s.CreatedAt
|
|||
|
|
l.UpdatedAt = s.UpdatedAt
|
|||
|
|
return l, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetUserByUuid 根据uuid获取用户信息
|
|||
|
|
func (r *eonlineRepo) GetUserByUuid(ctx context.Context, uuid string) (*biz.PayoutUser, error) {
|
|||
|
|
var item PayoutUser
|
|||
|
|
if err := r.data.DB(ctx).Where("uuid = ?", uuid).First(&item).Error; err != nil && err != gorm.ErrRecordNotFound {
|
|||
|
|
return nil, fmt.Errorf("get user by uuid: %w", err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return r.unmarshalPayoutUserFromDB(item)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// CreatePayoutUser 创建提现用户信息
|
|||
|
|
func (r *eonlineRepo) CreatePayoutUser(ctx context.Context, item *biz.PayoutUser) (*biz.PayoutUser, error) {
|
|||
|
|
tx := r.data.DB(ctx).Model(&PayoutUser{})
|
|||
|
|
if err := tx.Create(&item).Error; err != nil {
|
|||
|
|
return nil, fmt.Errorf("create: %w", err)
|
|||
|
|
}
|
|||
|
|
return item, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// SearchPayoutRecord 查询提现记录信息
|
|||
|
|
func (r *eonlineRepo) SearchPayoutRecord(ctx context.Context, opts *biz.SearchPayoutRecordReq) (*biz.ListPayoutRecord, error) {
|
|||
|
|
tx := r.data.DB(ctx).Model(&PayoutRecord{})
|
|||
|
|
sortBy := "id desc"
|
|||
|
|
if opts.SortBy != "" {
|
|||
|
|
sortBy = opts.SortBy
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if opts.SelectFields != "" {
|
|||
|
|
tx.Select(opts.SelectFields)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if opts.StartTime.Unix() > 0 {
|
|||
|
|
tx.Where("start_time <= ?", stime.FormatTimeString(opts.StartTime))
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if opts.EndTime.Unix() > 0 {
|
|||
|
|
tx.Where("end_time >= ?", stime.FormatTimeString(opts.EndTime))
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if opts.Uuid != "" {
|
|||
|
|
tx.Where("name = ?", opts.Uuid)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// count
|
|||
|
|
var count int64
|
|||
|
|
if !opts.DisablePaging {
|
|||
|
|
if err := tx.Count(&count).Error; err != nil {
|
|||
|
|
return nil, fmt.Errorf("count search user: %w", err)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if page := opts.Page; page != nil && !opts.DisablePaging {
|
|||
|
|
tx.Limit(int(page.Limit())).Offset(int(page.Offset()))
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var list []PayoutRecord
|
|||
|
|
if err := tx.Order(sortBy).Find(&list).Error; err != nil {
|
|||
|
|
return nil, fmt.Errorf("find: %w", err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if opts.DisablePaging {
|
|||
|
|
count = int64(len(list))
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
rv := make([]*biz.PayoutRecord, 0)
|
|||
|
|
for _, item := range list {
|
|||
|
|
mitem, err := r.unmarshalPayoutRecordFromDB(item)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, fmt.Errorf("unmarshalPayoutRecordFromDB err: %w", err)
|
|||
|
|
}
|
|||
|
|
rv = append(rv, mitem)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return &biz.ListPayoutRecord{
|
|||
|
|
Items: rv,
|
|||
|
|
Total: int32(count),
|
|||
|
|
}, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// CreatePayoutRecord 创建提现记录信息
|
|||
|
|
func (r *eonlineRepo) CreatePayoutRecord(ctx context.Context, item *biz.PayoutRecord) (*biz.PayoutRecord, error) {
|
|||
|
|
tx := r.data.DB(ctx).Model(&PayoutRecord{})
|
|||
|
|
if err := tx.Create(&item).Error; err != nil {
|
|||
|
|
return nil, fmt.Errorf("create: %w", err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return item, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// UpdatePayoutRecordStatus 更新提现记录状态
|
|||
|
|
func (r *eonlineRepo) UpdatePayoutRecordNotify(ctx context.Context, recordNo string, payoutId string, status uint8, fail string) error {
|
|||
|
|
tx := r.data.DB(ctx).Model(&PayoutRecord{})
|
|||
|
|
if err := tx.Where("record_no = ?", recordNo).Updates(map[string]interface{}{"payout_id": payoutId, "status": status, "fail": fail}).Error; err != nil {
|
|||
|
|
return fmt.Errorf("update: %w", err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// CheckPayoutValid 获取可提现次数
|
|||
|
|
func (r *eonlineRepo) GetPayoutLeftum(ctx context.Context, uuid string) (int, error) {
|
|||
|
|
avalidNum := 2 // 默认可提现次数
|
|||
|
|
var count int64
|
|||
|
|
if err := r.data.DB(ctx).Model(&PayoutRecord{}).Where("uuid = ? and status = ?", uuid, int(biz.PayoutStatusPayouted)).Count(&count).Error; err != nil {
|
|||
|
|
return 0, fmt.Errorf("count: %w", err)
|
|||
|
|
}
|
|||
|
|
return avalidNum - int(count), nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetPayoutRecordCountByAccount 根据account获取成功提现的次数
|
|||
|
|
func (r *eonlineRepo) GetPayoutRecordCountByAccount(ctx context.Context, account string) (int, error) {
|
|||
|
|
var count int64
|
|||
|
|
if err := r.data.DB(ctx).Model(&PayoutRecord{}).Where("account = ? and (status=? or status=?)", account, int(biz.PayoutStatusPayouted), int(biz.PayoutStatusPayouting)).Count(&count).Error; err != nil {
|
|||
|
|
return 0, fmt.Errorf("count: %w", err)
|
|||
|
|
}
|
|||
|
|
return int(count), nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetPayoutRecordListByUuid 根据uuid获取提现记录列表
|
|||
|
|
func (r *eonlineRepo) GetPayoutRecordListByUuid(ctx context.Context, uuid string) ([]*biz.PayoutRecord, error) {
|
|||
|
|
var list []PayoutRecord
|
|||
|
|
if err := r.data.DB(ctx).Model(&PayoutRecord{}).Where("uuid = ?", uuid).Order("id DESC").Find(&list).Error; err != nil {
|
|||
|
|
return nil, fmt.Errorf("find: %w", err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
rv := make([]*biz.PayoutRecord, 0)
|
|||
|
|
for _, item := range list {
|
|||
|
|
mitem, err := r.unmarshalPayoutRecordFromDB(item)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, fmt.Errorf("unmarshalPayoutRecordFromDB err: %w", err)
|
|||
|
|
}
|
|||
|
|
rv = append(rv, mitem)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return rv, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 根据account、status 获取提现记录
|
|||
|
|
func (r *eonlineRepo) GetPayoutRecordByAccountStatus(ctx context.Context, account string, status, pageIndex, pageSize int) ([]*biz.PayoutRecord, error) {
|
|||
|
|
var list []PayoutRecord
|
|||
|
|
var err error
|
|||
|
|
|
|||
|
|
if account != "" {
|
|||
|
|
err = r.data.DB(ctx).Model(&PayoutRecord{}).Where("account = ? and status = ?", account, status).Offset(pageIndex * pageSize).Limit(pageSize).Order("id DESC").Find(&list).Error
|
|||
|
|
} else {
|
|||
|
|
err = r.data.DB(ctx).Model(&PayoutRecord{}).Where("status = ?", status).Offset(pageIndex * pageSize).Limit(pageSize).Order("id DESC").Find(&list).Error
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, fmt.Errorf("find: %w", err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
rv := make([]*biz.PayoutRecord, 0)
|
|||
|
|
for _, item := range list {
|
|||
|
|
mitem, err := r.unmarshalPayoutRecordFromDB(item)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, fmt.Errorf("unmarshalPayoutRecordFromDB err: %w", err)
|
|||
|
|
}
|
|||
|
|
rv = append(rv, mitem)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return rv, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// CheckPayoutValid 检查是否可提现
|
|||
|
|
func (r *eonlineRepo) CheckPayoutValid(ctx context.Context, uuid string) (bool, error) {
|
|||
|
|
leftNum, err := r.GetPayoutLeftum(ctx, uuid)
|
|||
|
|
if err != nil {
|
|||
|
|
return false, fmt.Errorf("GetPayoutLeftum: %w", err)
|
|||
|
|
}
|
|||
|
|
return leftNum > 0, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// unmarshalPayoutRecordFromDB 数据库数据转换为业务数据
|
|||
|
|
func (r *eonlineRepo) unmarshalPayoutRecordFromDB(s PayoutRecord) (*biz.PayoutRecord, error) {
|
|||
|
|
l := &biz.PayoutRecord{}
|
|||
|
|
err := marshal.ConvertStructToOtherType(s, l)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, err
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 单独处理时间
|
|||
|
|
l.CreatedAt = s.CreatedAt
|
|||
|
|
l.UpdatedAt = s.UpdatedAt
|
|||
|
|
return l, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetPayoutRecordByRecordNo 根据recordNo获取提现记录
|
|||
|
|
func (r *eonlineRepo) GetPayoutRecordByRecordNo(ctx context.Context, recordNo string) (*biz.PayoutRecord, error) {
|
|||
|
|
var item PayoutRecord
|
|||
|
|
if err := r.data.DB(ctx).Model(&PayoutRecord{}).Where("record_no = ?", recordNo).First(&item).Error; err != nil {
|
|||
|
|
return nil, fmt.Errorf("first: %w", err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return r.unmarshalPayoutRecordFromDB(item)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// UpdatePayoutUserLoginDays 更新用户登录天数
|
|||
|
|
func (r *eonlineRepo) UpdatePayoutUserLoginDays(ctx context.Context, uuid string, loginDays uint) error {
|
|||
|
|
tx := r.data.DB(ctx).Model(&PayoutUser{})
|
|||
|
|
if err := tx.Where("uuid = ?", uuid).Updates(map[string]interface{}{"login_days": loginDays}).Error; err != nil {
|
|||
|
|
return fmt.Errorf("update: %w", err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 更新 客户端上传的、需要保持的数据
|
|||
|
|
func (r *eonlineRepo) UpdatePayoutUserClientData(ctx context.Context, uuid string, clientData string) error {
|
|||
|
|
tx := r.data.DB(ctx).Model(&PayoutUser{})
|
|||
|
|
if err := tx.Where("uuid = ?", uuid).Updates(map[string]interface{}{"clientData": clientData}).Error; err != nil {
|
|||
|
|
return fmt.Errorf("update: %w", err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 创建分红记录信息
|
|||
|
|
func (r *eonlineRepo) CreateBonusRecord(ctx context.Context, item *biz.BonusRecord) (*biz.BonusRecord, error) {
|
|||
|
|
tx := r.data.DB(ctx).Model(&BonusRecord{})
|
|||
|
|
if err := tx.Create(&item).Error; err != nil {
|
|||
|
|
return nil, fmt.Errorf("CreateBonusRecord: %w", err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return item, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 更新服务器领取的分红总额
|
|||
|
|
func (r *eonlineRepo) UpdateBonusCur(ctx context.Context, id uint32, bonusCur uint32) error {
|
|||
|
|
tx := r.data.DB(ctx).Model(&BonusRecord{})
|
|||
|
|
if err := tx.Where("id = ?", id).Updates(map[string]interface{}{"bonus_cur": bonusCur}).Error; err != nil {
|
|||
|
|
return fmt.Errorf("UpdateBonusCur: %w", err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return nil
|
|||
|
|
}
|