作者 陈志颖

refactor:重构贡献值、财富值排行榜

@@ -5,6 +5,7 @@ import ( @@ -5,6 +5,7 @@ import (
5 "github.com/linmadan/egglib-go/utils/tool_funs" 5 "github.com/linmadan/egglib-go/utils/tool_funs"
6 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/factory" 6 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/factory"
7 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/statistics/command" 7 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/statistics/command"
  8 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/suMoney/query"
8 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain" 9 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
9 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/dao" 10 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/dao"
10 "time" 11 "time"
@@ -423,6 +424,41 @@ func (statisticsService *StatisticsService) EmployeesRankingListStatistics(emplo @@ -423,6 +424,41 @@ func (statisticsService *StatisticsService) EmployeesRankingListStatistics(emplo
423 } 424 }
424 } 425 }
425 426
  427 +// 贡献值、财富值榜单
  428 +func (statisticsService *StatisticsService) ContributionsWealthRanking(contributionsWealthRankingQuery *query.ContributionsWealthRankingQuery) (interface{}, error) {
  429 + if err := contributionsWealthRankingQuery.ValidateQuery(); err != nil {
  430 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  431 + }
  432 + transactionContext, err := factory.CreateTransactionContext(nil)
  433 + if err != nil {
  434 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  435 + }
  436 + if err := transactionContext.StartTransaction(); err != nil {
  437 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  438 + }
  439 + defer func() {
  440 + transactionContext.RollbackTransaction()
  441 + }()
  442 +
  443 + var employeeDao *dao.EmployeeDao
  444 + if value, err := factory.CreateEmployeeDao(map[string]interface{}{
  445 + "transactionContext": transactionContext,
  446 + }); err != nil {
  447 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  448 + } else {
  449 + employeeDao = value
  450 + }
  451 +
  452 + if contributionsWealthRankingStatistics, err := employeeDao.ContributionsWealthRanking(tool_funs.SimpleStructToMap(contributionsWealthRankingQuery)); err != nil {
  453 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  454 + } else {
  455 + if err := transactionContext.CommitTransaction(); err != nil {
  456 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  457 + }
  458 + return contributionsWealthRankingStatistics, nil
  459 + }
  460 +}
  461 +
