task_dao.go 25.1 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 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
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 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 
		where "child_user".deleted_at isnull
		)
	),
	-- 根据任务负责人和相关人员查询
	t_task_0 as (
		(select task.id from task 
		join t_user on task.leader ->>'id'=t_user.id::text 
		where task.deleted_at isnull
		)
		union
		(select task.id from task where task.related_user@>'[%d]' and task.deleted_at isnull)
	),
	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   //只查看我负责的任务
	LevelName  string //任务类型名称
	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"`
}

// 获取任务总数,用于页面展示; 有过滤查看权限
func (d *TaskDao) CountTaskNotHrbp(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.deleted_at isnull 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 ?)`
	}
	if len(param.LevelName) > 0 {
		condition = append(condition, param.LevelName)
		whereSql += ` and task.level_name = ? `
	}
	sqlStr := withSql + whereSql
	tx := d.transactionContext.PgTx
	var cnt int
	_, err := tx.QueryOne(pg.Scan(&cnt), sqlStr, condition...)
	return cnt, err
}

// 获取任务总数,用于页面展示; 无过滤查看权限
func (d *TaskDao) CountTaskByHrbp(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.deleted_at isnull and t_task_ignore.id isnull 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 ?)`
	}
	if len(param.LevelName) > 0 {
		condition = append(condition, param.LevelName)
		whereSql += ` and task.level_name = ? `
	}
	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
	join t_task_1 on task.id =t_task_1.id
	where 1=1
	and task.company_id =?
	and task.deleted_at isnull
	and (
			(
			cast(COALESCE(task.current_stage ->>'planCompletedAt','0') as int)>0 and  
			cast(COALESCE(task.current_stage ->>'planCompletedAt','0') as int)<floor(extract(epoch from now()))
			)
		or 
		task.warn_flag = 1
	)
	`
	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 ?)`
	}
	if len(param.LevelName) > 0 {
		condition = append(condition, param.LevelName)
		whereSql += ` and task.level_name = ? `
	}
	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
	left join t_task_ignore on t_task_ignore.task_id=task.id
	where 1=1
	and task.company_id =?
	and task.deleted_at isnull
	and t_task_ignore.id isnull 
	and (
		 (
			cast(COALESCE(task.current_stage ->>'planCompletedAt','0') as int)>0 and  
			cast(COALESCE(task.current_stage ->>'planCompletedAt','0') as int)<floor(extract(epoch from now()))
		 )
		or 
		task.warn_flag = 1
	)`
	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 ?)`
	}
	if len(param.LevelName) > 0 {
		condition = append(condition, param.LevelName)
		whereSql += ` and task.level_name = ? `
	}

	sqlStr := withSql + whereSql
	tx := d.transactionContext.PgTx
	var cnt int
	_, err := tx.QueryOne(pg.Scan(&cnt), sqlStr, condition...)
	return cnt, err
}

type TaskData3 struct {
	TaskId     int    `pg:"task_id"`
	TaskName   string `pg:"task_name"`
	TaskAlias  string `pg:"task_alias"`
	LeaderName string `pg:"leader_name"`
	LeaderId   string `pg:"leader_id"`
}

// 根据负责人获取超期未完成的异常的里程碑任务
func (d *TaskDao) TaskStageAnomalyByLeader(leaderId []string) ([]TaskData3, error) {
	sqlStr := `select 
	task.id as task_id,
	task."name" as task_name ,
	task.alias as task_alias,
	task.leader ->>'id' as leader_id,
	task.leader ->>'name' as leader_name
from task where 1=1
and task.deleted_at isnull 
and (
 cast(COALESCE(task.current_stage ->>'planCompletedAt','0') as int) between 1 and floor(extract(epoch from now())) 
) 
and task.leader ->>'id' in(?) `

	result := []TaskData3{}
	tx := d.transactionContext.PgTx
	_, err := tx.Query(&result, sqlStr, pg.In(leaderId))
	return result, err
}

