作者 yangfu

排行榜修改

@@ -139,3 +139,15 @@ func MergeMap(to map[string]interface{}, from map[string]interface{}) { @@ -139,3 +139,15 @@ func MergeMap(to map[string]interface{}, from map[string]interface{}) {
139 to[k] = v 139 to[k] = v
140 } 140 }
141 } 141 }
  142 +
  143 +func GetPageInfo(pageIndex, pageSize int) (offset, size int) {
  144 + if pageSize == 0 {
  145 + pageSize = 20
  146 + }
  147 + if pageIndex == 0 {
  148 + pageIndex = 1
  149 + }
  150 + offset = (pageIndex - 1) * pageSize
  151 + size = pageSize
  152 + return
  153 +}
@@ -2,6 +2,7 @@ package models @@ -2,6 +2,7 @@ package models
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "opp/internal/utils"
5 "time" 6 "time"
6 7
7 "github.com/astaxie/beego/orm" 8 "github.com/astaxie/beego/orm"
@@ -77,32 +78,33 @@ func GetRank(companyId, rankTypeId, rankRangeId, rankPeriodId int, relationId in @@ -77,32 +78,33 @@ func GetRank(companyId, rankTypeId, rankRangeId, rankPeriodId int, relationId in
77 return nil, err 78 return nil, err
78 } 79 }
79 80
80 -//func GetRanksByUser(companyId, rankTypeId, rankRangeId, rankPeriodId int, relationId int64,v interface{})(total int, err error) {  
81 -// //var filterDepartment string = getFilterSqlByDIds(dIds)  
82 -// sql := fmt.Sprintf(`select a.*,b.nick_name name from (  
83 -//select ranking,score,relation_id,user_id from(  
84 -//select a.discovery_score score,a.relation_id,(@rowno:=@rowno+1) as ranking,user_id from rank a inner join user_company c on a.relation_id=c.id,(select (@rowno:=0)) b  
85 -//where a.company_id=%v and rank_type_id=%v and rank_range_id=%v and rank_period_id=%v  
86 -//order by a.discovery_score desc,c.create_at asc  
87 -//) a  
88 -//#where relation_id=?  
89 -//limit %v,%v  
90 -//)a inner join user b on a.user_id = b.id  
91 -//`, companyId,rankTypeId,rankRangeId,rankPeriodId,)  
92 -//  
93 -// //if public==protocol.pu  
94 -//  
95 -// sqlCount := fmt.Sprintf(`select count(0) from (  
96 -//select id from chance  
97 -//where company_id=? and review_status=3 and (%v=0 or chance_type_id =%v) and enable_status=1 and status=1 %v  
98 -//) a left JOIN chance_data b on a.id =b.chance_id`, chanceTypeId, chanceTypeId, filterDepartment)  
99 -// if err = utils.ExecuteQueryOne(&total, sqlCount, cid); err != nil {  
100 -// return  
101 -// }  
102 -// if v != nil {  
103 -// if err = utils.ExecuteQueryAll(v, sql, cid, chanceTypeId, chanceTypeId, lastId, lastId, pageSize); err != nil {  
104 -// return  
105 -// }  
106 -// }  
107 -// return  
108 -//} 81 +func GetRanksByUser(companyId int64, rankTypeId, rankRangeId, rankPeriodId int, pageIndex, pageSize int, v interface{}) (total int, err error) {
  82 + //var filterDepartment string = getFilterSqlByDIds(dIds)
  83 + pageIndex, pageSize = utils.GetPageInfo(pageIndex, pageSize)
  84 + sql := fmt.Sprintf(`select a.*,b.nick_name name from (
  85 +select ranking,score,relation_id,user_id from(
  86 +select a.discovery_score score,a.relation_id,(@rowno:=@rowno+1) as ranking,user_id from rank a inner join user_company c on a.relation_id=c.id,(select (@rowno:=0)) b
  87 +where a.company_id=%v and rank_type_id=%v and rank_range_id=%v and rank_period_id=%v
  88 +order by a.discovery_score desc,c.create_at asc
  89 +) a
  90 +#where relation_id=?
  91 +limit %v,%v
  92 +)a inner join user b on a.user_id = b.id
  93 +`, companyId, rankTypeId, rankRangeId, rankPeriodId, pageIndex, pageSize)
  94 +
  95 + //if public==protocol.pu
  96 +
  97 + sqlCount := fmt.Sprintf(`
  98 +select count(0) from rank
  99 +where a.company_id=%v and rank_type_id=%v and rank_range_id=%v and rank_period_id=%v
  100 +`, companyId, rankTypeId, rankRangeId, rankPeriodId)
  101 + if err = utils.ExecuteQueryOne(&total, sqlCount); err != nil {
  102 + return
  103 + }
  104 + if v != nil {
  105 + if err = utils.ExecuteQueryAll(v, sql); err != nil {
  106 + return
  107 + }
  108 + }
  109 + return
  110 +}
