作者 陈志颖

feat:合并排行榜

package command
import (
"fmt"
"github.com/astaxie/beego/validation"
"time"
)
type EmployeesRankingListStatisticsCommand struct {
CompanyId int `json:"companyId" valid:"Required"` // 公司id
StartTime time.Time `json:"startTime"` // 年榜开始时间
EndTime time.Time `json:"endTime"` // 年榜结束时间
}
func (employeesRankingListStatisticsCommand *EmployeesRankingListStatisticsCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(employeesRankingListStatisticsCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
... ... @@ -361,6 +361,46 @@ func (statisticsService *StatisticsService) EmployeesContributionsStatistics(emp
}
}
// TODO 员工排行榜统计
func (statisticsService *StatisticsService) EmployeesRankingListStatistics(employeesRankingListStatisticsCommand *command.EmployeesRankingListStatisticsCommand) (interface{}, error) {
if err := employeesRankingListStatisticsCommand.ValidateCommand(); 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 employeesRankingListStatisticsCommand.StartTime.IsZero() && employeesRankingListStatisticsCommand.EndTime.IsZero() {
employeesRankingListStatisticsCommand.StartTime = time.Date(1971, time.Month(1), 1, 0, 0, 0, 0, time.Now().Location())
employeesRankingListStatisticsCommand.EndTime = time.Now().Local()
}
if employeesRankingListStatistics, err := employeeDao.CalculateEmployeesRankingList(employeesRankingListStatisticsCommand.CompanyId, employeesRankingListStatisticsCommand.StartTime, employeesRankingListStatisticsCommand.EndTime); 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 employeesRankingListStatistics, nil
}
}
func NewStatisticsService(options map[string]interface{}) *StatisticsService {
newStatisticsService := &StatisticsService{}
return newStatisticsService
... ...
... ... @@ -283,6 +283,83 @@ func (dao *EmployeeDao) CalculateEmployeesSuMoney(companyId int, startTime time.
// }, nil
//}
// 排行榜统计
func (dao *EmployeeDao) CalculateEmployeesRankingList(companyId int, startTime time.Time, endTime time.Time) (map[string]interface{}, error) {
var retWealth []struct {
Uid int
EmployeeName string
EmployeeSuMoney float64
}
var retContributions []struct { // 员工贡献值
Uid int
EmployeeName string
EmployeesContributions float64
}
var retContributionDecrease []struct { // 员工减少的贡献值
Uid int
EmployeeName string
EmployeesContributions float64
}
tx := dao.transactionContext.PgTx
suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
// 员工财富值
if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid").
ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name").
ColumnExpr("sum(su_money_transaction_record.su_money) AS employee_su_money").
Where(`e.company_id = ?`, companyId).
Where(`e.status = ?`, 1).
Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3})). // 增加,任务奖励的
Where(`su_money_transaction_record.create_time > ?`, startTime).
Where(`su_money_transaction_record.create_time < ?`, endTime).
Group("su_money_transaction_record.employee").
Order("employee_su_money DESC").
Select(&retWealth); err != nil {
return nil, err
}
// 增加的贡献值
if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid").
ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name").
ColumnExpr("sum(su_money_transaction_record.su_money) AS employees_contributions").
Where(`e.company_id = ?`, companyId).
Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3})).
Where(`e.status = ?`, 1).
Where(`su_money_transaction_record.create_time > ?`, startTime).
Where(`su_money_transaction_record.create_time < ?`, endTime).
Group("su_money_transaction_record.employee").
Order("employees_contributions DESC").
Select(&retContributions); err != nil {
return nil, err
}
// 减少的贡献值
if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid").
ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name").
ColumnExpr("sum(su_money_transaction_record.su_money) AS employees_contributions").
Where(`e.company_id = ?`, companyId).
Where(`su_money_transaction_record.record_type = ?`, 4).
Where(`e.status = ?`, 1).
Where(`su_money_transaction_record.create_time > ?`, startTime).
Where(`su_money_transaction_record.create_time < ?`, endTime).
Group("su_money_transaction_record.employee").
Order("employees_contributions DESC").
Select(&retContributionDecrease); err != nil {
return nil, err
}
for i := 0; i < len(retContributions); i++ {
for j := 0; j < len(retContributionDecrease); j++ {
if retContributions[i].Uid == retContributionDecrease[j].Uid {
retContributions[i].EmployeesContributions -= retContributionDecrease[j].EmployeesContributions
}
}
}
return map[string]interface{}{
"employeesContributions": retContributions,
"employeesWealth": retWealth,
}, nil
}
// 员工贡献值统计
func (dao *EmployeeDao) CalculateEmployeesContributions(companyId int, startTime time.Time, endTime time.Time) (map[string]interface{}, error) {
var ret []struct { // 员工贡献值
... ...
... ... @@ -126,7 +126,6 @@ func (controller *StatisticsController) EmployeesContributionsStatistics() {
statisticsService := service.NewStatisticsService(nil)
employeesContributionsStatisticsCommand := &command.EmployeesContributionsStatisticsCommand{}
json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), employeesContributionsStatisticsCommand)
fmt.Print(employeesContributionsStatisticsCommand, "\n")
data, err := statisticsService.EmployeesContributionsStatistics(employeesContributionsStatisticsCommand)
var response utils.JsonResponse
if err != nil {
... ... @@ -136,4 +135,20 @@ func (controller *StatisticsController) EmployeesContributionsStatistics() {
}
controller.Data["json"] = response
controller.ServeJSON()
}
// TODO 合并员工财富值、贡献值排行榜
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)
var response utils.JsonResponse
if err != nil {
response = utils.ResponseError(controller.Ctx, err)
} else {
response = utils.ResponseData(controller.Ctx, data)
}
controller.Data["json"] = response
controller.ServeJSON()
}
\ No newline at end of file
... ...
... ... @@ -14,4 +14,5 @@ func init() {
beego.Router("/statistics/system-cash", &controllers.StatisticsController{}, "Post:SystemCashStatistics") // 系统现金统计
beego.Router("/statistics/employees-su-money", &controllers.StatisticsController{}, "Post:EmployeesSuMoneyStatistics") // 员工财富值统计
beego.Router("/statistics/employees-contributions", &controllers.StatisticsController{}, "Post:EmployeesContributionsStatistics") // 员工贡献值统计
beego.Router("/statistics/employees-ranking-list", &controllers.StatisticsController{}, "Post:RankingListStatistics") // 员工财富值、贡献值排行榜
}
... ...