作者 yangfu

排榜作业统计

@@ -132,3 +132,10 @@ func Profiling(msg string) func() { @@ -132,3 +132,10 @@ func Profiling(msg string) func() {
132 log.Info(fmt.Sprintf("%s[%s]:%s", msg, "use", time.Since(start))) 132 log.Info(fmt.Sprintf("%s[%s]:%s", msg, "use", time.Since(start)))
133 } 133 }
134 } 134 }
  135 +
  136 +//合并字典
  137 +func MergeMap(to map[string]interface{}, from map[string]interface{}) {
  138 + for k, v := range from {
  139 + to[k] = v
  140 + }
  141 +}
@@ -50,3 +50,11 @@ func TestValidVersion(t *testing.T) { @@ -50,3 +50,11 @@ func TestValidVersion(t *testing.T) {
50 } 50 }
51 } 51 }
52 } 52 }
  53 +
  54 +func TestMergeMap(t *testing.T) {
  55 + to := map[string]interface{}{"1": 1, "2": 2}
  56 + from := map[string]interface{}{"3": 3, "4": 4}
  57 + t.Log("merge 前:", to)
  58 + MergeMap(to, from)
  59 + t.Log("merge 后:", to)
  60 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "time"
  6 +
  7 + "github.com/astaxie/beego/orm"
  8 +)
  9 +
  10 +type Rank struct {
  11 + Id int `orm:"column(id);auto"`
  12 + CompanyId int `orm:"column(company_id)" description:"公司编号 表company.id"`
  13 + RankTypeId int `orm:"column(rank_type_id)" description:"榜单类型编号"`
  14 + RankRangeId int `orm:"column(rank_range_id)" description:"榜单范围编号"`
  15 + RankPeriodId int `orm:"column(rank_period_id)" description:"赛季周期编号"`
  16 + RelationId int64 `orm:"column(relation_id)" description:"用户编号/部门编号"`
  17 + TotalScore float64 `orm:"column(total_score);null;digits(4);decimals(1)" description:"总分"`
  18 + DiscoveryScore float64 `orm:"column(discovery_score);null;digits(4);decimals(1)" description:"发现得分"`
  19 + GraspScore float64 `orm:"column(grasp_score);null;digits(4);decimals(1)" description:"把握分"`
  20 + DiscoveryTotal int `orm:"column(discovery_total);null" description:"发现数量"`
  21 + CommentTotal int `orm:"column(comment_total);null" description:"评论数量"`
  22 + CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
  23 + UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
  24 + EnableStatus int8 `orm:"column(enable_status);null" description:"有效状态 0:无效 1:有效 "`
  25 + Type int8 `orm:"column(type);null" description:"1:所有员工 2:指定员工 3:所有部门 4:指定部门"`
  26 +}
  27 +
  28 +func (t *Rank) TableName() string {
  29 + return "rank"
  30 +}
  31 +
  32 +func init() {
  33 + orm.RegisterModel(new(Rank))
  34 +}
  35 +
  36 +// AddRank insert a new Rank into database and returns
  37 +// last inserted Id on success.
  38 +func AddRank(m *Rank) (id int64, err error) {
  39 + o := orm.NewOrm()
  40 + id, err = o.Insert(m)
  41 + return
  42 +}
  43 +
  44 +// GetRankById retrieves Rank by Id. Returns error if
  45 +// Id doesn't exist
  46 +func GetRankById(id int) (v *Rank, err error) {
  47 + o := orm.NewOrm()
  48 + v = &Rank{Id: id}
  49 + if err = o.Read(v); err == nil {
  50 + return v, nil
  51 + }
  52 + return nil, err
  53 +}
  54 +
  55 +// UpdateRank updates Rank by Id and returns error if
  56 +// the record to be updated doesn't exist
  57 +func UpdateRankById(m *Rank) (err error) {
  58 + o := orm.NewOrm()
  59 + v := Rank{Id: m.Id}
  60 + // ascertain id exists in the database
  61 + if err = o.Read(&v); err == nil {
  62 + var num int64
  63 + if num, err = o.Update(m); err == nil {
  64 + fmt.Println("Number of records updated in database:", num)
  65 + }
  66 + }
  67 + return
  68 +}
  69 +
  70 +//取用户当前榜单数据
  71 +func GetRank(companyId, rankTypeId, rankRangeId, rankPeriodId int, relationId int64) (v *Rank, err error) {
  72 + o := orm.NewOrm()
  73 + sql := "select * from rank where company_id=? and rank_type_id=? and rank_period_id=? and relation_id=?"
  74 + if err = o.Raw(sql, companyId, rankTypeId, rankRangeId, rankPeriodId, relationId).QueryRow(v); err == nil {
  75 + return v, nil
  76 + }
  77 + return nil, err
  78 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "time"
  6 +
  7 + "github.com/astaxie/beego/orm"
  8 +)
  9 +
  10 +type RankItem struct {
  11 + Id int `orm:"column(id);auto"`
  12 + CompanyId int `orm:"column(company_id);null" description:"公司编号 company.id"`
  13 + RankTypeId int `orm:"column(rank_type_id)" description:"表rank_type.id 榜单类型编号"`
  14 + CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
  15 + UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
  16 + SortNum int `orm:"column(sort_num);null" description:"序号"`
  17 + ItemName string `orm:"column(item_name);size(50);null" description:"评比项名称"`
  18 + ItemKey string `orm:"column(item_key);size(50);null" description:"评比项键值(排行榜排序使用)"`
  19 +}
  20 +
  21 +func (t *RankItem) TableName() string {
  22 + return "rank_item"
  23 +}
  24 +
  25 +func init() {
  26 + orm.RegisterModel(new(RankItem))
  27 +}
  28 +
  29 +// AddRankItem insert a new RankItem into database and returns
  30 +// last inserted Id on success.
  31 +func AddRankItem(m *RankItem) (id int64, err error) {
  32 + o := orm.NewOrm()
  33 + id, err = o.Insert(m)
  34 + return
  35 +}
  36 +
  37 +// GetRankItemById retrieves RankItem by Id. Returns error if
  38 +// Id doesn't exist
  39 +func GetRankItemById(id int) (v *RankItem, err error) {
  40 + o := orm.NewOrm()
  41 + v = &RankItem{Id: id}
  42 + if err = o.Read(v); err == nil {
  43 + return v, nil
  44 + }
  45 + return nil, err
  46 +}
  47 +
  48 +// UpdateRankItem updates RankItem by Id and returns error if
  49 +// the record to be updated doesn't exist
  50 +func UpdateRankItemById(m *RankItem) (err error) {
  51 + o := orm.NewOrm()
  52 + v := RankItem{Id: m.Id}
  53 + // ascertain id exists in the database
  54 + if err = o.Read(&v); err == nil {
  55 + var num int64
  56 + if num, err = o.Update(m); err == nil {
  57 + fmt.Println("Number of records updated in database:", num)
  58 + }
  59 + }
  60 + return
  61 +}
  62 +
  63 +// DeleteRankItem deletes RankItem by Id and returns error if
  64 +// the record to be deleted doesn't exist
  65 +func DeleteRankItem(id int) (err error) {
  66 + o := orm.NewOrm()
  67 + v := RankItem{Id: id}
  68 + // ascertain id exists in the database
  69 + if err = o.Read(&v); err == nil {
  70 + var num int64
  71 + if num, err = o.Delete(&RankItem{Id: id}); err == nil {
  72 + fmt.Println("Number of records deleted in database:", num)
  73 + }
  74 + }
  75 + return
  76 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
  6 + "opp/internal/utils"
  7 + "time"
  8 +
  9 + "github.com/astaxie/beego/orm"
  10 +)
  11 +
  12 +type RankPeriod struct {
  13 + Id int `orm:"column(id);auto" description:"主键"`
  14 + CompanyId int `orm:"column(company_id);null" description:"公司编号 表company.id"`
  15 + RankTypeId int `orm:"column(rank_type_id)" description:"表rank_type.id 榜单类型编号"`
  16 + SeasonName string `orm:"column(season_name);size(50);null" description:"赛季名称"`
  17 + BeginTime time.Time `orm:"column(begin_time);type(timestamp);null" description:"开始时间"`
  18 + EndTime time.Time `orm:"column(end_time);type(timestamp);null" description:"结束时间"`
  19 + CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
  20 + UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
  21 + Status int8 `orm:"column(status);null" description:"状态 0:未开始 1:开始 2:结束 "`
  22 + RankRangeBack string `orm:"column(rank_range_back);null" description:"赛季结束时,备份赛季参与人设置"`
  23 + RankItemBack string `orm:"column(rank_item_back);null" description:"赛季结束时,备份赛季评比项设置"`
  24 +}
  25 +
  26 +func (t *RankPeriod) TableName() string {
  27 + return "rank_period"
  28 +}
  29 +
  30 +func init() {
  31 + orm.RegisterModel(new(RankPeriod))
  32 +}
  33 +
  34 +// AddRankPeriod insert a new RankPeriod into database and returns
  35 +// last inserted Id on success.
  36 +func AddRankPeriod(m *RankPeriod) (id int64, err error) {
  37 + o := orm.NewOrm()
  38 + id, err = o.Insert(m)
  39 + return
  40 +}
  41 +
  42 +// GetRankPeriodById retrieves RankPeriod by Id. Returns error if
  43 +// Id doesn't exist
  44 +func GetRankPeriodById(id int) (v *RankPeriod, err error) {
  45 + o := orm.NewOrm()
  46 + v = &RankPeriod{Id: id}
  47 + if err = o.Read(v); err == nil {
  48 + return v, nil
  49 + }
  50 + return nil, err
  51 +}
  52 +
  53 +// UpdateRankPeriod updates RankPeriod by Id and returns error if
  54 +// the record to be updated doesn't exist
  55 +func UpdateRankPeriodById(m *RankPeriod) (err error) {
  56 + o := orm.NewOrm()
  57 + v := RankPeriod{Id: m.Id}
  58 + // ascertain id exists in the database
  59 + if err = o.Read(&v); err == nil {
  60 + var num int64
  61 + if num, err = o.Update(m); err == nil {
  62 + fmt.Println("Number of records updated in database:", num)
  63 + }
  64 + }
  65 + return
  66 +}
  67 +
  68 +// DeleteRankPeriod deletes RankPeriod by Id and returns error if
  69 +// the record to be deleted doesn't exist
  70 +func DeleteRankPeriod(id int) (err error) {
  71 + o := orm.NewOrm()
  72 + v := RankPeriod{Id: id}
  73 + // ascertain id exists in the database
  74 + if err = o.Read(&v); err == nil {
  75 + var num int64
  76 + if num, err = o.Delete(&RankPeriod{Id: id}); err == nil {
  77 + fmt.Println("Number of records deleted in database:", num)
  78 + }
  79 + }
  80 + return
  81 +}
  82 +
  83 +//获取进行中的赛季周期 状态 0:未开始 1:开始 2:结束
  84 +func GetRankPeriods(status []int) (v []*RankPeriod, err error) {
  85 + sql := mybeego.NewSqlExutor()
  86 + sql.Table((&RankPeriod{}).TableName())
  87 + if len(status) > 0 {
  88 + sql.Where(fmt.Sprintf("status in (%v)", utils.JoinInts(status, ",")))
  89 + }
  90 + sql.Order("begin_time")
  91 + _, err = sql.Querys(&v)
  92 + return
  93 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
  6 + "time"
  7 +
  8 + "github.com/astaxie/beego/orm"
  9 +)
  10 +
  11 +type RankRange struct {
  12 + Id int `orm:"column(id);auto"`
  13 + CompanyId int `orm:"column(company_id);null" description:"公司编号 表company.id"`
  14 + RankTypeId int `orm:"column(rank_type_id)" description:"表rank_type.id 榜单类型编号"`
  15 + Name string `orm:"column(name);size(50);null" description:"名称"`
  16 + Type int8 `orm:"column(type);null" description:"1:所有员工 2:指定员工 3:所有部门 4:指定部门"`
  17 + SortNum int `orm:"column(sort_num);null" description:"序号"`
  18 + CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
  19 + UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
  20 + Status int8 `orm:"column(status);null" description:"【0:显示】【1:隐藏】"`
  21 + Data string `orm:"column(data);null"`
  22 +}
  23 +
  24 +func (t *RankRange) TableName() string {
  25 + return "rank_range"
  26 +}
  27 +
  28 +func init() {
  29 + orm.RegisterModel(new(RankRange))
  30 +}
  31 +
  32 +// AddRankRange insert a new RankRange into database and returns
  33 +// last inserted Id on success.
  34 +func AddRankRange(m *RankRange) (id int64, err error) {
  35 + o := orm.NewOrm()
  36 + id, err = o.Insert(m)
  37 + return
  38 +}
  39 +
  40 +// GetRankRangeById retrieves RankRange by Id. Returns error if
  41 +// Id doesn't exist
  42 +func GetRankRangeById(id int) (v *RankRange, err error) {
  43 + o := orm.NewOrm()
  44 + v = &RankRange{Id: id}
  45 + if err = o.Read(v); err == nil {
  46 + return v, nil
  47 + }
  48 + return nil, err
  49 +}
  50 +
  51 +// UpdateRankRange updates RankRange by Id and returns error if
  52 +// the record to be updated doesn't exist
  53 +func UpdateRankRangeById(m *RankRange) (err error) {
  54 + o := orm.NewOrm()
  55 + v := RankRange{Id: m.Id}
  56 + // ascertain id exists in the database
  57 + if err = o.Read(&v); err == nil {
  58 + var num int64
  59 + if num, err = o.Update(m); err == nil {
  60 + fmt.Println("Number of records updated in database:", num)
  61 + }
  62 + }
  63 + return
  64 +}
  65 +
  66 +// DeleteRankRange deletes RankRange by Id and returns error if
  67 +// the record to be deleted doesn't exist
  68 +func DeleteRankRange(id int) (err error) {
  69 + o := orm.NewOrm()
  70 + v := RankRange{Id: id}
  71 + // ascertain id exists in the database
  72 + if err = o.Read(&v); err == nil {
  73 + var num int64
  74 + if num, err = o.Delete(&RankRange{Id: id}); err == nil {
  75 + fmt.Println("Number of records deleted in database:", num)
  76 + }
  77 + }
  78 + return
  79 +}
  80 +
  81 +//获取榜单范围数据
  82 +func GetRankRanges(companyId int) (v []*RankRange, err error) {
  83 + sql := mybeego.NewSqlExutor()
  84 + sql.Table((&RankRange{}).TableName())
  85 + if companyId > 0 {
  86 + sql.Where(fmt.Sprintf("companyId=%v", companyId))
  87 + }
  88 + sql.Order("sort_num")
  89 + _, err = sql.Querys(&v)
  90 + return
  91 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/astaxie/beego/orm"
  6 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
  7 +)
  8 +
  9 +type RankRangeData struct {
  10 + Id int `orm:"column(id);auto"`
  11 + RankRangeId int `orm:"column(rank_range_id);null"`
  12 + RangeType string `orm:"column(range_type);size(255);null" description:"类型"`
  13 + RelationId int `orm:"column(relation_id);null"`
  14 +}
  15 +
  16 +func (t *RankRangeData) TableName() string {
  17 + return "rank_range_data"
  18 +}
  19 +
  20 +func init() {
  21 + orm.RegisterModel(new(RankRangeData))
  22 +}
  23 +
  24 +// AddRankRangeData insert a new RankRangeData into database and returns
  25 +// last inserted Id on success.
  26 +func AddRankRangeData(m *RankRangeData) (id int64, err error) {
  27 + o := orm.NewOrm()
  28 + id, err = o.Insert(m)
  29 + return
  30 +}
  31 +
  32 +// GetRankRangeDataById retrieves RankRangeData by Id. Returns error if
  33 +// Id doesn't exist
  34 +func GetRankRangeDataById(id int) (v *RankRangeData, err error) {
  35 + o := orm.NewOrm()
  36 + v = &RankRangeData{Id: id}
  37 + if err = o.Read(v); err == nil {
  38 + return v, nil
  39 + }
  40 + return nil, err
  41 +}
  42 +
  43 +// UpdateRankRangeData updates RankRangeData by Id and returns error if
  44 +// the record to be updated doesn't exist
  45 +func UpdateRankRangeDataById(m *RankRangeData) (err error) {
  46 + o := orm.NewOrm()
  47 + v := RankRangeData{Id: m.Id}
  48 + // ascertain id exists in the database
  49 + if err = o.Read(&v); err == nil {
  50 + var num int64
  51 + if num, err = o.Update(m); err == nil {
  52 + fmt.Println("Number of records updated in database:", num)
  53 + }
  54 + }
  55 + return
  56 +}
  57 +
  58 +// DeleteRankRangeData deletes RankRangeData by Id and returns error if
  59 +// the record to be deleted doesn't exist
  60 +func DeleteRankRangeData(id int) (err error) {
  61 + o := orm.NewOrm()
  62 + v := RankRangeData{Id: id}
  63 + // ascertain id exists in the database
  64 + if err = o.Read(&v); err == nil {
  65 + var num int64
  66 + if num, err = o.Delete(&RankRangeData{Id: id}); err == nil {
  67 + fmt.Println("Number of records deleted in database:", num)
  68 + }
  69 + }
  70 + return
  71 +}
  72 +
  73 +//参榜范围列表
  74 +func GetRankRangeDataList(rankRangeId int) (v []*RankRangeData, err error) {
  75 + sql := mybeego.NewSqlExutor()
  76 + sql.Table((&RankRangeData{}).TableName())
  77 + if rankRangeId > 0 {
  78 + sql.Where(fmt.Sprintf("rank_range_id=%v", rankRangeId))
  79 + }
  80 + sql.Order("relation_id")
  81 + _, err = sql.Querys(&v)
  82 + return
  83 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "time"
  6 +
  7 + "github.com/astaxie/beego/orm"
  8 +)
  9 +
  10 +type RankType struct {
  11 + Id int `orm:"column(id);auto"`
  12 + Name string `orm:"column(name);size(50)" description:"榜单名称"`
  13 + CompanyId int `orm:"column(company_id)" description:"公司编号 company.id"`
  14 + CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
  15 + UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
  16 + EnableStatus int8 `orm:"column(enable_status);null" description:"是否有效 【1:禁用】【2:启用】"`
  17 + Type int8 `orm:"column(type);null" description:"榜单类型(1赛季榜、2年榜)"`
  18 +}
  19 +
  20 +func (t *RankType) TableName() string {
  21 + return "rank_type"
  22 +}
  23 +
  24 +func init() {
  25 + orm.RegisterModel(new(RankType))
  26 +}
  27 +
  28 +// AddRankType insert a new RankType into database and returns
  29 +// last inserted Id on success.
  30 +func AddRankType(m *RankType) (id int64, err error) {
  31 + o := orm.NewOrm()
  32 + id, err = o.Insert(m)
  33 + return
  34 +}
  35 +
  36 +// GetRankTypeById retrieves RankType by Id. Returns error if
  37 +// Id doesn't exist
  38 +func GetRankTypeById(id int) (v *RankType, err error) {
  39 + o := orm.NewOrm()
  40 + v = &RankType{Id: id}
  41 + if err = o.Read(v); err == nil {
  42 + return v, nil
  43 + }
  44 + return nil, err
  45 +}
  46 +
  47 +// UpdateRankType updates RankType by Id and returns error if
  48 +// the record to be updated doesn't exist
  49 +func UpdateRankTypeById(m *RankType) (err error) {
  50 + o := orm.NewOrm()
  51 + v := RankType{Id: m.Id}
  52 + // ascertain id exists in the database
  53 + if err = o.Read(&v); err == nil {
  54 + var num int64
  55 + if num, err = o.Update(m); err == nil {
  56 + fmt.Println("Number of records updated in database:", num)
  57 + }
  58 + }
  59 + return
  60 +}
  61 +
  62 +// DeleteRankType deletes RankType by Id and returns error if
  63 +// the record to be deleted doesn't exist
  64 +func DeleteRankType(id int) (err error) {
  65 + o := orm.NewOrm()
  66 + v := RankType{Id: id}
  67 + // ascertain id exists in the database
  68 + if err = o.Read(&v); err == nil {
  69 + var num int64
  70 + if num, err = o.Delete(&RankType{Id: id}); err == nil {
  71 + fmt.Println("Number of records deleted in database:", num)
  72 + }
  73 + }
  74 + return
  75 +}
