inner.go 4.98 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3
package group_power_r

import (
4
	"fmt"
hujiebin's avatar
hujiebin committed
5 6 7 8
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mycontext"
	"git.hilo.cn/hilo-common/resource/mysql"
	"github.com/gin-gonic/gin"
9
	"github.com/jinzhu/now"
hujiebin's avatar
hujiebin committed
10 11
	"hilo-group/_const/enum/groupPower_e"
	"hilo-group/_const/enum/msg_e"
12
	"hilo-group/_const/redis_key/groupPower_k"
hujiebin's avatar
hujiebin committed
13 14 15 16
	"hilo-group/cv/group_power_cv"
	"hilo-group/domain/model/groupPower_m"
	"hilo-group/domain/model/msg_m"
	"hilo-group/domain/model/user_m"
chenweijian's avatar
chenweijian committed
17
	"hilo-group/domain/service/group_power_s"
18
	"hilo-group/myerr/bizerr"
hujiebin's avatar
hujiebin committed
19
	"hilo-group/resp"
20
	"time"
hujiebin's avatar
hujiebin committed
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
)

type MGetGroupPowerReq struct {
	Ids []mysql.ID `form:"ids" binding:"required"`
}

// @Tags 国家势力-内部
// @Summary 批量获取国家势力信息
// @Param ids query string true "用户id,如:ids=1&ids=2&ids=3"
// @Success 200 {object} map[mysql.ID]group_power_cv.CvGroupPowerInfo
// @Router /inner/groupPower/infos [get]
func MGetGroupPowers(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	var model = domain.CreateModelContext(myCtx)
	var req MGetGroupPowerReq
	if err := c.ShouldBindQuery(&req); err != nil {
		return myCtx, err
	}
	groupPowers, err := groupPower_m.MGetGroupPowerInfoMap(model, req.Ids)
	if err != nil {
		return myCtx, err
	}
	groupPowerUsers, err := groupPower_m.MGetGroupPowerUsers(model, req.Ids)
	if err != nil {
		return myCtx, err
	}
	groupPowerGrade, err := groupPower_m.MGetGroupPowerGrade(model, req.Ids)
	if err != nil {
		return myCtx, err
	}
	response := make(map[mysql.ID]group_power_cv.CvGroupPowerInfo)
	for groupPowerId, info := range groupPowers {
		// 成员上限
		var memberMax mysql.Num
		grade := groupPowerGrade[groupPowerId].Grade
		if grade >= groupPower_e.GroupPowerGrade0 && grade <= groupPower_e.GroupPowerGradeMax {
chenweijian's avatar
chenweijian committed
57 58
			//memberMax = mysql.Num(group_power_cv.GroupPowerGradePrivilegeNum[groupPowerGrade[groupPowerId].Grade][0].Num)
			memberMax = mysql.Num(group_power_s.GetGroupPowerMaxMemberNum(model, groupPowerId, grade))
hujiebin's avatar
hujiebin committed
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
		}
		memberNum := mysql.Num(len(groupPowerUsers[groupPowerId]))
		if memberNum > memberMax {
			memberMax = memberNum
		}
		response[groupPowerId] = group_power_cv.CvGroupPowerInfo{
			CvGroupPowerBase: group_power_cv.CvGroupPowerBase{
				Id:        groupPowerId,
				Icon:      info.Icon,
				Name:      info.Name,
				Nameplate: info.Nameplate,
			},
			CvGroupPowerMember: group_power_cv.CvGroupPowerMember{
				MemberNum: memberNum,
				MemberMax: memberMax,
			},
			CvGroupPowerGrade: group_power_cv.CvGroupPowerGrade{
				Grade: grade,
				Exp:   groupPowerGrade[groupPowerId].Exp,
			},
		}
	}
	resp.ResponseOk(c, response)
	return myCtx, nil
}

func Test(c *gin.Context) (*mycontext.MyContext, error) {
	var model = domain.CreateModelNil()
87
	if err := msg_m.NewUserRecord(model, 4549, msg_e.GroupPowerExpireNotice, "", 4549, "", "", "", "", "").Persistent(); err != nil {
hujiebin's avatar
hujiebin committed
88 89
		return nil, err
	}
90
	mgrUser, _ := user_m.GetUser(model, 4549)
hujiebin's avatar
hujiebin committed
91 92 93 94 95 96 97 98 99 100
	err := msg_m.SendEmasMsgAssistant(model, mgrUser.ExternalId, mgrUser.DeviceType)
	if err != nil {
		return nil, err
	}
	//err := emas.SendIosMsgAll("全部消息测试")
	//if err != nil {
	//	return nil, err
	//}
	return nil, nil
}
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

type GroupPowerDayStar struct {
	Date         time.Time
	GroupPowerId uint64
	Type         groupPower_e.GroupPowerStarType
	UserId       uint64
	Score        uint64
}

// @Tags 国家势力-内部
// @Summary 同步家族之星到redis
// @Success 200
// @Router /inner/groupPower/sync/star [get]
func SyncStar(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	t := time.Now()
	start := c.Query("start")
	end := c.Query("end")
	if len(start) <= 0 || len(end) <= 0 {
		return myCtx, bizerr.InvalidParameter
	}
	var model = domain.CreateModelContext(myCtx)
	var stars []GroupPowerDayStar
	if err := model.DB().Table("group_power_day_star").
		Select("`date`,group_power_id,type,user_id,SUM(score) score").
		Where("`date` BETWEEN ? AND ?", start, end).
		Group("`date`,group_power_id,type,user_id").Find(&stars).Error; err != nil {
		model.Log.Errorf("SyncStar fail:%v", err)
		return myCtx, err
	}
	ttl := map[string]time.Duration{
		"day":   time.Hour * 24 * 7,
		"week":  time.Hour * 24 * 7 * 30,
		"month": time.Hour * 24 * 7 * 30 * 2,
	}
	num := len(stars)
	for i, star := range stars {
		for _, period := range []string{"day", "week", "month"} {
			var dateStr string
			switch period {
			case "day":
				dateStr = star.Date.Format("2006-01-02")
			case "week":
				dateStr = now.With(star.Date).BeginningOfWeek().Format("2006-01-02")
			case "month":
				dateStr = now.With(star.Date).BeginningOfMonth().Format("2006-01-02")
			}
			key := groupPower_k.GetGroupPowerStarRankKey(star.Type, period, star.GroupPowerId, dateStr)
			model.RedisCluster.ZIncrBy(model, key, float64(star.Score), fmt.Sprintf("%d", star.UserId))
			model.RedisCluster.Expire(model, key, ttl[period])
			model.Log.Infof("SyncStar i:%v,star:%v", i, star)
		}
	}
	type res struct {
		Num  int
		Cost float64
	}
	resp.ResponseOk(c, res{
		Num:  num,
		Cost: time.Now().Sub(t).Seconds(),
	})
	return myCtx, nil
}