|
|
package contrab
|
|
|
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"github.com/astaxie/beego/orm"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
|
|
|
"opp/internal/utils"
|
|
|
"opp/models"
|
|
|
"opp/protocol"
|
|
|
"reflect"
|
|
|
"sync/atomic"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
var ComputeRankScoreFlag int32
|
|
|
var rankList = initRankList()
|
|
|
|
|
|
func initRankList() []Rank {
|
|
|
var list []Rank
|
|
|
list = append(list, RankDiscovery{})
|
|
|
list = append(list, RankGrasp{})
|
|
|
list = append(list, RankComment{})
|
|
|
return list
|
|
|
}
|
|
|
|
|
|
type Rank interface {
|
|
|
RankUser(o RankOption) (error, RankResult)
|
...
|
...
|
@@ -16,16 +30,17 @@ type Rank interface { |
|
|
}
|
|
|
|
|
|
type RankOption struct {
|
|
|
Type int //RankRangeType
|
|
|
ObjectId int64 //对象id 用户编号 /部门编号
|
|
|
BeginTime time.Time
|
|
|
EndTime time.Time
|
|
|
//*model.user_rank
|
|
|
}
|
|
|
|
|
|
type RankResult struct {
|
|
|
//Params map[string]interface{}
|
|
|
CompanyId int //公司编号
|
|
|
RankTypeId int //榜单类型编号
|
|
|
RankRangeId int //榜单范围编号
|
|
|
RankPeriodId int //赛季周期编号
|
|
|
Type int //Relation类型
|
|
|
RelationId int64 //对象id 用户编号 /部门编号
|
|
|
BeginTime time.Time
|
|
|
EndTime time.Time
|
|
|
Rank *models.Rank //排行榜
|
|
|
}
|
|
|
type RankResult struct{}
|
|
|
|
|
|
//计算排行分
|
|
|
func ComputeRankScore() error {
|
...
|
...
|
@@ -45,15 +60,52 @@ func ComputeRankScore() error { |
|
|
|
|
|
return computeRankScore()
|
|
|
}
|
|
|
|
|
|
func computeRankScore() error {
|
|
|
func computeRankScore() (err error) {
|
|
|
var (
|
|
|
//rankList []Rank = initRankList()
|
|
|
periods []*models.RankPeriod
|
|
|
rankRanges []*models.RankRange
|
|
|
rankRangeDatas []*models.RankRangeData
|
|
|
)
|
|
|
//1.从rank_period 查询进行中的赛季
|
|
|
defer func() {
|
|
|
if p := recover(); p != nil {
|
|
|
log.Error(p)
|
|
|
}
|
|
|
}()
|
|
|
//1.l 查询进行中的赛季
|
|
|
if periods, err = models.GetRankPeriods([]int{protocol.RankPeriodBegin}); err != nil {
|
|
|
return
|
|
|
}
|
|
|
//2.查询对应 rank_type_id 的rank_data
|
|
|
//按类型
|
|
|
//3.查询用户列表/部门列表
|
|
|
for i := range periods {
|
|
|
period := periods[i]
|
|
|
if rankRanges, err = models.GetRankRanges(period.CompanyId); err == orm.ErrNoRows {
|
|
|
continue
|
|
|
}
|
|
|
//所有部门
|
|
|
|
|
|
//所有用户
|
|
|
|
|
|
//按类型
|
|
|
//3.查询用户列表/部门列表
|
|
|
for j := range rankRanges {
|
|
|
var relationIds []int64
|
|
|
rankRange := rankRanges[j]
|
|
|
if rankRange.Type == protocol.RankRangeTypeAllCompanyUser {
|
|
|
|
|
|
} else if rankRange.Type == protocol.RankRangeTypeAllCompanyDepartment {
|
|
|
|
|
|
} else {
|
|
|
if rankRangeDatas, err = models.GetRankRangeDataList(rankRange.Id); err == orm.ErrNoRows {
|
|
|
continue
|
|
|
}
|
|
|
}
|
|
|
for k := range rankRangeDatas {
|
|
|
relationIds = append(relationIds, int64(rankRangeDatas[k].RelationId))
|
|
|
}
|
|
|
updateRankByRelationIds(relationIds, period, rankRange)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//4.调用统计接口列表
|
|
|
//5.汇聚所有的rankResult
|
|
|
//6.查询当前user_rank是否有这条记录,比较记录里面数据是否有变化
|
...
|
...
|
@@ -61,6 +113,211 @@ func computeRankScore() error { |
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func initRankList() []Rank {
|
|
|
return []Rank{}
|
|
|
//更新排行榜按关联id
|
|
|
func updateRankByRelationIds(relationIds []int64, period *models.RankPeriod, rankRange *models.RankRange) {
|
|
|
var option RankOption
|
|
|
for i := range relationIds {
|
|
|
option = RankOption{
|
|
|
CompanyId: period.CompanyId,
|
|
|
RankTypeId: period.RankTypeId,
|
|
|
RankRangeId: rankRange.Id,
|
|
|
RankPeriodId: period.Id,
|
|
|
RelationId: relationIds[i],
|
|
|
Type: int(rankRange.Type),
|
|
|
}
|
|
|
updateRank(option)
|
|
|
}
|
|
|
updateTotalScore(option)
|
|
|
}
|
|
|
|
|
|
//更新榜单数据
|
|
|
func updateRank(o RankOption) {
|
|
|
var (
|
|
|
rank *models.Rank
|
|
|
e error
|
|
|
isNew = true
|
|
|
rankTmp models.Rank
|
|
|
)
|
|
|
if rank, e = models.GetRank(o.CompanyId, o.RankTypeId, o.RankRangeId, o.RankPeriodId, o.RelationId); e != nil && e == orm.ErrNoRows {
|
|
|
isNew = true
|
|
|
rank = &models.Rank{
|
|
|
CompanyId: o.CompanyId,
|
|
|
RankTypeId: o.RankTypeId,
|
|
|
RankRangeId: o.RankRangeId,
|
|
|
RankPeriodId: o.RankPeriodId,
|
|
|
RelationId: o.RelationId,
|
|
|
UpdateAt: time.Now(),
|
|
|
CreateAt: time.Now(),
|
|
|
EnableStatus: protocol.Valid,
|
|
|
Type: int8(o.Type),
|
|
|
}
|
|
|
} else {
|
|
|
rankTmp = *rank
|
|
|
}
|
|
|
for i := range rankList {
|
|
|
item := rankList[i]
|
|
|
if o.Type == protocol.RankRangeTypeAllCompanyDepartment || o.Type == protocol.RankRangeTypeAllSpecifyDepartment {
|
|
|
item.RankDepartment(o)
|
|
|
continue
|
|
|
}
|
|
|
item.RankUser(o)
|
|
|
}
|
|
|
if isNew {
|
|
|
if _, e = models.AddRank(rank); e != nil {
|
|
|
log.Error(e)
|
|
|
}
|
|
|
} else {
|
|
|
//数据有变化的时候才更新到表
|
|
|
if !reflect.DeepEqual(rankTmp, *rank) {
|
|
|
if e = models.UpdateRankById(rank); e != nil {
|
|
|
log.Error(e)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//更新排行榜总分 //更新总分 系数*发现分 + 系数*把握分
|
|
|
func updateTotalScore(o RankOption) {
|
|
|
var (
|
|
|
config *models.SysConfig
|
|
|
err error
|
|
|
scoreConfig protocol.ScoreConfig
|
|
|
sql = `update rank set total_score=discovery_score*%v + grasp_score*%v where
|
|
|
company_id=%v and rank_type_id=%v and rank_period_id=%v`
|
|
|
)
|
|
|
if config, err = models.GetSysConfigByCompanyId(int(o.CompanyId), models.KeyScore); err != nil {
|
|
|
err = protocol.NewErrWithMessage(5510)
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
if err = json.Unmarshal([]byte(config.Content), &scoreConfig); err != nil {
|
|
|
err = protocol.NewErrWithMessage(5510)
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
sql = fmt.Sprintf(sql,
|
|
|
scoreConfig.SumScore.DiscoveryFactor,
|
|
|
scoreConfig.SumScore.CatchFactor, o.CompanyId, o.RankTypeId, o.RankPeriodId)
|
|
|
orm := orm.NewOrm()
|
|
|
if err := utils.ExecuteSQLWithOrmer(orm, sql); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//发现计算规则
|
|
|
type RankDiscovery struct{}
|
|
|
|
|
|
func (rank RankDiscovery) RankUser(o RankOption) (err error, result RankResult) {
|
|
|
var (
|
|
|
sql = "select sum(discovery_score) score,count(0) total from chance where user_id =? and review_status=3"
|
|
|
score float64
|
|
|
total int
|
|
|
)
|
|
|
orm := orm.NewOrm()
|
|
|
if err = orm.Raw(sql, o.RelationId).QueryRow(&score, &total); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
o.Rank.DiscoveryScore = score
|
|
|
o.Rank.DiscoveryTotal = total
|
|
|
return
|
|
|
}
|
|
|
func (rank RankDiscovery) RankDepartment(o RankOption) (err error, result RankResult) {
|
|
|
var (
|
|
|
sql = "select sum(discovery_score) score,count(0) total from chance where department_id =? and review_status=3"
|
|
|
score float64
|
|
|
total int
|
|
|
)
|
|
|
orm := orm.NewOrm()
|
|
|
if err = orm.Raw(sql, o.RelationId).QueryRow(&score, &total); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
o.Rank.DiscoveryScore = score
|
|
|
o.Rank.DiscoveryTotal = total
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//把握分计算规则
|
|
|
type RankGrasp struct{}
|
|
|
|
|
|
func (rank RankGrasp) RankUser(o RankOption) (err error, result RankResult) {
|
|
|
var (
|
|
|
sql = `select sum(a.user_grasp_score) score from(
|
|
|
select user_grasp_score from achievement where user_company_id=?
|
|
|
UNION ALL
|
|
|
select user_grasp_score from achievement_provider where user_company_id=?
|
|
|
) a
|
|
|
`
|
|
|
score float64
|
|
|
)
|
|
|
orm := orm.NewOrm()
|
|
|
if err = orm.Raw(sql, o.RelationId, o.RelationId).QueryRow(&score); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
o.Rank.GraspScore = score
|
|
|
return
|
|
|
}
|
|
|
func (rank RankGrasp) RankDepartment(o RankOption) (err error, result RankResult) {
|
|
|
var (
|
|
|
sql = `select sum(a.user_grasp_score) score from(
|
|
|
select user_grasp_score from achievement where department_id=?
|
|
|
UNION ALL
|
|
|
select user_grasp_score from achievement_provider where department_id=?
|
|
|
) a
|
|
|
`
|
|
|
score float64
|
|
|
)
|
|
|
orm := orm.NewOrm()
|
|
|
if err = orm.Raw(sql, o.RelationId, o.RelationId).QueryRow(&score); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
o.Rank.GraspScore = score
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//评论数计算规则
|
|
|
type RankComment struct{}
|
|
|
|
|
|
func (rank RankComment) RankUser(o RankOption) (err error, result RankResult) {
|
|
|
var (
|
|
|
sql = `select count(0) total from comment where user_id=?
|
|
|
`
|
|
|
total int
|
|
|
)
|
|
|
orm := orm.NewOrm()
|
|
|
if err = orm.Raw(sql, o.RelationId).QueryRow(&total); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
o.Rank.CommentTotal = total
|
|
|
return
|
|
|
}
|
|
|
func (rank RankComment) RankDepartment(o RankOption) (err error, result RankResult) {
|
|
|
var (
|
|
|
sql = `select count(0) total from comment where user_id in (%v)
|
|
|
`
|
|
|
sqlUserDepartment = `select user_company_id from user_department where department_id=?`
|
|
|
total int
|
|
|
userIds []int
|
|
|
)
|
|
|
orm := orm.NewOrm()
|
|
|
if _, err = orm.Raw(sqlUserDepartment, o.RelationId).QueryRows(&userIds); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
if len(userIds) == 0 {
|
|
|
return
|
|
|
}
|
|
|
sql = fmt.Sprintf(sql, utils.JoinInts(userIds, ","))
|
|
|
if err = orm.Raw(sql).QueryRow(&total); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
o.Rank.CommentTotal = total
|
|
|
return
|
|
|
} |
...
|
...
|
|