92
data/kyc.go
Normal file
92
data/kyc.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Kyc struct {
|
||||
Id uint `json:"id" gorm:"primarykey;autoincrement"`
|
||||
UUID uuid.UUID `json:"uuid" gorm:"type:uuid;uniqueindex;not null"`
|
||||
UserId uuid.UUID `json:"user_id" gorm:"type:uuid;not null"` // 已移除 uniqueindex
|
||||
KycId uuid.UUID `json:"kyc_id" gorm:"type:uuid;uniqueindex;not null"`
|
||||
Type string `json:"type" gorm:"type:varchar(255);not null"`
|
||||
KycInfo string `json:"kyc_info" gorm:"type:text"` // aes256(base64)
|
||||
}
|
||||
|
||||
func (self *Kyc) SetUserId(id uuid.UUID) *Kyc {
|
||||
self.UserId = id
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *Kyc) SetType(t string) *Kyc {
|
||||
self.Type = t
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *Kyc) SetKycInfo(info string) *Kyc {
|
||||
self.KycInfo = info
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *Kyc) Create(ctx context.Context) (uuid.UUID, error) {
|
||||
self.UUID = uuid.New()
|
||||
self.KycId = uuid.New()
|
||||
|
||||
err := Database.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
return tx.Create(self).Error
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return uuid.Nil, err
|
||||
}
|
||||
|
||||
return self.KycId, nil
|
||||
}
|
||||
|
||||
func (self *Kyc) GetByKycId(ctx context.Context, kycId *uuid.UUID) (*Kyc, error) {
|
||||
var kyc Kyc
|
||||
err := Database.WithContext(ctx).
|
||||
Where("kyc_id = ?", kycId).
|
||||
First(&kyc).Error
|
||||
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &kyc, nil
|
||||
}
|
||||
|
||||
func (self *Kyc) ListByUserId(ctx context.Context, userId *uuid.UUID) ([]Kyc, error) {
|
||||
var list []Kyc
|
||||
err := Database.WithContext(ctx).
|
||||
Where("user_id = ?", userId).
|
||||
Find(&list).Error
|
||||
|
||||
return list, err
|
||||
}
|
||||
|
||||
func (self *Kyc) UpdateByKycID(ctx context.Context, kycId *uuid.UUID, updates map[string]any) error {
|
||||
return Database.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
return tx.Model(&Kyc{}).
|
||||
Where("kyc_id = ?", kycId).
|
||||
Updates(updates).Error
|
||||
})
|
||||
}
|
||||
|
||||
func (self *Kyc) DeleteByKycID(ctx context.Context, kycId *uuid.UUID) error {
|
||||
return Database.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
return tx.Where("kyc_id = ?", kycId).
|
||||
Delete(&Kyc{}).Error
|
||||
})
|
||||
}
|
||||
|
||||
func (self *Kyc) DeleteAllByUserId(ctx context.Context, userId *uuid.UUID) error {
|
||||
return Database.WithContext(ctx).
|
||||
Where("user_id = ?", userId).
|
||||
Delete(&Kyc{}).Error
|
||||
}
|
||||
Reference in New Issue
Block a user