support.go 4.76 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
package group_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
	"git.hilo.cn/hilo-common/utils"
	"gorm.io/gorm"
	"hilo-group/_const/enum/group_e"
	"hilo-group/_const/enum/res_e"
	"hilo-group/domain/model/res_m"
	"hilo-group/myerr"
	"time"
)

type GroupSupportResult struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	GroupUid      mysql.Str
	Grade         res_e.ResGroupSupportGrade
	Period        mysql.Str
	Heat          uint // 群热度
}

// 根据指定时间计算支持度时间范围:时间到上一个周一的沙特18PM,即北京时间23PM 到 下一个周一同样时间
func GetSupportLevelTime(t time.Time) (time.Time, time.Time, string) {
	t = utils.GetMonday(t)
	beginTime := time.Date(t.Year(), t.Month(), t.Day(), group_e.SUPPORT_LEVEL_BOUNDARY_HOUR, 0, 0, 0, time.Local)
	endTime := beginTime.AddDate(0, 0, group_e.SUPPORT_LEVEL_PERIOD_DAY)
	return beginTime, endTime, endTime.Format(utils.DATE_FORMAT)
}

func InitGroupSupportResult(model *domain.Model, groupUid string, grade res_e.ResGroupSupportGrade, period string, heat uint) *GroupSupportResult {
	return &GroupSupportResult{
		Model:    model,
		GroupUid: groupUid,
		Grade:    grade,
		Period:   period,
		Heat:     heat,
	}
}

func GetAllGroupSupportResult(db *gorm.DB, period string) (map[string]uint8, error) {
	if len(period) <= 0 {
		return nil, nil
	}
	gsr := GroupSupportResult{
		Period: period,
	}
	rows := make([]GroupSupportResult, 0)
	if err := db.Where(gsr).Find(&rows).Error; err != nil {
		return nil, err
	}
	result := make(map[string]uint8)
	for _, i := range rows {
		result[i.GroupUid] = i.Grade
	}
	return result, nil
}

chenweijian's avatar
chenweijian committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
func GetGroupSupportResult(db *gorm.DB, period string, groupIds []string) (map[string]uint8, error) {
	if len(period) <= 0 {
		return nil, nil
	}
	gsr := GroupSupportResult{
		Period: period,
	}
	rows := make([]GroupSupportResult, 0)
	if err := db.Where(gsr).Where("group_uid in (?)", groupIds).Find(&rows).Error; err != nil {
		return nil, err
	}
	result := make(map[string]uint8)
	for _, i := range rows {
		result[i.GroupUid] = i.Grade
	}
	return result, nil
}

hujiebin's avatar
hujiebin committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
//群组扶持奖励,利益分配者
type GroupSupportAwardAdmin struct {
	mysql.Entity
	*domain.Model     `gorm:"-"`
	GroupUid          mysql.Str
	IssuerUserId      mysql.ID
	UserId            mysql.ID
	DiamondNum        mysql.Num
	Grade             res_e.ResGroupSupportGrade
	ResGroupSupportId mysql.ID
	Period            mysql.Str
}

func (gsa *GroupSupportAwardAdmin) Get(db *gorm.DB) ([]GroupSupportAwardAdmin, error) {
	result := make([]GroupSupportAwardAdmin, 0)
	err := db.Model(&GroupSupportAwardAdmin{}).Where("group_uid = ? AND period >= ?", gsa.GroupUid, gsa.Period).Find(&result).Error
	if err != nil {
		return nil, err
	}
	return result, nil
}

//群组扶持奖励,助手
type GroupSupportAwardMgr struct {
	mysql.Entity
	*domain.Model     `gorm:"-"`
	GroupUid          mysql.Str
	IssuerUserId      mysql.ID
	UserId            mysql.ID
	DiamondNum        mysql.Num
	Grade             res_e.ResGroupSupportGrade
	ResGroupSupportId mysql.ID
	Period            mysql.Str
}

func (gsa *GroupSupportAwardMgr) Get(db *gorm.DB) ([]GroupSupportAwardMgr, error) {
	result := make([]GroupSupportAwardMgr, 0)
	err := db.Model(&GroupSupportAwardMgr{}).Where("period >= ?", gsa.Period).Find(&result).Error
	if err != nil {
		return nil, err
	}
	return result, nil
}

//添加记录
chenweijian's avatar
chenweijian committed
123 124 125 126 127 128 129 130
func AddGroupSupportAward(model *domain.Model, groupUid string, issuerUserId mysql.ID, resSupport *res_m.ResGroupSupport, userIds []mysql.ID, period string) (*GroupSupportAwardAdmin, []GroupSupportAwardMgr, error) {
	////资源获取
	//resGroupSupport, err := res_m.GetResGroupSupportById(model, resGroupSupportId)
	//if err != nil {
	//	return nil, nil, err
	//}
	if int(resSupport.MgrNum) < len(userIds) {
		return nil, nil, myerr.NewSysErrorF("AddGroupSupportAward mgrNum:%v 同 len(userIds)=%v 不一致", resSupport.MgrNum, len(userIds))
hujiebin's avatar
hujiebin committed
131 132 133 134 135 136 137 138
	}

	//增加群主奖励
	groupSupportAwardAdmin := GroupSupportAwardAdmin{
		Model:             model,
		GroupUid:          groupUid,
		IssuerUserId:      issuerUserId,
		UserId:            issuerUserId,
chenweijian's avatar
chenweijian committed
139 140 141
		DiamondNum:        resSupport.AdminAward,
		Grade:             resSupport.Grade,
		ResGroupSupportId: resSupport.ID,
hujiebin's avatar
hujiebin committed
142 143 144 145 146 147 148 149 150 151 152 153
		Period:            period,
	}

	groupSupportAwardMgrs := make([]GroupSupportAwardMgr, 0, len(userIds))

	//增加管理人奖励
	for i, _ := range userIds {
		groupSupportAwardMgrs = append(groupSupportAwardMgrs, GroupSupportAwardMgr{
			Model:             model,
			GroupUid:          groupUid,
			IssuerUserId:      issuerUserId,
			UserId:            userIds[i],
chenweijian's avatar
chenweijian committed
154 155 156
			DiamondNum:        resSupport.MgrAward,
			Grade:             resSupport.Grade,
			ResGroupSupportId: resSupport.ID,
hujiebin's avatar
hujiebin committed
157 158 159 160 161
			Period:            period,
		})
	}
	return &groupSupportAwardAdmin, groupSupportAwardMgrs, nil
}