rank.go
6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package models
import (
"fmt"
"opp/internal/utils"
"time"
"github.com/astaxie/beego/orm"
)
type Rank struct {
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:"榜单范围编号"`
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 int64) (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_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
}
//排行列表-用户
func GetRanksByUser(companyId int64, rankTypeId, rankRangeId, rankPeriodId int, key string, pageIndex, pageSize int, v interface{}) (total int, err error) {
//var filterDepartment string = getFilterSqlByDIds(dIds)
pageIndex, pageSize = utils.GetPageInfo(pageIndex, pageSize)
sql := fmt.Sprintf(`select (@rowno:=@rowno+1) as ranking,a.*,b.nick_name name from
(
select score,relation_id,user_id from(
select a.%v score,a.relation_id,user_id from rank a inner join user_company c on a.relation_id=c.id
where a.company_id=%v and rank_type_id=%v and rank_range_id=%v and rank_period_id=%v and a.enable_status=1
order by a.%v desc,c.create_at asc
) a,(select @rowno:=%v) b
limit %v,%v
)a inner join user b on a.user_id = b.id
`, key, companyId, rankTypeId, rankRangeId, rankPeriodId, key, pageIndex, pageIndex, pageSize)
sqlCount := fmt.Sprintf(`
select count(0) from rank
where company_id=%v and rank_type_id=%v and rank_range_id=%v and rank_period_id=%v
`, companyId, rankTypeId, rankRangeId, rankPeriodId)
if err = utils.ExecuteQueryOne(&total, sqlCount); err != nil {
return
}
if v != nil {
if err = utils.ExecuteQueryAll(v, sql); err != nil {
return
}
}
return
}
//排行榜列表-用户-自己的排名
func GetRanksByUserSelf(companyId int64, rankTypeId, rankRangeId, rankPeriodId int, key string, relationId int64, v interface{}) (err error) {
//var filterDepartment string = getFilterSqlByDIds(dIds)
sql := fmt.Sprintf(`
select a.*,b.nick_name name from
(
select score,relation_id,user_id,(@rowno:=@rowno+1) as ranking from(
select a.%v score,a.relation_id,user_id from rank a inner join user_company c on a.relation_id=c.id
where a.company_id=%v and rank_type_id=%v and rank_range_id=%v and rank_period_id=%v and a.enable_status=1
order by a.%v desc,c.create_at asc
) a,(select @rowno:=0 ) b
)a inner join user b on a.user_id = b.id
where relation_id=%v
`, key, companyId, rankTypeId, rankRangeId, rankPeriodId, key, relationId)
if v != nil {
if err = utils.ExecuteQueryOne(v, sql); err != nil {
if err == orm.ErrNoRows {
err = nil
}
return
}
}
return
}
//排行榜列表-部门
func GetRanksByDepartment(companyId int64, rankTypeId, rankRangeId, rankPeriodId int, key string, pageIndex, pageSize int, v interface{}) (total int, err error) {
//var filterDepartment string = getFilterSqlByDIds(dIds)
pageIndex, pageSize = utils.GetPageInfo(pageIndex, pageSize)
sql := fmt.Sprintf(`
select a.*,(@rowno:=@rowno+1) as ranking from
(
select a.%v score,a.relation_id,c.name
from rank a inner join department c on a.relation_id=c.id
where a.company_id=%v and rank_type_id=%v and rank_range_id=%v and rank_period_id=%v and a.enable_status=1
order by a.%v desc,c.create_at asc
)a,(select (@rowno:=%v)) b
limit %v,%v
`, key, companyId, rankTypeId, rankRangeId, rankPeriodId, key, pageIndex, pageIndex, pageSize)
sqlCount := fmt.Sprintf(`
select count(0) from rank
where company_id=%v and rank_type_id=%v and rank_range_id=%v and rank_period_id=%v
`, companyId, rankTypeId, rankRangeId, rankPeriodId)
if err = utils.ExecuteQueryOne(&total, sqlCount); err != nil {
return
}
if v != nil {
if err = utils.ExecuteQueryAll(v, sql); err != nil {
if err == orm.ErrNoRows {
err = nil
}
return
}
}
return
}
//排行榜列表-部门-用户所属部门排名
func GetRanksByDepartmentSelf(companyId int64, rankTypeId, rankRangeId, rankPeriodId int, key string, relationId int64, v interface{}) (err error) {
sql := fmt.Sprintf(`select * from (
select a.*,(@rowno:=@rowno+1) as ranking from (
select a.%v score,a.relation_id,c.name from rank a inner join department c on a.relation_id=c.id
where a.company_id=%v and rank_type_id=%v and rank_range_id=%v and rank_period_id=%v and a.enable_status=1
order by a.%v desc,c.create_at asc
) a,(select (@rowno:=0)) b
)a
where a.relation_id=%v
`, key, companyId, rankTypeId, rankRangeId, rankPeriodId, key, relationId)
if v != nil {
if err = utils.ExecuteQueryOne(v, sql); err != nil {
if err == orm.ErrNoRows {
err = nil
}
return
}
}
return
}