作者 陈志颖

feat:合并排行榜

  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/astaxie/beego/validation"
  6 + "time"
  7 +)
  8 +
  9 +type EmployeesRankingListStatisticsCommand struct {
  10 + CompanyId int `json:"companyId" valid:"Required"` // 公司id
  11 + StartTime time.Time `json:"startTime"` // 年榜开始时间
  12 + EndTime time.Time `json:"endTime"` // 年榜结束时间
  13 +
  14 +}
  15 +
  16 +func (employeesRankingListStatisticsCommand *EmployeesRankingListStatisticsCommand) ValidateCommand() error {
  17 + valid := validation.Validation{}
  18 + b, err := valid.Valid(employeesRankingListStatisticsCommand)
  19 + if err != nil {
  20 + return err
  21 + }
  22 + if !b {
  23 + for _, validErr := range valid.Errors {
  24 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  25 + }
  26 + }
  27 + return nil
  28 +}
  29 +
@@ -361,6 +361,46 @@ func (statisticsService *StatisticsService) EmployeesContributionsStatistics(emp @@ -361,6 +361,46 @@ func (statisticsService *StatisticsService) EmployeesContributionsStatistics(emp
361 } 361 }
362 } 362 }
363 363
  364 +// TODO 员工排行榜统计
  365 +func (statisticsService *StatisticsService) EmployeesRankingListStatistics(employeesRankingListStatisticsCommand *command.EmployeesRankingListStatisticsCommand) (interface{}, error) {
  366 + if err := employeesRankingListStatisticsCommand.ValidateCommand(); err != nil {
  367 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  368 + }
  369 + transactionContext, err := factory.CreateTransactionContext(nil)
  370 + if err != nil {
  371 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  372 + }
  373 + if err := transactionContext.StartTransaction(); err != nil {
  374 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  375 + }
  376 + defer func() {
  377 + transactionContext.RollbackTransaction()
  378 + }()
  379 +
  380 + var employeeDao *dao.EmployeeDao
  381 + if value, err := factory.CreateEmployeeDao(map[string]interface{}{
  382 + "transactionContext": transactionContext,
  383 + }); err != nil {
  384 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  385 + } else {
  386 + employeeDao = value
  387 + }
  388 +
  389 + if employeesRankingListStatisticsCommand.StartTime.IsZero() && employeesRankingListStatisticsCommand.EndTime.IsZero() {
  390 + employeesRankingListStatisticsCommand.StartTime = time.Date(1971, time.Month(1), 1, 0, 0, 0, 0, time.Now().Location())
  391 + employeesRankingListStatisticsCommand.EndTime = time.Now().Local()
  392 + }
  393 +
  394 + if employeesRankingListStatistics, err := employeeDao.CalculateEmployeesRankingList(employeesRankingListStatisticsCommand.CompanyId, employeesRankingListStatisticsCommand.StartTime, employeesRankingListStatisticsCommand.EndTime); err != nil {
  395 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  396 + } else {
  397 + if err := transactionContext.CommitTransaction(); err != nil {
  398 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  399 + }
  400 + return employeesRankingListStatistics, nil
  401 + }
  402 +}
  403 +
364 func NewStatisticsService(options map[string]interface{}) *StatisticsService { 404 func NewStatisticsService(options map[string]interface{}) *StatisticsService {
365 newStatisticsService := &StatisticsService{} 405 newStatisticsService := &StatisticsService{}
366 return newStatisticsService 406 return newStatisticsService
@@ -283,6 +283,83 @@ func (dao *EmployeeDao) CalculateEmployeesSuMoney(companyId int, startTime time. @@ -283,6 +283,83 @@ func (dao *EmployeeDao) CalculateEmployeesSuMoney(companyId int, startTime time.
283 // }, nil 283 // }, nil
284 //} 284 //}
285 285
  286 +// 排行榜统计
  287 +func (dao *EmployeeDao) CalculateEmployeesRankingList(companyId int, startTime time.Time, endTime time.Time) (map[string]interface{}, error) {
  288 + var retWealth []struct {
  289 + Uid int
  290 + EmployeeName string
  291 + EmployeeSuMoney float64
  292 + }
  293 + var retContributions []struct { // 员工贡献值
  294 + Uid int
  295 + EmployeeName string
  296 + EmployeesContributions float64
  297 + }
  298 + var retContributionDecrease []struct { // 员工减少的贡献值
  299 + Uid int
  300 + EmployeeName string
  301 + EmployeesContributions float64
  302 + }
  303 + tx := dao.transactionContext.PgTx
  304 + suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
  305 + // 员工财富值
  306 + if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
  307 + ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid").
  308 + ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name").
  309 + ColumnExpr("sum(su_money_transaction_record.su_money) AS employee_su_money").
  310 + Where(`e.company_id = ?`, companyId).
  311 + Where(`e.status = ?`, 1).
  312 + Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3})). // 增加,任务奖励的
  313 + Where(`su_money_transaction_record.create_time > ?`, startTime).
  314 + Where(`su_money_transaction_record.create_time < ?`, endTime).
  315 + Group("su_money_transaction_record.employee").
  316 + Order("employee_su_money DESC").
  317 + Select(&retWealth); err != nil {
  318 + return nil, err
  319 + }
  320 + // 增加的贡献值
  321 + if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
  322 + ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid").
  323 + ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name").
  324 + ColumnExpr("sum(su_money_transaction_record.su_money) AS employees_contributions").
  325 + Where(`e.company_id = ?`, companyId).
  326 + Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3})).
  327 + Where(`e.status = ?`, 1).
  328 + Where(`su_money_transaction_record.create_time > ?`, startTime).
  329 + Where(`su_money_transaction_record.create_time < ?`, endTime).
  330 + Group("su_money_transaction_record.employee").
  331 + Order("employees_contributions DESC").
  332 + Select(&retContributions); err != nil {
  333 + return nil, err
  334 + }
  335 + // 减少的贡献值
  336 + if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
  337 + ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid").
  338 + ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name").
  339 + ColumnExpr("sum(su_money_transaction_record.su_money) AS employees_contributions").
  340 + Where(`e.company_id = ?`, companyId).
  341 + Where(`su_money_transaction_record.record_type = ?`, 4).
  342 + Where(`e.status = ?`, 1).
  343 + Where(`su_money_transaction_record.create_time > ?`, startTime).
  344 + Where(`su_money_transaction_record.create_time < ?`, endTime).
  345 + Group("su_money_transaction_record.employee").
  346 + Order("employees_contributions DESC").
  347 + Select(&retContributionDecrease); err != nil {
  348 + return nil, err
  349 + }
  350 + for i := 0; i < len(retContributions); i++ {
  351 + for j := 0; j < len(retContributionDecrease); j++ {
  352 + if retContributions[i].Uid == retContributionDecrease[j].Uid {
  353 + retContributions[i].EmployeesContributions -= retContributionDecrease[j].EmployeesContributions
  354 + }
  355 + }
  356 + }
  357 + return map[string]interface{}{
  358 + "employeesContributions": retContributions,
  359 + "employeesWealth": retWealth,
  360 + }, nil
  361 +}
  362 +
