task_dao.go
15.9 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
package dao
import (
"fmt"
"time"
"github.com/go-pg/pg/v10"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
)
type TaskDao struct {
transactionContext *pgTransaction.TransactionContext
}
func NewTaskDao(options map[string]interface{}) *TaskDao {
var transactionContext *pgTransaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pgTransaction.TransactionContext)
}
return &TaskDao{
transactionContext: transactionContext,
}
}
func (d *TaskDao) catchTaskIdByPermission(userId int) string {
sqlStr := `with
-- 人员自身以及全下级
recursive t_user as (
(
select "user".id,"user".parent_id
from "user"
where "user".id=%d
)
union
(
select "child_user".id,"child_user".parent_id
from "user" as "child_user"
join t_user as "parent_user" on "parent_user".id="child_user".parent_id
)
),
-- 根据任务负责人和相关人员查询
t_task_0 as (
(select task.id from task
join t_user on task.leader ->>'id'=t_user.id::text
)
union
(select task.id from task where task.related_user@>'[%d]')
),
t_task_ignore as (
select * from task_ignore where task_ignore.user_id =%d
),
-- 过滤取消关注的
t_task_1 as (
select t_task_0.id
from t_task_0
left join t_task_ignore on t_task_0.id=t_task_ignore.task_id
where t_task_ignore.id isnull
)`
return fmt.Sprintf(sqlStr, userId, userId, userId)
}
type ListTaskCondition struct {
Limit int //分页
Offset int //分页
CompanyId int //公司id
UserId int //谁要查看任务数据
OnlyMy bool //只查看我负责的任务
SearchWord string
}
// 任务和里程碑列表
type ListTaskStage struct {
TaskId int `pg:"task_id"`
TaskName string `pg:"task_name"`
TaskStatus int `pg:"task_status"`
TaskAliasName string `pg:"task_alias_name"`
LeaderName string `pg:"leader_name"`
LeaderId string `pg:"leader_id"`
LevelName string `pg:"level_name"`
Level int `pg:"level"`
Anomaly int `pg:"anomaly"`
WarnFlag int `pg:"warn_flag"`
UpdatedAt time.Time `pg:"updated_at"`
CreatedAt time.Time `pg:"created_at"`
StageId int `pg:"stage_id"`
StageName string `pg:"stage_name"`
StageSortBy int `pg:"stage_sort_by"`
PlanCompletedAt int `pg:"plan_completed_at"`
RealCompletedAt int `pg:"real_completed_at"`
}
// 获取任务以及里程碑列表,用于页面展示; 有过滤查看权限
// userid 谁要查看任务数据
// limit 分页
// offset 分页
func (d *TaskDao) ListTaskStageNotHrbp(param ListTaskCondition) ([]ListTaskStage, error) {
task1 := d.catchTaskIdByPermission(param.UserId)
withSql := task1 + `,
-- 获取的里程碑数据,以及排序
t_task_tage_1 as(
select
task.id as task_id,
task."name" as task_name,
task."alias" as task_alias_name,
task.leader ->>'name' as leader_name,
task.leader ->>'id' as leader_id,
task.level_name ,
task.anomaly ,
task.updated_at ,
task.created_at ,
task."level" ,
task."warn_flag",
task."status" as task_status,
task_stage."id" as stage_id,
task_stage."name" as stage_name,
task_stage.sort_by as stage_sort_by,
task_stage.plan_completed_at,
task_stage.real_completed_at,
(case
when task_stage.real_completed_at =0
then task_stage.plan_completed_at - floor( extract(epoch from now()))
else task_stage.plan_completed_at - task_stage.real_completed_at
end) as diff_time
from task
join t_task_1 on task.id=t_task_1.id
join task_stage on task.id =task_stage.task_id
where 1=1 and task.company_id=?
order by diff_time,task."level",task.created_at desc
),
-- 按任务数据分页获取
t_task_page as (
select distinct t_task_tage_1.task_id
from t_task_tage_1
where 1=1
%s
limit ? offset ?
)
select
t_task_tage_1.task_id,
t_task_tage_1.task_name,
t_task_tage_1.task_alias_name,
t_task_tage_1.leader_name,
t_task_tage_1.level_name ,
t_task_tage_1.anomaly ,
t_task_tage_1.updated_at ,
t_task_tage_1.created_at ,
t_task_tage_1."level" ,
t_task_tage_1.plan_completed_at,
t_task_tage_1.real_completed_at,
t_task_tage_1.stage_name,
t_task_tage_1.stage_id,
t_task_tage_1.stage_sort_by,
t_task_tage_1."warn_flag"
from t_task_tage_1
where t_task_tage_1.task_id in(
select t_task_page.task_id from t_task_page
)`
condition := []interface{}{param.CompanyId}
whereSql := ``
if param.OnlyMy {
condition = append(condition, param.UserId)
whereSql += ` and t_task_tage_1.leader_id = '?' `
}
if param.SearchWord != "" {
condition = append(condition, param.SearchWord, param.SearchWord)
whereSql += ` and (t_task_tage_1.leader_name like ? or t_task_tage_1.task_alias_name like ?)`
}
condition = append(condition, param.Limit, param.Offset)
sqlStr := fmt.Sprintf(withSql, whereSql)
result := []ListTaskStage{}
tx := d.transactionContext.PgTx
_, err := tx.Query(&result, sqlStr, condition...)
return result, err
}
// 获取任务总数,用于页面展示; 有过滤查看权限
func (d *TaskDao) CountTaskStageNotHrbp(param ListTaskCondition) (int, error) {
task1 := d.catchTaskIdByPermission(param.UserId)
withSql := task1 + `select count(*) from task
join t_task_1 on task.id =t_task_1.id
where 1=1 and task.company_id=?`
condition := []interface{}{param.CompanyId}
whereSql := ``
if param.OnlyMy {
condition = append(condition, param.UserId)
whereSql += ` and task.leader ->>'id' = '?' `
}
if param.SearchWord != "" {
condition = append(condition, param.SearchWord, param.SearchWord)
whereSql += ` and (task.leader->>'name' like ? or task.alias like ?)`
}
sqlStr := withSql + whereSql
tx := d.transactionContext.PgTx
var cnt int
_, err := tx.QueryOne(pg.Scan(&cnt), sqlStr, condition...)
return cnt, err
}
// 获取任务以及里程碑列表,用于页面展示; 无过滤查看权限
// userid 谁要查看任务数据
// limit 分页
// offset 分页
func (d *TaskDao) ListTaskStageByHrbp(param ListTaskCondition) ([]ListTaskStage, error) {
withSql := `with
t_task_ignore as (
select task_ignore.task_id,task_ignore.id from task_ignore where task_ignore.user_id =?
),
-- 获取的里程碑数据,以及排序
t_task_tage_1 as(
select
task.id as task_id,
task."name" as task_name,
task."alias" as task_alias_name,
task."status" as task_status,
task.leader ->>'name' as leader_name,
task.leader ->>'id' as leader_id,
task.level_name ,
task.anomaly ,
task.updated_at ,
task."warn_flag",
task.created_at ,
task."level" ,
task_stage."id" as stage_id,
task_stage."name" as stage_name,
task_stage.sort_by as stage_sort_by,
task_stage.plan_completed_at,
task_stage.real_completed_at,
(case
when task_stage.real_completed_at =0
then task_stage.plan_completed_at - floor( extract(epoch from now()))
else task_stage.plan_completed_at - task_stage.real_completed_at
end) as diff_time
from task
join task_stage on task.id =task_stage.task_id
where 1=1 and task.company_id=?
order by diff_time,task."level",task.created_at desc
),
-- 按任务数据分页获取
t_task_page as (
select distinct t_task_tage_1.task_id
from t_task_tage_1
left join t_task_ignore on t_task_ignore.task_id=t_task_tage_1.task_id
where t_task_ignore.id isnull
%s
limit ? offset ?
)
select
t_task_tage_1.task_id,
t_task_tage_1.task_name,
t_task_tage_1.task_alias_name,
t_task_tage_1.task_status,
t_task_tage_1.leader_name,
t_task_tage_1.level_name ,
t_task_tage_1.anomaly ,
t_task_tage_1.updated_at ,
t_task_tage_1.created_at ,
t_task_tage_1."level" ,
t_task_tage_1.plan_completed_at,
t_task_tage_1.real_completed_at,
t_task_tage_1.stage_name,
t_task_tage_1.stage_id,
t_task_tage_1."warn_flag",
t_task_tage_1.stage_sort_by
from t_task_tage_1
where t_task_tage_1.task_id in(
select t_task_page.task_id from t_task_page
)`
condition := []interface{}{param.UserId, param.CompanyId}
whereSql := ``
if param.OnlyMy {
condition = append(condition, param.UserId)
whereSql += ` and t_task_tage_1.leader_id = '?' `
}
if param.SearchWord != "" {
condition = append(condition, param.SearchWord, param.SearchWord)
whereSql += ` and (t_task_tage_1.leader_name like ? or t_task_tage_1.task_alias_name like ?) `
}
condition = append(condition, param.Limit, param.Offset)
sqlStr := fmt.Sprintf(withSql, whereSql)
result := []ListTaskStage{}
tx := d.transactionContext.PgTx
_, err := tx.Query(&result, sqlStr, condition...)
return result, err
}
// 获取任务总数,用于页面展示; 无过滤查看权限
func (d *TaskDao) CountTaskStageByHrbp(param ListTaskCondition) (int, error) {
withSql := `with
t_task_ignore as (
select task_ignore.task_id,task_ignore.id from task_ignore where task_ignore.user_id =?
)select count(*) from task
left join t_task_ignore on t_task_ignore.task_id=task.id
where 1=1 and task.company_id=?`
condition := []interface{}{param.UserId, param.CompanyId}
whereSql := ``
if param.OnlyMy {
condition = append(condition, param.UserId)
whereSql += ` and task.leader ->>'id' = '?' `
}
if param.SearchWord != "" {
condition = append(condition, param.SearchWord, param.SearchWord)
whereSql += ` and (task.leader->>'name' like ? or task.alias like ?)`
}
sqlStr := withSql + whereSql
tx := d.transactionContext.PgTx
var cnt int
_, err := tx.QueryOne(pg.Scan(&cnt), sqlStr, condition...)
return cnt, err
}
// 统计里程碑异常的数量,以非hrbp角色统计
func (d *TaskDao) CountTaskStageAnomalyNotHrbp(param ListTaskCondition) (int, error) {
task1 := d.catchTaskIdByPermission(param.UserId)
withSql := task1 + ` select count(*)
from task_stage
join task on task_stage.task_id =task.id
join t_task_1 on task.id =t_task_1.id
where 1=1 and task.company_id=?
and(
(task_stage.plan_completed_at <task_stage.real_completed_at)
or
(task_stage.real_completed_at=0 and task_stage.plan_completed_at<extract(epoch from now()))
) `
condition := []interface{}{param.CompanyId}
whereSql := ``
if param.OnlyMy {
condition = append(condition, param.UserId)
whereSql += ` and task.leader ->>'id' = '?' `
}
if param.SearchWord != "" {
condition = append(condition, param.SearchWord, param.SearchWord)
whereSql += ` and (task.leader->>'name' like ? or task.alias like ?)`
}
sqlStr := withSql + whereSql
tx := d.transactionContext.PgTx
var cnt int
_, err := tx.QueryOne(pg.Scan(&cnt), sqlStr, condition...)
return cnt, err
}
// 统计里程碑异常的数量,以hrbp角色统计
func (d *TaskDao) CountTaskStageAnomalyByHrbp(param ListTaskCondition) (int, error) {
withSql := `with
t_task_ignore as (
select task_ignore.task_id,task_ignore.id from task_ignore where task_ignore.user_id =?
)select count(*)
from task_stage
join task on task_stage.task_id =task.id
left join t_task_ignore on t_task_ignore.task_id=task.id
where 1=1 and task.company_id=?
and(
(task_stage.plan_completed_at <task_stage.real_completed_at)
or
(task_stage.real_completed_at=0 and task_stage.plan_completed_at<extract(epoch from now()))
) `
condition := []interface{}{param.UserId, param.CompanyId}
whereSql := ``
if param.OnlyMy {
condition = append(condition, param.UserId)
whereSql += ` and task.leader ->>'id' = '?' `
}
if param.SearchWord != "" {
condition = append(condition, param.SearchWord, param.SearchWord)
whereSql += ` and (task.leader->>'name' like ? or task.alias like ?)`
}
sqlStr := withSql + whereSql
tx := d.transactionContext.PgTx
var cnt int
_, err := tx.QueryOne(pg.Scan(&cnt), sqlStr, condition...)
return cnt, err
}
func (d *TaskDao) IncreaseAnomaly(id []int, incr int) error {
if len(id) == 0 {
return nil
}
sqlStr := `update task set anomaly=task.anomaly+?
where id in(?)`
tx := d.transactionContext.PgTx
_, err := tx.Exec(sqlStr, incr, pg.In(id))
return err
}
// TaskStageData
type TaskStageData struct {
Id int
Name string
LeaderName string
}
// 异常的里程碑任务
func (d *TaskDao) TaskStageAnomaly(leaderId []string) ([]TaskStageData, error) {
sqlStr := `select
task_stage.id,
task_stage."name" ,
task.leader ->>'name' as leader_name
from task_stage
join task on task.id=task_stage.task_id
where 1=1
and task_stage.real_completed_at=0 and task_stage.plan_completed_at<extract(epoch from now())
and task.leader ->>'id' in(?) `
result := []TaskStageData{}
tx := d.transactionContext.PgTx
_, err := tx.Query(&result, sqlStr, pg.In(leaderId))
return result, err
}
type TaskData2 struct {
Id int `pg:"id"`
Name string `pg:"name"`
Alias string `pg:"alias"`
LeaderName string `pg:"leader_name"`
Anomaly string `pg:"anomaly"`
}
// 异常的任务
func (d *TaskDao) TaskAnomaly(leaderId []string, anomaly int) ([]TaskData2, error) {
sqlStr := `select
task."id",
task."name" ,
task.alias ,
task.leader ->>'name' as leader_name,
task.anomaly
from task
where task.anomaly >=? and task.leader ->>'id' in(?) `
result := []TaskData2{}
tx := d.transactionContext.PgTx
_, err := tx.Query(&result, sqlStr, anomaly, pg.In(leaderId))
return result, err
}
// 统计任务异常的数量,以非hrbp角色统计
func (d *TaskDao) CountTaskAnomalyNotHrbp(param ListTaskCondition) (int, error) {
task1 := d.catchTaskIdByPermission(param.UserId)
withSql := task1 + ` select count(*)
from task
join t_task_1 on task.id =t_task_1.id
where 1=1
and task.anomaly>1 `
condition := []interface{}{}
whereSql := ``
if param.OnlyMy {
condition = append(condition, param.UserId)
whereSql += ` and task.leader ->>'id' = '?' `
}
if param.SearchWord != "" {
condition = append(condition, param.SearchWord, param.SearchWord)
whereSql += ` and (task.leader->>'name' like ? or task.alias like ?)`
}
sqlStr := withSql + whereSql
tx := d.transactionContext.PgTx
var cnt int
_, err := tx.QueryOne(pg.Scan(&cnt), sqlStr, condition...)
return cnt, err
}
// 统计任务异常的数量,以hrbp角色统计
func (d *TaskDao) CountTaskAnomalyByHrbp(param ListTaskCondition) (int, error) {
withSql := `with
t_task_ignore as (
select task_ignore.task_id,task_ignore.id from task_ignore where task_ignore.user_id =?
)select count(*)
from task
left join t_task_ignore on t_task_ignore.task_id=task.id
where 1=1 and task.anomaly>1 and task.company_id=?`
condition := []interface{}{param.UserId, param.CompanyId}
whereSql := ``
if param.OnlyMy {
condition = append(condition, param.UserId)
whereSql += ` and task.leader ->>'id' = '?' `
}
if param.SearchWord != "" {
condition = append(condition, param.SearchWord, param.SearchWord)
whereSql += ` and (task.leader->>'name' like ? or task.alias like ?)`
}
sqlStr := withSql + whereSql
tx := d.transactionContext.PgTx
var cnt int
_, err := tx.QueryOne(pg.Scan(&cnt), sqlStr, condition...)
return cnt, err
}
// 任务数据
type TaskData1 struct {
Name string `pg:"name"`
Alias string `pg:"alias"`
LeaderId string `pg:"leader_id"`
LeaderName string `pg:"leader_name"`
LevelName string `pg:"level_name"`
}
// 获取任务列表
func (d *TaskDao) ListTask2(userId int) ([]TaskData1, error) {
task1 := d.catchTaskIdByPermission(userId)
sqlStr := task1 + ` select task."name" ,task.alias ,
task.leader ->>'id' as leader_id,
task.leader ->>'name' as leader_name,
task.level_name
from task
join t_task_1 on task.id =t_task_1.id
`
result := []TaskData1{}
tx := d.transactionContext.PgTx
_, err := tx.Query(&result, sqlStr)
return result, err
}
// 获取任务列表
func (d *TaskDao) ListTask2ForHrbp(userId int, companyId int) ([]TaskData1, error) {
sqlStr := `with
t_task_ignore as (
select task_ignore.task_id,task_ignore.id from task_ignore where task_ignore.user_id =?
)select task."name" ,task.alias ,
task.leader ->>'id' as leader_id,
task.leader ->>'name' as leader_name,
task.level_name
from task
left join t_task_ignore on t_task_ignore.task_id=task.id
where 1=1 and task.company_id=? `
result := []TaskData1{}
tx := d.transactionContext.PgTx
_, err := tx.Query(&result, sqlStr, userId, companyId)
return result, err
}