426 func NewStatisticsService(options map[string]interface{}) *StatisticsService { 462 func NewStatisticsService(options map[string]interface{}) *StatisticsService {
427 newStatisticsService := &StatisticsService{} 463 newStatisticsService := &StatisticsService{}
428 return newStatisticsService 464 return newStatisticsService
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/astaxie/beego/validation"
  6 +)
  7 +
  8 +type ContributionsWealthRankingQuery struct {
  9 + CompanyId int64 `json:"companyId"` // 公司ID
  10 + Offset int `json:"offset,omitempty"` // 查询偏离量
  11 + Limit int `json:"limit,omitempty"` // 查询限制
  12 + Uid int64 `json:"uid"` // 统一用户id
  13 +}
  14 +
  15 +func (contributionsWealthRankingQuery *ContributionsWealthRankingQuery) ValidateQuery() error {
  16 + valid := validation.Validation{}
  17 + b, err := valid.Valid(contributionsWealthRankingQuery)
  18 + if err != nil {
  19 + return err
  20 + }
  21 + if !b {
  22 + for _, validErr := range valid.Errors {
  23 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  24 + }
  25 + }
  26 + return nil
  27 +}
@@ -283,22 +283,173 @@ func (dao *EmployeeDao) CalculateEmployeesSuMoney(companyId int, startTime time. @@ -283,22 +283,173 @@ func (dao *EmployeeDao) CalculateEmployeesSuMoney(companyId int, startTime time.
283 // }, nil 283 // }, nil
284 //} 284 //}
285 285
286 -// TODO 排行榜统计,排名 286 +// TODO 贡献值、财富值排行榜
  287 +func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]interface{}) (interface{}, error) {
  288 + var retWealth []struct {
  289 + Uid int
  290 + EmployeeName string
  291 + EmployeeSuMoney float64
  292 + Ranking int
  293 + }
  294 + var retContributions []struct { // 员工贡献值
  295 + Uid int
  296 + EmployeeName string
  297 + EmployeesContributions float64
  298 + Ranking int
  299 + }
  300 +
  301 + tx := dao.transactionContext.PgTx
  302 + suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
  303 +
  304 + // 财富值排名
  305 + queryWealth := tx.Model(suMoneyTransactionRecordModel)
  306 + queryWealth = queryWealth.Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint")
  307 + queryWealth = queryWealth.ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid")
  308 + queryWealth = queryWealth.ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name")
  309 + queryWealth = queryWealth.ColumnExpr("sum(su_money_transaction_record.su_money) AS employee_su_money")
  310 + queryWealth = queryWealth.ColumnExpr("RANK() OVER (ORDER BY sum(su_money_transaction_record.su_money) DESC) AS ranking")
  311 + queryWealth = queryWealth.Where(`e.status = ?`, 1)
  312 + queryWealth = queryWealth.Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3}))
  313 + if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
  314 + queryWealth = queryWealth.Where("e.company_id = ?", companyId)
  315 + }
  316 + if startTime, ok := queryOptions["startTime"]; ok {
  317 + queryWealth = queryWealth.Where(`su_money_transaction_record.create_time > ?`, startTime)
  318 + }
  319 + if endTime, ok := queryOptions["endTime"]; ok {
  320 + queryWealth = queryWealth.Where(`su_money_transaction_record.create_time < ?`, endTime)
  321 + }
  322 + queryWealth = queryWealth.Group("su_money_transaction_record.employee")
  323 + if offset, ok := queryOptions["offset"]; ok {
  324 + offset := offset.(int)
  325 + if offset > -1 {
  326 + queryWealth = queryWealth.Offset(offset)
  327 + }
  328 + } else {
  329 + queryWealth = queryWealth.Offset(0)
  330 + }
  331 + if limit, ok := queryOptions["limit"]; ok {
  332 + limit := limit.(int)
  333 + if limit > -1 {
  334 + queryWealth = queryWealth.Limit(limit)
  335 + }
  336 + } else {
  337 + queryWealth = queryWealth.Limit(20)
  338 + }
  339 + if err := queryWealth.Order("employee_su_money DESC").Select(&retWealth); err != nil {
  340 + return nil, err
  341 + }
  342 +
  343 + // 当前员工财富值排名
  344 +
  345 + // 扣除的贡献值子查询
  346 + queryContributionsDecrease := tx.Model(suMoneyTransactionRecordModel)
  347 + 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")
  349 + queryContributionsDecrease = queryContributionsDecrease.ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name")
  350 + queryContributionsDecrease = queryContributionsDecrease.ColumnExpr("sum(su_money_transaction_record.su_money) AS employee_contributions_decrease")
  351 + queryContributionsDecrease = queryContributionsDecrease.ColumnExpr("RANK() OVER (ORDER BY sum(su_money_transaction_record.su_money) DESC) AS ranking")
  352 + if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
  353 + queryContributionsDecrease = queryContributionsDecrease.Where("e.company_id = ?", companyId)
  354 + }
  355 + queryContributionsDecrease = queryContributionsDecrease.Where(`e.status = ?`, 1)
  356 + queryContributionsDecrease = queryContributionsDecrease.Where(`su_money_transaction_record.record_type = ?`, 4)
  357 + if startTime, ok := queryOptions["startTime"]; ok {
  358 + queryContributionsDecrease = queryContributionsDecrease.Where(`su_money_transaction_record.create_time > ?`, startTime)
  359 + }
  360 + if endTime, ok := queryOptions["endTime"]; ok {
  361 + queryContributionsDecrease = queryContributionsDecrease.Where(`su_money_transaction_record.create_time < ?`, endTime)
  362 + }
  363 + contributionsDecrease := queryContributionsDecrease.Group("su_money_transaction_record.employee")
  364 +
  365 + //if offset, ok := queryOptions["offset"]; ok {
  366 + // offset := offset.(int)
  367 + // if offset > -1 {
  368 + // queryContributionsDecrease = queryContributionsDecrease.Offset(offset)
  369 + // }
  370 + //} else {
  371 + // queryContributionsDecrease = queryContributionsDecrease.Offset(0)
  372 + //}
  373 + //if limit, ok := queryOptions["limit"]; ok {
  374 + // limit := limit.(int)
  375 + // if limit > -1 {
  376 + // queryContributionsDecrease = queryContributionsDecrease.Limit(limit)
  377 + // }
  378 + //} else {
  379 + // queryContributionsDecrease = queryContributionsDecrease.Limit(20)
  380 + //}
  381 + //contributionsDecrease := queryWealth.Order("employee_contributions_decrease DESC")
  382 +
  383 + // 贡献值排名
  384 + queryContributions := tx.Model()
  385 + queryContributions = queryContributions.With("t", contributionsDecrease)
  386 + queryContributions = queryContributions.Table("t")
  387 + queryContributions = queryContributions.Table("su_money_transaction_records")
  388 + queryContributions = queryContributions.Join("JOIN employees AS e ON e.uid = (su_money_transaction_records.employee->>'uid')::bigint")
  389 + queryContributions = queryContributions.ColumnExpr("su_money_transaction_records.employee->>'uid' AS uid")
  390 + queryContributions = queryContributions.ColumnExpr("su_money_transaction_records.employee->>'employeeName' AS employee_name")
  391 + queryContributions = queryContributions.ColumnExpr(`(sum(su_money_transaction_records.su_money) - t.employee_contributions_decrease) AS employees_contributions`)
  392 + queryContributions = queryContributions.ColumnExpr("RANK() OVER (ORDER BY sum(su_money_transaction_records.su_money) - t.employee_contributions_decrease DESC) AS ranking")
  393 + if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
  394 + queryContributions = queryContributions.Where("e.company_id = ?", companyId)
  395 + }
  396 + queryContributions = queryContributions.Where(`e.status = ?`, 1)
  397 + queryContributions = queryContributions.Where(`su_money_transaction_records.record_type IN (?)`, pg.In([]int{2, 3}))
  398 + if startTime, ok := queryOptions["startTime"]; ok {
  399 + queryContributions = queryContributions.Where(`su_money_transaction_records.create_time > ?`, startTime)
  400 + }
  401 + if endTime, ok := queryOptions["endTime"]; ok {
  402 + queryContributions = queryContributions.Where(`su_money_transaction_records.create_time < ?`, endTime)
  403 + }
  404 + queryContributions = queryContributions.Group("su_money_transaction_records.employee")
  405 + queryContributions = queryContributions.Group("t.employee_contributions_decrease")
  406 + if offset, ok := queryOptions["offset"]; ok {
  407 + offset := offset.(int)
  408 + if offset > -1 {
  409 + queryContributions = queryContributions.Offset(offset)
  410 + }
  411 + } else {
  412 + queryContributions = queryContributions.Offset(0)
  413 + }
  414 + if limit, ok := queryOptions["limit"]; ok {
  415 + limit := limit.(int)
  416 + if limit > -1 {
  417 + queryContributions = queryContributions.Limit(limit)
  418 + }
  419 + } else {
  420 + queryContributions = queryContributions.Limit(20)
  421 + }
  422 + if err := queryContributions.Order("employees_contributions DESC").Select(&retContributions); err != nil {
  423 + return nil, err
  424 + }
  425 +
  426 + // 当前员工贡献值排名
  427 +
  428 + return map[string]interface{}{
  429 + "employeesWealth": retWealth,
  430 + "employeesContributions": retContributions,
  431 + }, nil
  432 +}
  433 +
  434 +// 排行榜统计,排名
