family.go 1.67 KB
Newer Older
chenweijian's avatar
chenweijian 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
package groupPower_m

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

type GroupPowerGrade struct {
	GroupPowerId int64     `json:"group_power_id"`
	Exp          int64     `json:"exp"`
	Grade        int32     `json:"grade"`
	ExpireAt     time.Time `json:"expire_at"`
}

func (this *GroupPower) Get(model *domain.Model) (*GroupPower, error) {
	group := new(GroupPower)
	err := model.Db.Where(this).First(&group).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		}
		return nil, err
	}
	grade := new(GroupPowerGrade)
	err = model.Db.Model(&GroupPowerGrade{}).Where("group_power_id=? and expire_at > ?", this.ID, time.Now().Format(utime.Layout)).First(&grade).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	if grade.GroupPowerId > 0 {
		group.Grade = mysql.Num(grade.Grade)
		group.Exp = mysql.Num(grade.Exp)
		group.NextExp = mysql.Num(grade.Exp)
	}
	return group, nil
}

chenweijian's avatar
chenweijian committed
40
func (gpu *GroupPowerUser) GetBy(model *domain.Model, pageSize, pageIndex int) ([]*GroupPowerUser, error) {
chenweijian's avatar
chenweijian committed
41
	rows := make([]*GroupPowerUser, 0)
chenweijian's avatar
chenweijian committed
42
	err := model.Db.Where(gpu).Order("field(`role`, 2, 3, 1)").Limit(pageSize).Offset(pageIndex - 1).Find(&rows).Error
chenweijian's avatar
chenweijian committed
43 44 45 46 47
	if err != nil {
		return nil, err
	}
	return rows, nil
}
chenweijian's avatar
chenweijian committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

func (gpu *GroupPowerUser) GetGroupPowerUser(model *domain.Model) (*GroupPowerUser, error) {
	rows := make([]*GroupPowerUser, 0)
	err := model.Db.Where(gpu).Find(&rows).Error
	if err != nil {
		return nil, err
	}
	if len(rows) == 0 {
		return nil, nil
	}
	return rows[0], nil
}

func (gpu *GroupPowerUser) Create(db *gorm.DB) error {
	return db.Create(gpu).Error
}