task_anomaly.go
11.8 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package dao
import (
"fmt"
"time"
"github.com/go-pg/pg/v10"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
// 异常的任务记录
type TaskAnomalyDao struct {
transactionContext *pgTransaction.TransactionContext
}
func NewTaskAnomalyDao(options map[string]interface{}) *TaskAnomalyDao {
var transactionContext *pgTransaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pgTransaction.TransactionContext)
}
return &TaskAnomalyDao{
transactionContext: transactionContext,
}
}
type ListTaskAnomaly struct {
Id int `pg:"id"`
TaskId int `pg:"task_id"`
TaskRecordId int `pg:"task_record_id"`
Category int `pg:"category"`
CurrentStage domain.TaskStage `pg:"current_stage"` // 计划执行的里程碑
LastStage domain.TaskStage `pg:"last_stage"` // 上一个完成的里程碑
TaskStageCheck domain.TaskStage `pg:"task_stage_check"` //
CreatedAt time.Time `pg:"created_at"`
AssessFlag int `pg:"assess_flag"`
WarnFlag int `pg:"warn_flag"`
AssistFlag int `pg:"assist_flag"`
RecordBegin int64 `pg:"record_begin"`
Marks map[string]string `pg:"marks"`
NoticeWho []map[string]string `pg:"notice_who"`
TaskAlias string `pg:"task_alias"`
TaskName string `pg:"task_name"`
TaskEndTime int `pg:"task_end_time"`
TaskSortBy int `pg:"task_sort_by"`
LeaderName string `pg:"leader_name"`
LeaderId string `pg:"leader_id"`
LevelName string `pg:"level_name"`
}
// 异常的任务记录, 获取我负责的任务
// userId 谁要查看数据
// companyId 公司id
// dayTime 搜索条件日期 ,例:"2006-01-02"
// pageSize 分页大小
// pageNumber 分页页码
func (d *TaskAnomalyDao) List1(userId int, companyId int, taskName string, category int, dayTime string, limit int, offset int) (int, []ListTaskAnomaly, error) {
sqlStr1 := ` with t_task_ignore as (
select task_ignore.id ,task_ignore.task_id
from task_ignore
where user_id = ?
)select
task_anomaly.id,
task_anomaly.task_id,
task_anomaly.task_record_id,
task_anomaly.category,
task_anomaly.current_stage,
task_anomaly.last_stage,
task_anomaly.task_stage_check,
task_anomaly.assess_flag,
task_anomaly.warn_flag,
task_anomaly.assist_flag,
task_anomaly.record_begin,
task_anomaly.marks,
task_anomaly.notice_who,
task_anomaly.created_at,
task.alias as "task_alias",
task."name" as "task_name",
task.end_time as "task_end_time",
task.sort_by as "task_sort_by",
task.leader->>'name' as "leader_name",
task.leader->>'id' as "leader_id",
task.level_name
from task_anomaly
join task on task.id = task_anomaly.task_id
left join t_task_ignore on task_anomaly.task_id=t_task_ignore.task_id
where t_task_ignore.id isnull and task.deleted_at isnull
and task_anomaly.company_id = ?
and task.leader->>'id'='?' `
sqlStr2 := ` with t_task_ignore as (
select task_ignore.id ,task_ignore.task_id
from task_ignore
where user_id = ?
)select count(*) as cnt
from task_anomaly
join task on task.id = task_anomaly.task_id
left join t_task_ignore on task_anomaly.task_id=t_task_ignore.task_id
where t_task_ignore.id isnull and task.deleted_at isnull
and task_anomaly.company_id = ?
and task.leader->>'id'='?' `
condition := []interface{}{userId, companyId, userId}
if len(dayTime) > 0 {
condition = append(condition, dayTime)
sqlStr2 += ` and to_char(task_anomaly.created_at,'yyyy-MM-dd') =? `
sqlStr1 += ` and to_char(task_anomaly.created_at,'yyyy-MM-dd') =? `
}
if len(taskName) > 0 {
condition = append(condition, "%"+taskName+"%")
sqlStr2 += ` and task.alias like ? `
sqlStr1 += ` and task.alias like ? `
}
if category > 0 {
condition = append(condition, category)
sqlStr1 += ` and task_anomaly.category=? `
sqlStr2 += ` and task_anomaly.category=? `
}
condition = append(condition, limit, offset)
sqlStr1 += ` order by task_anomaly.id desc limit ? offset ? `
result := []ListTaskAnomaly{}
tx := d.transactionContext.PgTx
_, err := tx.Query(&result, sqlStr1, condition...)
if err != nil {
return 0, result, err
}
var cnt int
_, err = tx.QueryOne(pg.Scan(&cnt), sqlStr2, condition...)
return cnt, result, err
}
// 异常的任务记录,获取我的下级负责的任务
// userId 谁要查看数据
// companyId 公司id
// dayTime 搜索条件日期 ,例:"2006-01-02"
// taskName 任务名称
// category 异常分类
// pageSize 分页大小
// pageNumber 分页页码
// subLevel 我的下级的层级
func (d *TaskAnomalyDao) List2(userId int, companyId int, taskName string, category int, leaderId []string, dayTime string, subLevel int, limit int, offset int) (int, []ListTaskAnomaly, error) {
sqlStr1 := `with
-- 人员自身以及全下级
recursive t_user as (
(
select "user".id,"user".parent_id ,"user".account,"user".name, 1 as "level"
from "user"
where "user".id=? and "user".deleted_at isnull
)
union
(
select "child_user".id,"child_user".parent_id,"child_user".account,"child_user".name,
"parent_user"."level"+1 as "level"
from "user" as "child_user"
join t_user as "parent_user" on "parent_user".id="child_user".parent_id
where "child_user".deleted_at isnull
)
),
t_task_ignore as (
select task_ignore.id ,task_ignore.task_id
from task_ignore
where user_id = ?
),
t_task as (
select
task.id as "task_id",
task.alias as "task_alias",
task."name" as "task_name",
task.end_time as "task_end_time",
task.sort_by as "task_sort_by",
task.leader->>'name' as "leader_name",
task.leader->>'id' as "leader_id",
task.level_name
from task
left join t_task_ignore on task.id=t_task_ignore.task_id
where task.company_id=?
and t_task_ignore.id isnull
and task.deleted_at isnull
and task.leader ->>'id' in (
select t_user.id::text from t_user where t_user."level">=?
)
)
select
t_task.task_alias,t_task.task_name,t_task.task_end_time,
t_task.task_sort_by,t_task.leader_name,t_task.leader_id,
t_task.level_name,
task_anomaly.id,
task_anomaly.task_id,
task_anomaly.task_record_id,
task_anomaly.category,
task_anomaly.current_stage,
task_anomaly.last_stage,
task_anomaly.task_stage_check,
task_anomaly.assess_flag,
task_anomaly.warn_flag,
task_anomaly.assist_flag,
task_anomaly.record_begin,
task_anomaly.marks,
task_anomaly.notice_who,
task_anomaly.created_at
from t_task
join task_anomaly on t_task.task_id= task_anomaly.task_id `
sqlStr2 := `with
-- 人员自身以及全下级
recursive t_user as (
(
select "user".id,"user".parent_id ,"user".account,"user".name, 1 as "level"
from "user"
where "user".id=? and "user".deleted_at isnull
)
union
(
select "child_user".id,"child_user".parent_id,"child_user".account,"child_user".name,
"parent_user"."level"+1 as "level"
from "user" as "child_user"
join t_user as "parent_user" on "parent_user".id="child_user".parent_id
where "child_user".deleted_at isnull
)
),
t_task_ignore as (
select task_ignore.id ,task_ignore.task_id
from task_ignore
where user_id = ?
),
t_task as (
select
task.id as "task_id",
task.alias as "task_alias",
task."name" as "task_name",
task.end_time as "task_end_time",
task.sort_by as "task_sort_by",
task.leader->>'name' as "leader_name",
task.leader ->>'id' as "leader_id",
task.level_name
from task
left join t_task_ignore on task.id=t_task_ignore.task_id
where task.company_id=?
and t_task_ignore.id isnull
and task.deleted_at isnull
and task.leader ->>'id' in (
select t_user.id::text from t_user where t_user."level">=?
)
)
select count(*) as cnt
from t_task
join task_anomaly on t_task.task_id= task_anomaly.task_id `
condition := []interface{}{userId, userId, companyId, subLevel}
if len(dayTime) > 0 {
condition = append(condition, dayTime)
sqlStr2 += ` and to_char(task_anomaly.created_at,'yyyy-MM-dd') =? `
sqlStr1 += ` and to_char(task_anomaly.created_at,'yyyy-MM-dd') =? `
}
if len(taskName) > 0 {
condition = append(condition, "%"+taskName+"%")
sqlStr2 += ` and t_task.task_alias like ? `
sqlStr1 += ` and t_task.task_alias like ? `
}
if category > 0 {
condition = append(condition, category)
sqlStr1 += ` and task_anomaly.category=? `
sqlStr2 += ` and task_anomaly.category=? `
}
if len(leaderId) > 0 {
condition = append(condition, pg.In(leaderId))
sqlStr2 += ` and t_task.leader_id in (?) `
sqlStr1 += ` and t_task.leader_id in (?) `
}
condition = append(condition, limit, offset)
sqlStr1 += ` order by task_anomaly.id desc limit ? offset ? `
result := []ListTaskAnomaly{}
tx := d.transactionContext.PgTx
_, err := tx.Query(&result, sqlStr1, condition...)
if err != nil {
return 0, result, err
}
var cnt int
_, err = tx.QueryOne(pg.Scan(&cnt), sqlStr2, condition...)
return cnt, result, err
}
// 异常的任务记录,我作为相关人的任务
// userId 谁要查看数据
// companyId 公司id
// dayTime 搜索条件日期 ,例:"2006-01-02"
// pageSize 分页大小
// pageNumber 分页页码
func (d *TaskAnomalyDao) List3(userId int, companyId int, taskName string, category int, dayTime string, leaderId []string, limit int, offset int) (int, []ListTaskAnomaly, error) {
sqlStr1 := ` with t_task_ignore as (
select task_ignore.id ,task_ignore.task_id
from task_ignore
where user_id = ?
)select
task_anomaly.id,
task_anomaly.task_id,
task_anomaly.task_record_id,
task_anomaly.category,
task_anomaly.current_stage,
task_anomaly.last_stage,
task_anomaly.task_stage_check,
task_anomaly.assess_flag,
task_anomaly.warn_flag,
task_anomaly.assist_flag,
task_anomaly.record_begin,
task_anomaly.marks,
task_anomaly.notice_who,
task_anomaly.created_at,
task.alias as "task_alias",
task."name" as "task_name",
task.end_time as "task_end_time",
task.sort_by as "task_sort_by",
task.leader->>'name' as "leader_name",
task.leader->>'id' as "leader_id",
task.level_name
from task_anomaly
join task on task.id = task_anomaly.task_id
left join t_task_ignore on task_anomaly.task_id=t_task_ignore.task_id
where t_task_ignore.id isnull and task.deleted_at isnull
and task_anomaly.company_id = ?
and task.related_user@> ? `
sqlStr2 := ` with t_task_ignore as (
select task_ignore.id ,task_ignore.task_id
from task_ignore
where user_id = ?
)select count(*) as cnt
from task_anomaly
join task on task.id = task_anomaly.task_id
left join t_task_ignore on task_anomaly.task_id=t_task_ignore.task_id
where t_task_ignore.id isnull and task.deleted_at isnull
and task_anomaly.company_id = ?
and task.related_user@> ? `
condition := []interface{}{userId, companyId, fmt.Sprintf("[%d]", userId)}
if len(dayTime) > 0 {
condition = append(condition, dayTime)
sqlStr2 += ` and to_char(task_anomaly.created_at,'yyyy-MM-dd') =? `
sqlStr1 += ` and to_char(task_anomaly.created_at,'yyyy-MM-dd') =? `
}
if len(taskName) > 0 {
condition = append(condition, "%"+taskName+"%")
sqlStr2 += ` and task.alias like ? `
sqlStr1 += ` and task.alias like ? `
}
if category > 0 {
condition = append(condition, category)
sqlStr1 += ` and task_anomaly.category=? `
sqlStr2 += ` and task_anomaly.category=? `
}
if len(leaderId) > 0 {
condition = append(condition, pg.In(leaderId))
sqlStr1 += ` and task.leader->>'id' in (?) `
sqlStr2 += ` and task.leader->>'id' in (?) `
}
condition = append(condition, limit, offset)
sqlStr1 += ` order by task_anomaly.id desc limit ? offset ? `
result := []ListTaskAnomaly{}
tx := d.transactionContext.PgTx
_, err := tx.Query(&result, sqlStr1, condition...)
if err != nil {
return 0, result, err
}
var cnt int
_, err = tx.QueryOne(pg.Scan(&cnt), sqlStr2, condition...)
return cnt, result, err
}