作者 陈志颖

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

... ... @@ -5,6 +5,7 @@ import (
"github.com/linmadan/egglib-go/utils/tool_funs"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/factory"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/statistics/command"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/suMoney/query"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/dao"
"time"
... ... @@ -423,6 +424,41 @@ func (statisticsService *StatisticsService) EmployeesRankingListStatistics(emplo
}
}
// 贡献值、财富值榜单
func (statisticsService *StatisticsService) ContributionsWealthRanking(contributionsWealthRankingQuery *query.ContributionsWealthRankingQuery) (interface{}, error) {
if err := contributionsWealthRankingQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
employeeDao = value
}
if contributionsWealthRankingStatistics, err := employeeDao.ContributionsWealthRanking(tool_funs.SimpleStructToMap(contributionsWealthRankingQuery)); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return contributionsWealthRankingStatistics, nil
}
}
func NewStatisticsService(options map[string]interface{}) *StatisticsService {
newStatisticsService := &StatisticsService{}
return newStatisticsService
... ...
package query
import (
"fmt"
"github.com/astaxie/beego/validation"
)
type ContributionsWealthRankingQuery struct {
CompanyId int64 `json:"companyId"` // 公司ID
Offset int `json:"offset,omitempty"` // 查询偏离量
Limit int `json:"limit,omitempty"` // 查询限制
Uid int64 `json:"uid"` // 统一用户id
}
func (contributionsWealthRankingQuery *ContributionsWealthRankingQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(contributionsWealthRankingQuery)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
... ... @@ -283,22 +283,173 @@ func (dao *EmployeeDao) CalculateEmployeesSuMoney(companyId int, startTime time.
// }, nil
//}
// TODO 排行榜统计,排名
// TODO 贡献值、财富值排行榜
func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]interface{}) (interface{}, error) {
var retWealth []struct {
Uid int
EmployeeName string
EmployeeSuMoney float64
Ranking int
}
var retContributions []struct { // 员工贡献值
Uid int
EmployeeName string
EmployeesContributions float64
Ranking int
}
tx := dao.transactionContext.PgTx
suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
// 财富值排名
queryWealth := tx.Model(suMoneyTransactionRecordModel)
queryWealth = queryWealth.Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint")
queryWealth = queryWealth.ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid")
queryWealth = queryWealth.ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name")
queryWealth = queryWealth.ColumnExpr("sum(su_money_transaction_record.su_money) AS employee_su_money")
queryWealth = queryWealth.ColumnExpr("RANK() OVER (ORDER BY sum(su_money_transaction_record.su_money) DESC) AS ranking")
queryWealth = queryWealth.Where(`e.status = ?`, 1)
queryWealth = queryWealth.Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3}))
if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
queryWealth = queryWealth.Where("e.company_id = ?", companyId)
}
if startTime, ok := queryOptions["startTime"]; ok {
queryWealth = queryWealth.Where(`su_money_transaction_record.create_time > ?`, startTime)
}
if endTime, ok := queryOptions["endTime"]; ok {
queryWealth = queryWealth.Where(`su_money_transaction_record.create_time < ?`, endTime)
}
queryWealth = queryWealth.Group("su_money_transaction_record.employee")
if offset, ok := queryOptions["offset"]; ok {
offset := offset.(int)
if offset > -1 {
queryWealth = queryWealth.Offset(offset)
}
} else {
queryWealth = queryWealth.Offset(0)
}
if limit, ok := queryOptions["limit"]; ok {
limit := limit.(int)
if limit > -1 {
queryWealth = queryWealth.Limit(limit)
}
} else {
queryWealth = queryWealth.Limit(20)
}
if err := queryWealth.Order("employee_su_money DESC").Select(&retWealth); err != nil {
return nil, err
}
// 当前员工财富值排名
// 扣除的贡献值子查询
queryContributionsDecrease := tx.Model(suMoneyTransactionRecordModel)
queryContributionsDecrease = queryContributionsDecrease.Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint")
queryContributionsDecrease = queryContributionsDecrease.ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid")
queryContributionsDecrease = queryContributionsDecrease.ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name")
queryContributionsDecrease = queryContributionsDecrease.ColumnExpr("sum(su_money_transaction_record.su_money) AS employee_contributions_decrease")
queryContributionsDecrease = queryContributionsDecrease.ColumnExpr("RANK() OVER (ORDER BY sum(su_money_transaction_record.su_money) DESC) AS ranking")
if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
queryContributionsDecrease = queryContributionsDecrease.Where("e.company_id = ?", companyId)
}
queryContributionsDecrease = queryContributionsDecrease.Where(`e.status = ?`, 1)
queryContributionsDecrease = queryContributionsDecrease.Where(`su_money_transaction_record.record_type = ?`, 4)
if startTime, ok := queryOptions["startTime"]; ok {
queryContributionsDecrease = queryContributionsDecrease.Where(`su_money_transaction_record.create_time > ?`, startTime)
}
if endTime, ok := queryOptions["endTime"]; ok {
queryContributionsDecrease = queryContributionsDecrease.Where(`su_money_transaction_record.create_time < ?`, endTime)
}
contributionsDecrease := queryContributionsDecrease.Group("su_money_transaction_record.employee")
//if offset, ok := queryOptions["offset"]; ok {
// offset := offset.(int)
// if offset > -1 {
// queryContributionsDecrease = queryContributionsDecrease.Offset(offset)
// }
//} else {
// queryContributionsDecrease = queryContributionsDecrease.Offset(0)
//}
//if limit, ok := queryOptions["limit"]; ok {
// limit := limit.(int)
// if limit > -1 {
// queryContributionsDecrease = queryContributionsDecrease.Limit(limit)
// }
//} else {
// queryContributionsDecrease = queryContributionsDecrease.Limit(20)
//}
//contributionsDecrease := queryWealth.Order("employee_contributions_decrease DESC")
// 贡献值排名
queryContributions := tx.Model()
queryContributions = queryContributions.With("t", contributionsDecrease)
queryContributions = queryContributions.Table("t")
queryContributions = queryContributions.Table("su_money_transaction_records")
queryContributions = queryContributions.Join("JOIN employees AS e ON e.uid = (su_money_transaction_records.employee->>'uid')::bigint")
queryContributions = queryContributions.ColumnExpr("su_money_transaction_records.employee->>'uid' AS uid")
queryContributions = queryContributions.ColumnExpr("su_money_transaction_records.employee->>'employeeName' AS employee_name")
queryContributions = queryContributions.ColumnExpr(`(sum(su_money_transaction_records.su_money) - t.employee_contributions_decrease) AS employees_contributions`)
queryContributions = queryContributions.ColumnExpr("RANK() OVER (ORDER BY sum(su_money_transaction_records.su_money) - t.employee_contributions_decrease DESC) AS ranking")
if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
queryContributions = queryContributions.Where("e.company_id = ?", companyId)
}
queryContributions = queryContributions.Where(`e.status = ?`, 1)
queryContributions = queryContributions.Where(`su_money_transaction_records.record_type IN (?)`, pg.In([]int{2, 3}))
if startTime, ok := queryOptions["startTime"]; ok {
queryContributions = queryContributions.Where(`su_money_transaction_records.create_time > ?`, startTime)
}
if endTime, ok := queryOptions["endTime"]; ok {
queryContributions = queryContributions.Where(`su_money_transaction_records.create_time < ?`, endTime)
}
queryContributions = queryContributions.Group("su_money_transaction_records.employee")
queryContributions = queryContributions.Group("t.employee_contributions_decrease")
if offset, ok := queryOptions["offset"]; ok {
offset := offset.(int)
if offset > -1 {
queryContributions = queryContributions.Offset(offset)
}
} else {
queryContributions = queryContributions.Offset(0)
}
if limit, ok := queryOptions["limit"]; ok {
limit := limit.(int)
if limit > -1 {
queryContributions = queryContributions.Limit(limit)
}
} else {
queryContributions = queryContributions.Limit(20)
}
if err := queryContributions.Order("employees_contributions DESC").Select(&retContributions); err != nil {
return nil, err
}
// 当前员工贡献值排名
return map[string]interface{}{
"employeesWealth": retWealth,
"employeesContributions": retContributions,
}, nil
}
// 排行榜统计,排名
func (dao *EmployeeDao) CalculateEmployeesRankingList(companyId int, startTime time.Time, endTime time.Time, offset int, limit int) (map[string]interface{}, error) {
var retWealth []struct {
Uid int
EmployeeName string
EmployeeSuMoney float64
Ranking int
}
var retContributions []struct { // 员工贡献值
Uid int
EmployeeName string
EmployeesContributions float64
Ranking int
}
var retContributionDecrease []struct { // 员工减少的贡献值
Uid int
EmployeeName string
EmployeesContributions float64
Ranking int
}
tx := dao.transactionContext.PgTx
suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
... ...
... ... @@ -3,6 +3,7 @@ package controllers
import (
"encoding/json"
"fmt"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/suMoney/query"
"github.com/astaxie/beego"
"github.com/linmadan/egglib-go/web/beego/utils"
... ... @@ -140,9 +141,12 @@ func (controller *StatisticsController) EmployeesContributionsStatistics() {
// 员工财富值、贡献值排行榜
func (controller *StatisticsController) RankingListStatistics() {
statisticsService := service.NewStatisticsService(nil)
employeesRankingListStatisticsCommand := &command.EmployeesRankingListStatisticsCommand{}
json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), employeesRankingListStatisticsCommand)
data, err := statisticsService.EmployeesRankingListStatistics(employeesRankingListStatisticsCommand)
//employeesRankingListStatisticsCommand := &command.EmployeesRankingListStatisticsCommand{}
//json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), employeesRankingListStatisticsCommand)
contributionsWealthRankingQuery := &query.ContributionsWealthRankingQuery{}
json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), contributionsWealthRankingQuery)
//data, err := statisticsService.EmployeesRankingListStatistics(employeesRankingListStatisticsCommand)
data, err := statisticsService.ContributionsWealthRanking(contributionsWealthRankingQuery)
var response utils.JsonResponse
if err != nil {
response = utils.ResponseError(controller.Ctx, err)
... ...