作者 yangfu

排行榜修改

... ... @@ -131,7 +131,6 @@ func PrintLogSql(sql string, param ...interface{}) {
//ExecuteQueryOne 执行原生sql查询单条记录;结果用结构体接收
func ExecuteQueryOne(result interface{}, sqlstr string, param ...interface{}) error {
//PrintLogSql(sqlstr, param...)
var err error
o := orm.NewOrm()
err = ExecuteQueryOneWithOrmer(o, result, sqlstr, param)
... ...
... ... @@ -13,6 +13,7 @@ import (
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/websocket"
"opp/internal/utils"
_ "opp/routers"
"opp/services/contrab"
"os"
"strings"
"time"
... ... @@ -91,7 +92,7 @@ func main() {
//beego.BConfig.Listen.HTTPSPort = 8089
//beego.BConfig.Listen.HTTPSCertFile = "conf/server.crt"
//beego.BConfig.Listen.HTTPSKeyFile = "conf/server.key"
//contrab.Run()
contrab.Run()
beego.Run()
}
... ...
... ... @@ -113,3 +113,22 @@ order by parent_id,id`
}
return
}
//获取用户所有部门
func GetDepartmentByUser(uid int64) (v []*Department, err error) {
o := orm.NewOrm()
sql := `
select id from (
select department_id from user_department where user_company_id=? and enable_status = 1
)a inner join department b on a.department_id = b.id
where b.delete_at =0
order by parent_id,id`
if _, err = o.Raw(sql, uid).QueryRows(&v); err == nil {
if err == orm.ErrNoRows {
err = nil
return
}
return
}
return
}
... ...
... ... @@ -78,31 +78,112 @@ func GetRank(companyId, rankTypeId, rankRangeId, rankPeriodId int, relationId in
return nil, err
}
func GetRanksByUser(companyId int64, rankTypeId, rankRangeId, rankPeriodId int, pageIndex, pageSize int, v interface{}) (total int, err error) {
//排行列表-用户
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 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
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
order by a.discovery_score desc,c.create_at asc
) a
#where relation_id=?
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
`, companyId, rankTypeId, rankRangeId, rankPeriodId, pageIndex, pageSize)
//if public==protocol.pu
`, 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
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
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
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 {
return
}
}
... ...
... ... @@ -75,9 +75,11 @@ 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()
func GetRankItemKeys(companyId int64, rankTypeId int) (v []string, name []string, err error) {
sql := "select item_key,item_name from rank_item where company_id=? and rank_type_id=? order by sort_num"
o := orm.NewOrm()
if _, err = o.Raw(sql, companyId, rankTypeId).QueryRows(&v, &name); err != nil {
return
}
return
}
... ...
... ... @@ -22,15 +22,16 @@ type GetRankListRequest struct {
PageSize int `json:"pageSize" valid:"Required"` //每页数量
}
type GetRankListResponse struct {
Self []RankItem `json:"self"` //自己或所在部门的排名分数
Lists [][]RankItem `json:"lists"` //排名列表
Total int `json:"total"` //总数
SortItems []string `json:"sortItems"` //评比项
Self []RankItem `json:"self"` //自己或所在部门的排名分数
Lists [][]RankItem `json:"lists"` //排名列表
Total int `json:"total"` //总数
}
type RankItem struct {
Ranking string `json:"ranking" orm:"column(ranking)"` //排名
Name string `json:"name,omitempty" orm:"column(name)"` //名称
Score float64 `json:"score" orm:"column(score)"` //分数
Ranking string `json:"ranking" orm:"column(ranking)"` //排名
}
/*GetRankType */
... ...
... ... @@ -2,7 +2,7 @@ package rank
import (
"github.com/astaxie/beego/orm"
"github.com/prometheus/common/log"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"opp/models"
"opp/protocol"
)
... ... @@ -10,25 +10,86 @@ import (
//排行榜
func GetRankList(header *protocol.RequestHeader, request *protocol.GetRankListRequest) (rsp *protocol.GetRankListResponse, err error) {
var (
//ranks []*models.Rank
ranks []protocol.RankItem
rankRange *models.RankRange
ranks []protocol.RankItem
selfRank *protocol.RankItem
rankRange *models.RankRange
itemKeys []string
itemNames []string
departments []*models.Department
departmentId int64
hasDepartmentInRank bool = true
)
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
if itemKeys, itemNames, err = models.GetRankItemKeys(header.CompanyId, request.RankTypeId); err != nil && err != orm.ErrNoRows {
log.Error(err)
return
}
rsp = &protocol.GetRankListResponse{
Self: make([]protocol.RankItem, 0),
Lists: make([][]protocol.RankItem, 0),
}
rsp.SortItems = itemNames
for i := range itemKeys {
key := itemKeys[i]
if rankRange.Type == protocol.RankRangeTypeAllCompanyDepartment || rankRange.Type == protocol.RankRangeTypeAllSpecifyDepartment {
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 {
log.Error(err)
return
}
if !hasDepartmentInRank {
continue
}
if hasDepartmentInRank || departmentId > 0 {
if departments, err = models.GetDepartmentByUser(header.UserId); err != nil {
log.Error(err)
return
}
if len(departments) == 0 {
hasDepartmentInRank = false
} else {
if departmentId == 0 {
for i := range departments {
d := departments[i]
if err = models.GetRanksByDepartmentSelf(header.CompanyId, request.RankTypeId, request.RankRangeId, request.RankPeriodId, key, int64(d.Id), &selfRank); err != nil {
log.Error(err)
return
}
if selfRank != nil {
departmentId = int64(d.Id)
break
}
}
if departmentId == 0 {
hasDepartmentInRank = false
}
} else {
if err = models.GetRanksByDepartmentSelf(header.CompanyId, request.RankTypeId, request.RankRangeId, request.RankPeriodId, key, departmentId, &selfRank); err != nil {
log.Error(err)
return
}
}
}
}
} else if rankRange.Type == protocol.RankRangeTypeAllCompanyUser || rankRange.Type == protocol.RankRangeTypeSpecifyUser {
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 {
log.Error(err)
return
}
if err = models.GetRanksByUserSelf(header.CompanyId, request.RankTypeId, request.RankRangeId, request.RankPeriodId, key, header.UserId, &selfRank); err != nil {
log.Error(err)
return
}
}
if selfRank != nil {
rsp.Self = append(rsp.Self, *selfRank)
}
if len(ranks) > 0 {
rsp.Lists = append(rsp.Lists, ranks)
}
rsp.Lists = append(rsp.Lists, ranks)
}
return
}
... ...