// 根据任务相关人获取超期未完成的异常的里程碑任务
func (d *TaskDao) TaskStageAnomalyByRelatedUser(relatedUserId int) ([]TaskData3, error) {
	sqlStr := `select 
	task.id as task_id,
	task."name" as task_name ,
	task.alias as task_alias,
	task.leader ->>'id' as leader_id,
	task.leader ->>'name' as leader_name
from task where 1=1
and task.deleted_at isnull 
and (
 cast(COALESCE(task.current_stage ->>'planCompletedAt','0') as int) between 1 and floor(extract(epoch from now())) 
) 
and task.related_user @>? `

	relatedUser := fmt.Sprintf("[%d]", relatedUserId)
	result := []TaskData3{}
	tx := d.transactionContext.PgTx
	_, err := tx.Query(&result, sqlStr, relatedUser)
	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) TaskAnomalyByLeader(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(?) and task.deleted_at isnull `
	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>0 and company_id=? and task.deleted_at isnull `
	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 ?)`
	}
	if len(param.LevelName) > 0 {
		condition = append(condition, param.LevelName)
		whereSql += ` and task.level_name = ? `
	}
	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>0 and task.company_id=? and  t_task_ignore.id isnull and task.deleted_at isnull`
	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 ?) `
	}
	if len(param.LevelName) > 0 {
		condition = append(condition, param.LevelName)
		whereSql += ` and task.level_name = ? `
	}
	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
	where task.deleted_at isnull
	`
	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=? and task.deleted_at isnull`
	result := []TaskData1{}
	tx := d.transactionContext.PgTx
	_, err := tx.Query(&result, sqlStr, userId, companyId)
	return result, err

}

type TaskData4 struct {
	TaskId      int    `pg:"task_id"`
	TaskName    string `pg:"task_name"`
	TaskAlias   string `pg:"task_alias"`
	LeaderName  string `pg:"leader_name"`
	LeaderId    int    `pg:"leader_id"`
	RelatedUser []int  `pg:"related_user,type:jsonb"`
}

// TaskStageAnomalyAll 获取所有里程碑异常的任务,
func (d TaskDao) TaskStageAnomalyAll() ([]TaskData4, error) {
	sqlStr := `select 
	task.id as task_id,
	task."name" as task_name ,
	task.alias as task_alias,
	task.leader ->>'id' as leader_id,
	task.leader ->>'name' as leader_name,
	task.related_user 
from task where 1=1
and task.deleted_at isnull 
and (
 cast(COALESCE(task.current_stage ->>'planCompletedAt','0') as int) between 1 and floor(extract(epoch from now())) 
) `
	result := []TaskData4{}
	tx := d.transactionContext.PgTx
	_, err := tx.Query(&result, sqlStr)
	return result, err
}

type TaskData5 struct {
	TaskId      int    `pg:"task_id"`
	TaskName    string `pg:"task_name"`
	TaskAlias   string `pg:"task_alias"`
	LeaderName  string `pg:"leader_name"`
	LeaderId    int    `pg:"leader_id"`
	RelatedUser []int  `pg:"related_user,type:jsonb"`
	Anomaly     int    `pg:"anomaly"`
}

// TaskRecordAnomalyAll 获取所有反馈异常的任务,
func (d TaskDao) TaskRecordAnomalyAll() ([]TaskData5, error) {
	sqlStr := `select 
	task.id as task_id,
	task."name" as task_name ,
	task.alias as task_alias,
	task.anomaly,
	task.leader ->>'id' as leader_id,
	task.leader ->>'name' as leader_name,
	task.related_user 