@@ -7,6 +7,12 @@ const ( @@ -7,6 +7,12 @@ const (
7 RankRangeTypeAllSpecifyDepartment = 4 7 RankRangeTypeAllSpecifyDepartment = 4
8 ) 8 )
9 9
  10 +const (
  11 + RankPeriodWaiting = iota
  12 + RankPeriodBegin
  13 + RankPeriodEnd
  14 +)
  15 +
10 /*GetRankList 排行榜*/ 16 /*GetRankList 排行榜*/
11 type GetRankListRequest struct { 17 type GetRankListRequest struct {
12 RankTypeId int `json:"rankTypeId" valid:"Required"` //榜单类型编号(赛季榜、年榜) 18 RankTypeId int `json:"rankTypeId" valid:"Required"` //榜单类型编号(赛季榜、年榜)
@@ -16,14 +22,14 @@ type GetRankListRequest struct { @@ -16,14 +22,14 @@ type GetRankListRequest struct {
16 PageSize int `json:"pageSize" valid:"Required"` //每页数量 22 PageSize int `json:"pageSize" valid:"Required"` //每页数量
17 } 23 }
18 type GetRankListResponse struct { 24 type GetRankListResponse struct {
19 - Self RankItem `json:"self"` //自己或所在部门的排名分数  
20 - Lists []RankItem `json:"lists"` //排名列表 25 + Self []RankItem `json:"self"` //自己或所在部门的排名分数
  26 + Lists [][]RankItem `json:"lists"` //排名列表
21 } 27 }
22 28
23 type RankItem struct { 29 type RankItem struct {
24 - Name string `json:"name,omitempty"` //名称  
25 - Score string `json:"score"` //分数  
26 - Ranking string `json:"ranking"` //排名 30 + Name string `json:"name,omitempty"` //名称
  31 + Score float64 `json:"score"` //分数
  32 + Ranking string `json:"ranking"` //排名
27 } 33 }
28 34
29 /*GetRankType */ 35 /*GetRankType */
1 package contrab 1 package contrab
2 2
3 import ( 3 import (
  4 + "encoding/json"
4 "fmt" 5 "fmt"
  6 + "github.com/astaxie/beego/orm"
5 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" 7 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
6 "opp/internal/utils" 8 "opp/internal/utils"
  9 + "opp/models"
  10 + "opp/protocol"
  11 + "reflect"
7 "sync/atomic" 12 "sync/atomic"
8 "time" 13 "time"
9 ) 14 )
10 15
11 var ComputeRankScoreFlag int32 16 var ComputeRankScoreFlag int32
  17 +var rankList = initRankList()
  18 +
  19 +func initRankList() []Rank {
  20 + var list []Rank
  21 + list = append(list, RankDiscovery{})
  22 + list = append(list, RankGrasp{})
  23 + list = append(list, RankComment{})
  24 + return list
  25 +}
12 26
13 type Rank interface { 27 type Rank interface {
14 RankUser(o RankOption) (error, RankResult) 28 RankUser(o RankOption) (error, RankResult)
@@ -16,16 +30,17 @@ type Rank interface { @@ -16,16 +30,17 @@ type Rank interface {
16 } 30 }
17 31
18 type RankOption struct { 32 type RankOption struct {
19 - Type int //RankRangeType  
20 - ObjectId int64 //对象id 用户编号 /部门编号  
21 - BeginTime time.Time  
22 - EndTime time.Time  
23 - //*model.user_rank  
24 -}  
25 -  
26 -type RankResult struct {  
27 - //Params map[string]interface{} 33 + CompanyId int //公司编号
  34 + RankTypeId int //榜单类型编号
  35 + RankRangeId int //榜单范围编号
  36 + RankPeriodId int //赛季周期编号
  37 + Type int //Relation类型
  38 + RelationId int64 //对象id 用户编号 /部门编号
  39 + BeginTime time.Time
  40 + EndTime time.Time
  41 + Rank *models.Rank //排行榜
28 } 42 }
  43 +type RankResult struct{}
29 44
30 //计算排行分 45 //计算排行分
31 func ComputeRankScore() error { 46 func ComputeRankScore() error {
@@ -45,15 +60,52 @@ func ComputeRankScore() error { @@ -45,15 +60,52 @@ func ComputeRankScore() error {
45 60
46 return computeRankScore() 61 return computeRankScore()
47 } 62 }
48 -  
49 -func computeRankScore() error { 63 +func computeRankScore() (err error) {
50 var ( 64 var (
51 - //rankList []Rank = initRankList() 65 + periods []*models.RankPeriod
  66 + rankRanges []*models.RankRange
  67 + rankRangeDatas []*models.RankRangeData
52 ) 68 )
53 - //1.从rank_period 查询进行中的赛季 69 + defer func() {
  70 + if p := recover(); p != nil {
  71 + log.Error(p)
  72 + }
  73 + }()
  74 + //1.l 查询进行中的赛季
  75 + if periods, err = models.GetRankPeriods([]int{protocol.RankPeriodBegin}); err != nil {
  76 + return
  77 + }
54 //2.查询对应 rank_type_id 的rank_data 78 //2.查询对应 rank_type_id 的rank_data
55 - //按类型  
56 - //3.查询用户列表/部门列表 79 + for i := range periods {
  80 + period := periods[i]
  81 + if rankRanges, err = models.GetRankRanges(period.CompanyId); err == orm.ErrNoRows {
  82 + continue
  83 + }
  84 + //所有部门
  85 +
  86 + //所有用户
  87 +
  88 + //按类型
  89 + //3.查询用户列表/部门列表
  90 + for j := range rankRanges {
  91 + var relationIds []int64
  92 + rankRange := rankRanges[j]
  93 + if rankRange.Type == protocol.RankRangeTypeAllCompanyUser {
  94 +
  95 + } else if rankRange.Type == protocol.RankRangeTypeAllCompanyDepartment {
  96 +
  97 + } else {
  98 + if rankRangeDatas, err = models.GetRankRangeDataList(rankRange.Id); err == orm.ErrNoRows {
  99 + continue
  100 + }
  101 + }
  102 + for k := range rankRangeDatas {
  103 + relationIds = append(relationIds, int64(rankRangeDatas[k].RelationId))
  104 + }
  105 + updateRankByRelationIds(relationIds, period, rankRange)
  106 + }
  107 + }
  108 +
57 //4.调用统计接口列表 109 //4.调用统计接口列表
58 //5.汇聚所有的rankResult 110 //5.汇聚所有的rankResult
59 //6.查询当前user_rank是否有这条记录,比较记录里面数据是否有变化 111 //6.查询当前user_rank是否有这条记录,比较记录里面数据是否有变化
@@ -61,6 +113,211 @@ func computeRankScore() error { @@ -61,6 +113,211 @@ func computeRankScore() error {
61 return nil 113 return nil
62 } 114 }
63 115
64 -func initRankList() []Rank {  
65 - return []Rank{} 116 +//更新排行榜按关联id
  117 +func updateRankByRelationIds(relationIds []int64, period *models.RankPeriod, rankRange *models.RankRange) {
  118 + var option RankOption
  119 + for i := range relationIds {
  120 + option = RankOption{
  121 + CompanyId: period.CompanyId,
  122 + RankTypeId: period.RankTypeId,
  123 + RankRangeId: rankRange.Id,
  124 + RankPeriodId: period.Id,
  125 + RelationId: relationIds[i],
  126 + Type: int(rankRange.Type),
  127 + }
  128 + updateRank(option)
  129 + }
  130 + updateTotalScore(option)
  131 +}
  132 +
  133 +//更新榜单数据
  134 +func updateRank(o RankOption) {
  135 + var (
  136 + rank *models.Rank
  137 + e error
  138 + isNew = true
  139 + rankTmp models.Rank
  140 + )
  141 + if rank, e = models.GetRank(o.CompanyId, o.RankTypeId, o.RankRangeId, o.RankPeriodId, o.RelationId); e != nil && e == orm.ErrNoRows {
  142 + isNew = true
  143 + rank = &models.Rank{
  144 + CompanyId: o.CompanyId,
  145 + RankTypeId: o.RankTypeId,
  146 + RankRangeId: o.RankRangeId,
  147 + RankPeriodId: o.RankPeriodId,
  148 + RelationId: o.RelationId,
  149 + UpdateAt: time.Now(),
  150 + CreateAt: time.Now(),
  151 + EnableStatus: protocol.Valid,
  152 + Type: int8(o.Type),
  153 + }
  154 + } else {
  155 + rankTmp = *rank
  156 + }
  157 + for i := range rankList {
  158 + item := rankList[i]
  159 + if o.Type == protocol.RankRangeTypeAllCompanyDepartment || o.Type == protocol.RankRangeTypeAllSpecifyDepartment {
  160 + item.RankDepartment(o)
  161 + continue
  162 + }
  163 + item.RankUser(o)
  164 + }
  165 + if isNew {
  166 + if _, e = models.AddRank(rank); e != nil {
  167 + log.Error(e)
  168 + }
  169 + } else {
  170 + //数据有变化的时候才更新到表
  171 + if !reflect.DeepEqual(rankTmp, *rank) {
  172 + if e = models.UpdateRankById(rank); e != nil {
  173 + log.Error(e)
  174 + }
  175 + }
  176 + }
  177 +
  178 +}
  179 +
  180 +//更新排行榜总分 //更新总分 系数*发现分 + 系数*把握分
  181 +func updateTotalScore(o RankOption) {
  182 + var (
  183 + config *models.SysConfig
  184 + err error
  185 + scoreConfig protocol.ScoreConfig
  186 + sql = `update rank set total_score=discovery_score*%v + grasp_score*%v where
  187 +company_id=%v and rank_type_id=%v and rank_period_id=%v`
  188 + )
  189 + if config, err = models.GetSysConfigByCompanyId(int(o.CompanyId), models.KeyScore); err != nil {
  190 + err = protocol.NewErrWithMessage(5510)
  191 + log.Error(err)
  192 + return
  193 + }
  194 + if err = json.Unmarshal([]byte(config.Content), &scoreConfig); err != nil {
  195 + err = protocol.NewErrWithMessage(5510)
  196 + log.Error(err)
  197 + return
  198 + }
  199 + sql = fmt.Sprintf(sql,
  200 + scoreConfig.SumScore.DiscoveryFactor,
  201 + scoreConfig.SumScore.CatchFactor, o.CompanyId, o.RankTypeId, o.RankPeriodId)
  202 + orm := orm.NewOrm()
  203 + if err := utils.ExecuteSQLWithOrmer(orm, sql); err != nil {
  204 + log.Error(err)
  205 + return
  206 + }
  207 +}
  208 +
  209 +//发现计算规则
  210 +type RankDiscovery struct{}
  211 +
  212 +func (rank RankDiscovery) RankUser(o RankOption) (err error, result RankResult) {
  213 + var (
  214 + sql = "select sum(discovery_score) score,count(0) total from chance where user_id =? and review_status=3"
  215 + score float64
  216 + total int
  217 + )
  218 + orm := orm.NewOrm()
  219 + if err = orm.Raw(sql, o.RelationId).QueryRow(&score, &total); err != nil {
  220 + log.Error(err)
  221 + return
  222 + }
  223 + o.Rank.DiscoveryScore = score
  224 + o.Rank.DiscoveryTotal = total
  225 + return
  226 +}
  227 +func (rank RankDiscovery) RankDepartment(o RankOption) (err error, result RankResult) {
  228 + var (
  229 + sql = "select sum(discovery_score) score,count(0) total from chance where department_id =? and review_status=3"
  230 + score float64
  231 + total int
  232 + )
  233 + orm := orm.NewOrm()
  234 + if err = orm.Raw(sql, o.RelationId).QueryRow(&score, &total); err != nil {
  235 + log.Error(err)
  236 + return
  237 + }
  238 + o.Rank.DiscoveryScore = score
  239 + o.Rank.DiscoveryTotal = total
  240 + return
  241 +}
  242 +
  243 +//把握分计算规则
  244 +type RankGrasp struct{}
  245 +
  246 +func (rank RankGrasp) RankUser(o RankOption) (err error, result RankResult) {
  247 + var (
  248 + sql = `select sum(a.user_grasp_score) score from(
  249 +select user_grasp_score from achievement where user_company_id=?
  250 +UNION ALL
  251 +select user_grasp_score from achievement_provider where user_company_id=?
  252 +) a
  253 +`
  254 + score float64
  255 + )
  256 + orm := orm.NewOrm()
  257 + if err = orm.Raw(sql, o.RelationId, o.RelationId).QueryRow(&score); err != nil {
  258 + log.Error(err)
  259 + return
  260 + }
  261 + o.Rank.GraspScore = score
  262 + return
  263 +}
  264 +func (rank RankGrasp) RankDepartment(o RankOption) (err error, result RankResult) {
  265 + var (
  266 + sql = `select sum(a.user_grasp_score) score from(
  267 +select user_grasp_score from achievement where department_id=?
  268 +UNION ALL
  269 +select user_grasp_score from achievement_provider where department_id=?
  270 +) a
  271 +`
  272 + score float64
  273 + )
  274 + orm := orm.NewOrm()
  275 + if err = orm.Raw(sql, o.RelationId, o.RelationId).QueryRow(&score); err != nil {
  276 + log.Error(err)
  277 + return
  278 + }
  279 + o.Rank.GraspScore = score
  280 + return
  281 +}
  282 +
  283 +//评论数计算规则
  284 +type RankComment struct{}
  285 +
  286 +func (rank RankComment) RankUser(o RankOption) (err error, result RankResult) {
  287 + var (
  288 + sql = `select count(0) total from comment where user_id=?
  289 +`
  290 + total int
  291 + )
  292 + orm := orm.NewOrm()
  293 + if err = orm.Raw(sql, o.RelationId).QueryRow(&total); err != nil {
  294 + log.Error(err)
  295 + return
  296 + }
  297 + o.Rank.CommentTotal = total
  298 + return
  299 +}
  300 +func (rank RankComment) RankDepartment(o RankOption) (err error, result RankResult) {
  301 + var (
  302 + sql = `select count(0) total from comment where user_id in (%v)
  303 +`
  304 + sqlUserDepartment = `select user_company_id from user_department where department_id=?`
  305 + total int
  306 + userIds []int
  307 + )
  308 + orm := orm.NewOrm()
  309 + if _, err = orm.Raw(sqlUserDepartment, o.RelationId).QueryRows(&userIds); err != nil {
  310 + log.Error(err)
  311 + return
  312 + }
  313 + if len(userIds) == 0 {
  314 + return
  315 + }
  316 + sql = fmt.Sprintf(sql, utils.JoinInts(userIds, ","))
  317 + if err = orm.Raw(sql).QueryRow(&total); err != nil {
  318 + log.Error(err)
  319 + return
  320 + }
  321 + o.Rank.CommentTotal = total
  322 + return
66 } 323 }