groupMedal.go 2.13 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
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/myerr"
)

//群组勋章
type GroupMedal struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	ImGroupId     string
	ResMedalId    uint64
}

func (gm *GroupMedal) Create(db *gorm.DB) (int64, error) {
	result := db.Clauses(clause.OnConflict{DoNothing: true}).Create(gm)
	return result.RowsAffected, result.Error
}

//增加群组勋章
func (groupInfo *GroupInfo) GroupMedalMgrAdd(model *domain.Model, resMedalId uint64) *GroupMedal {
	return &GroupMedal{
		Model:      model,
		ImGroupId:  groupInfo.ImGroupId,
		ResMedalId: resMedalId,
	}
}


func GetGroupMedalOrErr(model *domain.Model, id mysql.ID) (*GroupMedal, error) {
	groupMedal := GroupMedal{}
	if err := model.Db.Model(&GroupMedal{}).First(&groupMedal, id).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	groupMedal.Model = model
	return &groupMedal, nil
}

func GetGroupMedalOrInit(model *domain.Model, imGroupId string, resMedalId mysql.ID) (*GroupMedal, error) {
	groupMedal := GroupMedal{}
	if err := model.Db.Model(&GroupMedal{}).Where(&GroupMedal{
		ImGroupId:  imGroupId,
		ResMedalId: resMedalId,
	}).First(&groupMedal).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return &GroupMedal{
				Model:      model,
				ImGroupId:  imGroupId,
				ResMedalId: resMedalId,
			}, nil
		} else {
			return nil, myerr.WrapErr(err)
		}
	}
	groupMedal.Model = model
	return &groupMedal, nil
}

func BatchGetMedals(db *gorm.DB, groupIds []string) (map[string][]uint64, error) {
	if len(groupIds) <= 0 {
		return nil, nil
	}
	rows := make([]GroupMedal, 0)
	if err := db.Model(&GroupMedal{}).Where("im_group_id IN ?", groupIds).Find(&rows).Error; err != nil {
		return nil, err
	}
	result := make(map[string][]uint64, 0)
	for _, i := range rows {
		if _, ok := result[i.ImGroupId]; !ok {
			result[i.ImGroupId] = make([]uint64, 0)
		}
		result[i.ImGroupId] = append(result[i.ImGroupId], i.ResMedalId)
	}
	return result, nil
}

//移除群组勋章
func (groupMedal *GroupMedal) GroupMedalMgrDel() *GroupMedal {
	groupMedal.SetDel()
	return groupMedal
}