su_money.go 13.1 KB
package service

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

// 素币服务
type SuMoneyService struct {
}

// 操作素币
func (suMoneyService *SuMoneyService) OperationSuMoney(operationSuMoneyCommand *command.OperationSuMoneyCommand) (interface{}, error) {
	if err := operationSuMoneyCommand.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 operationSuMoneyService service.OperationSuMoneyService
	if value, err := factory.CreateOperationSuMoneyService(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		operationSuMoneyService = value
	}
	if task, err := operationSuMoneyService.Operation(operationSuMoneyCommand.Uid, operationSuMoneyCommand.Operator, operationSuMoneyCommand.SuMoney, operationSuMoneyCommand.OperationType, operationSuMoneyCommand.OperationDescription); 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 task, nil
	}
}

// 返回素币事务记录
func (suMoneyService *SuMoneyService) GetSuMoneyTransactionRecord(getSuMoneyTransactionRecordQuery *query.GetSuMoneyTransactionRecordQuery) (interface{}, error) {
	if err := getSuMoneyTransactionRecordQuery.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 suMoneyTransactionRecordRepository domain.SuMoneyTransactionRecordRepository
	if value, err := factory.CreateSuMoneyTransactionRecordRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		suMoneyTransactionRecordRepository = value
	}
	if suMoneyTransactionRecord, err := suMoneyTransactionRecordRepository.FindOne(map[string]interface{}{"suMoneyTransactionRecordId": getSuMoneyTransactionRecordQuery.SuMoneyTransactionRecordId}); 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 suMoneyTransactionRecord, nil
	}
}

// 素币兑换
func (suMoneyService *SuMoneyService) ExchangeSuMoney(exchangeSuMoneyCommand *command.ExchangeSuMoneyCommand) (interface{}, error) {
	if err := exchangeSuMoneyCommand.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 exchangeSuMoneyService service.ExchangeSuMoneyService
	if value, err := factory.CreateExchangeSuMoneyService(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		exchangeSuMoneyService = value
	}
	if task, err := exchangeSuMoneyService.Exchange(exchangeSuMoneyCommand.Uid, exchangeSuMoneyCommand.Operator, exchangeSuMoneyCommand.SuMoney, exchangeSuMoneyCommand.ExchangeDescription); 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 task, nil
	}
}

// 搜索素币事务记录
func (suMoneyService *SuMoneyService) SearchSuMoneyTransactionRecord(searchSuMoneyTransactionRecordCommand *command.SearchSuMoneyTransactionRecordCommand) (interface{}, error) {
	if err := searchSuMoneyTransactionRecordCommand.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 suMoneyTransactionRecordRepository domain.SuMoneyTransactionRecordRepository
	if value, err := factory.CreateSuMoneyTransactionRecordRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		suMoneyTransactionRecordRepository = value
	}

	if count, suMoneyTransactionRecords, err := suMoneyTransactionRecordRepository.Find(tool_funs.SimpleStructToMap(searchSuMoneyTransactionRecordCommand)); 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())
		}

		for _, suMoneyTransactionRecord := range suMoneyTransactionRecords {
			contributions := []int{2,3,4}
			suMoney := []int{1,2,3,4,5}
			// 查询贡献值时规范描述
			if utils.StringSliceEqualBCE(searchSuMoneyTransactionRecordCommand.RecordTypes, contributions) {
				switch suMoneyTransactionRecord.RecordType {
				case 2:  // 任务奖励
					suMoneyTransactionRecord.RecordDescription = suMoneyTransactionRecord.RecordDescription + "奖励"
				case 3:  // 增加
					suMoneyTransactionRecord.RecordDescription = "增加贡献"
				case 4:  // 扣除
					suMoneyTransactionRecord.RecordDescription = "扣除贡献"
				}
			}
			// 查询素币值时规范描述
			if utils.StringSliceEqualBCE(searchSuMoneyTransactionRecordCommand.RecordTypes, suMoney) {
				switch suMoneyTransactionRecord.RecordType {
				case 1:  // 兑换物资
					suMoneyTransactionRecord.RecordDescription = "兑换物资"
				case 2:  // 任务奖励
					suMoneyTransactionRecord.RecordDescription = suMoneyTransactionRecord.RecordDescription + "奖励"
				case 3:  // 增加
					suMoneyTransactionRecord.RecordDescription = "增加素币"
				case 4:  // 扣除
					suMoneyTransactionRecord.RecordDescription = "扣除素币"
				case 5:  // 兑换现金
					suMoneyTransactionRecord.RecordDescription = "素币兑换现金"
				}
			}
		}
		return map[string]interface{}{
			"count":                     count,
			"suMoneyTransactionRecords": suMoneyTransactionRecords,
		}, nil
	}
}

// 素币事务记录统计
func (suMoneyService *SuMoneyService) SuMoneyTransactionRecordStatistics(suMoneyTransactionRecordStatisticsCommand *command.SuMoneyTransactionRecordStatisticsCommand) (interface{}, error) {
	if err := suMoneyTransactionRecordStatisticsCommand.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 calculateResult, err := employeeDao.CalculateSuMoneyTransactionRecord(suMoneyTransactionRecordStatisticsCommand.Uid, suMoneyTransactionRecordStatisticsCommand.TransactionStartTime, suMoneyTransactionRecordStatisticsCommand.TransactionEndTime); 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 calculateResult, nil
	}
}

// 贡献值事务记录统计
func (suMoneyService *SuMoneyService) ContributionsTransactionRecordStatistics(contributionsTransactionRecordStatisticsCommand *command.ContributionsTransactionRecordStatisticsCommand) (interface{}, error) {
	if err := contributionsTransactionRecordStatisticsCommand.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 calculateResult, err := employeeDao.CalculateContributionsTransactionRecord(contributionsTransactionRecordStatisticsCommand.Uid, contributionsTransactionRecordStatisticsCommand.TransactionStartTime, contributionsTransactionRecordStatisticsCommand.TransactionEndTime); 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 calculateResult, nil
	}
}

// 根据id获取事务记录
func (suMoneyService *SuMoneyService) ListSuMoneyTransactionRecordById(exportSuMoneyTransactionRecordCommand *command.ExportTransactionRecordCommand) ([]*domain.SuMoneyTransactionRecord, error) {
	if err := exportSuMoneyTransactionRecordCommand.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()
	}()

	if len(exportSuMoneyTransactionRecordCommand.IDs) == 0 {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "未勾选需要导出的数据")
	}

	var suMoneyTransactionRecordRepository domain.SuMoneyTransactionRecordRepository
	if value, err := factory.CreateSuMoneyTransactionRecordRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		suMoneyTransactionRecordRepository = value
	}

	if _, suMoneyTransactionRecords, err := suMoneyTransactionRecordRepository.FindById(tool_funs.SimpleStructToMap(exportSuMoneyTransactionRecordCommand)); 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 suMoneyTransactionRecords, nil
	}
}


func NewSuMoneyService(options map[string]interface{}) *SuMoneyService {
	newSuMoneyService := &SuMoneyService{}
	return newSuMoneyService
}