作者 陈志颖

fix:贡献值、财富值排行榜

... ... @@ -2,16 +2,12 @@ package command
import (
"fmt"
"time"
"github.com/astaxie/beego/validation"
)
type RemoveEmployeeCommand struct {
// 统一用户UID
Uid int64 `json:"uid" valid:"Required"`
// 创建时间
CreateTime time.Time `json:"createTime"`
}
func (removeEmployeeCommand *RemoveEmployeeCommand) ValidateCommand() error {
... ...
... ... @@ -8,6 +8,7 @@ import (
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/employee/query"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/factory"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
"time"
)
// 员工服务
... ... @@ -36,6 +37,7 @@ func (employeeService *EmployeeService) CreateEmployee(createEmployeeCommand *co
EmployeeAccount: createEmployeeCommand.EmployeeAccount,
},
SuMoney: 0,
CreateTime: time.Now(),
}
var employeeRepository domain.EmployeeRepository
if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
... ... @@ -122,6 +124,7 @@ func (employeeService *EmployeeService) UpdateEmployee(updateEmployeeCommand *co
if employee == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateEmployeeCommand.Uid)))
}
updateEmployeeCommand.CreateTime = time.Now()
if err := employee.Update(tool_funs.SimpleStructToMap(updateEmployeeCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
... ...
... ... @@ -402,15 +402,15 @@ func (statisticsService *StatisticsService) ContributionsWealthRanking(contribut
transactionContext.RollbackTransaction()
}()
//// 员工仓储初始化
//var employeeRepository domain.EmployeeRepository
//if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
// "transactionContext": transactionContext,
//}); err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//} else {
// employeeRepository = value
//}
// 员工仓储初始化
var employeeRepository domain.EmployeeRepository
if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
employeeRepository = value
}
// 员工DAO初始化
var employeeDao *dao.EmployeeDao
... ... @@ -476,23 +476,52 @@ func (statisticsService *StatisticsService) ContributionsWealthRanking(contribut
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())
uidName := map[int64]interface{}{}
if _, employees, err := employeeRepository.Find(map[string]interface{}{
"companyId": contributionsWealthRankingQuery.CompanyId,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
for _, employee := range employees {
uidName[employee.EmployeeInfo.Uid] = employee.EmployeeInfo.EmployeeName
}
}
// TODO 排行榜姓名获取
//if count, employees, err := employeeRepository.Find(map[string]interface{}{
// "companyId": contributionsWealthRankingQuery.CompanyId,
// "offset": contributionsWealthRankingQuery.Offset,
// "limit": contributionsWealthRankingQuery.Limit,
//}); err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//}
// 财富值排行榜
for i, _ := range contributionsWealthRankingStatistics.(map[string]interface{})["employeesWealth"].([]struct{Uid int64;EmployeeName string;EmployeeSuMoney float64;Ranking int}) {
uid := contributionsWealthRankingStatistics.(map[string]interface{})["employeesWealth"].([]struct{Uid int64;EmployeeName string;EmployeeSuMoney float64;Ranking int})[i].Uid
if uidName[uid] != nil {
contributionsWealthRankingStatistics.(map[string]interface{})["employeesWealth"].([]struct{Uid int64;EmployeeName string;EmployeeSuMoney float64;Ranking int})[i].EmployeeName = uidName[uid].(string)
}
}
// 个人财富值
currentEmployeeWealth := contributionsWealthRankingStatistics.(map[string]interface{})["currentEmployeeWealth"].(struct{Uid int64;EmployeeName string;EmployeeSuMoney float64;Ranking int})
uidWealth := contributionsWealthRankingStatistics.(map[string]interface{})["currentEmployeeWealth"].(struct{Uid int64;EmployeeName string;EmployeeSuMoney float64;Ranking int}).Uid
if uidName[uidWealth] != nil {
currentEmployeeWealth.EmployeeName = uidName[uidWealth].(string)
}
contributionsWealthRankingStatistics.(map[string]interface{})["currentEmployeeWealth"] = currentEmployeeWealth
// 贡献值排行版
for i, _ := range contributionsWealthRankingStatistics.(map[string]interface{})["employeesContributions"].([]struct { Uid int64; EmployeeName string; EmployeesContributions float64; Ranking int }) {
uid := contributionsWealthRankingStatistics.(map[string]interface{})["employeesContributions"].([]struct{Uid int64;EmployeeName string;EmployeesContributions float64;Ranking int})[i].Uid
if uidName[uid] != nil {
contributionsWealthRankingStatistics.(map[string]interface{})["employeesContributions"].([]struct{Uid int64;EmployeeName string;EmployeesContributions float64;Ranking int})[i].EmployeeName = uidName[uid].(string)
}
}
// 个人贡献值
currentEmployeeContributions := contributionsWealthRankingStatistics.(map[string]interface{})["currentEmployeeContributions"].(struct{Uid int64;EmployeeName string;EmployeesContributions float64;Ranking int})
uidContributions := contributionsWealthRankingStatistics.(map[string]interface{})["currentEmployeeContributions"].(struct{Uid int64;EmployeeName string;EmployeesContributions float64;Ranking int}).Uid
if uidName[uidContributions] != nil {
currentEmployeeContributions.EmployeeName = uidName[uidContributions].(string)
}
contributionsWealthRankingStatistics.(map[string]interface{})["currentEmployeeContributions"] = currentEmployeeContributions
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return contributionsWealthRankingStatistics, nil
}
... ...
... ... @@ -134,9 +134,6 @@ func (unifiedUserCenterService *UnifiedUserCenterService) SyncEmployeeCallback(s
if value, ok := addEmployee["status"]; ok {
status = int(value.(float64))
}
// TODO 增加员工判断
employee := &domain.Employee{
CompanyId: companyId,
EmployeeInfo: &domain.EmployeeInfo{
... ...
... ... @@ -309,25 +309,25 @@ func (dao *EmployeeDao) CalculateContributionsTransactionRecord(uid int64, trans
// 贡献值、财富值总榜和年榜,
func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]interface{}) (interface{}, error) {
var retWealth []struct { // 个人财富值
Uid int
Uid int64
EmployeeName string
EmployeeSuMoney float64
Ranking int
}
var retEmployeeWealth []struct { // 个人贡献值
Uid int
Uid int64
EmployeeName string
EmployeeSuMoney float64
Ranking int
}
var retContributions []struct { // 员工贡献值
Uid int
Uid int64
EmployeeName string
EmployeesContributions float64
Ranking int
}
var retEmployeeContributions []struct { // 员工贡献值
Uid int
Uid int64
EmployeeName string
EmployeesContributions float64
Ranking int
... ... @@ -342,7 +342,7 @@ func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]inter
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(case WHEN su_money_transaction_record.record_type IN (2, 3, 6) THEN su_money_transaction_record.su_money ELSE 0 end) - sum(case WHEN su_money_transaction_record.record_type IN (4, 1, 5) THEN su_money_transaction_record.su_money ELSE 0 end) AS employee_su_money")
queryWealth = queryWealth.ColumnExpr("ROW_NUMBER() OVER (ORDER BY sum(case WHEN su_money_transaction_record.record_type IN (2, 3, 6) THEN su_money_transaction_record.su_money ELSE 0 end) - sum(case WHEN su_money_transaction_record.record_type IN (4, 1, 5) THEN su_money_transaction_record.su_money ELSE 0 end), e.create_time DESC) AS ranking")
queryWealth = queryWealth.ColumnExpr("ROW_NUMBER() OVER (ORDER BY sum(case WHEN su_money_transaction_record.record_type IN (2, 3, 6) THEN su_money_transaction_record.su_money ELSE 0 end) - sum(case WHEN su_money_transaction_record.record_type IN (4, 1, 5) THEN su_money_transaction_record.su_money ELSE 0 end) DESC) AS ranking")
queryWealth = queryWealth.Where(`e.status = ?`, 1)
//queryWealth = queryWealth.GroupExpr("su_money_transaction_record.employee->>'uid',su_money_transaction_record.employee->>'employeeName'")
queryWealth = queryWealth.GroupExpr("su_money_transaction_record.employee->>'uid'")
... ... @@ -371,7 +371,7 @@ func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]inter
} else {
queryWealth = queryWealth.Limit(20)
}
queryWealth = queryWealth.Order("employee_su_money DESC")
queryWealth = queryWealth.Order("ranking ASC")
if err := queryWealth.Select(&retWealth); err != nil {
return nil, err
}
... ... @@ -380,9 +380,9 @@ func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]inter
queryWealthWith := tx.Model(suMoneyTransactionRecordModel)
queryWealthWith = queryWealthWith.Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint")
queryWealthWith = queryWealthWith.ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid")
queryWealthWith = queryWealthWith.ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name")
//queryWealthWith = queryWealthWith.ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name")
queryWealthWith = queryWealthWith.ColumnExpr("sum(case WHEN su_money_transaction_record.record_type IN (2, 3, 6) THEN su_money_transaction_record.su_money ELSE 0 end) - sum(case WHEN su_money_transaction_record.record_type IN (4, 1, 5) THEN su_money_transaction_record.su_money ELSE 0 end) AS employee_su_money")
queryWealthWith = queryWealthWith.ColumnExpr("ROW_NUMBER() OVER (ORDER BY sum(case WHEN su_money_transaction_record.record_type IN (2, 3, 6) THEN su_money_transaction_record.su_money ELSE 0 end) - sum(case WHEN su_money_transaction_record.record_type IN (4, 1, 5) THEN su_money_transaction_record.su_money ELSE 0 end), e.create_time DESC) AS ranking")
queryWealthWith = queryWealthWith.ColumnExpr("ROW_NUMBER() OVER (ORDER BY sum(case WHEN su_money_transaction_record.record_type IN (2, 3, 6) THEN su_money_transaction_record.su_money ELSE 0 end) - sum(case WHEN su_money_transaction_record.record_type IN (4, 1, 5) THEN su_money_transaction_record.su_money ELSE 0 end) DESC) AS ranking")
queryWealthWith = queryWealthWith.Where(`e.status = ?`, 1)
if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
queryWealthWith = queryWealthWith.Where("e.company_id = ?", companyId)
... ... @@ -393,12 +393,13 @@ func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]inter
if endTime, ok := queryOptions["endTime"]; ok && !endTime.(time.Time).IsZero() {
queryWealthWith = queryWealthWith.Where(`su_money_transaction_record.create_time < ?`, endTime)
}
queryWealthRestoreWith := queryWealthWith.GroupExpr("su_money_transaction_record.employee->>'uid',su_money_transaction_record.employee->>'employeeName'")
//queryWealthRestoreWith := queryWealthWith.GroupExpr("su_money_transaction_record.employee->>'uid',su_money_transaction_record.employee->>'employeeName', e.create_time")
queryWealthRestoreWith := queryWealthWith.GroupExpr("su_money_transaction_record.employee->>'uid'")
querySelfWealth := tx.Model()
querySelfWealth = querySelfWealth.With("t", queryWealthRestoreWith)
querySelfWealth = querySelfWealth.Table("t")
querySelfWealth = querySelfWealth.ColumnExpr("t.uid AS uid")
querySelfWealth = querySelfWealth.ColumnExpr("t.employee_name AS employee_name")
//querySelfWealth = querySelfWealth.ColumnExpr("t.employee_name AS employee_name")
querySelfWealth = querySelfWealth.ColumnExpr("t.employee_su_money AS employee_su_money")
querySelfWealth = querySelfWealth.ColumnExpr("t.ranking AS ranking")
if uid, ok := queryOptions["uid"]; ok {
... ... @@ -420,7 +421,7 @@ func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]inter
queryContributions = queryContributions.ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid")
//queryContributions = queryContributions.ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name")
queryContributions = queryContributions.ColumnExpr("sum(case WHEN su_money_transaction_record.record_type IN (2, 3) THEN su_money_transaction_record.su_money ELSE 0 end) - sum(case WHEN su_money_transaction_record.record_type = 4 THEN su_money_transaction_record.su_money ELSE 0 end) AS employees_contributions")
queryContributions = queryContributions.ColumnExpr("ROW_NUMBER() OVER (ORDER BY sum(case WHEN su_money_transaction_record.record_type IN (2, 3) THEN su_money_transaction_record.su_money ELSE 0 end) - sum(case WHEN su_money_transaction_record.record_type = 4 THEN su_money_transaction_record.su_money ELSE 0 end), e.create_time DESC) AS ranking")
queryContributions = queryContributions.ColumnExpr("ROW_NUMBER() OVER (ORDER BY sum(case WHEN su_money_transaction_record.record_type IN (2, 3) THEN su_money_transaction_record.su_money ELSE 0 end) - sum(case WHEN su_money_transaction_record.record_type = 4 THEN su_money_transaction_record.su_money ELSE 0 end) DESC) AS ranking")
queryContributions = queryContributions.Where(`e.status = ?`, 1)
//queryContributions = queryContributions.GroupExpr("su_money_transaction_record.employee->>'uid',su_money_transaction_record.employee->>'employeeName'")
queryContributions = queryContributions.GroupExpr("su_money_transaction_record.employee->>'uid'")
... ... @@ -449,7 +450,7 @@ func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]inter
} else {
queryContributions = queryContributions.Limit(20)
}
if err := queryContributions.Order("employees_contributions DESC").Select(&retContributions); err != nil {
if err := queryContributions.Order("ranking ASC").Select(&retContributions); err != nil {
return nil, err
}
... ... @@ -457,9 +458,9 @@ func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]inter
queryContributionsWith := tx.Model(suMoneyTransactionRecordModel)
queryContributionsWith = queryContributionsWith.Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint")
queryContributionsWith = queryContributionsWith.ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid")
queryContributionsWith = queryContributionsWith.ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name")
//queryContributionsWith = queryContributionsWith.ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name")
queryContributionsWith = queryContributionsWith.ColumnExpr("sum(case WHEN su_money_transaction_record.record_type IN (2, 3) THEN su_money_transaction_record.su_money ELSE 0 end) - sum(case WHEN su_money_transaction_record.record_type = 4 THEN su_money_transaction_record.su_money ELSE 0 end) AS employees_contributions")
queryContributionsWith = queryContributionsWith.ColumnExpr("ROW_NUMBER() OVER (ORDER BY sum(case WHEN su_money_transaction_record.record_type IN (2, 3) THEN su_money_transaction_record.su_money ELSE 0 end) - sum(case WHEN su_money_transaction_record.record_type = 4 THEN su_money_transaction_record.su_money ELSE 0 end) DESC), e.create_time AS ranking")
queryContributionsWith = queryContributionsWith.ColumnExpr("ROW_NUMBER() OVER (ORDER BY sum(case WHEN su_money_transaction_record.record_type IN (2, 3) THEN su_money_transaction_record.su_money ELSE 0 end) - sum(case WHEN su_money_transaction_record.record_type = 4 THEN su_money_transaction_record.su_money ELSE 0 end) DESC) AS ranking")
queryContributionsWith = queryContributionsWith.Where(`e.status = ?`, 1)
if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
queryContributionsWith = queryContributionsWith.Where("e.company_id = ?", companyId)
... ... @@ -470,12 +471,13 @@ func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]inter
if endTime, ok := queryOptions["endTime"]; ok && !endTime.(time.Time).IsZero() {
queryContributionsWith = queryContributionsWith.Where(`su_money_transaction_record.create_time < ?`, endTime)
}
contributionsWith := queryContributionsWith.GroupExpr("su_money_transaction_record.employee->>'uid',su_money_transaction_record.employee->>'employeeName'")
//contributionsWith := queryContributionsWith.GroupExpr("su_money_transaction_record.employee->>'uid',su_money_transaction_record.employee->>'employeeName', e.create_time")
contributionsWith := queryContributionsWith.GroupExpr("su_money_transaction_record.employee->>'uid'")
querySelfContributions := tx.Model()
querySelfContributions = querySelfContributions.With("t", contributionsWith)
querySelfContributions = querySelfContributions.Table("t")
querySelfContributions = querySelfContributions.ColumnExpr("t.uid AS uid")
querySelfContributions = querySelfContributions.ColumnExpr("t.employee_name AS employee_name")
//querySelfContributions = querySelfContributions.ColumnExpr("t.employee_name AS employee_name")
querySelfContributions = querySelfContributions.ColumnExpr("t.employees_contributions AS employees_contributions")
querySelfContributions = querySelfContributions.ColumnExpr("t.ranking AS ranking")
if uid, ok := queryOptions["uid"]; ok {
... ...
... ... @@ -34,6 +34,15 @@ func IsContain(items []interface{}, item string) bool {
return false
}
func IsContainInt(items map[int64]interface{}, item int64) bool {
for _, eachItem := range items {
if eachItem == item {
return true
}
}
return false
}
type LocalTime time.Time
// MarshalJSON satify the json marshal interface
... ...