作者 yangfu

排榜作业统计

... ... @@ -132,3 +132,10 @@ func Profiling(msg string) func() {
log.Info(fmt.Sprintf("%s[%s]:%s", msg, "use", time.Since(start)))
}
}
//合并字典
func MergeMap(to map[string]interface{}, from map[string]interface{}) {
for k, v := range from {
to[k] = v
}
}
... ...
... ... @@ -50,3 +50,11 @@ func TestValidVersion(t *testing.T) {
}
}
}
func TestMergeMap(t *testing.T) {
to := map[string]interface{}{"1": 1, "2": 2}
from := map[string]interface{}{"3": 3, "4": 4}
t.Log("merge 前:", to)
MergeMap(to, from)
t.Log("merge 后:", to)
}
... ...
package models
import (
"fmt"
"time"
"github.com/astaxie/beego/orm"
)
type Rank struct {
Id int `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:"榜单范围编号"`
RankPeriodId int `orm:"column(rank_period_id)" description:"赛季周期编号"`
RelationId int64 `orm:"column(relation_id)" description:"用户编号/部门编号"`
TotalScore float64 `orm:"column(total_score);null;digits(4);decimals(1)" description:"总分"`
DiscoveryScore float64 `orm:"column(discovery_score);null;digits(4);decimals(1)" description:"发现得分"`
GraspScore float64 `orm:"column(grasp_score);null;digits(4);decimals(1)" description:"把握分"`
DiscoveryTotal int `orm:"column(discovery_total);null" description:"发现数量"`
CommentTotal int `orm:"column(comment_total);null" description:"评论数量"`
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:"有效状态 0:无效 1:有效 "`
Type int8 `orm:"column(type);null" description:"1:所有员工 2:指定员工 3:所有部门 4:指定部门"`
}
func (t *Rank) TableName() string {
return "rank"
}
func init() {
orm.RegisterModel(new(Rank))
}
// AddRank insert a new Rank into database and returns
// last inserted Id on success.
func AddRank(m *Rank) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetRankById retrieves Rank by Id. Returns error if
// Id doesn't exist
func GetRankById(id int) (v *Rank, err error) {
o := orm.NewOrm()
v = &Rank{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// UpdateRank updates Rank by Id and returns error if
// the record to be updated doesn't exist
func UpdateRankById(m *Rank) (err error) {
o := orm.NewOrm()
v := Rank{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num)
}
}
return
}
//取用户当前榜单数据
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 {
return v, nil
}
return nil, err
}
... ...
package models
import (
"fmt"
"time"
"github.com/astaxie/beego/orm"
)
type RankItem struct {
Id int `orm:"column(id);auto"`
CompanyId int `orm:"column(company_id);null" description:"公司编号 company.id"`
RankTypeId int `orm:"column(rank_type_id)" description:"表rank_type.id 榜单类型编号"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
SortNum int `orm:"column(sort_num);null" description:"序号"`
ItemName string `orm:"column(item_name);size(50);null" description:"评比项名称"`
ItemKey string `orm:"column(item_key);size(50);null" description:"评比项键值(排行榜排序使用)"`
}
func (t *RankItem) TableName() string {
return "rank_item"
}
func init() {
orm.RegisterModel(new(RankItem))
}
// AddRankItem insert a new RankItem into database and returns
// last inserted Id on success.
func AddRankItem(m *RankItem) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetRankItemById retrieves RankItem by Id. Returns error if
// Id doesn't exist
func GetRankItemById(id int) (v *RankItem, err error) {
o := orm.NewOrm()
v = &RankItem{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// UpdateRankItem updates RankItem by Id and returns error if
// the record to be updated doesn't exist
func UpdateRankItemById(m *RankItem) (err error) {
o := orm.NewOrm()
v := RankItem{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num)
}
}
return
}
// DeleteRankItem deletes RankItem by Id and returns error if
// the record to be deleted doesn't exist
func DeleteRankItem(id int) (err error) {
o := orm.NewOrm()
v := RankItem{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&RankItem{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
... ...
package models
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
"opp/internal/utils"
"time"
"github.com/astaxie/beego/orm"
)
type RankPeriod struct {
Id int `orm:"column(id);auto" description:"主键"`
CompanyId int `orm:"column(company_id);null" description:"公司编号 表company.id"`
RankTypeId int `orm:"column(rank_type_id)" description:"表rank_type.id 榜单类型编号"`
SeasonName string `orm:"column(season_name);size(50);null" description:"赛季名称"`
BeginTime time.Time `orm:"column(begin_time);type(timestamp);null" description:"开始时间"`
EndTime time.Time `orm:"column(end_time);type(timestamp);null" description:"结束时间"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
Status int8 `orm:"column(status);null" description:"状态 0:未开始 1:开始 2:结束 "`
RankRangeBack string `orm:"column(rank_range_back);null" description:"赛季结束时,备份赛季参与人设置"`
RankItemBack string `orm:"column(rank_item_back);null" description:"赛季结束时,备份赛季评比项设置"`
}
func (t *RankPeriod) TableName() string {
return "rank_period"
}
func init() {
orm.RegisterModel(new(RankPeriod))
}
// AddRankPeriod insert a new RankPeriod into database and returns
// last inserted Id on success.
func AddRankPeriod(m *RankPeriod) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetRankPeriodById retrieves RankPeriod by Id. Returns error if
// Id doesn't exist
func GetRankPeriodById(id int) (v *RankPeriod, err error) {
o := orm.NewOrm()
v = &RankPeriod{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// UpdateRankPeriod updates RankPeriod by Id and returns error if
// the record to be updated doesn't exist
func UpdateRankPeriodById(m *RankPeriod) (err error) {
o := orm.NewOrm()
v := RankPeriod{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num)
}
}
return
}
// DeleteRankPeriod deletes RankPeriod by Id and returns error if
// the record to be deleted doesn't exist
func DeleteRankPeriod(id int) (err error) {
o := orm.NewOrm()
v := RankPeriod{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&RankPeriod{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
//获取进行中的赛季周期 状态 0:未开始 1:开始 2:结束
func GetRankPeriods(status []int) (v []*RankPeriod, err error) {
sql := mybeego.NewSqlExutor()
sql.Table((&RankPeriod{}).TableName())
if len(status) > 0 {
sql.Where(fmt.Sprintf("status in (%v)", utils.JoinInts(status, ",")))
}
sql.Order("begin_time")
_, err = sql.Querys(&v)
return
}
... ...
package models
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
"time"
"github.com/astaxie/beego/orm"
)
type RankRange struct {
Id int `orm:"column(id);auto"`
CompanyId int `orm:"column(company_id);null" description:"公司编号 表company.id"`
RankTypeId int `orm:"column(rank_type_id)" description:"表rank_type.id 榜单类型编号"`
Name string `orm:"column(name);size(50);null" description:"名称"`
Type int8 `orm:"column(type);null" description:"1:所有员工 2:指定员工 3:所有部门 4:指定部门"`
SortNum int `orm:"column(sort_num);null" description:"序号"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
Status int8 `orm:"column(status);null" description:"【0:显示】【1:隐藏】"`
Data string `orm:"column(data);null"`
}
func (t *RankRange) TableName() string {
return "rank_range"
}
func init() {
orm.RegisterModel(new(RankRange))
}
// AddRankRange insert a new RankRange into database and returns
// last inserted Id on success.
func AddRankRange(m *RankRange) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetRankRangeById retrieves RankRange by Id. Returns error if
// Id doesn't exist
func GetRankRangeById(id int) (v *RankRange, err error) {
o := orm.NewOrm()
v = &RankRange{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// UpdateRankRange updates RankRange by Id and returns error if
// the record to be updated doesn't exist
func UpdateRankRangeById(m *RankRange) (err error) {
o := orm.NewOrm()
v := RankRange{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num)
}
}
return
}
// DeleteRankRange deletes RankRange by Id and returns error if
// the record to be deleted doesn't exist
func DeleteRankRange(id int) (err error) {
o := orm.NewOrm()
v := RankRange{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&RankRange{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
//获取榜单范围数据
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.Order("sort_num")
_, err = sql.Querys(&v)
return
}
... ...
package models
import (
"fmt"
"github.com/astaxie/beego/orm"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
)
type RankRangeData struct {
Id int `orm:"column(id);auto"`
RankRangeId int `orm:"column(rank_range_id);null"`
RangeType string `orm:"column(range_type);size(255);null" description:"类型"`
RelationId int `orm:"column(relation_id);null"`
}
func (t *RankRangeData) TableName() string {
return "rank_range_data"
}
func init() {
orm.RegisterModel(new(RankRangeData))
}
// AddRankRangeData insert a new RankRangeData into database and returns
// last inserted Id on success.
func AddRankRangeData(m *RankRangeData) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetRankRangeDataById retrieves RankRangeData by Id. Returns error if
// Id doesn't exist
func GetRankRangeDataById(id int) (v *RankRangeData, err error) {
o := orm.NewOrm()
v = &RankRangeData{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// UpdateRankRangeData updates RankRangeData by Id and returns error if
// the record to be updated doesn't exist
func UpdateRankRangeDataById(m *RankRangeData) (err error) {
o := orm.NewOrm()
v := RankRangeData{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num)
}
}
return
}
// DeleteRankRangeData deletes RankRangeData by Id and returns error if
// the record to be deleted doesn't exist
func DeleteRankRangeData(id int) (err error) {
o := orm.NewOrm()
v := RankRangeData{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&RankRangeData{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
//参榜范围列表
func GetRankRangeDataList(rankRangeId int) (v []*RankRangeData, err error) {
sql := mybeego.NewSqlExutor()
sql.Table((&RankRangeData{}).TableName())
if rankRangeId > 0 {
sql.Where(fmt.Sprintf("rank_range_id=%v", rankRangeId))
}
sql.Order("relation_id")
_, err = sql.Querys(&v)
return
}
... ...
package models
import (
"fmt"
"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年榜)"`
}
func (t *RankType) TableName() string {
return "rank_type"
}
func init() {
orm.RegisterModel(new(RankType))
}
// AddRankType insert a new RankType into database and returns
// last inserted Id on success.
func AddRankType(m *RankType) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetRankTypeById retrieves RankType by Id. Returns error if
// Id doesn't exist
func GetRankTypeById(id int) (v *RankType, err error) {
o := orm.NewOrm()
v = &RankType{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// UpdateRankType updates RankType by Id and returns error if
// the record to be updated doesn't exist
func UpdateRankTypeById(m *RankType) (err error) {
o := orm.NewOrm()
v := RankType{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num)
}
}
return
}
// DeleteRankType deletes RankType by Id and returns error if
// the record to be deleted doesn't exist
func DeleteRankType(id int) (err error) {
o := orm.NewOrm()
v := RankType{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&RankType{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
... ...
... ... @@ -7,6 +7,12 @@ const (
RankRangeTypeAllSpecifyDepartment = 4
)
const (
RankPeriodWaiting = iota
RankPeriodBegin
RankPeriodEnd
)
/*GetRankList 排行榜*/
type GetRankListRequest struct {
RankTypeId int `json:"rankTypeId" valid:"Required"` //榜单类型编号(赛季榜、年榜)
... ... @@ -16,14 +22,14 @@ type GetRankListRequest struct {
PageSize int `json:"pageSize" valid:"Required"` //每页数量
}
type GetRankListResponse struct {
Self RankItem `json:"self"` //自己或所在部门的排名分数
Lists []RankItem `json:"lists"` //排名列表
Self []RankItem `json:"self"` //自己或所在部门的排名分数
Lists [][]RankItem `json:"lists"` //排名列表
}
type RankItem struct {
Name string `json:"name,omitempty"` //名称
Score string `json:"score"` //分数
Ranking string `json:"ranking"` //排名
Name string `json:"name,omitempty"` //名称
Score float64 `json:"score"` //分数
Ranking string `json:"ranking"` //排名
}
/*GetRankType */
... ...
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
}
... ...