family.go 3.43 KB
Newer Older
chenweijian's avatar
chenweijian committed
1 2 3 4 5 6
package groupPower_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
	"gorm.io/gorm"
chenweijian's avatar
chenweijian committed
7
	"hilo-group/_const/enum/groupPower_e"
chenweijian's avatar
chenweijian committed
8
	"hilo-group/common"
chenweijian's avatar
chenweijian committed
9 10 11 12 13
	"hilo-group/common/utime"
	"time"
)

type GroupPowerGrade struct {
chenweijian's avatar
chenweijian committed
14
	GroupPowerId uint64    `json:"group_power_id"`
chenweijian's avatar
chenweijian committed
15 16 17 18 19
	Exp          int64     `json:"exp"`
	Grade        int32     `json:"grade"`
	ExpireAt     time.Time `json:"expire_at"`
}

chenweijian's avatar
chenweijian committed
20
type GroupPowerQuitLog struct {
chenweijian's avatar
chenweijian committed
21 22 23 24 25
	Id           uint64    `json:"id"`
	UserId       uint64    `json:"user_id"`
	MgrId        uint64    `json:"mgr_id"`
	CreatedTime  time.Time `json:"created_time"`
	GroupPowerId uint64    `json:"group_power_id"`
chenweijian's avatar
chenweijian committed
26 27
}

chenweijian's avatar
chenweijian committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
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
50
func (gpu *GroupPowerUser) GetBy(model *domain.Model, pageSize, pageIndex int) ([]*GroupPowerUser, int, bool, error) {
chenweijian's avatar
chenweijian committed
51
	rows := make([]*GroupPowerUser, 0)
chenweijian's avatar
chenweijian committed
52
	db := model.Db.Model(GroupPowerUser{}).Where(gpu).Order("field(`role`, 2, 3, 1)")
chenweijian's avatar
chenweijian committed
53 54
	var count int64
	err := db.Count(&count).Error
chenweijian's avatar
chenweijian committed
55
	if err != nil {
chenweijian's avatar
chenweijian committed
56 57 58 59 60
		return nil, 0, false, err
	}
	err = db.Limit(pageSize).Offset(pageIndex - 1).Find(&rows).Error
	if err != nil {
		return nil, 0, false, err
chenweijian's avatar
chenweijian committed
61
	}
chenweijian's avatar
chenweijian committed
62 63
	nextPageIndex, hasNextPage := common.PageNext(count, pageIndex, pageSize)
	return rows, nextPageIndex, hasNextPage, nil
chenweijian's avatar
chenweijian committed
64
}
chenweijian's avatar
chenweijian committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

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
}
chenweijian's avatar
chenweijian committed
81 82 83 84 85 86 87

func QuitFamily(model *domain.Model, userId, mgrId, familyId uint64) error {
	err := model.Db.Exec("delete from group_power_user where group_power_id = ? and user_id = ?", familyId, userId).Error
	if err != nil {
		return err
	}
	// log
chenweijian's avatar
chenweijian committed
88
	log := &GroupPowerQuitLog{UserId: userId, MgrId: mgrId, GroupPowerId: familyId}
chenweijian's avatar
chenweijian committed
89 90
	return model.Db.Create(log).Error
}
chenweijian's avatar
chenweijian committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

func GroupPowerQuitList(model *domain.Model, familyId uint64, pageSize, pageIndex int) ([]*GroupPowerQuitLog, int, bool, error) {
	rows := make([]*GroupPowerQuitLog, 0)
	db := model.Db.Model(GroupPowerQuitLog{}).Where("group_power_id = ?", familyId).Order("created_time desc")
	var count int64
	err := db.Count(&count).Error
	if err != nil {
		return nil, 0, false, err
	}
	err = db.Limit(pageSize).Offset(pageIndex - 1).Find(&rows).Error
	if err != nil {
		return nil, 0, false, err
	}
	nextIdx, hasNext := common.PageNext(count, pageIndex, pageSize)
	return rows, nextIdx, hasNext, nil
}
chenweijian's avatar
chenweijian committed
107 108 109 110 111 112 113 114

func UpdateFamilyAdmin(model *domain.Model, userId, familyId uint64, role groupPower_e.GroupPowerUserRole) error {
	err := model.Db.Exec("update group_power_user set role = ? where group_power_id = ? and user_id = ?", role, familyId, userId).Error
	if err != nil {
		return err
	}
	return nil
}