You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.3 KiB
75 lines
2.3 KiB
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"laic-backend/common"
|
|
"laic-backend/logger"
|
|
"laic-backend/model"
|
|
"laic-backend/vo"
|
|
)
|
|
|
|
type ExecutionService struct{}
|
|
|
|
var DefaultExecutionService = &ExecutionService{}
|
|
|
|
// GetPage 执行记录分页列表(按所属机巢过滤)
|
|
func (s *ExecutionService) GetPage(userID int64, isAdmin bool, req *vo.ExecutionPageReq) (*common.PageResponse[model.TaskExecution], *common.BusiError) {
|
|
db := common.DB.Model(&model.TaskExecution{}).Scopes(withDockFilter(userID, isAdmin))
|
|
if req.Status != "" {
|
|
db = db.Where("status = ?", req.Status)
|
|
}
|
|
if req.TaskID != "" {
|
|
db = db.Where("task_id = ?", req.TaskID)
|
|
}
|
|
if req.DockID != "" {
|
|
db = db.Where("dock_id = ?", req.DockID)
|
|
}
|
|
|
|
var total int64
|
|
if err := db.Count(&total).Error; err != nil {
|
|
logger.ERROR("统计执行记录失败", err)
|
|
return nil, common.ErrInternal
|
|
}
|
|
var list []model.TaskExecution
|
|
if err := db.Scopes(req.Paginate).Order("created_at DESC, id DESC").Find(&list).Error; err != nil {
|
|
logger.ERROR("查询执行记录失败", err)
|
|
return nil, common.ErrInternal
|
|
}
|
|
return common.Page(req.Pagination, total, list), nil
|
|
}
|
|
|
|
// GetDetail 执行记录详情
|
|
func (s *ExecutionService) GetDetail(userID int64, isAdmin bool, id int64) (*model.TaskExecution, *common.BusiError) {
|
|
var exec model.TaskExecution
|
|
if err := common.DB.Scopes(withDockFilter(userID, isAdmin)).First(&exec, id).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, common.ErrExecutionNotFound
|
|
}
|
|
return nil, common.ErrInternal
|
|
}
|
|
return &exec, nil
|
|
}
|
|
|
|
// GetTrajectory 执行轨迹(解析 trajectory_json)
|
|
func (s *ExecutionService) GetTrajectory(userID int64, isAdmin bool, id int64) (*vo.TrajectoryVO, *common.BusiError) {
|
|
var exec model.TaskExecution
|
|
if err := common.DB.Scopes(withDockFilter(userID, isAdmin)).First(&exec, id).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, common.ErrExecutionNotFound
|
|
}
|
|
return nil, common.ErrInternal
|
|
}
|
|
|
|
points := []vo.TrajectoryPoint{}
|
|
if len(exec.TrajectoryJSON) > 0 {
|
|
if err := json.Unmarshal([]byte(exec.TrajectoryJSON), &points); err != nil {
|
|
logger.ERROR("解析轨迹失败", err)
|
|
return nil, common.ErrInternal
|
|
}
|
|
}
|
|
return &vo.TrajectoryVO{ExecutionID: exec.ID, DockID: exec.DockID, Points: points}, nil
|
|
}
|
|
|