作者 yangfu

增加排行榜类型/周期/范围

... ... @@ -13,6 +13,7 @@ import (
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/websocket"
"opp/internal/utils"
_ "opp/routers"
"opp/services/contrab"
"os"
"strings"
"time"
... ... @@ -91,7 +92,7 @@ func main() {
//beego.BConfig.Listen.HTTPSPort = 8089
//beego.BConfig.Listen.HTTPSCertFile = "conf/server.crt"
//beego.BConfig.Listen.HTTPSKeyFile = "conf/server.key"
//contrab.Run()
contrab.Run()
beego.Run()
}
... ...
... ... @@ -100,3 +100,16 @@ select id from department where company_id=? and relation like '%v%%' and delet
}
return
}
func GetDepartmentIdsByCompanyId(companyId int) (v []int64, err error) {
o := orm.NewOrm()
sql := `
select id
from department
where company_id =? and delete_at =0
order by parent_id,id`
if _, err = o.Raw(sql, companyId).QueryRows(&v); err == nil {
return
}
return
}
... ...
... ... @@ -8,7 +8,7 @@ import (
)
type Rank struct {
Id int `orm:"column(id);auto"`
Id int64 `orm:"column(id);auto"`
CompanyId int `orm:"column(company_id)" description:"公司编号 表company.id"`
RankTypeId int `orm:"column(rank_type_id)" description:"榜单类型编号"`
RankRangeId int `orm:"column(rank_range_id)" description:"榜单范围编号"`
... ... @@ -43,7 +43,7 @@ func AddRank(m *Rank) (id int64, err error) {
// GetRankById retrieves Rank by Id. Returns error if
// Id doesn't exist
func GetRankById(id int) (v *Rank, err error) {
func GetRankById(id int64) (v *Rank, err error) {
o := orm.NewOrm()
v = &Rank{Id: id}
if err = o.Read(v); err == nil {
... ... @@ -70,8 +70,8 @@ func UpdateRankById(m *Rank) (err error) {
//取用户当前榜单数据
func GetRank(companyId, rankTypeId, rankRangeId, rankPeriodId int, relationId int64) (v *Rank, err error) {
o := orm.NewOrm()
sql := "select * from rank where company_id=? and rank_type_id=? and rank_period_id=? and relation_id=?"
if err = o.Raw(sql, companyId, rankTypeId, rankRangeId, rankPeriodId, relationId).QueryRow(v); err == nil {
sql := "select * from rank where company_id=? and rank_type_id=? and rank_range_id=? and rank_period_id=? and relation_id=?"
if err = o.Raw(sql, companyId, rankTypeId, rankRangeId, rankPeriodId, relationId).QueryRow(&v); err == nil {
return v, nil
}
return nil, err
... ...
... ... @@ -83,7 +83,7 @@ func GetRankRanges(companyId int) (v []*RankRange, err error) {
sql := mybeego.NewSqlExutor()
sql.Table((&RankRange{}).TableName())
if companyId > 0 {
sql.Where(fmt.Sprintf("companyId=%v", companyId))
sql.Where(fmt.Sprintf("company_id=%v", companyId))
}
sql.Order("sort_num")
_, err = sql.Querys(&v)
... ...
... ... @@ -2,19 +2,20 @@ package models
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
"time"
"github.com/astaxie/beego/orm"
)
type RankType struct {
Id int `orm:"column(id);auto"`
Name string `orm:"column(name);size(50)" description:"榜单名称"`
CompanyId int `orm:"column(company_id)" description:"公司编号 company.id"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
EnableStatus int8 `orm:"column(enable_status);null" description:"是否有效 【1:禁用】【2:启用】"`
Type int8 `orm:"column(type);null" description:"榜单类型(1赛季榜、2年榜)"`
Id int `orm:"column(id);auto" json:"id"`
Name string `orm:"column(name);size(50)" description:"榜单名称" json:"name"`
CompanyId int `orm:"column(company_id)" description:"公司编号 company.id" json:"-"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间" json:"-"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间" json:"-"`
EnableStatus int8 `orm:"column(enable_status);null" description:"是否有效 【1:禁用】【2:启用】" json:"-"`
Type int8 `orm:"column(type);null" description:"榜单类型(1赛季榜、2年榜) json:"type"`
}
func (t *RankType) TableName() string {
... ... @@ -73,3 +74,17 @@ func DeleteRankType(id int) (err error) {
}
return
}
//
func GetRankTypes(companyId int64) (v []*RankType, err error) {
sql := mybeego.NewSqlExutor()
sql.Table((&RankType{}).TableName())
if companyId > 0 {
sql.Where(fmt.Sprintf("company_id=%v", companyId))
}
sql.Where(fmt.Sprintf("enable_status=2"))
sql.Order("type")
sql.Limit(0, 2)
_, err = sql.Querys(&v)
return
}
... ...
... ... @@ -109,3 +109,12 @@ func GetUserCompanysFirst(uid int64) (v *UserCompany, err error) {
}
return nil, err
}
func GetUserCompanyIdAll(companyId int) (v []int64, err error) {
o := orm.NewOrm()
sql := "select id from user_company where company_id=? and enable=1" //and enable=1
if _, err = o.Raw(sql, companyId).QueryRows(&v); err == nil {
return v, nil
}
return nil, err
}
... ...
... ... @@ -9,7 +9,7 @@ type GetConfigScoreResponse struct {
type ScoreConfig struct {
DiscoveryScore *DiscoveryScore `json:"discoveryScore"`
SumScore *SumScore `json:"-"` //sumScore
SumScore *SumScore `json:"sumScore"` //sumScore
BasicScore *ScoreRange `json:"basicScore"`
ExtraScore *ScoreRange `json:"extraScore"`
ValueScore *ScoreRange `json:"valueScore"`
... ...
... ... @@ -36,7 +36,7 @@ type RankItem struct {
type GetRankTypeRequest struct {
}
type GetRankTypeResponse struct {
List []RankType
List []NameItem `json:"rankTypes"`
}
type RankType struct {
Id int `json:"id"`
... ... @@ -47,12 +47,19 @@ type RankType struct {
type GetRankRangeRequest struct {
}
type GetRankRangeResponse struct {
}
type RankRange struct {
List []NameItem `json:"rankRanges"`
}
/*GetRankPeriods 获取榜单竞争范围列表*/
type GetRankPeriodsRequest struct {
}
type GetRankPeriodsResponse struct {
List []RankPeriod `json:"rankPeriods"`
}
type RankPeriod struct {
Id int `json:"id"`
SeasonName string `json:"seasonName"`
BeginTime int64 `json:"beginTime"`
EndTime int64 `json:"endTime"`
}
... ...
... ... @@ -7,6 +7,8 @@ var (
)
func Run() {
//ComputeRankScore()
taskRank := toolbox.NewTask("taskComputeRankScore", taskComputeRankScore, ComputeRankScore)
toolbox.AddTask("taskComputeRankScore", taskRank)
... ...
... ... @@ -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
... ...
package rank
import "opp/protocol"
import (
"github.com/astaxie/beego/orm"
"github.com/prometheus/common/log"
"opp/models"
"opp/protocol"
)
//排行榜
func GetRankList(header *protocol.RequestHeader, request *protocol.GetRankListRequest) (rsp *protocol.GetRankListResponse, err error) {
... ... @@ -11,21 +16,66 @@ func GetRankList(header *protocol.RequestHeader, request *protocol.GetRankListRe
//获取榜单类型列表
func GetRankType(header *protocol.RequestHeader, request *protocol.GetRankTypeRequest) (rsp *protocol.GetRankTypeResponse, err error) {
var ()
var (
lists []*models.RankType
)
if lists, err = models.GetRankTypes(header.CompanyId); err != nil {
if err == orm.ErrNoRows {
err = nil
return
}
log.Error(err)
return
}
rsp = &protocol.GetRankTypeResponse{}
for i := range lists {
rsp.List = append(rsp.List, protocol.NameItem{Id: lists[i].Id, Name: lists[i].Name})
}
return
}
//获取榜单范围列表
func GetRankRange(header *protocol.RequestHeader, request *protocol.GetRankRangeRequest) (rsp *protocol.GetRankRangeResponse, err error) {
var ()
var (
lists []*models.RankRange
)
if lists, err = models.GetRankRanges(int(header.CompanyId)); err != nil {
if err == orm.ErrNoRows {
err = nil
return
}
log.Error(err)
return
}
rsp = &protocol.GetRankRangeResponse{}
for i := range lists {
rsp.List = append(rsp.List, protocol.NameItem{Id: lists[i].Id, Name: lists[i].Name})
}
return
}
//获取排行榜周期列表
func GetRankPeriods(header *protocol.RequestHeader, request *protocol.GetRankPeriodsRequest) (rsp *protocol.GetRankPeriodsResponse, err error) {
var ()
rsp = &protocol.GetRankPeriodsResponse{}
var (
lists []*models.RankPeriod
)
rsp = &protocol.GetRankPeriodsResponse{List: make([]protocol.RankPeriod, 0)}
if lists, err = models.GetRankPeriods([]int{protocol.RankPeriodBegin, protocol.RankPeriodEnd}); err != nil {
if err == orm.ErrNoRows {
err = nil
return
}
log.Error(err)
return
}
for i := range lists {
item := lists[i]
rsp.List = append(rsp.List, protocol.RankPeriod{
Id: item.Id,
SeasonName: item.SeasonName,
BeginTime: item.BeginTime.Unix() * 1000,
EndTime: item.EndTime.Unix() * 1000,
})
}
return
}
... ...