task_anomaly.go 7.3 KB
package dao

import (
	"fmt"

	"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"`
	Category       int                 `pg:"category"`
	CurrentStage   domain.TaskStage    `pg:"current_stage"`    // 计划执行的里程碑
	LastStage      domain.TaskStage    `pg:"last_stage"`       // 上一个完成的里程碑
	TaskStageCheck domain.TaskStage    `pg:"task_stage_check"` //
	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"`
	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.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.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.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"
// pageSize 分页大小
// pageNumber 分页页码
func (d *TaskAnomalyDao) List2(userId int, companyId int, leaderId string, dayTime string, pageSize int, pageNumber int) error {

	return nil
}

// 异常的任务记录,我作为相关人的任务
// 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.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.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.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, fmt.Sprintf(`{"id":"%s"}`, leaderId))
		sqlStr1 += ` and task.leader@>? `
		sqlStr2 += ` and task.leader@>? `
	}
	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

}