statistics.go 9.7 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	"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/domain"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/dao"
)

// 数据统计服务
type StatisticsService struct {
}

// 获取个人消息通知统计
func (statisticsService *StatisticsService) PersonNotificationStatistics(personNotificationStatisticsCommand *command.PersonNotificationStatisticsCommand) (interface{}, error) {
	if err := personNotificationStatisticsCommand.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
	}
	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
	}
	employee, err := employeeRepository.FindOne(map[string]interface{}{
		"uid": personNotificationStatisticsCommand.Uid,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if employee == nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的企业员工")
	}
	if personNotificationStatistics, err := employeeDao.CalculatePersonUnReadNotification(personNotificationStatisticsCommand.Uid); 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 personNotificationStatistics, nil
	}
}

// 获取系统任务统计
func (statisticsService *StatisticsService) SystemTaskStatistics(systemTaskStatisticsCommand *command.SystemTaskStatisticsCommand) (interface{}, error) {
	if err := systemTaskStatisticsCommand.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 taskDao *dao.TaskDao
	if value, err := factory.CreateTaskDao(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	} else {
		taskDao = value
	}
	if systemTaskStatistics, err := taskDao.CalculateSystemTask(systemTaskStatisticsCommand.CompanyId); 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 systemTaskStatistics, nil
	}
}

// 获取个人任务统计
func (statisticsService *StatisticsService) PersonTaskStatistics(personTaskStatisticsCommand *command.PersonTaskStatisticsCommand) (interface{}, error) {
	if err := personTaskStatisticsCommand.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 taskDao *dao.TaskDao
	if value, err := factory.CreateTaskDao(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	} else {
		taskDao = 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
	}
	employee, err := employeeRepository.FindOne(map[string]interface{}{
		"uid": personTaskStatisticsCommand.Uid,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if employee == nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的企业员工")
	}
	if personTaskStatistics, err := taskDao.CalculatePersonTask(personTaskStatisticsCommand.Uid); 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 personTaskStatistics, nil
	}
}

// 获取个人素币统计
func (statisticsService *StatisticsService) PersonSuMoneyStatistics(personSuMoneyStatisticsCommand *command.PersonSuMoneyStatisticsCommand) (interface{}, error) {
	if err := personSuMoneyStatisticsCommand.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
	}

	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
	}
	employee, err := employeeRepository.FindOne(map[string]interface{}{
		"uid": personSuMoneyStatisticsCommand.Uid,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if employee == nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的企业员工")
	}

	if personSuMoneyStatistics, err := employeeDao.CalculatePersonSuMoney(personSuMoneyStatisticsCommand.Uid); 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())
		}
		personSuMoneyStatistics["currentSuMoney"] = employee.SuMoney
		return personSuMoneyStatistics, nil
	}
}

// TODO 获取系统素币统计
func (statisticsService *StatisticsService) SystemSuMoneyStatistics(systemSuMoneyStatisticsCommand *command.SystemSuMoneyStatisticsCommand) (interface{}, error) {
	if err := systemSuMoneyStatisticsCommand.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.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		employeeDao = value
	}

	if systemSuMoneyStatistics, err := employeeDao.CalculateSystemCash(systemSuMoneyStatisticsCommand.CompanyId); 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())
		}
		//systemSuMoneyStatistics["currentSuMoney"] = employee.SuMoney
		return systemSuMoneyStatistics, nil
	}
}

// TODO 获取系统现金统计
func (statisticsService *StatisticsService) SystemCashStatistics(systemCashStatisticsCommand *command.SystemCashStatisticsCommand) (interface{}, error) {


	return nil, nil
}

func NewStatisticsService(options map[string]interface{}) *StatisticsService {
	newStatisticsService := &StatisticsService{}
	return newStatisticsService
}