cash_pool.go 11.6 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/suMoney/command"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/suMoney/query"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain/service"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/dao"
)

// 现金池服务
type CashPoolService struct {
}

// 投入现金
func (cashPoolService *CashPoolService) OperationCashPool(createCashPoolCommand *command.CreateCashPoolCommand) (interface{}, error) {
	if err := createCashPoolCommand.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
	}

	var systemExchangedCash float64
	if value, err := employeeDao.CalculateSystemCash(createCashPoolCommand.CompanyId); err != nil  {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		systemExchangedCash = value
	}

	var (
		systemChangedSuMoney float64
		systemUnChangeSuMoney float64
	)
	if value1, value2, err := employeeDao.CalculateSystemSuMoney(createCashPoolCommand.CompanyId); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		systemChangedSuMoney = value1
		systemUnChangeSuMoney = value2
	}

	var inputCashPoolService service.InputCashPoolService

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

	if task, err := inputCashPoolService.Input(createCashPoolCommand.CompanyId, createCashPoolCommand.Operator, createCashPoolCommand.Cash, systemChangedSuMoney, systemUnChangeSuMoney, systemExchangedCash); 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
	}
}

// TODO 返回现金池
func (cashPoolService *CashPoolService) CashPool(getCashPoolQuery *query.GetCashPoolQuery) (interface{}, error) {
	if err := getCashPoolQuery.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()
	}()


	return nil, nil
}

// TODO 返回兑换现金活动
func (cashPoolService *CashPoolService) ListExchangeCashActivity(listExchangeCashActivityQuery *query.ListExchangeCashActivityQuery) (interface{}, error) {
	if err := listExchangeCashActivityQuery.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()
	}()

	return nil, nil
}

// TODO 新增兑换现金活动
func (cashPoolService *CashPoolService) CreateExchangeCashActivity(createExchangeCashActivityCommand *command.CreateExchangeCashActivityCommand) (interface{}, error) {
	if err := createExchangeCashActivityCommand.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()
	}()

	return nil, nil
}

// TODO 返回兑换现金活动列表
func (cashPoolService *CashPoolService) ExchangeCashActivityList(listExchangeCashActivityQuery *query.ListExchangeCashActivityQuery) (interface{}, error) {
	if err := listExchangeCashActivityQuery.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()
	}()

	return nil, nil
}

// TODO 移除兑换现金活动
func (cashPoolService *CashPoolService) RemoveExchangeCashActivity(removeExchangeCashActivityCommand *command.RemoveExchangeCashActivityCommand) (interface{}, error) {
	if err := removeExchangeCashActivityCommand.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()
	}()

	return nil, nil
}

// TODO 返回兑换现金活动
func (cashPoolService *CashPoolService) GetExchangeCashActivity(getExchangeCashActivityQuery *query.GetExchangeCashActivityQuery) (interface{}, error) {
	if err := getExchangeCashActivityQuery.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()
	}()

	return nil, nil
}

// TODO 更新兑换现金活动
func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchangeCashActivityCommand *command.UpdateExchangeCashActivityCommand) (interface{}, error) {
	if err := updateExchangeCashActivityCommand.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()
	}()

	return nil, nil
}

// TODO 新建兑换现金人员
func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeCashPersonCommand *command.CreateExchangeCashPersonCommand) (interface{}, error) {
	if err := createExchangeCashPersonCommand.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()
	}()

	return nil, nil
}

// TODO 返回兑换现金人员
func (cashPoolService *CashPoolService) GetExchangeCashPerson(getExchangeCashPersonQuery *query.GetExchangeCashPersonQuery) (interface{}, error) {
	if err := getExchangeCashPersonQuery.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()
	}()

	return nil, nil
}

// 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())
	}

	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()
	}()

	return nil, nil
}

// TODO 移除兑换现金人员
func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeCashPersonCommand *command.RemoveExchangeCashPersonCommand) (interface{}, error) {
	if err := removeExchangeCashPersonCommand.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()
	}()

	return nil, nil
}

// TODO 更新兑换现金人员
func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeCashCommand *command.UpdateExchangeCashPersonCommand) (interface{}, error) {
	if err := updateExchangeCashCommand.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()
	}()

	// TODO 新增流水记录


	return nil, nil
}

func NewCashPoolService(options map[string]interface{}) *CashPoolService {
	newCashPoolService := &CashPoolService{}
	return newCashPoolService
}