Commit c39d806b authored by hujiebin's avatar hujiebin

feat:家族之星

parent a6c36dee
package groupPower_e
type GroupPowerStarType int
const (
GroupPowerStarTypeFamous GroupPowerStarType = 1 // 送礼
GroupPowerStarTypeActive GroupPowerStarType = 2 // 活跃
GroupPowerStarTypeCharm GroupPowerStarType = 3 // 收礼
)
func IsGroupPowerStarType(t GroupPowerStarType) bool {
if t != GroupPowerStarTypeFamous && t != GroupPowerStarTypeActive && t != GroupPowerStarTypeCharm {
return false
}
return true
}
......@@ -3,8 +3,15 @@ package group_power_cv
import (
"git.hilo.cn/hilo-common/resource/mysql"
"hilo-group/_const/enum/groupPower_e"
"hilo-group/cv/user_cv"
)
// 家族榜单
type CvGroupPowerRank struct {
MyGroupPower CvGroupPowerRankData `json:"myGroupPower"`
Items []CvGroupPowerRankData `json:"items"`
}
type CvGroupPowerRankData struct {
GroupPowerId mysql.ID `json:"groupPowerId"` // 家族id
GroupPic string `json:"groupPic"` // 家族图片
......@@ -16,7 +23,8 @@ type CvGroupPowerRankData struct {
// todo 铭牌,图片等字段, 等伟健哥哥的结构体
}
type CvGroupPowerRank struct {
MyGroupPower CvGroupPowerRankData `json:"myGroupPower"`
Items []CvGroupPowerRankData `json:"items"`
// 家族之星
type CvGroupPowerStarData struct {
user_cv.CvUserBase `json:",inline"`
Score mysql.Num `json:"score"` // 对应分值
}
......@@ -666,3 +666,18 @@ func GetGroupPowerGroups(model *domain.Model, groupPowerId mysql.ID) ([]*group_m
}
return res, nil
}
// 批量获取势力用户
// return userId->GroupPowerUser
func BatchGetGroupPowerUser(model *domain.Model, userIds []mysql.ID) (map[mysql.ID]GroupPowerUser, error) {
var rows []GroupPowerUser
res := make(map[mysql.ID]GroupPowerUser)
if err := model.Db.Model(GroupPowerUser{}).Where("user_id in ?", userIds).Find(&rows).Error; err != nil {
model.Log.Errorf("BatchGetGroupPowerUser fail:%v", err)
return res, err
}
for i, v := range rows {
res[v.UserId] = rows[i]
}
return res, nil
}
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
}
......@@ -6,6 +6,7 @@ import (
"git.hilo.cn/hilo-common/resource/mysql"
"git.hilo.cn/hilo-common/rpc"
"git.hilo.cn/hilo-common/sdk/tencentyun"
"hilo-group/_const/enum/groupPower_e"
"hilo-group/_const/enum/group_e"
"hilo-group/_const/enum/msg_e"
"hilo-group/_const/enum/task_e"
......@@ -417,4 +418,36 @@ func SendGift() {
}
return nil
})
// 家族之星-送礼/收礼
gift_ev.AddSendGiftEventAsync(func(model *domain.Model, event interface{}) error {
sendGiftEvent, ok := event.(*gift_ev.SendGiftEvent)
if !ok {
model.Log.Errorf("AddSendGiftEventAsync event type err")
return nil
}
var userIds = []mysql.ID{sendGiftEvent.SendUserId}
userIds = append(userIds, sendGiftEvent.ReceiveUserIds...)
groupPowers, err := groupPower_m.BatchGetGroupPowerUser(model, userIds)
if err != nil {
model.Log.Errorf("AddSendGiftEventAsync fail:%v", err)
return err
}
// 送礼加分
if data, ok := groupPowers[sendGiftEvent.SendUserId]; ok {
diamonds := sendGiftEvent.GiftN * sendGiftEvent.ResGift.DiamondNum * mysql.Num(len(sendGiftEvent.ReceiveUserIds))
if err := groupPower_m.IncrGroupPowerMonthStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeFamous, diamonds); err != nil {
model.Log.Errorf("IncrGroupPowerMonthStarScore famous fail:%v", err)
}
}
// 收礼加分
for _, userId := range sendGiftEvent.ReceiveUserIds {
if data, ok := groupPowers[userId]; ok {
diamonds := sendGiftEvent.GiftN * sendGiftEvent.ResGift.DiamondNum
if err := groupPower_m.IncrGroupPowerMonthStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeCharm, diamonds); err != nil {
model.Log.Errorf("IncrGroupPowerMonthStarScore charm fail:%v", err)
}
}
}
return nil
})
}
......@@ -9,7 +9,9 @@ import (
"github.com/jinzhu/now"
"hilo-group/_const/enum/groupPower_e"
"hilo-group/cv/group_power_cv"
"hilo-group/cv/user_cv"
"hilo-group/domain/model/groupPower_m"
"hilo-group/domain/model/user_m"
"hilo-group/myerr/bizerr"
"hilo-group/req"
"hilo-group/resp"
......@@ -33,7 +35,6 @@ func GroupPowerRank(c *gin.Context) (*mycontext.MyContext, error) {
if period != "day" && period != "week" && period != "month" {
return myContext, bizerr.InvalidParameter
}
// day
var beginDate, endDate string
switch period {
case "day":
......@@ -93,3 +94,58 @@ func GroupPowerRank(c *gin.Context) (*mycontext.MyContext, error) {
resp.ResponseOk(c, response)
return myContext, nil
}
type GroupPowerStarReq struct {
GroupPowerId mysql.ID `form:"groupPowerId" binding:"required"`
Type groupPower_e.GroupPowerStarType `form:"type" binding:"required"`
PageSize int `form:"pageSize" binding:"required"`
PageIndex int `form:"pageIndex" binding:"required"`
}
// @Tags 国家势力
// @Summary 家族之星
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupPowerId query int true "分页大小 默认:10" default(10)
// @Param type query string true "类型 1:送礼 2:活跃 3:收礼物"
// @Param pageSize query int false "分页大小 默认:10" default(10)
// @Param pageIndex query int false "第几个分页,从1开始 默认:1" default(1)
// @Success 200 {object} []group_power_cv.CvGroupPowerStarData
// @Router /v1/groupPower/star [get]
func GroupPowerStar(c *gin.Context) (*mycontext.MyContext, error) {
myContext := mycontext.CreateMyContext(c.Keys)
param := new(GroupPowerStarReq)
if err := c.ShouldBindQuery(param); err != nil {
return myContext, err
}
if param.PageIndex <= 0 {
param.PageIndex = 1
}
var model = domain.CreateModelContext(myContext)
offset, limit := (param.PageIndex-1)*param.PageSize, param.PageSize
rank, err := groupPower_m.GetGroupPowerMonthStarRank(model, param.GroupPowerId, param.Type, offset, limit)
if err != nil {
return myContext, err
}
var response []group_power_cv.CvGroupPowerStarData
var userIds []mysql.ID
for _, row := range rank {
userIds = append(userIds, row.UserId)
}
users, err := user_m.GetUserMapByIds(model, userIds)
for _, row := range rank {
user := users[row.UserId]
response = append(response, group_power_cv.CvGroupPowerStarData{
CvUserBase: user_cv.CvUserBase{
Id: &user.ID,
ExternalId: &user.ExternalId,
Code: &user.Code,
Nick: &user.Nick,
Avatar: &user.Avatar,
},
Score: row.Score,
})
}
resp.ResponseOk(c, response)
return myContext, nil
}
......@@ -126,6 +126,7 @@ func InitRouter() *gin.Engine {
groupPower.GET("/billboard/week", wrapper(group_power_r.GroupPowerBillboardWeek))
groupPower.GET("/billboard/owner/week", wrapper(group_power_r.GroupPowerBillboardOwnerWeek))
groupPower.GET("/rank/:period", wrapper(group_power_r.GroupPowerRank))
groupPower.GET("/star", wrapper(group_power_r.GroupPowerStar))
}
return r
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment