作者 陈志颖

feat:增加贡献值、财富值榜单个人数据统计

@@ -130,7 +130,6 @@ func (dao *CashPoolDao) ExchangeCashListRanking(queryOptions map[string]interfac @@ -130,7 +130,6 @@ func (dao *CashPoolDao) ExchangeCashListRanking(queryOptions map[string]interfac
130 } else { 130 } else {
131 queryPeople = queryPeople.Limit(20) 131 queryPeople = queryPeople.Limit(20)
132 } 132 }
133 -  
134 if err := queryPeople.Order("su_money DESC").Select(&retPeople); err != nil { 133 if err := queryPeople.Order("su_money DESC").Select(&retPeople); err != nil {
135 return nil, err 134 return nil, err
136 } 135 }
@@ -283,7 +283,7 @@ func (dao *EmployeeDao) CalculateEmployeesSuMoney(companyId int, startTime time. @@ -283,7 +283,7 @@ func (dao *EmployeeDao) CalculateEmployeesSuMoney(companyId int, startTime time.
283 // }, nil 283 // }, nil
284 //} 284 //}
285 285
286 -// TODO 贡献值、财富值排行榜 286 +// 贡献值、财富值排行榜
287 func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]interface{}) (interface{}, error) { 287 func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]interface{}) (interface{}, error) {
288 var retWealth []struct { 288 var retWealth []struct {
289 Uid int 289 Uid int
@@ -291,12 +291,24 @@ func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]inter @@ -291,12 +291,24 @@ func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]inter
291 EmployeeSuMoney float64 291 EmployeeSuMoney float64
292 Ranking int 292 Ranking int
293 } 293 }
  294 + var retEmployeeWealth []struct {
  295 + Uid int
  296 + EmployeeName string
  297 + EmployeeSuMoney float64
  298 + Ranking int
  299 + }
294 var retContributions []struct { // 员工贡献值 300 var retContributions []struct { // 员工贡献值
295 Uid int 301 Uid int
296 EmployeeName string 302 EmployeeName string
297 EmployeesContributions float64 303 EmployeesContributions float64
298 Ranking int 304 Ranking int
299 } 305 }
  306 + var retEmployeeContributions []struct { // 员工贡献值
  307 + Uid int
  308 + EmployeeName string
  309 + EmployeesContributions float64
  310 + Ranking int
  311 + }
300 312
301 tx := dao.transactionContext.PgTx 313 tx := dao.transactionContext.PgTx
302 suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord) 314 suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
@@ -341,8 +353,32 @@ func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]inter @@ -341,8 +353,32 @@ func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]inter
341 } 353 }
342 354
343 // 当前员工财富值排名 355 // 当前员工财富值排名
  356 + queryEmployeeWealth := tx.Model(suMoneyTransactionRecordModel)
  357 + queryEmployeeWealth = queryEmployeeWealth.Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint")
  358 + queryEmployeeWealth = queryEmployeeWealth.ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid")
  359 + queryEmployeeWealth = queryEmployeeWealth.ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name")
  360 + queryEmployeeWealth = queryEmployeeWealth.ColumnExpr("sum(su_money_transaction_record.su_money) AS employee_su_money")
  361 + queryEmployeeWealth = queryEmployeeWealth.ColumnExpr("RANK() OVER (ORDER BY sum(su_money_transaction_record.su_money) DESC) AS ranking")
  362 + queryEmployeeWealth = queryEmployeeWealth.Where(`e.status = ?`, 1)
  363 + queryEmployeeWealth = queryEmployeeWealth.Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3}))
  364 + if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
  365 + queryEmployeeWealth = queryEmployeeWealth.Where("e.company_id = ?", companyId)
  366 + }
  367 + if startTime, ok := queryOptions["startTime"]; ok {
  368 + queryEmployeeWealth = queryEmployeeWealth.Where(`su_money_transaction_record.create_time > ?`, startTime)
  369 + }
  370 + if endTime, ok := queryOptions["endTime"]; ok {
  371 + queryEmployeeWealth = queryEmployeeWealth.Where(`su_money_transaction_record.create_time < ?`, endTime)
  372 + }
  373 + if uid, ok := queryOptions["uid"]; ok {
  374 + queryEmployeeWealth = queryEmployeeWealth.Where(`su_money_transaction_record.employee @> '{"uid":?}'`, uid)
  375 + }
  376 + queryEmployeeWealth = queryEmployeeWealth.Group("su_money_transaction_record.employee")
  377 + if err := queryEmployeeWealth.Order("employee_su_money DESC").Select(&retEmployeeWealth); err != nil {
  378 + return nil, err
  379 + }
