groupSetting.go 2.03 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
package group_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"hilo-group/_const/enum/group_e"
)

type GroupSetting struct {
	mysql.Entity
	GroupId         string
	DiceNum         uint16
	DiceType        uint16
	ProfitAllocator uint64
	IsHidden        bool
}

// 冲突时只更新diceNum
func (gs *GroupSetting) SetDiceNum(db *gorm.DB) error {
	return db.Clauses(clause.OnConflict{
		DoUpdates: clause.AssignmentColumns([]string{"dice_num"}),
	}).Create(gs).Error
}

// 冲突时只更新diceType
func (gs *GroupSetting) SetDiceType(db *gorm.DB) error {
	return db.Clauses(clause.OnConflict{
		DoUpdates: clause.AssignmentColumns([]string{"dice_type"}),
	}).Create(gs).Error
}

// 冲突时只更新IsHidden
func (gs *GroupSetting) SetIsHidden(db *gorm.DB) error {
	gs.DiceNum = group_e.GROUP_DICE_NUM_DEFAULT
	return db.Clauses(clause.OnConflict{
		DoUpdates: clause.AssignmentColumns([]string{"is_hidden"}),
	}).Create(gs).Error
}

func (gs *GroupSetting) GetHidden(db *gorm.DB) (map[string]struct{}, error) {
	rows := make([]GroupSetting, 0)
	result := make(map[string]struct{}, 0)
	gs.IsHidden = true
	if err := db.Where(gs).Find(&rows).Error; err != nil {
		return nil, err
	}
	for _, i := range rows {
		result[i.GroupId] = struct{}{}
	}
	return result, nil
}

func IsHiddenGroup(db *gorm.DB, groupId string) (bool, error) {
	gs := GroupSetting{IsHidden: true}
	hiddenGroups, err := gs.GetHidden(db)

	if err != nil {
		return false, err
	} else {
		_, ok := hiddenGroups[groupId]
		return ok, nil
	}
}

func (gs *GroupSetting) Get(db *gorm.DB) error {
	return db.Where(gs).First(gs).Error
}

func GetProfitAllocator(model *domain.Model, groupId string) (uint64, error) {
	gs := GroupSetting{GroupId: groupId}
	err := gs.Get(model.Db)
	if err != nil && err != gorm.ErrRecordNotFound {
		return 0, err
	}

	if gs.ProfitAllocator > 0 {
		return gs.ProfitAllocator, nil
	} else {
		owner, err := GetGroupOwner(model, groupId)
		if err != nil {
			return 0, err
		}
		return owner, nil
	}
}