作者 yangfu

排行榜修改

... ... @@ -139,3 +139,15 @@ func MergeMap(to map[string]interface{}, from map[string]interface{}) {
to[k] = v
}
}
func GetPageInfo(pageIndex, pageSize int) (offset, size int) {
if pageSize == 0 {
pageSize = 20
}
if pageIndex == 0 {
pageIndex = 1
}
offset = (pageIndex - 1) * pageSize
size = pageSize
return
}
... ...
... ... @@ -2,6 +2,7 @@ package models
import (
"fmt"
"opp/internal/utils"
"time"
"github.com/astaxie/beego/orm"
... ... @@ -77,32 +78,33 @@ func GetRank(companyId, rankTypeId, rankRangeId, rankPeriodId int, relationId in
return nil, err
}
//func GetRanksByUser(companyId, rankTypeId, rankRangeId, rankPeriodId int, relationId int64,v interface{})(total int, err error) {
// //var filterDepartment string = getFilterSqlByDIds(dIds)
// sql := fmt.Sprintf(`select a.*,b.nick_name name from (
//select ranking,score,relation_id,user_id from(
//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
//where a.company_id=%v and rank_type_id=%v and rank_range_id=%v and rank_period_id=%v
//order by a.discovery_score desc,c.create_at asc
//) a
//#where relation_id=?
//limit %v,%v
//)a inner join user b on a.user_id = b.id
//`, companyId,rankTypeId,rankRangeId,rankPeriodId,)
//
// //if public==protocol.pu
//
// sqlCount := fmt.Sprintf(`select count(0) from (
//select id from chance
//where company_id=? and review_status=3 and (%v=0 or chance_type_id =%v) and enable_status=1 and status=1 %v
//) a left JOIN chance_data b on a.id =b.chance_id`, chanceTypeId, chanceTypeId, filterDepartment)
// if err = utils.ExecuteQueryOne(&total, sqlCount, cid); err != nil {
// return
// }
// if v != nil {
// if err = utils.ExecuteQueryAll(v, sql, cid, chanceTypeId, chanceTypeId, lastId, lastId, pageSize); err != nil {
// return
// }
// }
// return
//}
func GetRanksByUser(companyId int64, rankTypeId, rankRangeId, rankPeriodId int, 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.*,b.nick_name name from (
select ranking,score,relation_id,user_id from(
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
where a.company_id=%v and rank_type_id=%v and rank_range_id=%v and rank_period_id=%v
order by a.discovery_score desc,c.create_at asc
) a
#where relation_id=?
limit %v,%v
)a inner join user b on a.user_id = b.id
`, companyId, rankTypeId, rankRangeId, rankPeriodId, pageIndex, pageSize)
//if public==protocol.pu
sqlCount := fmt.Sprintf(`
select count(0) from rank
where a.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
}
... ...
... ... @@ -74,3 +74,10 @@ func DeleteRankItem(id int) (err error) {
}
return
}
func GetRankItemKeys(companyId, rankTypeId int) (v []string, err error) {
//sql:="select item_key from rank_item where company_id=? and rank_type_id=? order by sort_num"
//o := orm.NewOrm()
//if o.Raw(sql,companyId,rankTypeId).QueryRows()
return
}
... ...
... ... @@ -15,21 +15,22 @@ const (
/*GetRankList 排行榜*/
type GetRankListRequest struct {
RankTypeId int `json:"rankTypeId" valid:"Required"` //榜单类型编号(赛季榜、年榜)
RankRangeId int `json:"rankRangeId" valid:"Required"` //排行榜范围编号(员工/部门)
RankPeriodId int `json:"rankPeriodId" valid:"Required"` //排行榜周期范围编号 (开始结束时间)
PageIndex int64 `json:"pageIndex" valid:"Required"` //页码(默认0代表第1页)
PageSize int `json:"pageSize" valid:"Required"` //每页数量
RankTypeId int `json:"rankTypeId" valid:"Required"` //榜单类型编号(赛季榜、年榜)
RankRangeId int `json:"rankRangeId" valid:"Required"` //排行榜范围编号(员工/部门)
RankPeriodId int `json:"rankPeriodId" valid:"Required"` //排行榜周期范围编号 (开始结束时间)
PageIndex int `json:"pageIndex" valid:"Required"` //页码(默认0代表第1页)
PageSize int `json:"pageSize" valid:"Required"` //每页数量
}
type GetRankListResponse struct {
Self []RankItem `json:"self"` //自己或所在部门的排名分数
Lists [][]RankItem `json:"lists"` //排名列表
Total int `json:"total"` //总数
}
type RankItem struct {
Name string `json:"name,omitempty"` //名称
Score float64 `json:"score"` //分数
Ranking string `json:"ranking"` //排名
Name string `json:"name,omitempty" orm:"column(name)"` //名称
Score float64 `json:"score" orm:"column(score)"` //分数
Ranking string `json:"ranking" orm:"column(ranking)"` //排名
}
/*GetRankType */
... ...
... ... @@ -10,10 +10,25 @@ import (
//排行榜
func GetRankList(header *protocol.RequestHeader, request *protocol.GetRankListRequest) (rsp *protocol.GetRankListResponse, err error) {
var (
//ranks []*models.Rank
//ranks []*models.Rank
ranks []protocol.RankItem
rankRange *models.RankRange
)
if rankRange, err = models.GetRankRangeById(request.RankRangeId); err != nil {
log.Error(err)
return
}
rsp = &protocol.GetRankListResponse{}
if rankRange.Type == protocol.RankRangeTypeAllCompanyDepartment || rankRange.Type == protocol.RankRangeTypeAllSpecifyDepartment {
} else if rankRange.Type == protocol.RankRangeTypeAllCompanyUser || rankRange.Type == protocol.RankRangeTypeSpecifyUser {
if rsp.Total, err = models.GetRanksByUser(header.CompanyId, request.RankTypeId, request.RankRangeId, request.RankPeriodId, request.PageIndex, request.PageSize, &ranks); err != nil && err != orm.ErrNoRows {
log.Error(err)
return
}
rsp.Lists = append(rsp.Lists, ranks)
}
return
}
... ...