@@ -74,3 +74,10 @@ func DeleteRankItem(id int) (err error) { @@ -74,3 +74,10 @@ func DeleteRankItem(id int) (err error) {
74 } 74 }
75 return 75 return
76 } 76 }
  77 +
  78 +func GetRankItemKeys(companyId, rankTypeId int) (v []string, err error) {
  79 + //sql:="select item_key from rank_item where company_id=? and rank_type_id=? order by sort_num"
  80 + //o := orm.NewOrm()
  81 + //if o.Raw(sql,companyId,rankTypeId).QueryRows()
  82 + return
  83 +}
@@ -15,21 +15,22 @@ const ( @@ -15,21 +15,22 @@ const (
15 15
16 /*GetRankList 排行榜*/ 16 /*GetRankList 排行榜*/
17 type GetRankListRequest struct { 17 type GetRankListRequest struct {
18 - RankTypeId int `json:"rankTypeId" valid:"Required"` //榜单类型编号(赛季榜、年榜)  
19 - RankRangeId int `json:"rankRangeId" valid:"Required"` //排行榜范围编号(员工/部门)  
20 - RankPeriodId int `json:"rankPeriodId" valid:"Required"` //排行榜周期范围编号 (开始结束时间)  
21 - PageIndex int64 `json:"pageIndex" valid:"Required"` //页码(默认0代表第1页)  
22 - PageSize int `json:"pageSize" valid:"Required"` //每页数量 18 + RankTypeId int `json:"rankTypeId" valid:"Required"` //榜单类型编号(赛季榜、年榜)
  19 + RankRangeId int `json:"rankRangeId" valid:"Required"` //排行榜范围编号(员工/部门)
  20 + RankPeriodId int `json:"rankPeriodId" valid:"Required"` //排行榜周期范围编号 (开始结束时间)
  21 + PageIndex int `json:"pageIndex" valid:"Required"` //页码(默认0代表第1页)
  22 + PageSize int `json:"pageSize" valid:"Required"` //每页数量
23 } 23 }
24 type GetRankListResponse struct { 24 type GetRankListResponse struct {
25 Self []RankItem `json:"self"` //自己或所在部门的排名分数 25 Self []RankItem `json:"self"` //自己或所在部门的排名分数
26 Lists [][]RankItem `json:"lists"` //排名列表 26 Lists [][]RankItem `json:"lists"` //排名列表
  27 + Total int `json:"total"` //总数
27 } 28 }
28 29
29 type RankItem struct { 30 type RankItem struct {
30 - Name string `json:"name,omitempty"` //名称  
31 - Score float64 `json:"score"` //分数  
32 - Ranking string `json:"ranking"` //排名 31 + Name string `json:"name,omitempty" orm:"column(name)"` //名称
  32 + Score float64 `json:"score" orm:"column(score)"` //分数
  33 + Ranking string `json:"ranking" orm:"column(ranking)"` //排名
33 } 34 }
34 35
35 /*GetRankType */ 36 /*GetRankType */
@@ -10,10 +10,25 @@ import ( @@ -10,10 +10,25 @@ import (
10 //排行榜 10 //排行榜
11 func GetRankList(header *protocol.RequestHeader, request *protocol.GetRankListRequest) (rsp *protocol.GetRankListResponse, err error) { 11 func GetRankList(header *protocol.RequestHeader, request *protocol.GetRankListRequest) (rsp *protocol.GetRankListResponse, err error) {
12 var ( 12 var (
13 - //ranks []*models.Rank 13 + //ranks []*models.Rank
  14 + ranks []protocol.RankItem
  15 + rankRange *models.RankRange
14 ) 16 )
15 - 17 + if rankRange, err = models.GetRankRangeById(request.RankRangeId); err != nil {
  18 + log.Error(err)
  19 + return
  20 + }
16 rsp = &protocol.GetRankListResponse{} 21 rsp = &protocol.GetRankListResponse{}
  22 + if rankRange.Type == protocol.RankRangeTypeAllCompanyDepartment || rankRange.Type == protocol.RankRangeTypeAllSpecifyDepartment {
  23 +
  24 + } else if rankRange.Type == protocol.RankRangeTypeAllCompanyUser || rankRange.Type == protocol.RankRangeTypeSpecifyUser {
  25 + if rsp.Total, err = models.GetRanksByUser(header.CompanyId, request.RankTypeId, request.RankRangeId, request.RankPeriodId, request.PageIndex, request.PageSize, &ranks); err != nil && err != orm.ErrNoRows {
  26 + log.Error(err)
  27 + return
  28 + }
  29 + rsp.Lists = append(rsp.Lists, ranks)
  30 + }
  31 +
17 return 32 return
18 } 33 }
19 34