task_anomaly.go 11.8 KB
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

}