344 380
345 - // 扣除的贡献值子查询 381 + // 贡献值排名
346 queryContributionsDecrease := tx.Model(suMoneyTransactionRecordModel) 382 queryContributionsDecrease := tx.Model(suMoneyTransactionRecordModel)
347 queryContributionsDecrease = queryContributionsDecrease.Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint") 383 queryContributionsDecrease = queryContributionsDecrease.Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint")
348 queryContributionsDecrease = queryContributionsDecrease.ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid") 384 queryContributionsDecrease = queryContributionsDecrease.ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid")
@@ -380,7 +416,6 @@ func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]inter @@ -380,7 +416,6 @@ func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]inter
380 //} 416 //}
381 //contributionsDecrease := queryWealth.Order("employee_contributions_decrease DESC") 417 //contributionsDecrease := queryWealth.Order("employee_contributions_decrease DESC")
382 418
383 - // 贡献值排名  
384 queryContributions := tx.Model() 419 queryContributions := tx.Model()
385 queryContributions = queryContributions.With("t", contributionsDecrease) 420 queryContributions = queryContributions.With("t", contributionsDecrease)
386 queryContributions = queryContributions.Table("t") 421 queryContributions = queryContributions.Table("t")
@@ -424,14 +459,63 @@ func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]inter @@ -424,14 +459,63 @@ func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]inter
424 } 459 }
425 460
426 // 当前员工贡献值排名 461 // 当前员工贡献值排名
  462 + queryEmployeeContributionsDecrease := tx.Model(suMoneyTransactionRecordModel)
  463 + queryEmployeeContributionsDecrease = queryEmployeeContributionsDecrease.Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint")
  464 + queryEmployeeContributionsDecrease = queryEmployeeContributionsDecrease.ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid")
  465 + queryEmployeeContributionsDecrease = queryEmployeeContributionsDecrease.ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name")
  466 + queryEmployeeContributionsDecrease = queryEmployeeContributionsDecrease.ColumnExpr("sum(su_money_transaction_record.su_money) AS employee_contributions_decrease")
  467 + queryEmployeeContributionsDecrease = queryEmployeeContributionsDecrease.ColumnExpr("RANK() OVER (ORDER BY sum(su_money_transaction_record.su_money) DESC) AS ranking")
  468 + if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
  469 + queryEmployeeContributionsDecrease = queryEmployeeContributionsDecrease.Where("e.company_id = ?", companyId)
  470 + }
  471 + queryEmployeeContributionsDecrease = queryEmployeeContributionsDecrease.Where(`e.status = ?`, 1)
  472 + queryEmployeeContributionsDecrease = queryEmployeeContributionsDecrease.Where(`su_money_transaction_record.record_type = ?`, 4)
  473 + if startTime, ok := queryOptions["startTime"]; ok {
  474 + queryEmployeeContributionsDecrease = queryEmployeeContributionsDecrease.Where(`su_money_transaction_record.create_time > ?`, startTime)
  475 + }
  476 + if endTime, ok := queryOptions["endTime"]; ok {
  477 + queryEmployeeContributionsDecrease = queryEmployeeContributionsDecrease.Where(`su_money_transaction_record.create_time < ?`, endTime)
  478 + }
  479 + employeeContributionsDecrease := queryEmployeeContributionsDecrease.Group("su_money_transaction_record.employee")
  480 +
  481 + queryEmployeeContributions := tx.Model()
  482 + queryEmployeeContributions = queryEmployeeContributions.With("t", employeeContributionsDecrease)
  483 + queryEmployeeContributions = queryEmployeeContributions.Table("t")
  484 + queryEmployeeContributions = queryEmployeeContributions.Table("su_money_transaction_records")
  485 + queryEmployeeContributions = queryEmployeeContributions.Join("JOIN employees AS e ON e.uid = (su_money_transaction_records.employee->>'uid')::bigint")
  486 + queryEmployeeContributions = queryEmployeeContributions.ColumnExpr("su_money_transaction_records.employee->>'uid' AS uid")
  487 + queryEmployeeContributions = queryEmployeeContributions.ColumnExpr("su_money_transaction_records.employee->>'employeeName' AS employee_name")
  488 + queryEmployeeContributions = queryEmployeeContributions.ColumnExpr(`(sum(su_money_transaction_records.su_money) - t.employee_contributions_decrease) AS employees_contributions`)
  489 + queryEmployeeContributions = queryEmployeeContributions.ColumnExpr("RANK() OVER (ORDER BY sum(su_money_transaction_records.su_money) - t.employee_contributions_decrease DESC) AS ranking")
  490 + if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
  491 + queryEmployeeContributions = queryContributions.Where("e.company_id = ?", companyId)
  492 + }
  493 + queryEmployeeContributions = queryEmployeeContributions.Where(`e.status = ?`, 1)
  494 + queryEmployeeContributions = queryEmployeeContributions.Where(`su_money_transaction_records.record_type IN (?)`, pg.In([]int{2, 3}))
  495 + if startTime, ok := queryOptions["startTime"]; ok {
  496 + queryEmployeeContributions = queryEmployeeContributions.Where(`su_money_transaction_records.create_time > ?`, startTime)
  497 + }
  498 + if endTime, ok := queryOptions["endTime"]; ok {
  499 + queryEmployeeContributions = queryEmployeeContributions.Where(`su_money_transaction_records.create_time < ?`, endTime)
  500 + }
  501 + if uid, ok := queryOptions["uid"]; ok {
  502 + queryEmployeeContributions = queryEmployeeContributions.Where(`su_money_transaction_records.employee @> '{"uid":?}'`, uid)
  503 + }
  504 + queryEmployeeContributions = queryEmployeeContributions.Group("su_money_transaction_records.employee")
  505 + queryEmployeeContributions = queryEmployeeContributions.Group("t.employee_contributions_decrease")
  506 + if err := queryEmployeeContributions.Order("employees_contributions DESC").Select(&retEmployeeContributions); err != nil {
  507 + return nil, err
  508 + }
427 509
428 return map[string]interface{}{ 510 return map[string]interface{}{
429 "employeesWealth": retWealth, 511 "employeesWealth": retWealth,
430 "employeesContributions": retContributions, 512 "employeesContributions": retContributions,
  513 + "currentEmployeeContributions": retEmployeeContributions,
  514 + "currentEmployeeWealth": retEmployeeWealth,
431 }, nil 515 }, nil
432 } 516 }
433 517
434 -// 排行榜统计,排名 518 +// 排行榜统计,排名(弃用)
435 func (dao *EmployeeDao) CalculateEmployeesRankingList(companyId int, startTime time.Time, endTime time.Time, offset int, limit int) (map[string]interface{}, error) { 519 func (dao *EmployeeDao) CalculateEmployeesRankingList(companyId int, startTime time.Time, endTime time.Time, offset int, limit int) (map[string]interface{}, error) {
436 var retWealth []struct { 520 var retWealth []struct {
437 Uid int 521 Uid int