作者 yangfu

排行榜单次周期榜单计算

机会变更详情修改
@@ -622,6 +622,7 @@ type CommentData struct { @@ -622,6 +622,7 @@ type CommentData struct {
622 622
623 type ChanceReviseLog struct { 623 type ChanceReviseLog struct {
624 Provider *BaseUserInfo `json:"provider,omitempty"` 624 Provider *BaseUserInfo `json:"provider,omitempty"`
  625 + CreateTime int64 `json:"createTime"`
625 DiffContents []ReviseContent `json:"reviseContents"` 626 DiffContents []ReviseContent `json:"reviseContents"`
626 Speechs []Speech `json:"speechs"` 627 Speechs []Speech `json:"speechs"`
627 Pictures []Picture `json:"pictures"` 628 Pictures []Picture `json:"pictures"`
@@ -70,6 +70,7 @@ type RankPeriod struct { @@ -70,6 +70,7 @@ type RankPeriod struct {
70 70
71 /*ComputeRankScore */ 71 /*ComputeRankScore */
72 type ComputeRankScoreRequest struct { 72 type ComputeRankScoreRequest struct {
  73 + RankPeriodId int `json:"rankPeriodId"`
73 } 74 }
74 type ComputeRankScoreResponse struct { 75 type ComputeRankScoreResponse struct {
75 } 76 }
@@ -798,7 +798,18 @@ func CheckChanceDifferent(header *protocol.RequestHeader, chance *models.Chance, @@ -798,7 +798,18 @@ func CheckChanceDifferent(header *protocol.RequestHeader, chance *models.Chance,
798 798
799 diffFormList(chance.SourceContent, request.FormList) 799 diffFormList(chance.SourceContent, request.FormList)
800 diffChanceData() 800 diffChanceData()
801 - 801 + checkModifyLog := func() {
  802 + if len(modifyLog.Speechs) == 0 {
  803 + modifyLog.Speechs = make([]protocol.Speech, 0)
  804 + }
  805 + if len(modifyLog.Pictures) == 0 {
  806 + modifyLog.Pictures = make([]protocol.Picture, 0)
  807 + }
  808 + if len(modifyLog.Videos) == 0 {
  809 + modifyLog.Videos = make([]protocol.Video, 0)
  810 + }
  811 + }
  812 + checkModifyLog()
802 if isSaveLog { 813 if isSaveLog {
803 orm := orm.NewOrm() 814 orm := orm.NewOrm()
804 orm.Begin() 815 orm.Begin()
@@ -2279,6 +2290,7 @@ func ChanceReviseDetail(header *protocol.RequestHeader, request *protocol.Chance @@ -2279,6 +2290,7 @@ func ChanceReviseDetail(header *protocol.RequestHeader, request *protocol.Chance
2279 } 2290 }
2280 json.Unmarshal([]byte(detail.Data), &rsp.ChanceReviseData) 2291 json.Unmarshal([]byte(detail.Data), &rsp.ChanceReviseData)
2281 rsp.ChanceReviseData.Provider = provider 2292 rsp.ChanceReviseData.Provider = provider
  2293 + rsp.ChanceReviseData.CreateTime = detail.CreateAt.Unix() * 1000
2282 rsp.ChanceId = detail.ChanceId 2294 rsp.ChanceId = detail.ChanceId
2283 return 2295 return
2284 } 2296 }
@@ -50,6 +50,9 @@ type RankResult struct{} @@ -50,6 +50,9 @@ type RankResult struct{}
50 50
51 //计算排行分 51 //计算排行分
52 func ComputeRankScore() (err error) { 52 func ComputeRankScore() (err error) {
  53 + var (
  54 + periods []*models.RankPeriod
  55 + )
53 defer func() { 56 defer func() {
54 if p := recover(); p != nil { 57 if p := recover(); p != nil {
55 log.Error(p) 58 log.Error(p)
@@ -64,12 +67,16 @@ func ComputeRankScore() (err error) { @@ -64,12 +67,16 @@ func ComputeRankScore() (err error) {
64 defer func() { 67 defer func() {
65 atomic.CompareAndSwapInt32(&ComputeRankScoreFlag, 1, 0) 68 atomic.CompareAndSwapInt32(&ComputeRankScoreFlag, 1, 0)
66 }() 69 }()
  70 + //1.l 查询进行中的赛季
  71 + if periods, err = models.GetRankPeriods(0, 0, []int{protocol.RankPeriodBegin}); err != nil {
  72 + return
  73 + }
67 //启用多个协程执行 74 //启用多个协程执行
68 var wg sync.WaitGroup 75 var wg sync.WaitGroup
69 for i := 0; i < RankGoroutineNum; i++ { 76 for i := 0; i < RankGoroutineNum; i++ {
70 index := i 77 index := i
71 work := func() { 78 work := func() {
72 - if err = computeRankScore(index, wg); err != nil { 79 + if err = computeRankScore(index, wg, RankGoroutineNum, periods); err != nil {
73 log.Error(err) 80 log.Error(err)
74 } 81 }
75 } 82 }
@@ -92,15 +99,14 @@ func updateRankPeriodStatus(fromStatus int, toStatus int) { @@ -92,15 +99,14 @@ func updateRankPeriodStatus(fromStatus int, toStatus int) {
92 sql = `update rank_period set status=?,update_at=NOW() where status=? and UNIX_TIMESTAMP(begin_time)>? ` 99 sql = `update rank_period set status=?,update_at=NOW() where status=? and UNIX_TIMESTAMP(begin_time)>? `
93 } 100 }
94 orm := orm.NewOrm() 101 orm := orm.NewOrm()
95 - if err := utils.ExecuteSQLWithOrmer(orm, sql, toStatus, fromStatus, time.Now().Unix()); err != nil { 102 + if _, err := orm.Raw(sql, toStatus, fromStatus, time.Now().Unix()).Exec(); err != nil {
96 log.Error(err) 103 log.Error(err)
97 return 104 return
98 } 105 }
99 } 106 }
100 107
101 -func computeRankScore(index int, wg sync.WaitGroup) (err error) { 108 +func computeRankScore(index int, wg sync.WaitGroup, gNum int, periods []*models.RankPeriod) (err error) {
102 var ( 109 var (
103 - periods []*models.RankPeriod  
104 rankRanges []*models.RankRange 110 rankRanges []*models.RankRange
105 rankRangeDatas []*models.RankRangeData 111 rankRangeDatas []*models.RankRangeData
106 ) 112 )
@@ -112,14 +118,10 @@ func computeRankScore(index int, wg sync.WaitGroup) (err error) { @@ -112,14 +118,10 @@ func computeRankScore(index int, wg sync.WaitGroup) (err error) {
112 log.Error(p) 118 log.Error(p)
113 } 119 }
114 }() 120 }()
115 - //1.l 查询进行中的赛季  
116 - if periods, err = models.GetRankPeriods(0, 0, []int{protocol.RankPeriodBegin}); err != nil {  
117 - return  
118 - }  
119 //2.查询对应 rank_type_id 的rank_data 121 //2.查询对应 rank_type_id 的rank_data
120 for i := range periods { 122 for i := range periods {
121 period := periods[i] 123 period := periods[i]
122 - if (period.Id % RankGoroutineNum) != index { 124 + if (period.Id % gNum) != index {
123 return 125 return
124 } 126 }
125 if rankRanges, err = models.GetRankRanges(period.CompanyId, period.RankTypeId); err == orm.ErrNoRows { 127 if rankRanges, err = models.GetRankRanges(period.CompanyId, period.RankTypeId); err == orm.ErrNoRows {
@@ -139,6 +141,7 @@ func computeRankScore(index int, wg sync.WaitGroup) (err error) { @@ -139,6 +141,7 @@ func computeRankScore(index int, wg sync.WaitGroup) (err error) {
139 } 141 }
140 //按类型 142 //按类型
141 //3.查询用户列表/部门列表 143 //3.查询用户列表/部门列表
  144 + logProcessInfo(period)
142 for j := range rankRanges { 145 for j := range rankRanges {
143 var relationIds []int64 146 var relationIds []int64
144 rankRange := rankRanges[j] 147 rankRange := rankRanges[j]
@@ -156,7 +159,6 @@ func computeRankScore(index int, wg sync.WaitGroup) (err error) { @@ -156,7 +159,6 @@ func computeRankScore(index int, wg sync.WaitGroup) (err error) {
156 } 159 }
157 updateRankByRelationIds(relationIds, period, rankRange) 160 updateRankByRelationIds(relationIds, period, rankRange)
158 } 161 }
159 - logProcessInfo(period)  
160 } 162 }
161 163
162 //4.调用统计接口列表 164 //4.调用统计接口列表
@@ -227,6 +229,7 @@ func updateRank(o RankOption) { @@ -227,6 +229,7 @@ func updateRank(o RankOption) {
227 if _, e = models.AddRank(rank); e != nil { 229 if _, e = models.AddRank(rank); e != nil {
228 log.Error(e) 230 log.Error(e)
229 } 231 }
  232 + log.Debug(fmt.Sprintf("【排行榜统计】 新增加 周期编号:%v 公司:%v 评分者编号:%v (榜单类型:%v 参与类型:%v) ", rank.RankPeriodId, rank.CompanyId, rank.RelationId, rank.RankTypeId, rank.RankRangeId))
230 } else { 233 } else {
231 //数据有变化的时候才更新到表 234 //数据有变化的时候才更新到表
232 if !reflect.DeepEqual(rankTmp, *rank) { 235 if !reflect.DeepEqual(rankTmp, *rank) {
@@ -234,6 +237,7 @@ func updateRank(o RankOption) { @@ -234,6 +237,7 @@ func updateRank(o RankOption) {
234 if e = models.UpdateRankById(rank); e != nil { 237 if e = models.UpdateRankById(rank); e != nil {
235 log.Error(e) 238 log.Error(e)
236 } 239 }
  240 + log.Debug(fmt.Sprintf("【排行榜统计】 更新 周期编号:%v 公司:%v 评分者编号:%v (榜单类型:%v 参与类型:%v)", rank.RankPeriodId, rank.CompanyId, rank.RelationId, rank.RankTypeId, rank.RankRangeId))
237 } 241 }
238 } 242 }
239 243
@@ -266,12 +270,29 @@ company_id=%v and rank_type_id=%v and rank_period_id=%v` @@ -266,12 +270,29 @@ company_id=%v and rank_type_id=%v and rank_period_id=%v`
266 scoreConfig.SumScore.DiscoveryFactor, 270 scoreConfig.SumScore.DiscoveryFactor,
267 scoreConfig.SumScore.CatchFactor, o.CompanyId, o.RankTypeId, o.RankPeriodId) 271 scoreConfig.SumScore.CatchFactor, o.CompanyId, o.RankTypeId, o.RankPeriodId)
268 orm := orm.NewOrm() 272 orm := orm.NewOrm()
269 - if err := utils.ExecuteSQLWithOrmer(orm, sql); err != nil { 273 + if _, err := orm.Raw(sql).Exec(); err != nil {
270 log.Error(err) 274 log.Error(err)
271 return 275 return
272 } 276 }
273 } 277 }
274 278
  279 +//手动单赛季统计
  280 +func ComputeRankScoreByPeriod(periodId int) (err error) {
  281 + //启用多个协程执行
  282 + var (
  283 + wg sync.WaitGroup
  284 + period *models.RankPeriod
  285 + )
  286 + if period, err = models.GetRankPeriodById(periodId); err != nil {
  287 + log.Error(err)
  288 + err = protocol.NewCustomMessage(1, "赛季不存在")
  289 + return
  290 + }
  291 + computeRankScore(0, wg, 1, []*models.RankPeriod{period})
  292 + wg.Wait()
  293 + return
  294 +}
  295 +
275 //发现计算规则 296 //发现计算规则
276 type RankDiscovery struct{} 297 type RankDiscovery struct{}
277 298
@@ -166,6 +166,15 @@ func GetRankPeriods(header *protocol.RequestHeader, request *protocol.GetRankPer @@ -166,6 +166,15 @@ func GetRankPeriods(header *protocol.RequestHeader, request *protocol.GetRankPer
166 func ComputeRankScore(header *protocol.RequestHeader, request *protocol.ComputeRankScoreRequest) (rsp *protocol.ComputeRankScoreResponse, err error) { 166 func ComputeRankScore(header *protocol.RequestHeader, request *protocol.ComputeRankScoreRequest) (rsp *protocol.ComputeRankScoreResponse, err error) {
167 var () 167 var ()
168 rsp = &protocol.ComputeRankScoreResponse{} 168 rsp = &protocol.ComputeRankScoreResponse{}
  169 + if request.RankPeriodId > 0 {
  170 + if err = contrab.ComputeRankScoreByPeriod(request.RankPeriodId); err != nil {
  171 + log.Error(err)
  172 + err = protocol.NewCustomMessage(1, err.Error())
  173 + return
  174 + }
  175 + err = protocol.NewSuccessWithMessage("排行榜统计成功")
  176 + return
  177 + }
169 if err = contrab.ComputeRankScore(); err != nil { 178 if err = contrab.ComputeRankScore(); err != nil {
170 log.Error(err) 179 log.Error(err)
171 err = protocol.NewCustomMessage(1, err.Error()) 180 err = protocol.NewCustomMessage(1, err.Error())