287 func (dao *EmployeeDao) CalculateEmployeesRankingList(companyId int, startTime time.Time, endTime time.Time, offset int, limit int) (map[string]interface{}, error) { 435 func (dao *EmployeeDao) CalculateEmployeesRankingList(companyId int, startTime time.Time, endTime time.Time, offset int, limit int) (map[string]interface{}, error) {
288 var retWealth []struct { 436 var retWealth []struct {
289 Uid int 437 Uid int
290 EmployeeName string 438 EmployeeName string
291 EmployeeSuMoney float64 439 EmployeeSuMoney float64
  440 + Ranking int
292 } 441 }
293 var retContributions []struct { // 员工贡献值 442 var retContributions []struct { // 员工贡献值
294 Uid int 443 Uid int
295 EmployeeName string 444 EmployeeName string
296 EmployeesContributions float64 445 EmployeesContributions float64
  446 + Ranking int
297 } 447 }
298 var retContributionDecrease []struct { // 员工减少的贡献值 448 var retContributionDecrease []struct { // 员工减少的贡献值
299 Uid int 449 Uid int
300 EmployeeName string 450 EmployeeName string
301 EmployeesContributions float64 451 EmployeesContributions float64
  452 + Ranking int
302 } 453 }
303 tx := dao.transactionContext.PgTx 454 tx := dao.transactionContext.PgTx
304 suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord) 455 suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
@@ -3,6 +3,7 @@ package controllers @@ -3,6 +3,7 @@ package controllers
3 import ( 3 import (
4 "encoding/json" 4 "encoding/json"
5 "fmt" 5 "fmt"
  6 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/suMoney/query"
6 7
7 "github.com/astaxie/beego" 8 "github.com/astaxie/beego"
8 "github.com/linmadan/egglib-go/web/beego/utils" 9 "github.com/linmadan/egglib-go/web/beego/utils"
@@ -140,9 +141,12 @@ func (controller *StatisticsController) EmployeesContributionsStatistics() { @@ -140,9 +141,12 @@ func (controller *StatisticsController) EmployeesContributionsStatistics() {
140 // 员工财富值、贡献值排行榜 141 // 员工财富值、贡献值排行榜
141 func (controller *StatisticsController) RankingListStatistics() { 142 func (controller *StatisticsController) RankingListStatistics() {
142 statisticsService := service.NewStatisticsService(nil) 143 statisticsService := service.NewStatisticsService(nil)
143 - employeesRankingListStatisticsCommand := &command.EmployeesRankingListStatisticsCommand{}  
144 - json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), employeesRankingListStatisticsCommand)  
145 - data, err := statisticsService.EmployeesRankingListStatistics(employeesRankingListStatisticsCommand) 144 + //employeesRankingListStatisticsCommand := &command.EmployeesRankingListStatisticsCommand{}
  145 + //json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), employeesRankingListStatisticsCommand)
  146 + contributionsWealthRankingQuery := &query.ContributionsWealthRankingQuery{}
  147 + json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), contributionsWealthRankingQuery)
  148 + //data, err := statisticsService.EmployeesRankingListStatistics(employeesRankingListStatisticsCommand)
  149 + data, err := statisticsService.ContributionsWealthRanking(contributionsWealthRankingQuery)
146 var response utils.JsonResponse 150 var response utils.JsonResponse
147 if err != nil { 151 if err != nil {
148 response = utils.ResponseError(controller.Ctx, err) 152 response = utils.ResponseError(controller.Ctx, err)