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) }