pg_task_dao.go
5.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
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
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) error {
tx := dao.transactionContext.PgTx
_, err := tx.QueryOne(
pg.Scan(),
"INSERT INTO bid_infos (task_id, bid_start_time, bid_end_time) VALUES (?, ?, ?)",
taskId, bidStartTime, bidEndTime)
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) (*domain.SystemTaskStatistics, error) {
tx := dao.transactionContext.PgTx
taskModel := new(models.Task)
systemTaskStatistics := &domain.SystemTaskStatistics{}
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 {
systemTaskStatistics.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 {
systemTaskStatistics.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 {
systemTaskStatistics.Completed = int64(count)
}
return systemTaskStatistics, nil
}
func (dao *TaskDao) CalculatePersonTask(uid int64) (*domain.PersonTaskStatistics, error) {
tx := dao.transactionContext.PgTx
taskModel := new(models.Task)
personTaskStatistics := &domain.PersonTaskStatistics{}
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 {
personTaskStatistics.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 {
personTaskStatistics.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 {
personTaskStatistics.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 {
personTaskStatistics.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 {
personTaskStatistics.UnClaimedAsSponsor = 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 {
personTaskStatistics.UnderwayAsReceiver = 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 {
personTaskStatistics.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 {
personTaskStatistics.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 {
personTaskStatistics.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 {
personTaskStatistics.CompletedAsParticipator = int64(count)
}
return personTaskStatistics, nil
}
func NewTaskDao(transactionContext *pgTransaction.TransactionContext) (*TaskDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &TaskDao{
transactionContext: transactionContext,
}, nil
}
}