作者 陈志颖

refactor:重构清单排行榜

... ... @@ -383,7 +383,7 @@ 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())
... ...
package query
import (
"fmt"
"github.com/astaxie/beego/validation"
)
type ExchangeListRankingQuery struct {
ActivityId int64 `json:"activityId"`
CompanyId int64 `json:"companyId"` // 公司ID
Offset int `json:"offset,omitempty"` // 查询偏离量
Limit int `json:"limit,omitempty"` // 查询限制
Uid int64 `json:"uid"` // 统一用户id
}
func (exchangeListRankingQuery *ExchangeListRankingQuery) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(exchangeListRankingQuery)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
... ... @@ -1060,7 +1060,7 @@ func (cashPoolService *CashPoolService) GetExchangeCashPerson(getExchangeCashPer
//
//}
// 返回兑换素币清单列表
// TODO 返回兑换素币清单列表
func (cashPoolService *CashPoolService) ListExchangeCashPerson(listExchangeCashPersonQuery *query.ListExchangeCashPersonQuery) (interface{}, error) {
if err := listExchangeCashPersonQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
... ... @@ -1598,6 +1598,41 @@ func (cashPoolService *CashPoolService) ListExchangeCashPersonById(exportExchang
}
}
// TODO 员工兑换清单榜单
func (cashPoolService *CashPoolService) ExchangeListRanking(exchangeListRankingQuery *query.ExchangeListRankingQuery) (interface{}, error) {
if err := exchangeListRankingQuery.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 cashPoolDao *dao.CashPoolDao
if value, err := factory.CreateCashPoolDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
cashPoolDao = value
}
if exchangeCashListRankingStatistics, err := cashPoolDao.ExchangeCashListRanking(tool_funs.SimpleStructToMap(exchangeListRankingQuery)); 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 exchangeCashListRankingStatistics, nil
}
}
func NewCashPoolService(options map[string]interface{}) *CashPoolService {
newCashPoolService := &CashPoolService{}
return newCashPoolService
... ...
... ... @@ -13,6 +13,8 @@ type SearchTaskCommand struct {
Sponsor int64 `json:"sponsor,omitempty"`
// 任务内容匹配
TaskContentMatch string `json:"taskContentMatch,omitempty"`
// 任务名称匹配
TaskNameMatch string `json:"taskNameMatch,omitempty"`
// 任务类型
TaskType int `json:"taskType,omitempty"`
// 任务类型ID列表
... ...
... ... @@ -77,6 +77,133 @@ func (dao *CashPoolDao) CalculateActivityExchangedSuMoney(activityId int64) (map
}, nil
}
// TODO 返回兑换清单榜单
func (dao *CashPoolDao) ExchangeCashListRanking(queryOptions map[string]interface{}) (map[string]interface{}, error) {
var retPeople []struct {
Uid int
EmployeeName string
SuMoney float64
Cash float64
Ranking int
}
var retEmployee []struct {
Uid int
EmployeeName string
SuMoney float64
Cash float64
Ranking int
}
tx := dao.transactionContext.PgTx
// 清单人员排名
exchangeCashPersonListModels := new(models.ExchangeCashPersonList)
queryPeople := tx.Model(exchangeCashPersonListModels)
queryPeople = queryPeople.Join("JOIN employees AS e ON e.uid = exchange_cash_person_list.uid")
queryPeople = queryPeople.ColumnExpr("exchange_cash_person_list.uid AS uid")
queryPeople = queryPeople.ColumnExpr("exchange_cash_person_list.employee_name AS employee_name")
queryPeople = queryPeople.ColumnExpr("sum(exchange_cash_person_list.exchanged_cash) AS cash")
queryPeople = queryPeople.ColumnExpr("sum(exchange_cash_person_list.exchanged_su_money) AS su_money")
queryPeople = queryPeople.ColumnExpr("RANK() OVER (ORDER BY sum(exchange_cash_person_list.exchanged_su_money) DESC) AS ranking")
if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
queryPeople = queryPeople.Where("e.company_id = ?", companyId)
}
if activityId, ok := queryOptions["activityId"]; ok && (activityId.(int64) != 0){
queryPeople = queryPeople.Where("exchange_cash_person_list.activity_id = ?", activityId)
}
queryPeople = queryPeople.Group("exchange_cash_person_list.uid")
queryPeople = queryPeople.Group("exchange_cash_person_list.employee_name")
if offset, ok := queryOptions["offset"]; ok {
offset := offset.(int)
if offset > -1 {
queryPeople = queryPeople.Offset(offset)
}
} else {
queryPeople = queryPeople.Offset(0)
}
if limit, ok := queryOptions["limit"]; ok {
limit := limit.(int)
if limit > -1 {
queryPeople = queryPeople.Limit(limit)
}
} else {
queryPeople = queryPeople.Limit(20)
}
if err := queryPeople.Order("su_money DESC").Select(&retPeople); err != nil {
return nil, err
}
// 当前员工排名
queryEmployee := tx.Model(exchangeCashPersonListModels)
queryEmployee = queryEmployee.Join("JOIN employees AS e ON e.uid = exchange_cash_person_list.uid")
queryEmployee = queryEmployee.ColumnExpr("exchange_cash_person_list.uid AS uid")
queryEmployee = queryEmployee.ColumnExpr("exchange_cash_person_list.employee_name AS employee_name")
queryEmployee = queryEmployee.ColumnExpr("sum(exchange_cash_person_list.exchanged_cash) AS cash")
queryEmployee = queryEmployee.ColumnExpr("sum(exchange_cash_person_list.exchanged_su_money) AS su_money")
queryEmployee = queryEmployee.ColumnExpr("RANK() OVER (ORDER BY sum(exchange_cash_person_list.exchanged_su_money) DESC) AS ranking")
if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
queryEmployee = queryEmployee.Where("e.company_id = ?", companyId)
}
if activityId, ok := queryOptions["activityId"]; ok && (activityId.(int64) != 0) {
queryEmployee = queryEmployee.Where("exchange_cash_person_list.activity_id = ?", activityId)
}
if uid, ok := queryOptions["uid"]; ok {
queryEmployee = queryEmployee.Where("exchange_cash_person_list.uid = ?", uid)
}
queryEmployee = queryEmployee.Group("exchange_cash_person_list.uid")
queryEmployee = queryEmployee.Group("exchange_cash_person_list.employee_name")
if err := queryEmployee.Order("su_money DESC").Select(&retEmployee); err != nil {
return nil, err
}
// 清单已兑换素币
var activityExchangedSuMoney float64
queryListSuMoney := tx.Model(exchangeCashPersonListModels)
queryListSuMoney = queryListSuMoney.Join("JOIN employees AS e ON e.uid = exchange_cash_person_list.uid")
queryListSuMoney = queryListSuMoney.ColumnExpr("sum(exchange_cash_person_list.exchanged_su_money) AS activity_exchanged_su_money")
if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
queryListSuMoney = queryListSuMoney.Where("e.company_id = ?", companyId)
}
if activityId, ok := queryOptions["activityId"]; ok && (activityId.(int64) != 0) {
queryListSuMoney = queryListSuMoney.Where("exchange_cash_person_list.activity_id = ?", activityId)
}
if err := queryListSuMoney.Select(&activityExchangedSuMoney); err != nil {
return nil, err
}
// 清单已兑换现金
var activityExchangedCash float64
queryListCash := tx.Model(exchangeCashPersonListModels)
queryListCash = queryListCash.Join("JOIN employees AS e ON e.uid = exchange_cash_person_list.uid")
queryListCash = queryListCash.ColumnExpr("sum(exchange_cash_person_list.exchanged_cash) AS activity_exchanged_cash")
if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
queryListCash = queryListCash.Where("e.company_id = ?", companyId)
}
if activityId, ok := queryOptions["activityId"]; ok && (activityId.(int64) != 0) {
queryListCash = queryListCash.Where("exchange_cash_person_list.activity_id = ?", activityId)
}
if err := queryListCash.Select(&activityExchangedCash); err != nil {
return nil, err
}
// 清单计数
queryCount := tx.Model(exchangeCashPersonListModels)
count, err := queryCount.Count()
if err != nil {
return nil, err
}
return map[string]interface{} {
"people": retPeople, // 员工排行榜
"count": count, // 计数
"currentEmployee": retEmployee[0], // 当前员工排名
"exchangedSuMoney": activityExchangedSuMoney, // 清单已兑换素币
"exchangedCash": activityExchangedCash, // 清单已兑换现金
}, nil
}
func NewCashPoolDao(transactionContext *pgTransaction.TransactionContext) (*CashPoolDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
... ...
... ... @@ -283,7 +283,7 @@ func (dao *EmployeeDao) CalculateEmployeesSuMoney(companyId int, startTime time.
// }, nil
//}
// 排行榜统计
// TODO 排行榜统计,排名
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
... ...
... ... @@ -288,7 +288,6 @@ func (dao *TaskDao) CalculatePersonTask(uid int64) (map[string]interface{}, erro
} else {
completedAsParticipator = int64(count)
}
// TODO 已过期竞标任务统计
if count, err := tx.Model(taskModel).
Where(`task.sponsor @> '{"uid":?}'`, uid).
//Where(`task.task_type = ? `, domain.TASK_STATUS_EXPIRED).
... ...
... ... @@ -95,6 +95,8 @@ func (repository *TaskRepository) FindOne(queryOptions map[string]interface{}) (
return repository.transformPgModelToDomainModel(taskModel)
}
}
// TODO 添加任务名称搜索
func (repository *TaskRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Task, error) {
tx := repository.transactionContext.PgTx
var taskModels []*models.Task
... ...
... ... @@ -479,4 +479,20 @@ func (controller *SuMoneyController) ExportExchangeList() {
// TODO 导出素币流水记录,选择导出(ids),增加导出失败信息
func (controller *SuMoneyController) ExportSuMoneyTransactionRecord() {
}
// 返回兑换素币清单排行榜
func (controller *SuMoneyController) ExchangeCashListRanking() {
cashPoolService := service.NewCashPoolService(nil)
exchangeListRankingQuery := &query.ExchangeListRankingQuery{}
json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), exchangeListRankingQuery)
data, err := cashPoolService.ExchangeListRanking(exchangeListRankingQuery)
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
... ...
... ... @@ -34,4 +34,5 @@ func init() {
beego.Router("/cash-pool/activity/exchange-list/:listId", &controllers.SuMoneyController{}, "Delete:RemoveExchangeCashPerson") // 删除素币兑换清单
beego.Router("/cash-pool/activity/exchange-list/import", &controllers.SuMoneyController{}, "Post:ImportExchangeList") // 导入素币兑换清单
beego.Router("/cash-pool/activity/exchange-list/export", &controllers.SuMoneyController{}, "Post:ExportExchangeList") // 导出素币兑换清单
beego.Router("/cash-pool/activity/exchange-list/ranking", &controllers.SuMoneyController{}, "Post:ExchangeCashListRanking") // 兑换素币清单排行榜
}
... ...