pg_operation_su_money_service.go
3.6 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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/dao"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/repository"
"time"
)
type OperationSuMoneyService struct {
coreDomain.BaseEventPublisher
transactionContext *pgTransaction.TransactionContext
}
func (service *OperationSuMoneyService) Operation(uid int64, operatorUid int64, suMoney float64, operationType int, recordDescription string) (*domain.SuMoneyTransactionRecord, error) {
var employeeRepository domain.EmployeeRepository
var suMoneyTransactionRecordRepository domain.SuMoneyTransactionRecordRepository
var employeeDao *dao.EmployeeDao
if repository, err := repository.NewEmployeeRepository(service.transactionContext); err != nil {
return nil, err
} else {
employeeRepository = repository
}
if repository, err := repository.NewSuMoneyTransactionRecordRepository(service.transactionContext); err != nil {
return nil, err
} else {
suMoneyTransactionRecordRepository = repository
}
if dao, err := dao.NewEmployeeDao(service.transactionContext); err != nil {
return nil, err
} else {
employeeDao = dao
}
employee, err := employeeRepository.FindOne(map[string]interface{}{
"uid": uid,
})
if err != nil {
return nil, err
}
if employee == nil {
return nil, fmt.Errorf("无效的企业员工")
}
operator, err := employeeRepository.FindOne(map[string]interface{}{
"uid": operatorUid,
})
if err != nil {
return nil, err
}
if operator == nil {
return nil, fmt.Errorf("无效的操作者")
}
if operationType == 2 && employee.SuMoney < suMoney {
return nil, fmt.Errorf("当前素币不足")
}
var recordType int
var transferSuMoney float64
if operationType == 1 { // 增加
recordType = domain.SU_MONEY_TRANSACTION_RECORD_TYPE_INCREASE
transferSuMoney = suMoney
}
if operationType == 2 { // 扣除
recordType = domain.SU_MONEY_TRANSACTION_RECORD_TYPE_DEDUCT
transferSuMoney = 0 - suMoney
}
if operationType == 3 { // 兑换物资
recordType = domain.SU_MONEY_TRANSACTION_RECORD_TYPE_EXCHANGE
transferSuMoney = 0 - suMoney
}
if operationType == 4 { // 兑换现金
recordType = domain.SU_MONEY_TRANSACTION_RECORD_TYPE_EXCHANGE_CASH
transferSuMoney = 0 - suMoney
}
if operationType == 5 { // 任务奖励
recordType = domain.SU_MONEY_TRANSACTION_RECORD_TYPE_AWARD
transferSuMoney = suMoney
}
suMoneyTransactionRecord := &domain.SuMoneyTransactionRecord{
RecordType: recordType,
Employee: employee.EmployeeInfo,
SuMoneyBeforeTransaction: employee.SuMoney,
CurrentSuMoney: employee.SuMoney + transferSuMoney,
SuMoney: suMoney,
Operator: operator.EmployeeInfo,
RecordDescription: recordDescription,
CreateTime: time.Now(),
}
if err := employeeDao.TransferSuMoney(employee.EmployeeInfo.Uid, transferSuMoney); err != nil {
return nil, err
}
if suMoneyTransactionRecord, err := suMoneyTransactionRecordRepository.Save(suMoneyTransactionRecord); err != nil {
return nil, err
} else {
return suMoneyTransactionRecord, nil
}
}
func NewOperationSuMoneyService(transactionContext *pgTransaction.TransactionContext) (*OperationSuMoneyService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &OperationSuMoneyService{
transactionContext: transactionContext,
}, nil
}
}