group_in.go 1.62 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
package event_m

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

// 进房事件消息
type EventGroupIn struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	Proto         mysql.Type
	Payload       []byte
	Mark          mysql.YesNo
	MarkHiloGroup mysql.YesNo
}

func (EventGroupIn) TableName() string {
	return "event_group_in"
}

// 偏移值
type EventGroupInOffsetHiloGroup struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	MarkOffset    mysql.ID
}

// 获取当前偏移值
func GroupOffset(model *domain.Model) (*EventGroupInOffsetHiloGroup, error) {
	offset := new(EventGroupInOffsetHiloGroup)
	if err := model.Db.WithContext(model).First(offset).Error; err != nil {
		if err != gorm.ErrRecordNotFound {
			model.Log.Errorf("Offset fail:%v", err)
			return nil, err
		}
		// gorm.ErrRecordNotFound
	}
	offset.Model = model
	return offset, nil
}

// 批量获取进房事件
func FetchEventGroupIn(model *domain.Model, limit int) ([]*EventGroupIn, *EventGroupInOffsetHiloGroup, error) {
	offset, err := GroupOffset(model)
	if err != nil {
		return nil, nil, err
	}
	var events []*EventGroupIn
	if err := model.Db.WithContext(model).Model(EventGroupIn{}).
		Where("id > ?", offset.MarkOffset).
		Order("id asc").Limit(limit).Find(&events).Error; err != nil {
		model.Log.Errorf("FetchEventGroupIn fail:%v", err)
		return nil, nil, err
	}
	return events, offset, nil
}

// 标记已完成
func (p *EventGroupIn) MarkDone() error {
	p.MarkHiloGroup = mysql.YES
	return p.Db.WithContext(p.Model).Model(EventGroupIn{}).Where("id = ?", p.ID).Update("mark_hilo_group", p.MarkHiloGroup).Limit(1).Error
}