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.

60 lines
1.6 KiB
Go

package model
import (
"context"
"git.noahlan.cn/northlan/ntools-go/gorm-zero/gormx"
"github.com/pkg/errors"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
var _ ZhgUserDetailsModel = (*customZhgUserDetailsModel)(nil)
type (
// ZhgUserDetailsModel is an interface to be customized, add more methods here,
// and implement the added methods in customZhgUserDetailsModel.
ZhgUserDetailsModel interface {
zhgUserDetailsModel
// Upsert 更新或新增
Upsert(ctx context.Context, tx *gorm.DB, param *UpsertParam) error
}
UpsertParam struct {
UserId int64
Elite *int64
Title *int64
}
customZhgUserDetailsModel struct {
*defaultZhgUserDetailsModel
}
)
// NewZhgUserDetailsModel returns a model for the database table.
func NewZhgUserDetailsModel(conn *gorm.DB) ZhgUserDetailsModel {
return &customZhgUserDetailsModel{
defaultZhgUserDetailsModel: newZhgUserDetailsModel(conn),
}
}
func (m *customZhgUserDetailsModel) Upsert(ctx context.Context, tx *gorm.DB, param *UpsertParam) error {
data := &ZhgUserDetails{UserId: param.UserId}
updateColumns := make([]string, 0)
if param.Title != nil {
updateColumns = append(updateColumns, "title")
data.Title = *param.Title
}
if param.Elite != nil {
updateColumns = append(updateColumns, "elite")
data.Elite = *param.Elite
}
if len(updateColumns) <= 0 {
return errors.New("错误待更新字段数不能小于0")
}
db := gormx.WithTx(ctx, m.DB, tx)
result := db.Clauses(clause.OnConflict{
DoUpdates: clause.AssignmentColumns(updateColumns),
}).Create(&data)
return gormx.WrapUpdateErr(result.Error, result.RowsAffected)
}