You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.3 KiB
Go

package model
import (
"context"
"github.com/pkg/errors"
"gorm.io/gorm"
)
var _ UserNobilityModel = (*customUserNobilityModel)(nil)
type (
// UserNobilityModel is an interface to be customized, add more methods here,
// and implement the added methods in customUserNobilityModel.
UserNobilityModel interface {
userNobilityModel
// RecordNobility 记录贵族信息. 已存在更新,不存在插入
RecordNobility(ctx context.Context, data *UserNobility) error
}
customUserNobilityModel struct {
*defaultUserNobilityModel
}
)
// NewUserNobilityModel returns a model for the database table.
func NewUserNobilityModel(conn *gorm.DB) UserNobilityModel {
return &customUserNobilityModel{
defaultUserNobilityModel: newUserNobilityModel(conn),
}
}
func (m *customUserNobilityModel) RecordNobility(ctx context.Context, data *UserNobility) error {
return m.TransactCtx(ctx, nil, func(tx *gorm.DB) error {
var err error
var dbModel *UserNobility
if dbModel, err = m.FindOne(ctx, tx, data.UserId); err != nil {
if !errors.Is(err, ErrNotFound) {
return err
}
}
if dbModel == nil {
// insert
if err = m.Insert(ctx, tx, data); err != nil {
return err
}
} else {
// update
if err = m.Update(ctx, tx, data); err != nil {
return err
}
}
return nil
})
}