from task where 1=1
and task.deleted_at isnull 
and task.anomaly>0 `
	result := []TaskData5{}
	tx := d.transactionContext.PgTx
	_, err := tx.Query(&result, sqlStr)
	return result, err
}

type ListTask struct {
	SortBy1          int              `pg:"sort_by_1"`
	CPlanCompletedAt int              `pg:"c_plan_completed_at"`
	SortBy2          int              `pg:"sort_by_2"`
	TaskId           int              `pg:"task_id"`
	Alias            string           `pg:"alias"`
	LeaderName       string           `pg:"leader_name"`
	LeaderId         string           `pg:"leader_id"`
	Level            int              `pg:"level"`
	LevelName        string           `pg:"level_name"`
	Anomaly          int              `pg:"anomaly"`
	Status           int              `pg:"status"`
	UpdatedAt        time.Time        `pg:"updated_at"`
	CreatedAt        time.Time        `pg:"created_at"`
	CurrentStage     domain.TaskStage `pg:"current_stage"` // 当前执行的里程碑
	LastStage        domain.TaskStage `pg:"last_stage"`    // 上一个完成的里程碑
	SortBy           int              `pg:"sort_by"`
	EndTime          int              `pg:"end_time"`
}

// (新)获取任务,用于页面展示; 无过滤查看权限
// userid 谁要查看任务数据
// limit 分页
// offset 分页
func (d *TaskDao) ListTaskByHrbpV2(param ListTaskCondition) ([]ListTask, error) {
	sqlStr := `
	with 
	t_task_ignore as (
		select task_ignore.task_id,task_ignore.id  from  task_ignore where task_ignore.user_id =?
	),
	t_task as (
		select 
		cast(COALESCE(task.current_stage ->>'planCompletedAt','0') as int)  as c_plan_completed_at,
		cast(COALESCE(task.last_stage ->>'planCompletedAt','0') as int)  as l_plan_completed_at,
		cast(COALESCE(task.last_stage ->>'realCompletedAt','0') as int) as l_real_completed_at,
		floor(extract(epoch from now())) as now_time,
		task.id ,
		task.alias ,
		task.leader ->>'name' as leader_name,
		task.leader ->>'id' as leader_id,
		task."level",
		task.level_name ,
		task.anomaly ,
		task.status ,
		task.updated_at,
		task.current_stage,
		task.last_stage,
		task.sort_by,
		task.end_time,
		task.created_at 
		from task 
		left join  t_task_ignore on t_task_ignore.task_id=task.id
		where 1=1 and t_task_ignore.id isnull and task.company_id=?
		and task.deleted_at isnull
		%s
	),
	t_task_1 as (
		select 
		(case 
			when t_task.c_plan_completed_at=0 then 0
			when t_task.c_plan_completed_at-now_time>0 then 0
			else t_task.c_plan_completed_at-now_time
			end) as sort_by_1,
		(case
			when t_task.l_plan_completed_at=0 then 0
			when t_task.l_plan_completed_at-t_task.l_real_completed_at>0 then 0
			else t_task.l_plan_completed_at-t_task.l_real_completed_at
		end) as sort_by_2,
		t_task.c_plan_completed_at ,
		t_task.id as task_id,
		t_task.alias ,
		t_task.leader_name,
		t_task.leader_id,
		t_task."level",
		t_task.level_name ,
		t_task.anomaly ,
		t_task.status ,
		t_task.updated_at ,
		t_task.current_stage,
		t_task.last_stage,
		t_task.sort_by,
		t_task.end_time,
		t_task.created_at
		from t_task  
		where 1=1
	)select *
	from t_task_1
	order by t_task_1.status, t_task_1.sort_by_1,
	t_task_1.sort_by_2,t_task_1.anomaly desc,
	t_task_1.sort_by,t_task_1."created_at"
	limit ? offset ?
	`
	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 ?)`
	}
	if len(param.LevelName) > 0 {
		condition = append(condition, param.LevelName)
		whereSql += ` and task.level_name = ? `
	}
	strSql2 := fmt.Sprintf(sqlStr, whereSql)
	condition = append(condition, param.Limit, param.Offset)
	result := []ListTask{}
	tx := d.transactionContext.PgTx
	_, err := tx.Query(&result, strSql2, condition...)
	return result, err
}

