pg_task_dao.go
8.3 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package dao
import (
"fmt"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg/models"
"time"
"github.com/go-pg/pg"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
)
type TaskDao struct {
transactionContext *pgTransaction.TransactionContext
}
func (dao *TaskDao) AddRobInfo(taskId int64, receiver *domain.EmployeeInfo) error {
tx := dao.transactionContext.PgTx
_, err := tx.QueryOne(
pg.Scan(),
"INSERT INTO rob_infos (task_id, receiver, receive_time) VALUES (?, ?, ?)",
taskId, receiver, time.Now())
return err
}
func (dao *TaskDao) AddBidInfo(taskId int64, bidStartTime time.Time, bidEndTime time.Time, isRemind bool) error {
tx := dao.transactionContext.PgTx
_, err := tx.QueryOne(
pg.Scan(),
"INSERT INTO bid_infos (task_id, bid_start_time, bid_end_time, is_remind) VALUES (?, ?, ?, ?)",
taskId, bidStartTime, bidEndTime, isRemind)
return err
}
func (dao *TaskDao) UpdateBidInfo(taskId int64, bidStartTime time.Time, bidEndTime time.Time, isRemind bool) error {
tx := dao.transactionContext.PgTx
_, err := tx.QueryOne(
pg.Scan(),
"UPDATE bid_infos SET bid_start_time=?, bid_end_time=?, is_remind=? WHERE task_id=?",
bidStartTime, bidEndTime, isRemind, taskId)
return err
}
func (dao *TaskDao) AddBidderInfo(taskId int64, bidder *domain.EmployeeInfo) error {
tx := dao.transactionContext.PgTx
_, err := tx.QueryOne(
pg.Scan(),
"INSERT INTO bidder_infos (task_id, bidder, bid_time) VALUES (?, ?, ?)",
taskId, bidder, time.Now())
return err
}
func (dao *TaskDao) SetSuccessfulBidder(taskId int64, successfulBidder *domain.EmployeeInfo, winBidTime time.Time) error {
tx := dao.transactionContext.PgTx
_, err := tx.QueryOne(
pg.Scan(),
"UPDATE bid_infos SET successful_bidder=?, win_bid_time=? WHERE task_id=?",
successfulBidder, winBidTime, taskId)
return err
}
func (dao *TaskDao) CalculateSystemTask(companyId int64) (map[string]interface{}, error) {
var released int64
var underway int64
var completed int64
tx := dao.transactionContext.PgTx
taskModel := new(models.Task)
if count, err := tx.Model(taskModel).
Where("task.company_id = ?", companyId).
Where("task.task_status = ? ", domain.TASK_STATUS_UNCLAIMED).
Count(); err != nil {
return nil, err
} else {
released = int64(count)
}
if count, err := tx.Model(taskModel).
Where("task.company_id = ?", companyId).
Where("task.task_status = ? ", domain.TASK_STATUS_UNDERWAY).
Count(); err != nil {
return nil, err
} else {
underway = int64(count)
}
if count, err := tx.Model(taskModel).
Where("task.company_id = ?", companyId).
Where("task.task_status = ? ", domain.TASK_STATUS_COMPLETED).
Count(); err != nil {
return nil, err
} else {
completed = int64(count)
}
return map[string]interface{}{
"released": released + underway + completed,
"underway": underway,
"completed": completed,
}, nil
}
func (dao *TaskDao) CalculatePersonTask(uid int64) (map[string]interface{}, error) {
var underwayAsAssignedPerson int64
var unAcceptanceAsAssignedPerson int64
var completedAsAssignedPerson int64
var unConfirmedAsReceiver int64
var underwayAsReceiver int64
var unAcceptanceAsReceiver int64
var completedAsReceiver int64
var unReleasedAsSponsor int64
var unClaimedAsSponsor int64
var unConfirmedAsSponsor int64
var underwayAsSponsor int64
var unAcceptanceAsSponsor int64
var completedAsSponsor int64
var bidAsParticipator int64
var completedAsParticipator int64
tx := dao.transactionContext.PgTx
taskModel := new(models.Task)
if count, err := tx.Model(taskModel).
Where(`task.assigned_person @> '{"uid":?}'`, uid).
Where("task.task_status = ? ", domain.TASK_STATUS_UNDERWAY).
Count(); err != nil {
return nil, err
} else {
underwayAsAssignedPerson = int64(count)
}
if count, err := tx.Model(taskModel).
Where(`task.assigned_person @> '{"uid":?}'`, uid).
Where("task.task_status = ? ", domain.TASK_STATUS_UNACCEPTANCE).
Count(); err != nil {
return nil, err
} else {
unAcceptanceAsAssignedPerson = int64(count)
}
if count, err := tx.Model(taskModel).
Where(`task.assigned_person @> '{"uid":?}'`, uid).
Where("task.task_status = ? ", domain.TASK_STATUS_COMPLETED).
Count(); err != nil {
return nil, err
} else {
completedAsAssignedPerson = int64(count)
}
if count, err := tx.Model(taskModel).
Where("task.receiver_uid = ?", uid).
Where("task.task_status = ? ", domain.TASK_STATUS_UNCONFIRMED).
Count(); err != nil {
return nil, err
} else {
unConfirmedAsReceiver = int64(count)
}
if count, err := tx.Model(taskModel).
Where("task.receiver_uid = ?", uid).
Where("task.task_status = ? ", domain.TASK_STATUS_UNDERWAY).
Count(); err != nil {
return nil, err
} else {
underwayAsReceiver = int64(count)
}
if count, err := tx.Model(taskModel).
Where("task.receiver_uid = ?", uid).
Where("task.task_status = ? ", domain.TASK_STATUS_UNACCEPTANCE).
Count(); err != nil {
return nil, err
} else {
unAcceptanceAsReceiver = int64(count)
}
if count, err := tx.Model(taskModel).
Where("task.receiver_uid = ?", uid).
Where("task.task_status = ? ", domain.TASK_STATUS_COMPLETED).
Count(); err != nil {
return nil, err
} else {
completedAsReceiver = int64(count)
}
if count, err := tx.Model(taskModel).
Where(`task.sponsor @> '{"uid":?}'`, uid).
Where("task.task_status = ? ", domain.TASK_STATUS_UNRELEASED).
Count(); err != nil {
return nil, err
} else {
unReleasedAsSponsor = int64(count)
}
if count, err := tx.Model(taskModel).
Where(`task.sponsor @> '{"uid":?}'`, uid).
Where("task.task_status = ? ", domain.TASK_STATUS_UNCLAIMED).
Count(); err != nil {
return nil, err
} else {
unClaimedAsSponsor = int64(count)
}
if count, err := tx.Model(taskModel).
Where(`task.sponsor @> '{"uid":?}'`, uid).
Where("task.task_status = ? ", domain.TASK_STATUS_UNCONFIRMED).
Count(); err != nil {
return nil, err
} else {
unConfirmedAsSponsor = int64(count)
}
if count, err := tx.Model(taskModel).
Where(`task.sponsor @> '{"uid":?}'`, uid).
Where("task.task_status = ? ", domain.TASK_STATUS_UNDERWAY).
Count(); err != nil {
return nil, err
} else {
underwayAsSponsor = int64(count)
}
if count, err := tx.Model(taskModel).
Where(`task.sponsor @> '{"uid":?}'`, uid).
Where("task.task_status = ? ", domain.TASK_STATUS_UNACCEPTANCE).
Count(); err != nil {
return nil, err
} else {
unAcceptanceAsSponsor = int64(count)
}
if count, err := tx.Model(taskModel).
Where(`task.sponsor @> '{"uid":?}'`, uid).
Where("task.task_status = ? ", domain.TASK_STATUS_COMPLETED).
Count(); err != nil {
return nil, err
} else {
completedAsSponsor = int64(count)
}
if count, err := tx.Model(taskModel).Join("JOIN bidder_infos AS bidder_info ON bidder_info.task_id = task.id").
Where(`bidder_info.bidder @> '{"uid":?}'`, uid).
Where("task.task_status = ? ", domain.TASK_STATUS_UNCLAIMED).
Count(); err != nil {
return nil, err
} else {
bidAsParticipator = int64(count)
}
if count, err := tx.Model(taskModel).
Where(`task.participators @> '[{"uid":?}]'`, uid).
Where("task.task_status = ? ", domain.TASK_STATUS_COMPLETED).
Count(); err != nil {
return nil, err
} else {
completedAsParticipator = int64(count)
}
return map[string]interface{}{
"underwayAsAssignedPerson": underwayAsAssignedPerson,
"unAcceptanceAsAssignedPerson": unAcceptanceAsAssignedPerson,
"completedAsAssignedPerson": completedAsAssignedPerson,
"unConfirmedAsReceiver": unConfirmedAsReceiver,
"underwayAsReceiver": underwayAsReceiver,
"unAcceptanceAsReceiver": unAcceptanceAsReceiver,
"completedAsReceiver": completedAsReceiver,
"unReleasedAsSponsor": unReleasedAsSponsor,
"unClaimedAsSponsor": unClaimedAsSponsor,
"unConfirmedAsSponsor": unConfirmedAsSponsor,
"underwayAsSponsor": underwayAsSponsor,
"unAcceptanceAsSponsor": unAcceptanceAsSponsor,
"completedAsSponsor": completedAsSponsor,
"bidAsParticipator": bidAsParticipator,
"completedAsParticipator": completedAsParticipator,
}, nil
}
func NewTaskDao(transactionContext *pgTransaction.TransactionContext) (*TaskDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &TaskDao{
transactionContext: transactionContext,
}, nil
}
}