package groupPower_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/groupPower_e" "time" ) // 家族之星 type GroupPowerMonthStar struct { Month string GroupPowerId mysql.ID UserId mysql.ID Type groupPower_e.GroupPowerStarType Score mysql.Num CreatedTime time.Time `gorm:"->"` UpdatedTime time.Time `gorm:"->"` } // 增加家族之星分数 func IncrGroupPowerMonthStarScore(model *domain.Model, groupPowerId, userId mysql.ID, _type groupPower_e.GroupPowerStarType, score mysql.Num) error { month := time.Now().Format("200601") star := &GroupPowerMonthStar{ Month: month, GroupPowerId: groupPowerId, UserId: userId, Type: _type, Score: score, } if err := model.DB().Model(GroupPowerMonthStar{}).Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: "month"}, {Name: "group_power_id"}, {Name: "user_id"}, {Name: "type"}}, DoUpdates: clause.Assignments(map[string]interface{}{ "score": gorm.Expr("score + ?", star.Score)}), }).Create(star).Error; err != nil { model.Log.Errorf("IncrGroupPowerMonthStarScore fail:%v", err) return err } return nil } // 获取家族之星排行 func GetGroupPowerMonthStarRank(model *domain.Model, groupPowerId mysql.ID, _type groupPower_e.GroupPowerStarType, offset, limit int) ([]*GroupPowerMonthStar, error) { var res []*GroupPowerMonthStar month := time.Now().Format("200601") if err := model.DB().Model(GroupPowerMonthStar{}).Where("month = ? AND group_power_id = ? AND `type` = ?", month, groupPowerId, _type). Order("score desc").Offset(offset).Limit(limit).Find(&res).Error; err != nil { model.Log.Errorf("GetGroupPowerMonthStarRank fail:%v", err) return res, err } return res, nil }