groupWelcomeText.go 1.05 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
package group_m

import (
	"git.hilo.cn/hilo-common/resource/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
)

// 群欢迎语
type GroupWelcomeText struct {
	mysql.Entity
	GroupId mysql.Str
	UserId  mysql.ID
	Text    mysql.Str
}

func (gwt *GroupWelcomeText) Save(db *gorm.DB) error {
	return db.Clauses(clause.OnConflict{
		Columns:   []clause.Column{{Name: "group_id"}},
		DoUpdates: clause.AssignmentColumns([]string{"text", "user_id"}),
	}).Create(gwt).Error
}

func (gwt *GroupWelcomeText) Remove(db *gorm.DB, groupId string, userId uint64) error {
	cond := GroupWelcomeText{GroupId: groupId, UserId: userId}
	return db.Where(&cond).Delete(gwt).Error
}

func (gwt *GroupWelcomeText) Get(db *gorm.DB) (*GroupWelcomeText, error) {
	rows := make([]GroupWelcomeText, 0)
	err := db.Where(gwt).Find(&rows).Error
	if err != nil {
		return nil, err
	}
	if len(rows) <= 0 {
		return nil, nil
	}
	return &rows[0], nil
}

func RemoveWelcomeText(db *gorm.DB, groupId string) error {
	cond := GroupWelcomeText{GroupId: groupId}
	return db.Where(&cond).Delete(&GroupWelcomeText{}).Error
}