func (d *TaskDao) ListTaskNotHrbpV2(param ListTaskCondition) ([]ListTask, error) {
	withsql := d.catchTaskIdByPermission(param.UserId)
	strSql := withsql + `
	,t_task as (
		select 
		cast(COALESCE(task.current_stage ->>'planCompletedAt','0') as int)  as c_plan_completed_at,
		cast(COALESCE(task.last_stage ->>'planCompletedAt','0') as int)  as l_plan_completed_at,
		cast(COALESCE(task.last_stage ->>'realCompletedAt','0') as int) as l_real_completed_at,
		floor(extract(epoch from now())) as now_time,
		task.id ,
		task.alias ,
		task.leader ->>'name' as leader_name,
		task.leader ->>'id' as leader_id,
		task."level",
		task.level_name ,
		task.anomaly ,
		task.current_stage,
		task.last_stage,
		task.status ,
		task.updated_at,
		task.sort_by,
		task.end_time,
		task.created_at 
		from task 
		join t_task_1 on t_task_1.id=task.id
		where 1=1 and task.company_id=? and task.deleted_at isnull
		%s
	),
	tt_task_1 as (
		select 
		(case 
			when t_task.c_plan_completed_at=0 then 0
			when t_task.c_plan_completed_at-now_time>0 then 0
			else t_task.c_plan_completed_at-now_time
			end) as sort_by_1,
		(case
			when t_task.l_plan_completed_at=0 then 0
			when t_task.l_plan_completed_at-t_task.l_real_completed_at>0 then 0
			else t_task.l_plan_completed_at-t_task.l_real_completed_at
		end) as sort_by_2,
		t_task.c_plan_completed_at ,
		t_task.id as task_id,
		t_task.alias ,
		t_task.leader_name,
		t_task.leader_id,
		t_task."level",
		t_task.level_name ,
		t_task.anomaly ,
		t_task.current_stage,
		t_task.last_stage,
		t_task.status ,
		t_task.updated_at ,
		t_task.sort_by,
		t_task.end_time,
		t_task.created_at
		from t_task  
		where 1=1
	)select *
	from tt_task_1
	order by tt_task_1.status,tt_task_1.sort_by_1,
	tt_task_1.sort_by_2,tt_task_1.anomaly desc,
	tt_task_1.sort_by,tt_task_1."created_at"
	limit ? offset ?
	`
	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 ?)`
	}
	if len(param.LevelName) > 0 {
		condition = append(condition, param.LevelName)
		whereSql += ` and task.level_name = ? `
	}
	strSql2 := fmt.Sprintf(strSql, whereSql)
	condition = append(condition, param.Limit, param.Offset)
	result := []ListTask{}
	tx := d.transactionContext.PgTx
	_, err := tx.Query(&result, strSql2, condition...)
	return result, err
}

// 获取已取消关注的任务列表
func (d *TaskDao) ListTaskIgnore(param ListTaskCondition) ([]ListTask, error) {
	sqlStr := `with
	t_task_ignore as(
		select task_ignore.task_id  
		from task_ignore 
		where user_id =?
	)select 
	task.id as task_id ,
	task.alias ,
	task.leader ->>'name' as leader_name,
	task.leader ->>'id' as leader_id,
	task."level" ,
	task.level_name ,
	task.anomaly ,
	task.status ,
	task.updated_at ,
	task.created_at ,
	task.current_stage ,
	task.last_stage ,
	task.sort_by ,
	task.end_time 
	from task 
	join t_task_ignore on t_task_ignore.task_id=task.id 
	where 1=1 and task.company_id=? and task.deleted_at isnull `
	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 ?)`
	}
	if len(param.LevelName) > 0 {
		condition = append(condition, param.LevelName)
		whereSql += ` and task.level_name = ? `
	}
	condition = append(condition, param.Limit, param.Offset)
	sqlStr = sqlStr + whereSql + ` limit ? offset ? `

	result := []ListTask{}
	tx := d.transactionContext.PgTx
	_, err := tx.Query(&result, sqlStr, condition...)
	return result, err
}

