作者 yangfu

部门统计排序

@@ -72,7 +72,31 @@ type DepartmentStatisticsRequest struct { @@ -72,7 +72,31 @@ type DepartmentStatisticsRequest struct {
72 Type int `json:"type"` //1:已审核 2:待审核 72 Type int `json:"type"` //1:已审核 2:待审核
73 } 73 }
74 type DepartmentStatisticsResponse struct { 74 type DepartmentStatisticsResponse struct {
75 - List []*DepartmentStatistics `json:"departmentStatistics"` 75 + Total int `json:"total"`
  76 + List []*DepartmentStatistics `json:"departmentStatistics"`
  77 +}
  78 +
  79 +func (this *DepartmentStatisticsResponse) Len() int { return len(this.List) }
  80 +func (this *DepartmentStatisticsResponse) Less(i, j int) bool {
  81 + //已审核 按照总数从大到小排序;若相同,按照已通过数量从大到小排序;若还相同,按照成果数量从大到小排序;*/
  82 + if this.List[i].ACTotal > this.List[j].ACTotal {
  83 + return true
  84 + }
  85 + if this.List[i].ChanceApprovedTotal > this.List[j].ChanceApprovedTotal {
  86 + return true
  87 + }
  88 + if this.List[i].AchievementTotal > this.List[j].AchievementTotal {
  89 + return true
  90 + }
  91 +
  92 + //待审核
  93 + if this.List[i].ChanceApprovingTotal > this.List[j].ChanceApprovingTotal {
  94 + return true
  95 + }
  96 + return false
  97 +}
  98 +func (this *DepartmentStatisticsResponse) Swap(i, j int) {
  99 + this.List[i], this.List[j] = this.List[j], this.List[i]
76 } 100 }
77 101
78 //部门统计项 102 //部门统计项
@@ -81,4 +105,6 @@ type DepartmentStatistics struct { @@ -81,4 +105,6 @@ type DepartmentStatistics struct {
81 ChanceApprovedTotal int `json:"chanceApprovedTotal"` //已审核的机会 105 ChanceApprovedTotal int `json:"chanceApprovedTotal"` //已审核的机会
82 ChanceApprovingTotal int `json:"chanceApprovingTotal"` //待审核的机会 106 ChanceApprovingTotal int `json:"chanceApprovingTotal"` //待审核的机会
83 AchievementTotal int `json:"achievementTotal"` //已创建的成果 (显示) 107 AchievementTotal int `json:"achievementTotal"` //已创建的成果 (显示)
  108 +
  109 + ACTotal int `json:"-"` //机会成果总数 (显示)
84 } 110 }
@@ -7,6 +7,7 @@ import ( @@ -7,6 +7,7 @@ import (
7 "opp/models" 7 "opp/models"
8 "opp/protocol" 8 "opp/protocol"
9 "opp/services/agg" 9 "opp/services/agg"
  10 + "sort"
10 ) 11 )
11 12
12 func Departments(header *protocol.RequestHeader, request *protocol.DepartmentsRequest) (rsp *protocol.DepartmentsResponse, err error) { 13 func Departments(header *protocol.RequestHeader, request *protocol.DepartmentsRequest) (rsp *protocol.DepartmentsResponse, err error) {
@@ -93,6 +94,11 @@ func DepartmentStatistics(header *protocol.RequestHeader, request *protocol.Depa @@ -93,6 +94,11 @@ func DepartmentStatistics(header *protocol.RequestHeader, request *protocol.Depa
93 Name: department.Name, 94 Name: department.Name,
94 } 95 }
95 rsp.List = append(rsp.List, item) 96 rsp.List = append(rsp.List, item)
  97 + if request.Type == protocol.StatisticApproved {
  98 + rsp.Total += item.ChanceApprovedTotal + item.AchievementTotal
  99 + } else {
  100 + rsp.Total += item.ChanceApprovingTotal
  101 + }
96 } 102 }
97 } 103 }
98 switch request.Type { 104 switch request.Type {
@@ -102,6 +108,7 @@ func DepartmentStatistics(header *protocol.RequestHeader, request *protocol.Depa @@ -102,6 +108,7 @@ func DepartmentStatistics(header *protocol.RequestHeader, request *protocol.Depa
102 dIds := departmentsResponse.GetChildDepartmentIds(d, true) 108 dIds := departmentsResponse.GetChildDepartmentIds(d, true)
103 rsp.AchievementTotal, _ = agg.AchievementDepartmentStatic(header, 0, dIds) 109 rsp.AchievementTotal, _ = agg.AchievementDepartmentStatic(header, 0, dIds)
104 rsp.ChanceApprovedTotal, _ = agg.GetChancePool(header.UserId, header.CompanyId, 0, d.DepartmentId, 0, 0, nil) 110 rsp.ChanceApprovedTotal, _ = agg.GetChancePool(header.UserId, header.CompanyId, 0, d.DepartmentId, 0, 0, nil)
  111 + rsp.ACTotal = rsp.AchievementTotal + rsp.ChanceApprovedTotal
105 return rsp 112 return rsp
106 }) 113 })
107 break 114 break
@@ -116,5 +123,6 @@ func DepartmentStatistics(header *protocol.RequestHeader, request *protocol.Depa @@ -116,5 +123,6 @@ func DepartmentStatistics(header *protocol.RequestHeader, request *protocol.Depa
116 err = protocol.NewErrWithMessage(2) 123 err = protocol.NewErrWithMessage(2)
117 return 124 return
118 } 125 }
  126 + sort.Sort(rsp)
119 return 127 return
120 } 128 }
@@ -2,6 +2,7 @@ package department @@ -2,6 +2,7 @@ package department
2 2
3 import ( 3 import (
4 "opp/protocol" 4 "opp/protocol"
  5 + "sort"
5 "testing" 6 "testing"
6 ) 7 )
7 8
@@ -28,3 +29,25 @@ func TestGetRootDepartment(t *testing.T) { @@ -28,3 +29,25 @@ func TestGetRootDepartment(t *testing.T) {
28 t.Log("部门:", d.DepartmentId, deps.GetChildDepartmentIds(d, true)) 29 t.Log("部门:", d.DepartmentId, deps.GetChildDepartmentIds(d, true))
29 } 30 }
30 } 31 }
  32 +
  33 +//排序
  34 +func TestSortDepartmentStastics(t *testing.T) {
  35 + s := &protocol.DepartmentStatisticsResponse{
  36 + //List:[]*protocol.DepartmentStatistics{
  37 + // {ChanceApprovedTotal:10,AchievementTotal:10,ACTotal:20},
  38 + // {ChanceApprovedTotal:10,AchievementTotal:10,ACTotal:20},
  39 + // {ChanceApprovedTotal:20,AchievementTotal:10,ACTotal:30},
  40 + // {ChanceApprovedTotal:5,AchievementTotal:10,ACTotal:15},
  41 + //},
  42 + List: []*protocol.DepartmentStatistics{
  43 + {ChanceApprovingTotal: 10},
  44 + {ChanceApprovingTotal: 10},
  45 + {ChanceApprovingTotal: 30},
  46 + {ChanceApprovingTotal: 40},
  47 + {ChanceApprovingTotal: 60},
  48 + {ChanceApprovingTotal: 50},
  49 + },
  50 + }
  51 + sort.Sort(s)
  52 + //t.Log(common.AssertJson(s))
  53 +}