作者 yangfu

排行榜修改

@@ -131,7 +131,6 @@ func PrintLogSql(sql string, param ...interface{}) { @@ -131,7 +131,6 @@ func PrintLogSql(sql string, param ...interface{}) {
131 131
132 //ExecuteQueryOne 执行原生sql查询单条记录;结果用结构体接收 132 //ExecuteQueryOne 执行原生sql查询单条记录;结果用结构体接收
133 func ExecuteQueryOne(result interface{}, sqlstr string, param ...interface{}) error { 133 func ExecuteQueryOne(result interface{}, sqlstr string, param ...interface{}) error {
134 - //PrintLogSql(sqlstr, param...)  
135 var err error 134 var err error
136 o := orm.NewOrm() 135 o := orm.NewOrm()
137 err = ExecuteQueryOneWithOrmer(o, result, sqlstr, param) 136 err = ExecuteQueryOneWithOrmer(o, result, sqlstr, param)
@@ -13,6 +13,7 @@ import ( @@ -13,6 +13,7 @@ import (
13 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/websocket" 13 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/websocket"
14 "opp/internal/utils" 14 "opp/internal/utils"
15 _ "opp/routers" 15 _ "opp/routers"
  16 + "opp/services/contrab"
16 "os" 17 "os"
17 "strings" 18 "strings"
18 "time" 19 "time"
@@ -91,7 +92,7 @@ func main() { @@ -91,7 +92,7 @@ func main() {
91 //beego.BConfig.Listen.HTTPSPort = 8089 92 //beego.BConfig.Listen.HTTPSPort = 8089
92 //beego.BConfig.Listen.HTTPSCertFile = "conf/server.crt" 93 //beego.BConfig.Listen.HTTPSCertFile = "conf/server.crt"
93 //beego.BConfig.Listen.HTTPSKeyFile = "conf/server.key" 94 //beego.BConfig.Listen.HTTPSKeyFile = "conf/server.key"
94 - //contrab.Run() 95 + contrab.Run()
95 96
96 beego.Run() 97 beego.Run()
97 } 98 }
@@ -113,3 +113,22 @@ order by parent_id,id` @@ -113,3 +113,22 @@ order by parent_id,id`
113 } 113 }
114 return 114 return
115 } 115 }
  116 +
  117 +//获取用户所有部门
  118 +func GetDepartmentByUser(uid int64) (v []*Department, err error) {
  119 + o := orm.NewOrm()
  120 + sql := `
  121 +select id from (
  122 +select department_id from user_department where user_company_id=? and enable_status = 1
  123 +)a inner join department b on a.department_id = b.id
  124 +where b.delete_at =0
  125 +order by parent_id,id`
  126 + if _, err = o.Raw(sql, uid).QueryRows(&v); err == nil {
  127 + if err == orm.ErrNoRows {
  128 + err = nil
  129 + return
  130 + }
  131 + return
  132 + }
  133 + return
  134 +}
@@ -78,31 +78,112 @@ func GetRank(companyId, rankTypeId, rankRangeId, rankPeriodId int, relationId in @@ -78,31 +78,112 @@ func GetRank(companyId, rankTypeId, rankRangeId, rankPeriodId int, relationId in
78 return nil, err 78 return nil, err
79 } 79 }
80 80
81 -func GetRanksByUser(companyId int64, rankTypeId, rankRangeId, rankPeriodId int, pageIndex, pageSize int, v interface{}) (total int, err error) { 81 +//排行列表-用户
  82 +func GetRanksByUser(companyId int64, rankTypeId, rankRangeId, rankPeriodId int, key string, pageIndex, pageSize int, v interface{}) (total int, err error) {
82 //var filterDepartment string = getFilterSqlByDIds(dIds) 83 //var filterDepartment string = getFilterSqlByDIds(dIds)
83 pageIndex, pageSize = utils.GetPageInfo(pageIndex, pageSize) 84 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 85 + sql := fmt.Sprintf(`select (@rowno:=@rowno+1) as ranking,a.*,b.nick_name name from
  86 +(
  87 +select score,relation_id,user_id from(
  88 +select a.%v score,a.relation_id,user_id from rank a inner join user_company c on a.relation_id=c.id
87 where a.company_id=%v and rank_type_id=%v and rank_range_id=%v and rank_period_id=%v 89 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=? 90 +order by a.%v desc,c.create_at asc
  91 +) a,(select @rowno:=%v) b
91 limit %v,%v 92 limit %v,%v
92 )a inner join user b on a.user_id = b.id 93 )a inner join user b on a.user_id = b.id
93 -`, companyId, rankTypeId, rankRangeId, rankPeriodId, pageIndex, pageSize)  
94 -  
95 - //if public==protocol.pu 94 +`, key, companyId, rankTypeId, rankRangeId, rankPeriodId, key, pageIndex, pageIndex, pageSize)
96 95
97 sqlCount := fmt.Sprintf(` 96 sqlCount := fmt.Sprintf(`
98 select count(0) from rank 97 select count(0) from rank
  98 +where company_id=%v and rank_type_id=%v and rank_range_id=%v and rank_period_id=%v
  99 +`, companyId, rankTypeId, rankRangeId, rankPeriodId)
  100 + if err = utils.ExecuteQueryOne(&total, sqlCount); err != nil {
  101 + return
  102 + }
  103 + if v != nil {
  104 + if err = utils.ExecuteQueryAll(v, sql); err != nil {
  105 + return
  106 + }
  107 + }
  108 + return
  109 +}
  110 +
  111 +//排行榜列表-用户-自己的排名
  112 +func GetRanksByUserSelf(companyId int64, rankTypeId, rankRangeId, rankPeriodId int, key string, relationId int64, v interface{}) (err error) {
  113 + //var filterDepartment string = getFilterSqlByDIds(dIds)
  114 + sql := fmt.Sprintf(`
  115 +
  116 +select a.*,b.nick_name name from
  117 +(
  118 +select score,relation_id,user_id,(@rowno:=@rowno+1) as ranking from(
  119 +select a.%v score,a.relation_id,user_id from rank a inner join user_company c on a.relation_id=c.id
  120 +where a.company_id=%v and rank_type_id=%v and rank_range_id=%v and rank_period_id=%v
  121 +order by a.%v desc,c.create_at asc
  122 +) a,(select @rowno:=0 ) b
  123 +)a inner join user b on a.user_id = b.id
  124 +where relation_id=%v
  125 +
  126 +`, key, companyId, rankTypeId, rankRangeId, rankPeriodId, key, relationId)
  127 +
  128 + if v != nil {
  129 + if err = utils.ExecuteQueryOne(v, sql); err != nil {
  130 + if err == orm.ErrNoRows {
  131 + err = nil
  132 + }
  133 + return
  134 + }
  135 + }
  136 + return
  137 +}
  138 +
  139 +//排行榜列表-部门
  140 +func GetRanksByDepartment(companyId int64, rankTypeId, rankRangeId, rankPeriodId int, key string, pageIndex, pageSize int, v interface{}) (total int, err error) {
  141 + //var filterDepartment string = getFilterSqlByDIds(dIds)
  142 + pageIndex, pageSize = utils.GetPageInfo(pageIndex, pageSize)
  143 + sql := fmt.Sprintf(`
  144 +select a.*,(@rowno:=@rowno+1) as ranking from
  145 +(
  146 +select a.%v score,a.relation_id,c.name
  147 +from rank a inner join department c on a.relation_id=c.id
99 where a.company_id=%v and rank_type_id=%v and rank_range_id=%v and rank_period_id=%v 148 where a.company_id=%v and rank_type_id=%v and rank_range_id=%v and rank_period_id=%v
  149 +order by a.%v desc,c.create_at asc
  150 +)a,(select (@rowno:=%v)) b
  151 +limit %v,%v
  152 +`, key, companyId, rankTypeId, rankRangeId, rankPeriodId, key, pageIndex, pageIndex, pageSize)
  153 +
  154 + sqlCount := fmt.Sprintf(`
  155 +select count(0) from rank
  156 +where company_id=%v and rank_type_id=%v and rank_range_id=%v and rank_period_id=%v
100 `, companyId, rankTypeId, rankRangeId, rankPeriodId) 157 `, companyId, rankTypeId, rankRangeId, rankPeriodId)
101 if err = utils.ExecuteQueryOne(&total, sqlCount); err != nil { 158 if err = utils.ExecuteQueryOne(&total, sqlCount); err != nil {
102 return 159 return
103 } 160 }
104 if v != nil { 161 if v != nil {
105 if err = utils.ExecuteQueryAll(v, sql); err != nil { 162 if err = utils.ExecuteQueryAll(v, sql); err != nil {
  163 + if err == orm.ErrNoRows {
  164 + err = nil
  165 + }
  166 + return
  167 + }
  168 + }
  169 + return
  170 +}
  171 +
  172 +//排行榜列表-部门
  173 +func GetRanksByDepartmentSelf(companyId int64, rankTypeId, rankRangeId, rankPeriodId int, key string, relationId int64, v interface{}) (err error) {
  174 + sql := fmt.Sprintf(`select * from (
  175 +select a.*,(@rowno:=@rowno+1) as ranking from (
  176 +select a.%v score,a.relation_id,c.name from rank a inner join department c on a.relation_id=c.id
  177 +where a.company_id=%v and rank_type_id=%v and rank_range_id=%v and rank_period_id=%v
  178 +order by a.%v desc,c.create_at asc
  179 +) a,(select (@rowno:=0)) b
  180 +)a
  181 +where a.relation_id=%v
  182 +
  183 +`, key, companyId, rankTypeId, rankRangeId, rankPeriodId, key, relationId)
  184 +
  185 + if v != nil {
  186 + if err = utils.ExecuteQueryOne(v, sql); err != nil {
106 return 187 return
107 } 188 }
108 } 189 }
@@ -75,9 +75,11 @@ func DeleteRankItem(id int) (err error) { @@ -75,9 +75,11 @@ func DeleteRankItem(id int) (err error) {
75 return 75 return
76 } 76 }
77 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() 78 +func GetRankItemKeys(companyId int64, rankTypeId int) (v []string, name []string, err error) {
  79 + sql := "select item_key,item_name from rank_item where company_id=? and rank_type_id=? order by sort_num"
  80 + o := orm.NewOrm()
  81 + if _, err = o.Raw(sql, companyId, rankTypeId).QueryRows(&v, &name); err != nil {
  82 + return
  83 + }
82 return 84 return
83 } 85 }
@@ -22,15 +22,16 @@ type GetRankListRequest struct { @@ -22,15 +22,16 @@ type GetRankListRequest struct {
22 PageSize int `json:"pageSize" valid:"Required"` //每页数量 22 PageSize int `json:"pageSize" valid:"Required"` //每页数量
23 } 23 }
24 type GetRankListResponse struct { 24 type GetRankListResponse struct {
25 - Self []RankItem `json:"self"` //自己或所在部门的排名分数  
26 - Lists [][]RankItem `json:"lists"` //排名列表  
27 - Total int `json:"total"` //总数 25 + SortItems []string `json:"sortItems"` //评比项
  26 + Self []RankItem `json:"self"` //自己或所在部门的排名分数
  27 + Lists [][]RankItem `json:"lists"` //排名列表
  28 + Total int `json:"total"` //总数
28 } 29 }
29 30
30 type RankItem struct { 31 type RankItem struct {
  32 + Ranking string `json:"ranking" orm:"column(ranking)"` //排名
31 Name string `json:"name,omitempty" orm:"column(name)"` //名称 33 Name string `json:"name,omitempty" orm:"column(name)"` //名称
32 Score float64 `json:"score" orm:"column(score)"` //分数 34 Score float64 `json:"score" orm:"column(score)"` //分数
33 - Ranking string `json:"ranking" orm:"column(ranking)"` //排名  
34 } 35 }
35 36
36 /*GetRankType */ 37 /*GetRankType */
@@ -2,7 +2,7 @@ package rank @@ -2,7 +2,7 @@ package rank
2 2
3 import ( 3 import (
4 "github.com/astaxie/beego/orm" 4 "github.com/astaxie/beego/orm"
5 - "github.com/prometheus/common/log" 5 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
6 "opp/models" 6 "opp/models"
7 "opp/protocol" 7 "opp/protocol"
8 ) 8 )
@@ -10,25 +10,86 @@ import ( @@ -10,25 +10,86 @@ 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  
14 - ranks []protocol.RankItem  
15 - rankRange *models.RankRange 13 + ranks []protocol.RankItem
  14 + selfRank *protocol.RankItem
  15 + rankRange *models.RankRange
  16 + itemKeys []string
  17 + itemNames []string
  18 + departments []*models.Department
  19 + departmentId int64
  20 + hasDepartmentInRank bool = true
16 ) 21 )
17 if rankRange, err = models.GetRankRangeById(request.RankRangeId); err != nil { 22 if rankRange, err = models.GetRankRangeById(request.RankRangeId); err != nil {
18 log.Error(err) 23 log.Error(err)
19 return 24 return
20 } 25 }
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 26 + if itemKeys, itemNames, err = models.GetRankItemKeys(header.CompanyId, request.RankTypeId); err != nil && err != orm.ErrNoRows {
  27 + log.Error(err)
  28 + return
  29 + }
  30 + rsp = &protocol.GetRankListResponse{
  31 + Self: make([]protocol.RankItem, 0),
  32 + Lists: make([][]protocol.RankItem, 0),
  33 + }
  34 + rsp.SortItems = itemNames
  35 + for i := range itemKeys {
  36 + key := itemKeys[i]
  37 + if rankRange.Type == protocol.RankRangeTypeAllCompanyDepartment || rankRange.Type == protocol.RankRangeTypeAllSpecifyDepartment {
  38 + if rsp.Total, err = models.GetRanksByDepartment(header.CompanyId, request.RankTypeId, request.RankRangeId, request.RankPeriodId, key, request.PageIndex, request.PageSize, &ranks); err != nil && err != orm.ErrNoRows {
  39 + log.Error(err)
  40 + return
  41 + }
  42 + if !hasDepartmentInRank {
  43 + continue
  44 + }
  45 + if hasDepartmentInRank || departmentId > 0 {
  46 + if departments, err = models.GetDepartmentByUser(header.UserId); err != nil {
  47 + log.Error(err)
  48 + return
  49 + }
  50 + if len(departments) == 0 {
  51 + hasDepartmentInRank = false
  52 + } else {
  53 + if departmentId == 0 {
  54 + for i := range departments {
  55 + d := departments[i]
  56 + if err = models.GetRanksByDepartmentSelf(header.CompanyId, request.RankTypeId, request.RankRangeId, request.RankPeriodId, key, int64(d.Id), &selfRank); err != nil {
  57 + log.Error(err)
  58 + return
  59 + }
  60 + if selfRank != nil {
  61 + departmentId = int64(d.Id)
  62 + break
  63 + }
  64 + }
  65 + if departmentId == 0 {
  66 + hasDepartmentInRank = false
  67 + }
  68 + } else {
  69 + if err = models.GetRanksByDepartmentSelf(header.CompanyId, request.RankTypeId, request.RankRangeId, request.RankPeriodId, key, departmentId, &selfRank); err != nil {
  70 + log.Error(err)
  71 + return
  72 + }
  73 + }
  74 + }
  75 + }
  76 + } else if rankRange.Type == protocol.RankRangeTypeAllCompanyUser || rankRange.Type == protocol.RankRangeTypeSpecifyUser {
  77 + if rsp.Total, err = models.GetRanksByUser(header.CompanyId, request.RankTypeId, request.RankRangeId, request.RankPeriodId, key, request.PageIndex, request.PageSize, &ranks); err != nil && err != orm.ErrNoRows {
  78 + log.Error(err)
  79 + return
  80 + }
  81 + if err = models.GetRanksByUserSelf(header.CompanyId, request.RankTypeId, request.RankRangeId, request.RankPeriodId, key, header.UserId, &selfRank); err != nil {
  82 + log.Error(err)
  83 + return
  84 + }
  85 + }
  86 + if selfRank != nil {
  87 + rsp.Self = append(rsp.Self, *selfRank)
  88 + }
  89 + if len(ranks) > 0 {
  90 + rsp.Lists = append(rsp.Lists, ranks)
28 } 91 }
29 - rsp.Lists = append(rsp.Lists, ranks)  
30 } 92 }
31 -  
32 return 93 return
33 } 94 }
34 95