52 lines
1.7 KiB
Go
52 lines
1.7 KiB
Go
package application
|
|
|
|
import (
|
|
"dddexample/pkg/application/external"
|
|
"dddexample/pkg/application/repository"
|
|
"dddexample/pkg/domain/entity"
|
|
"dddexample/pkg/domain/service"
|
|
"dddexample/pkg/infrastructure/externalimp"
|
|
"dddexample/pkg/infrastructure/repositoryimp"
|
|
)
|
|
|
|
var transferApplication *TransferApplication
|
|
|
|
type TransferApplication struct {
|
|
repository repository.UserRepository
|
|
exchangeRateRepository external.ExchangeRateRepository
|
|
queueRepository external.QueueMsgRepository
|
|
}
|
|
|
|
func init() {
|
|
transferApplication = &TransferApplication{
|
|
repository: &repositoryimp.UserRepositoryImp{},
|
|
exchangeRateRepository: &externalimp.ExchangeRage{},
|
|
queueRepository: &externalimp.KafkaQueue{},
|
|
}
|
|
}
|
|
|
|
func (a *TransferApplication) Transfer(sourceUserId int, targetAccountNumber string, targetAmount float64, targetCurrency string) error {
|
|
|
|
money := entity.Money{Amount: targetAmount, Currency: targetCurrency}
|
|
|
|
// 1. 从数据库读取数据,忽略所有校验逻辑如账号是否存在等
|
|
sourceAccountDO := a.repository.SelectByID(sourceUserId)
|
|
targetAccountDO := a.repository.SelectByAccountNumber(targetAccountNumber)
|
|
|
|
// 3. 获取外部数据,并且包含一定的业务逻辑 获取汇率
|
|
rate, _ := a.exchangeRateRepository.GetExchangeRage(sourceAccountDO.Currency, targetCurrency)
|
|
|
|
err := service.Transfer(sourceAccountDO, targetAccountDO, money, rate)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
a.repository.UpdateAccountMoney(sourceAccountDO)
|
|
a.repository.UpdateAccountMoney(targetAccountDO)
|
|
|
|
// 7. 发送审计消息String message = sourceUserId + "," + targetAccountNumber + "," + targetAmount + "," + targetCurrency;
|
|
err = a.queueRepository.SendMsg("topic", "")
|
|
|
|
return err
|
|
}
|