...
|
...
|
@@ -4,6 +4,7 @@ import ( |
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"github.com/astaxie/beego/orm"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/identity/idgen"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
|
|
|
"opp/internal/utils"
|
|
|
"opp/models"
|
...
|
...
|
@@ -57,7 +58,7 @@ func ComputeRankScore() error { |
|
|
defer func() {
|
|
|
atomic.CompareAndSwapInt32(&ComputeRankScoreFlag, 1, 0)
|
|
|
}()
|
|
|
|
|
|
//TODO:启用多个协程执行
|
|
|
return computeRankScore()
|
|
|
}
|
|
|
func computeRankScore() (err error) {
|
...
|
...
|
@@ -82,18 +83,26 @@ func computeRankScore() (err error) { |
|
|
continue
|
|
|
}
|
|
|
//所有部门
|
|
|
|
|
|
depIdAll, e := models.GetDepartmentIdsByCompanyId(period.CompanyId)
|
|
|
if e != nil {
|
|
|
log.Error(e)
|
|
|
continue
|
|
|
}
|
|
|
//所有用户
|
|
|
|
|
|
userIdAll, e := models.GetUserCompanyIdAll(period.CompanyId)
|
|
|
if e != nil {
|
|
|
log.Error(e)
|
|
|
continue
|
|
|
}
|
|
|
//按类型
|
|
|
//3.查询用户列表/部门列表
|
|
|
for j := range rankRanges {
|
|
|
var relationIds []int64
|
|
|
rankRange := rankRanges[j]
|
|
|
if rankRange.Type == protocol.RankRangeTypeAllCompanyUser {
|
|
|
|
|
|
relationIds = userIdAll
|
|
|
} else if rankRange.Type == protocol.RankRangeTypeAllCompanyDepartment {
|
|
|
|
|
|
relationIds = depIdAll
|
|
|
} else {
|
|
|
if rankRangeDatas, err = models.GetRankRangeDataList(rankRange.Id); err == orm.ErrNoRows {
|
|
|
continue
|
...
|
...
|
@@ -124,6 +133,8 @@ func updateRankByRelationIds(relationIds []int64, period *models.RankPeriod, ran |
|
|
RankPeriodId: period.Id,
|
|
|
RelationId: relationIds[i],
|
|
|
Type: int(rankRange.Type),
|
|
|
BeginTime: period.BeginTime,
|
|
|
EndTime: period.EndTime,
|
|
|
}
|
|
|
updateRank(option)
|
|
|
}
|
...
|
...
|
@@ -135,12 +146,13 @@ func updateRank(o RankOption) { |
|
|
var (
|
|
|
rank *models.Rank
|
|
|
e error
|
|
|
isNew = true
|
|
|
isNew = false
|
|
|
rankTmp models.Rank
|
|
|
)
|
|
|
if rank, e = models.GetRank(o.CompanyId, o.RankTypeId, o.RankRangeId, o.RankPeriodId, o.RelationId); e != nil && e == orm.ErrNoRows {
|
|
|
if rank, e = models.GetRank(o.CompanyId, o.RankTypeId, o.RankRangeId, o.RankPeriodId, o.RelationId); e != nil {
|
|
|
isNew = true
|
|
|
rank = &models.Rank{
|
|
|
Id: idgen.Next(),
|
|
|
CompanyId: o.CompanyId,
|
|
|
RankTypeId: o.RankTypeId,
|
|
|
RankRangeId: o.RankRangeId,
|
...
|
...
|
@@ -154,6 +166,7 @@ func updateRank(o RankOption) { |
|
|
} else {
|
|
|
rankTmp = *rank
|
|
|
}
|
|
|
o.Rank = rank
|
|
|
for i := range rankList {
|
|
|
item := rankList[i]
|
|
|
if o.Type == protocol.RankRangeTypeAllCompanyDepartment || o.Type == protocol.RankRangeTypeAllSpecifyDepartment {
|
...
|
...
|
@@ -169,6 +182,7 @@ func updateRank(o RankOption) { |
|
|
} else {
|
|
|
//数据有变化的时候才更新到表
|
|
|
if !reflect.DeepEqual(rankTmp, *rank) {
|
|
|
rank.UpdateAt = time.Now()
|
|
|
if e = models.UpdateRankById(rank); e != nil {
|
|
|
log.Error(e)
|
|
|
}
|
...
|
...
|
@@ -192,10 +206,14 @@ company_id=%v and rank_type_id=%v and rank_period_id=%v` |
|
|
return
|
|
|
}
|
|
|
if err = json.Unmarshal([]byte(config.Content), &scoreConfig); err != nil {
|
|
|
err = protocol.NewErrWithMessage(5510)
|
|
|
//err = protocol.NewErrWithMessage(5510)
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
if scoreConfig.SumScore == nil {
|
|
|
log.Error(o.CompanyId, config.Content, "未设置总分配置")
|
|
|
return
|
|
|
}
|
|
|
sql = fmt.Sprintf(sql,
|
|
|
scoreConfig.SumScore.DiscoveryFactor,
|
|
|
scoreConfig.SumScore.CatchFactor, o.CompanyId, o.RankTypeId, o.RankPeriodId)
|
...
|
...
|
@@ -211,12 +229,13 @@ 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"
|
|
|
sql = `select sum(discovery_score) score,count(0) total from chance where user_id =? and review_status=3
|
|
|
and UNIX_TIMESTAMP(approve_time)>=? and UNIX_TIMESTAMP(approve_time)<?`
|
|
|
score float64
|
|
|
total int
|
|
|
)
|
|
|
orm := orm.NewOrm()
|
|
|
if err = orm.Raw(sql, o.RelationId).QueryRow(&score, &total); err != nil {
|
|
|
if err = orm.Raw(sql, o.RelationId, o.BeginTime.Unix(), o.EndTime.Unix()).QueryRow(&score, &total); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
...
|
...
|
@@ -226,12 +245,14 @@ func (rank RankDiscovery) RankUser(o RankOption) (err error, result RankResult) |
|
|
}
|
|
|
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"
|
|
|
sql = `select sum(discovery_score) score,count(0) total from chance where department_id =? and review_status=3
|
|
|
and UNIX_TIMESTAMP(approve_time)>=? and UNIX_TIMESTAMP(approve_time)<?
|
|
|
`
|
|
|
score float64
|
|
|
total int
|
|
|
)
|
|
|
orm := orm.NewOrm()
|
|
|
if err = orm.Raw(sql, o.RelationId).QueryRow(&score, &total); err != nil {
|
|
|
if err = orm.Raw(sql, o.RelationId, o.BeginTime.Unix(), o.EndTime.Unix()).QueryRow(&score, &total); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
...
|
...
|
@@ -246,15 +267,15 @@ 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=?
|
|
|
select user_grasp_score from achievement where user_company_id=? and UNIX_TIMESTAMP(create_at) >=? and UNIX_TIMESTAMP(create_at)<?
|
|
|
UNION ALL
|
|
|
select user_grasp_score from achievement_provider where user_company_id=?
|
|
|
select user_grasp_score from achievement_provider where user_company_id=? and UNIX_TIMESTAMP(create_at) >=? and UNIX_TIMESTAMP(create_at)<?
|
|
|
) a
|
|
|
`
|
|
|
score float64
|
|
|
)
|
|
|
orm := orm.NewOrm()
|
|
|
if err = orm.Raw(sql, o.RelationId, o.RelationId).QueryRow(&score); err != nil {
|
|
|
if err = orm.Raw(sql, o.RelationId, o.BeginTime.Unix(), o.EndTime.Unix(), o.RelationId, o.BeginTime.Unix(), o.EndTime.Unix()).QueryRow(&score); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
...
|
...
|
@@ -264,15 +285,15 @@ select user_grasp_score from achievement_provider where user_company_id=? |
|
|
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=?
|
|
|
select user_grasp_score from achievement where department_id=? and UNIX_TIMESTAMP(create_at) >=? and UNIX_TIMESTAMP(create_at)<?
|
|
|
UNION ALL
|
|
|
select user_grasp_score from achievement_provider where department_id=?
|
|
|
select user_grasp_score from achievement_provider where department_id=? and UNIX_TIMESTAMP(create_at) >=? and UNIX_TIMESTAMP(create_at)<?
|
|
|
) a
|
|
|
`
|
|
|
score float64
|
|
|
)
|
|
|
orm := orm.NewOrm()
|
|
|
if err = orm.Raw(sql, o.RelationId, o.RelationId).QueryRow(&score); err != nil {
|
|
|
if err = orm.Raw(sql, o.RelationId, o.BeginTime.Unix(), o.EndTime.Unix(), o.RelationId, o.BeginTime.Unix(), o.EndTime.Unix()).QueryRow(&score); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
...
|
...
|
@@ -285,12 +306,12 @@ type RankComment struct{} |
|
|
|
|
|
func (rank RankComment) RankUser(o RankOption) (err error, result RankResult) {
|
|
|
var (
|
|
|
sql = `select count(0) total from comment where user_id=?
|
|
|
sql = `select count(0) total from comment where user_id=? and UNIX_TIMESTAMP(create_at) >=? and UNIX_TIMESTAMP(create_at)<?
|
|
|
`
|
|
|
total int
|
|
|
)
|
|
|
orm := orm.NewOrm()
|
|
|
if err = orm.Raw(sql, o.RelationId).QueryRow(&total); err != nil {
|
|
|
if err = orm.Raw(sql, o.RelationId, o.BeginTime.Unix(), o.EndTime.Unix()).QueryRow(&total); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
...
|
...
|
@@ -299,7 +320,7 @@ func (rank RankComment) RankUser(o RankOption) (err error, result RankResult) { |
|
|
}
|
|
|
func (rank RankComment) RankDepartment(o RankOption) (err error, result RankResult) {
|
|
|
var (
|
|
|
sql = `select count(0) total from comment where user_id in (%v)
|
|
|
sql = `select count(0) total from comment where user_id in (%v) and UNIX_TIMESTAMP(create_at) >=%v and UNIX_TIMESTAMP(create_at)<%v
|
|
|
`
|
|
|
sqlUserDepartment = `select user_company_id from user_department where department_id=?`
|
|
|
total int
|
...
|
...
|
@@ -313,7 +334,7 @@ func (rank RankComment) RankDepartment(o RankOption) (err error, result RankResu |
|
|
if len(userIds) == 0 {
|
|
|
return
|
|
|
}
|
|
|
sql = fmt.Sprintf(sql, utils.JoinInts(userIds, ","))
|
|
|
sql = fmt.Sprintf(sql, utils.JoinInts(userIds, ","), o.BeginTime.Unix(), o.EndTime.Unix())
|
|
|
if err = orm.Raw(sql).QueryRow(&total); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
...
|
...
|
|