pg_cash_pool_service.go
2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package domain_service
import (
"fmt"
coreDomain "github.com/linmadan/egglib-go/core/domain"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/repository"
"time"
)
type CashPoolService struct {
coreDomain.BaseEventPublisher
transactionContext *pgTransaction.TransactionContext
}
func (service *CashPoolService) Input(companyId int64, operatorUid int64, cash float64, systemUnExchangeSuMoney float64, systemExchangedSuMoney float64, systemExchangedCash float64) (*domain.CashPool, error) {
var cashPoolRepository domain.CashPoolRepository
var employeeRepository domain.EmployeeRepository
if repository, err := repository.NewEmployeeRepository(service.transactionContext); err != nil {
return nil, err
} else {
employeeRepository = repository
}
operator, err := employeeRepository.FindOne(map[string]interface{}{
"uid": operatorUid,
})
if err != nil {
return nil, err
}
if operator == nil {
return nil, fmt.Errorf("无效的操作者")
}
currentCashPool, err := cashPoolRepository.FindOne(map[string]interface{}{
"companyId": companyId,
})
if err != nil {
return nil, err
}
if currentCashPool.ExchangedCash > cash {
return nil, fmt.Errorf("当前投入现金少于系统已兑换现金值")
}
cashPoolInfo := &domain.CashPool{
CompanyId: companyId,
Cash: cash,
ExchangedCash: systemExchangedCash,
UnExchangeCash: currentCashPool.UnExchangeCash + cash,
ExchangedSuMoney: systemExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
Rate: systemExchangedCash / systemExchangedSuMoney,
Operator: operator.EmployeeInfo,
CreateTime: time.Now(),
}
if cashPool, err := cashPoolRepository.Save(cashPoolInfo); err != nil {
return nil, err
} else {
return cashPool, nil
}
}
func NewCashPoolService(transactionContext *pgTransaction.TransactionContext) (*CashPoolService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &CashPoolService{
transactionContext: transactionContext,
}, nil
}
}