pg_acceptance_task_service.go
7.5 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package domain_service
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
coreDomain "github.com/linmadan/egglib-go/core/domain"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"github.com/linmadan/egglib-go/utils/tool_funs"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain/event"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/repository"
"time"
)
type AcceptanceTaskService struct {
coreDomain.BaseEventPublisher
transactionContext *pgTransaction.TransactionContext
}
func (service *AcceptanceTaskService) Acceptance(taskId int64, operatorUid int64, participators []int64, suMoney float64, taskPercentage []*domain.TaskPercentageItem, referenceResourceScore []*domain.ReferenceResourceItem, solveReport string, solvePictureUrls []string, actualCompletionTime time.Time) (*domain.Task, error) {
var employeeRepository domain.EmployeeRepository
var taskRepository domain.TaskRepository
var suMoneyTransactionRecordRepository domain.SuMoneyTransactionRecordRepository
var cashPoolRepository domain.CashPoolRepository
var employeeDao *dao.EmployeeDao
if repository, err := repository.NewEmployeeRepository(service.transactionContext); err != nil {
return nil, err
} else {
employeeRepository = repository
}
if repository, err := repository.NewTaskRepository(service.transactionContext); err != nil {
return nil, err
} else {
taskRepository = repository
}
if repository, err := repository.NewSuMoneyTransactionRecordRepository(service.transactionContext); err != nil {
return nil, err
} else {
suMoneyTransactionRecordRepository = repository
}
if repository, err := repository.NewCashPoolRepository(service.transactionContext); err != nil {
return nil, err
} else {
cashPoolRepository = repository
}
if dao, err := dao.NewEmployeeDao(service.transactionContext); err != nil {
return nil, err
} else {
employeeDao = dao
}
operator, err := employeeRepository.FindOne(map[string]interface{}{
"uid": operatorUid,
})
if err != nil {
return nil, err
}
if operator == nil {
return nil, fmt.Errorf("无效的操作者")
}
task, err := taskRepository.FindOne(map[string]interface{}{
"taskId": taskId,
})
if err != nil {
return nil, err
}
if task == nil {
return nil, fmt.Errorf("无效的任务")
}
participatorMap := make(map[int64]*domain.Employee)
var participatorInfos []*domain.EmployeeInfo
for _, participatorUid := range participators {
if participator, err := employeeRepository.FindOne(map[string]interface{}{
"uid": participatorUid,
"status": 1,
}); err != nil {
return nil, err
} else {
if participator == nil {
return nil, fmt.Errorf("无效的参与人: " + participator.EmployeeInfo.EmployeeName)
}
participatorMap[participator.EmployeeInfo.Uid] = participator
participatorInfos = append(participatorInfos, participator.EmployeeInfo)
}
}
if err := task.Acceptance(participatorInfos, suMoney, taskPercentage, referenceResourceScore, solveReport, solvePictureUrls, actualCompletionTime); err != nil {
return nil, err
}
for _, taskPercentageItem := range taskPercentage {
var taskTypeName string
if task.TaskType == domain.TASK_TYPE_BID {
taskTypeName = "竞标任务"
}
if task.TaskType == domain.TASK_TYPE_ROB {
taskTypeName = "抢单任务"
}
if task.TaskType == domain.TASK_TYPE_DESIGNATE {
taskTypeName = "指派任务"
}
var employee *domain.Employee
if taskPercentageItem.IsReceiver {
receiver, err := employeeRepository.FindOne(map[string]interface{}{
"uid": taskPercentageItem.Contributor.Uid,
"status": 1,
})
if err != nil {
return nil, err
}
if receiver == nil {
return nil, fmt.Errorf("无效的任务领取者:" + receiver.EmployeeInfo.EmployeeName)
}
employee = receiver
} else {
participator, ok := participatorMap[taskPercentageItem.Contributor.Uid]
if !ok {
return nil, fmt.Errorf("无效的参与人")
}
employee = participator
}
suMoneyTransactionRecord := &domain.SuMoneyTransactionRecord{
RecordType: domain.SU_MONEY_TRANSACTION_RECORD_TYPE_AWARD,
Employee: taskPercentageItem.Contributor,
SuMoneyBeforeTransaction: employee.SuMoney,
CurrentSuMoney: employee.SuMoney + taskPercentageItem.SuMoney,
SuMoney: taskPercentageItem.SuMoney,
Operator: operator.EmployeeInfo,
RecordDescription: fmt.Sprintf("%s-%s", taskTypeName, task.TaskName),
CreateTime: time.Now(),
}
if _, err := suMoneyTransactionRecordRepository.Save(suMoneyTransactionRecord); err != nil {
return nil, err
}
if err := employeeDao.TransferSuMoney(taskPercentageItem.Contributor.Uid, taskPercentageItem.SuMoney); err != nil {
return nil, err
}
// 更新现金池未兑换素币
_, cashPoolFound, err := cashPoolRepository.Find(map[string]interface{}{
"companyId": task.CompanyId,
})
if err != nil {
return nil, err
}
if len(cashPoolFound) > 0 {
// 获取平台素币兑换情况
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(task.CompanyId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if systemSuMoneyStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
}
systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64)
// 现金池更新命令
cashPool := &domain.CashPool{
Cash: cashPoolFound[0].Cash,
CompanyId: cashPoolFound[0].CompanyId,
ExchangedCash: cashPoolFound[0].ExchangedCash,
UnExchangeCash: cashPoolFound[0].UnExchangeCash,
ExchangedSuMoney: cashPoolFound[0].ExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
Rate: cashPoolFound[0].Rate,
LastRate: cashPoolFound[0].LastRate,
}
// 更新现金池
if err := cashPoolFound[0].Update(tool_funs.SimpleStructToMap(cashPool)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
// 保存现金池更新
if _, err := cashPoolRepository.Save(cashPool); err != nil {
return nil, err
}
}
}
if task, err := taskRepository.Save(task); err != nil {
return nil, err
} else {
taskAcceptancedEvent := new(event.TaskAcceptanced)
taskAcceptancedEvent.OccurredOn = time.Now()
taskAcceptancedEvent.TaskId = task.TaskId
taskAcceptancedEvent.CompanyId = task.CompanyId
taskAcceptancedEvent.TaskName = task.TaskName
taskAcceptancedEvent.TaskType = task.TaskType
taskAcceptancedEvent.Sponsor = task.Sponsor
taskAcceptancedEvent.TaskStatus = task.TaskStatus
taskAcceptancedEvent.ReferenceResource = task.ReferenceResource
taskAcceptancedEvent.AcceptanceStandard = task.AcceptanceStandard
taskAcceptancedEvent.Participators = task.Participators
taskAcceptancedEvent.TaskPercentage = task.TaskPercentage
taskAcceptancedEvent.SolveReport = task.SolveReport
taskAcceptancedEvent.SolvePictureUrls = task.SolvePictureUrls
if err := service.Publish(taskAcceptancedEvent); err != nil {
return nil, err
}
return task, nil
}
}
func NewAcceptanceTaskService(transactionContext *pgTransaction.TransactionContext) (*AcceptanceTaskService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &AcceptanceTaskService{
transactionContext: transactionContext,
}, nil
}
}