package cron

import (
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
)

// GetHpcTaskList get hpc task list
func GetHpcTaskList(svc *svc.ServiceContext) ([]*types.TaskModel, error) {
	limit := 10
	offset := 0
	var list []*types.TaskModel
	db := svc.DbEngin.Model(&types.TaskModel{}).Table("task").
		Joins("join task_hpc hpc on task.id = hpc.task_id").
		Select("task.* ,hpc.job_id, hpc.work_dir, hpc.status, hpc.updated_time").
		Where("task.adapter_type_dict = 2  AND task.status NOT IN ('Succeeded', 'Failed') and task.deleted_at is null")

	//count total
	var total int64
	err := db.Count(&total).Error
	if err != nil {
		return nil, err
	}

	db.Limit(limit).Offset(offset)
	err = db.Order("created_time desc").Scan(&list).Error
	if err != nil {
		return nil, err
	}
	return list, nil
}