286 // 员工贡献值统计 363 // 员工贡献值统计
287 func (dao *EmployeeDao) CalculateEmployeesContributions(companyId int, startTime time.Time, endTime time.Time) (map[string]interface{}, error) { 364 func (dao *EmployeeDao) CalculateEmployeesContributions(companyId int, startTime time.Time, endTime time.Time) (map[string]interface{}, error) {
288 var ret []struct { // 员工贡献值 365 var ret []struct { // 员工贡献值
@@ -126,7 +126,6 @@ func (controller *StatisticsController) EmployeesContributionsStatistics() { @@ -126,7 +126,6 @@ func (controller *StatisticsController) EmployeesContributionsStatistics() {
126 statisticsService := service.NewStatisticsService(nil) 126 statisticsService := service.NewStatisticsService(nil)
127 employeesContributionsStatisticsCommand := &command.EmployeesContributionsStatisticsCommand{} 127 employeesContributionsStatisticsCommand := &command.EmployeesContributionsStatisticsCommand{}
128 json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), employeesContributionsStatisticsCommand) 128 json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), employeesContributionsStatisticsCommand)
129 - fmt.Print(employeesContributionsStatisticsCommand, "\n")  
130 data, err := statisticsService.EmployeesContributionsStatistics(employeesContributionsStatisticsCommand) 129 data, err := statisticsService.EmployeesContributionsStatistics(employeesContributionsStatisticsCommand)
131 var response utils.JsonResponse 130 var response utils.JsonResponse
132 if err != nil { 131 if err != nil {
@@ -136,4 +135,20 @@ func (controller *StatisticsController) EmployeesContributionsStatistics() { @@ -136,4 +135,20 @@ func (controller *StatisticsController) EmployeesContributionsStatistics() {
136 } 135 }
137 controller.Data["json"] = response 136 controller.Data["json"] = response
138 controller.ServeJSON() 137 controller.ServeJSON()
  138 +}
  139 +
  140 +// TODO 合并员工财富值、贡献值排行榜
  141 +func (controller *StatisticsController) RankingListStatistics() {
  142 + statisticsService := service.NewStatisticsService(nil)
  143 + employeesRankingListStatisticsCommand := &command.EmployeesRankingListStatisticsCommand{}
  144 + json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), employeesRankingListStatisticsCommand)
  145 + data, err := statisticsService.EmployeesRankingListStatistics(employeesRankingListStatisticsCommand)
  146 + var response utils.JsonResponse
  147 + if err != nil {
  148 + response = utils.ResponseError(controller.Ctx, err)
  149 + } else {
  150 + response = utils.ResponseData(controller.Ctx, data)
  151 + }
  152 + controller.Data["json"] = response
  153 + controller.ServeJSON()
139 } 154 }
@@ -14,4 +14,5 @@ func init() { @@ -14,4 +14,5 @@ func init() {
14 beego.Router("/statistics/system-cash", &controllers.StatisticsController{}, "Post:SystemCashStatistics") // 系统现金统计 14 beego.Router("/statistics/system-cash", &controllers.StatisticsController{}, "Post:SystemCashStatistics") // 系统现金统计
15 beego.Router("/statistics/employees-su-money", &controllers.StatisticsController{}, "Post:EmployeesSuMoneyStatistics") // 员工财富值统计 15 beego.Router("/statistics/employees-su-money", &controllers.StatisticsController{}, "Post:EmployeesSuMoneyStatistics") // 员工财富值统计
16 beego.Router("/statistics/employees-contributions", &controllers.StatisticsController{}, "Post:EmployeesContributionsStatistics") // 员工贡献值统计 16 beego.Router("/statistics/employees-contributions", &controllers.StatisticsController{}, "Post:EmployeesContributionsStatistics") // 员工贡献值统计
  17 + beego.Router("/statistics/employees-ranking-list", &controllers.StatisticsController{}, "Post:RankingListStatistics") // 员工财富值、贡献值排行榜
17 } 18 }