func (d *TaskDao) CountTaskIgnore(param ListTaskCondition) (int, error) {
	sqlStr := `with
	t_task_ignore as(
		select task_ignore.task_id  
		from task_ignore 
		where user_id =?
	)select 
	count(*) as cnt
	from task 
	join t_task_ignore on t_task_ignore.task_id=task.id 
	where 1=1 and task.company_id=? and task.deleted_at isnull `
	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 ?)`
	}
	if len(param.LevelName) > 0 {
		condition = append(condition, param.LevelName)
		whereSql += ` and task.level_name = ? `
	}

	sqlStr = sqlStr + whereSql
	result := 0
	tx := d.transactionContext.PgTx
	_, err := tx.Query(pg.Scan(&result), sqlStr, condition...)
	return result, err
}

type TaskData6 struct {
	TaskId      int    `pg:"task_id"`      //任务id
	TaskName    string `pg:"task_name"`    //任务名称
	TaskAlias   string `pg:"task_alias"`   //任务名称
	LeaderName  string `pg:"leader_name"`  //负责人名称
	LeaderId    string `pg:"leader_id"`    //负责人id
	WarnFlag    int    `pg:"warn_flag"`    //里程碑异常
	AssistFlag  int    `pg:"assist_flag"`  //辅导异常
	Anomaly     int    `pg:"anomaly"`      //反馈异常
	RelatedUser []int  `pg:"related_user"` //相关人员
}

// 查询每日任务异常消息弹窗需要的数据
func (d *TaskDao) TaskIsAnomalyForNotice(companyId int, leaderId []string, relatedUserId int) ([]TaskData6, error) {
	sqlStr := `
select 
	task.id as task_id,
	task."name" as task_name ,
	task.alias as task_alias,
	task.leader ->>'id' as leader_id,
	task.leader ->>'name' as leader_name,
	task.assist_flag ,
	task.warn_flag ,
	task.anomaly ,
	task.related_user
from task where 1=1
and task.deleted_at isnull 
and task.company_id=?
and (task.assist_flag+task.warn_flag +task.anomaly)>0
and (task.leader ->>'id' in(?) or task.related_user @>?)
`

	relatedUser := fmt.Sprintf("[%d]", relatedUserId)
	condition := []interface{}{companyId, pg.In(leaderId), relatedUser}
	result := []TaskData6{}
	tx := d.transactionContext.PgTx
	_, err := tx.Query(&result, sqlStr, condition...)
	return result, err
}

// 按照 任务的相关人员(related_user),排除 既是相关人员,又是任务负责人的情况, 获取出现异常的任务
// func (d *TaskDao) TaskIsAnomalyByRelatedUser(relatedUserId int) ([]TaskData6, error) {
// 	sqlStr := `select
// 	task.id as task_id,
// 	task."name" as task_name ,
// 	task.alias as task_alias,
// 	task.leader ->>'id' as leader_id,
// 	task.leader ->>'name' as leader_name,
// 	task.assist_flag ,
// 	task.warn_flag ,
// 	task.anomaly
// from task where 1=1
// and task.deleted_at isnull
// and (task.assist_flag+task.warn_flag +task.anomaly)>0
// and task.related_user @>?
// and task.leader ->>'id' <> ?
// `
// 	relatedUser := fmt.Sprintf("[%d]", relatedUserId)
// 	condition := []interface{}{relatedUser, strconv.Itoa(relatedUserId)}
// 	result := []TaskData6{}
// 	tx := d.transactionContext.PgTx
// 	_, err := tx.Query(&result, sqlStr, condition...)
// 	return result, err
// }