group_star.go 5.78 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5
package groupPower_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
6
	"github.com/jinzhu/now"
hujiebin's avatar
hujiebin committed
7 8 9 10 11 12
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"hilo-group/_const/enum/groupPower_e"
	"time"
)

13
// 家族之星-月
hujiebin's avatar
hujiebin committed
14 15 16 17 18 19 20 21 22 23 24
type GroupPowerMonthStar struct {
	Month        string
	GroupPowerId mysql.ID
	UserId       mysql.ID
	Type         groupPower_e.GroupPowerStarType
	Score        mysql.Num
	LastCalTs    int64
	CreatedTime  time.Time `gorm:"->"`
	UpdatedTime  time.Time `gorm:"->"`
}

25 26 27 28 29 30 31 32 33 34 35 36
// 家族之星-日
type GroupPowerDayStar struct {
	Date         time.Time
	GroupPowerId mysql.ID
	UserId       mysql.ID
	Type         groupPower_e.GroupPowerStarType
	Score        mysql.Num
	LastCalTs    int64
	CreatedTime  time.Time `gorm:"->"`
	UpdatedTime  time.Time `gorm:"->"`
}

hujiebin's avatar
hujiebin committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
// 增加家族之星分数
func IncrGroupPowerMonthStarScore(model *domain.Model, groupPowerId, userId mysql.ID, _type groupPower_e.GroupPowerStarType, score mysql.Num, lastCalTs int64) error {
	month := time.Now().Format("200601")
	star := &GroupPowerMonthStar{
		Month:        month,
		GroupPowerId: groupPowerId,
		UserId:       userId,
		Type:         _type,
		Score:        score,
		LastCalTs:    lastCalTs,
	}
	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),
			"last_cal_ts": lastCalTs,
		}),
	}).Create(star).Error; err != nil {
		model.Log.Errorf("IncrGroupPowerMonthStarScore fail:%v", err)
		return err
	}
	return nil
}

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 88 89 90 91 92 93 94
// 增加家族之星分数
func IncrGroupPowerDayStarScore(model *domain.Model, groupPowerId, userId mysql.ID, _type groupPower_e.GroupPowerStarType, score mysql.Num, lastCalTs int64) error {
	star := &GroupPowerDayStar{
		Date:         time.Now(),
		GroupPowerId: groupPowerId,
		UserId:       userId,
		Type:         _type,
		Score:        score,
		LastCalTs:    lastCalTs,
	}
	if err := model.DB().Model(GroupPowerDayStar{}).Clauses(clause.OnConflict{
		Columns: []clause.Column{{Name: "date"}, {Name: "group_power_id"}, {Name: "user_id"}, {Name: "type"}},
		DoUpdates: clause.Assignments(map[string]interface{}{
			"score":       gorm.Expr("score + ?", star.Score),
			"last_cal_ts": lastCalTs,
		}),
	}).Create(star).Error; err != nil {
		model.Log.Errorf("IncrGroupPowerDayStarScore fail:%v", err)
		return err
	}
	return nil
}

// 获取家族之星分数
// 允许返回gorm.ErrRecordNotFound
func GetGroupPowerDayStar(model *domain.Model, groupPowerId, userId mysql.ID, _type groupPower_e.GroupPowerStarType) (*GroupPowerDayStar, error) {
	res := new(GroupPowerDayStar)
	date := time.Now()
	if err := model.DB().Where("date = ? AND group_power_id = ? AND user_id = ? AND `type` = ?", date.Format("2006-01-02"), groupPowerId, userId, _type).First(res).Error; err != nil {
		return nil, err
	}
	return res, nil
}

hujiebin's avatar
hujiebin committed
95 96 97 98 99 100 101 102 103 104 105 106
// 获取家族之星分数
// 允许返回gorm.ErrRecordNotFound
func GetGroupPowerMonthStar(model *domain.Model, groupPowerId, userId mysql.ID, _type groupPower_e.GroupPowerStarType) (*GroupPowerMonthStar, error) {
	res := new(GroupPowerMonthStar)
	month := time.Now().Format("200601")
	if err := model.DB().Where("month = ? AND group_power_id = ? AND user_id = ? AND `type` = ?", month, groupPowerId, userId, _type).First(res).Error; err != nil {
		return nil, err
	}
	return res, nil
}

// 获取家族之星排行
107
func GetGroupPowerMonthStarRank(model *domain.Model, groupPowerId mysql.ID, _type groupPower_e.GroupPowerStarType, offset, limit int, month string) ([]*GroupPowerMonthStar, error) {
hujiebin's avatar
hujiebin committed
108
	var res []*GroupPowerMonthStar
109
	//month := time.Now().Format("200601")
hujiebin's avatar
hujiebin committed
110 111 112 113 114 115 116 117
	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
}

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
// 获取家族之星排行
func GetGroupPowerStarRankPeriod(model *domain.Model, period string, groupPowerId mysql.ID, _type groupPower_e.GroupPowerStarType, offset, limit int) ([]*GroupPowerDayStar, error) {
	var res []*GroupPowerDayStar
	var startDate, endDate string
	switch period {
	case "day":
		startDate, endDate = time.Now().Format("2006-01-02"), time.Now().Format("2006-01-02")
	case "week":
		startDate = now.BeginningOfWeek().Format("2006-01-02")
		endDate = now.EndOfWeek().Format("2006-01-02")
	case "month":
		startDate = now.BeginningOfMonth().Format("2006-01-02")
		endDate = now.EndOfMonth().Format("2006-01-02")
	}
	if err := model.DB().Model(GroupPowerDayStar{}).Where("date BETWEEN ? AND ? AND group_power_id = ? AND `type` = ?", startDate, endDate, groupPowerId, _type).
		Select("user_id,SUM(score) as score").Group("user_id").
		Order("score desc").Offset(offset).Limit(limit).Find(&res).Error; err != nil {
		model.Log.Errorf("GetGroupPowerStarRankPeriod fail:%v", err)
		return res, err
	}
	for i := range res {
		res[i].Type = _type
	}
	return res, nil
}

hujiebin's avatar
hujiebin committed
144
// 获取家族之星三个排行榜的各自第一名
145 146 147
func GetGroupPowerMonthStartTop1(model *domain.Model, groupPowerId mysql.ID) ([]*GroupPowerDayStar, error) {
	var res []*GroupPowerDayStar
	r1, err := GetGroupPowerStarRankPeriod(model, "month", groupPowerId, groupPower_e.GroupPowerStarTypeFamous, 0, 1)
hujiebin's avatar
hujiebin committed
148 149 150
	if err != nil {
		return res, err
	}
151
	r2, err := GetGroupPowerStarRankPeriod(model, "month", groupPowerId, groupPower_e.GroupPowerStarTypeActive, 0, 1)
hujiebin's avatar
hujiebin committed
152 153 154
	if err != nil {
		return res, err
	}
155
	r3, err := GetGroupPowerStarRankPeriod(model, "month", groupPowerId, groupPower_e.GroupPowerStarTypeCharm, 0, 1)
hujiebin's avatar
hujiebin committed
156 157 158 159 160 161 162 163
	if err != nil {
		return res, err
	}
	res = append(res, r1...)
	res = append(res, r2...)
	res = append(res, r3...)
	return res, nil
}