修bug
This commit is contained in:
@@ -52,6 +52,7 @@ func (app *Application) initializeSystemPlans(ctx context.Context) error {
|
|||||||
// 1. 获取所有已存在的系统计划
|
// 1. 获取所有已存在的系统计划
|
||||||
existingPlans, _, err := app.Infra.repos.planRepo.ListPlans(appCtx, repository.ListPlansOptions{
|
existingPlans, _, err := app.Infra.repos.planRepo.ListPlans(appCtx, repository.ListPlansOptions{
|
||||||
PlanType: repository.PlanTypeFilterSystem,
|
PlanType: repository.PlanTypeFilterSystem,
|
||||||
|
IncludeAssociations: true,
|
||||||
}, 1, 99999) // 使用一个较大的 pageSize 来获取所有系统计划
|
}, 1, 99999) // 使用一个较大的 pageSize 来获取所有系统计划
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("获取现有系统计划失败: %w", err)
|
return fmt.Errorf("获取现有系统计划失败: %w", err)
|
||||||
@@ -123,11 +124,6 @@ func (app *Application) initializePeriodicSystemHealthCheckPlan(ctx context.Cont
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 重新设置所有任务的 ExecutionOrder
|
|
||||||
for i := range newTasks {
|
|
||||||
newTasks[i].ExecutionOrder = i + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
predefinedPlan := &models.Plan{
|
predefinedPlan := &models.Plan{
|
||||||
Name: models.PlanNamePeriodicSystemHealthCheck,
|
Name: models.PlanNamePeriodicSystemHealthCheck,
|
||||||
Description: fmt.Sprintf("这是一个系统预定义的计划, 每 %d 分钟自动触发一次全量数据采集, 并进行阈值校验告警。", app.Config.Collection.Interval),
|
Description: fmt.Sprintf("这是一个系统预定义的计划, 每 %d 分钟自动触发一次全量数据采集, 并进行阈值校验告警。", app.Config.Collection.Interval),
|
||||||
@@ -139,6 +135,9 @@ func (app *Application) initializePeriodicSystemHealthCheckPlan(ctx context.Cont
|
|||||||
Tasks: newTasks,
|
Tasks: newTasks,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 刷新所有任务的 ExecutionOrder
|
||||||
|
predefinedPlan.ReorderSteps()
|
||||||
|
|
||||||
if foundExistingPlan, ok := existingPlanMap[predefinedPlan.Name]; ok {
|
if foundExistingPlan, ok := existingPlanMap[predefinedPlan.Name]; ok {
|
||||||
// 如果计划存在,则进行无差别更新
|
// 如果计划存在,则进行无差别更新
|
||||||
logger.Infof("预定义计划 '%s' 已存在,正在进行无差别更新...", predefinedPlan.Name)
|
logger.Infof("预定义计划 '%s' 已存在,正在进行无差别更新...", predefinedPlan.Name)
|
||||||
@@ -196,6 +195,11 @@ func (app *Application) initializeAlarmNotificationPlan(ctx context.Context, exi
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err := predefinedPlan.Tasks[0].SaveParameters(app.Config.AlarmNotification.NotificationIntervals)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("序列化告警通知任务参数失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
if foundExistingPlan, ok := existingPlanMap[predefinedPlan.Name]; ok {
|
if foundExistingPlan, ok := existingPlanMap[predefinedPlan.Name]; ok {
|
||||||
// 如果计划存在,则进行无差别更新
|
// 如果计划存在,则进行无差别更新
|
||||||
logger.Infof("预定义计划 '%s' 已存在,正在进行无差别更新...", predefinedPlan.Name)
|
logger.Infof("预定义计划 '%s' 已存在,正在进行无差别更新...", predefinedPlan.Name)
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ func (r *gormDeviceRepository) Create(ctx context.Context, device *models.Device
|
|||||||
func (r *gormDeviceRepository) FindByID(ctx context.Context, id uint32) (*models.Device, error) {
|
func (r *gormDeviceRepository) FindByID(ctx context.Context, id uint32) (*models.Device, error) {
|
||||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindByID")
|
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindByID")
|
||||||
var device models.Device
|
var device models.Device
|
||||||
if err := r.db.WithContext(repoCtx).Preload("AreaController").Preload("DeviceTemplate").First(&device, id).Error; err != nil {
|
if err := r.db.WithContext(repoCtx).Preload("AreaControllers").Preload("DeviceTemplates").First(&device, id).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &device, nil
|
return &device, nil
|
||||||
@@ -111,7 +111,7 @@ func (r *gormDeviceRepository) FindByIDString(ctx context.Context, id string) (*
|
|||||||
func (r *gormDeviceRepository) ListAll(ctx context.Context) ([]*models.Device, error) {
|
func (r *gormDeviceRepository) ListAll(ctx context.Context) ([]*models.Device, error) {
|
||||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListAll")
|
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListAll")
|
||||||
var devices []*models.Device
|
var devices []*models.Device
|
||||||
if err := r.db.WithContext(repoCtx).Preload("AreaController").Preload("DeviceTemplate").Find(&devices).Error; err != nil {
|
if err := r.db.WithContext(repoCtx).Preload("AreaControllers").Preload("DeviceTemplates").Find(&devices).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return devices, nil
|
return devices, nil
|
||||||
@@ -121,7 +121,7 @@ func (r *gormDeviceRepository) ListAll(ctx context.Context) ([]*models.Device, e
|
|||||||
func (r *gormDeviceRepository) ListAllSensors(ctx context.Context) ([]*models.Device, error) {
|
func (r *gormDeviceRepository) ListAllSensors(ctx context.Context) ([]*models.Device, error) {
|
||||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListAllSensors")
|
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListAllSensors")
|
||||||
var sensors []*models.Device
|
var sensors []*models.Device
|
||||||
err := r.db.WithContext(repoCtx).Preload("AreaController").Preload("DeviceTemplate").
|
err := r.db.WithContext(repoCtx).Preload("AreaControllers").Preload("DeviceTemplates").
|
||||||
Joins("JOIN device_templates ON device_templates.id = devices.device_template_id").
|
Joins("JOIN device_templates ON device_templates.id = devices.device_template_id").
|
||||||
Where("device_templates.category = ?", models.CategorySensor).
|
Where("device_templates.category = ?", models.CategorySensor).
|
||||||
Find(&sensors).Error
|
Find(&sensors).Error
|
||||||
@@ -135,7 +135,7 @@ func (r *gormDeviceRepository) ListAllSensors(ctx context.Context) ([]*models.De
|
|||||||
func (r *gormDeviceRepository) ListByAreaControllerID(ctx context.Context, areaControllerID uint32) ([]*models.Device, error) {
|
func (r *gormDeviceRepository) ListByAreaControllerID(ctx context.Context, areaControllerID uint32) ([]*models.Device, error) {
|
||||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListByAreaControllerID")
|
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListByAreaControllerID")
|
||||||
var devices []*models.Device
|
var devices []*models.Device
|
||||||
err := r.db.WithContext(repoCtx).Preload("AreaController").Preload("DeviceTemplate").Where("area_controller_id = ?", areaControllerID).Find(&devices).Error
|
err := r.db.WithContext(repoCtx).Preload("AreaControllers").Preload("DeviceTemplates").Where("area_controller_id = ?", areaControllerID).Find(&devices).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -171,7 +171,7 @@ func (r *gormDeviceRepository) Delete(ctx context.Context, id uint32) error {
|
|||||||
func (r *gormDeviceRepository) FindByAreaControllerAndPhysicalAddress(ctx context.Context, areaControllerID uint32, busNumber int, busAddress int) (*models.Device, error) {
|
func (r *gormDeviceRepository) FindByAreaControllerAndPhysicalAddress(ctx context.Context, areaControllerID uint32, busNumber int, busAddress int) (*models.Device, error) {
|
||||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindByAreaControllerAndPhysicalAddress")
|
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindByAreaControllerAndPhysicalAddress")
|
||||||
var device models.Device
|
var device models.Device
|
||||||
err := r.db.WithContext(repoCtx).Preload("AreaController").Preload("DeviceTemplate").
|
err := r.db.WithContext(repoCtx).Preload("AreaControllers").Preload("DeviceTemplates").
|
||||||
Where("area_controller_id = ?", areaControllerID).
|
Where("area_controller_id = ?", areaControllerID).
|
||||||
Where("properties->>'bus_number' = ?", strconv.Itoa(busNumber)).
|
Where("properties->>'bus_number' = ?", strconv.Itoa(busNumber)).
|
||||||
Where("properties->>'bus_address' = ?", strconv.Itoa(busAddress)).
|
Where("properties->>'bus_address' = ?", strconv.Itoa(busAddress)).
|
||||||
|
|||||||
@@ -165,7 +165,7 @@ func (r *gormExecutionLogRepository) ListTaskExecutionLogs(ctx context.Context,
|
|||||||
orderBy = opts.OrderBy
|
orderBy = opts.OrderBy
|
||||||
}
|
}
|
||||||
// 预加载关联的Task信息
|
// 预加载关联的Task信息
|
||||||
query = query.Order(orderBy).Preload("Task")
|
query = query.Order(orderBy).Preload("Tasks")
|
||||||
|
|
||||||
offset := (page - 1) * pageSize
|
offset := (page - 1) * pageSize
|
||||||
err := query.Limit(pageSize).Offset(offset).Find(&results).Error
|
err := query.Limit(pageSize).Offset(offset).Find(&results).Error
|
||||||
@@ -231,8 +231,8 @@ func (r *gormExecutionLogRepository) UpdateTaskExecutionLog(ctx context.Context,
|
|||||||
func (r *gormExecutionLogRepository) FindTaskExecutionLogByID(ctx context.Context, id uint32) (*models.TaskExecutionLog, error) {
|
func (r *gormExecutionLogRepository) FindTaskExecutionLogByID(ctx context.Context, id uint32) (*models.TaskExecutionLog, error) {
|
||||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindTaskExecutionLogByID")
|
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindTaskExecutionLogByID")
|
||||||
var log models.TaskExecutionLog
|
var log models.TaskExecutionLog
|
||||||
// 使用 Preload("Task") 来确保关联的任务信息被一并加载
|
// 使用 Preload("Tasks") 来确保关联的任务信息被一并加载
|
||||||
err := r.db.WithContext(repoCtx).Preload("Task").First(&log, id).Error
|
err := r.db.WithContext(repoCtx).Preload("Tasks").First(&log, id).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ func (r *gormPendingTaskRepository) FindAllPendingTasks(ctx context.Context) ([]
|
|||||||
var tasks []models.PendingTask
|
var tasks []models.PendingTask
|
||||||
// 预加载 Task 以便后续访问 Task.PlanID
|
// 预加载 Task 以便后续访问 Task.PlanID
|
||||||
// 预加载 TaskExecutionLog 以便后续访问 PlanExecutionLogID
|
// 预加载 TaskExecutionLog 以便后续访问 PlanExecutionLogID
|
||||||
err := r.db.WithContext(repoCtx).Preload("Task").Preload("TaskExecutionLog").Find(&tasks).Error
|
err := r.db.WithContext(repoCtx).Preload("Tasks").Preload("TaskExecutionLogs").Find(&tasks).Error
|
||||||
return tasks, err
|
return tasks, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,8 +154,8 @@ func (r *gormPendingTaskRepository) ClaimNextAvailableTask(ctx context.Context,
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 在 Preload("Task") 时,使用 Unscoped() 来忽略 Task 的软删除状态
|
// 在 Preload("Tasks") 时,使用 Unscoped() 来忽略 Task 的软删除状态
|
||||||
if err := tx.WithContext(repoCtx).Preload("Task", func(db *gorm.DB) *gorm.DB {
|
if err := tx.WithContext(repoCtx).Preload("Tasks", func(db *gorm.DB) *gorm.DB {
|
||||||
return db.Unscoped()
|
return db.Unscoped()
|
||||||
}).First(&log, pendingTask.TaskExecutionLogID).Error; err != nil {
|
}).First(&log, pendingTask.TaskExecutionLogID).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -36,6 +36,9 @@ const (
|
|||||||
// ListPlansOptions 定义了查询计划时的可选参数
|
// ListPlansOptions 定义了查询计划时的可选参数
|
||||||
type ListPlansOptions struct {
|
type ListPlansOptions struct {
|
||||||
PlanType PlanTypeFilter
|
PlanType PlanTypeFilter
|
||||||
|
|
||||||
|
// IncludeAssociations 控制是否预加载关联的任务和子计划,以便计算 HasTasks 和 HasSubPlans 字段。
|
||||||
|
IncludeAssociations bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// TaskListOptions 定义了查询任务时的可选参数
|
// TaskListOptions 定义了查询任务时的可选参数
|
||||||
@@ -154,6 +157,11 @@ func (r *gormPlanRepository) ListPlans(ctx context.Context, opts ListPlansOption
|
|||||||
query = query.Where("plan_type = ?", models.PlanTypeCustom)
|
query = query.Where("plan_type = ?", models.PlanTypeCustom)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 如果需要包含关联,则进行预加载
|
||||||
|
if opts.IncludeAssociations {
|
||||||
|
query = query.Preload("Tasks").Preload("SubPlans")
|
||||||
|
}
|
||||||
|
|
||||||
if err := query.Count(&total).Error; err != nil {
|
if err := query.Count(&total).Error; err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ func (r *gormRawMaterialRepository) ListRawMaterialPurchases(ctx context.Context
|
|||||||
if opts.OrderBy != "" {
|
if opts.OrderBy != "" {
|
||||||
orderBy = opts.OrderBy
|
orderBy = opts.OrderBy
|
||||||
}
|
}
|
||||||
query = query.Order(orderBy).Preload("RawMaterial")
|
query = query.Order(orderBy).Preload("RawMaterials")
|
||||||
|
|
||||||
offset := (page - 1) * pageSize
|
offset := (page - 1) * pageSize
|
||||||
err := query.Limit(pageSize).Offset(offset).Find(&results).Error
|
err := query.Limit(pageSize).Offset(offset).Find(&results).Error
|
||||||
@@ -178,7 +178,7 @@ func (r *gormRawMaterialRepository) ListFeedUsageRecords(ctx context.Context, op
|
|||||||
if opts.OrderBy != "" {
|
if opts.OrderBy != "" {
|
||||||
orderBy = opts.OrderBy
|
orderBy = opts.OrderBy
|
||||||
}
|
}
|
||||||
query = query.Order(orderBy).Preload("Pen").Preload("FeedFormula")
|
query = query.Order(orderBy).Preload("Pens").Preload("FeedFormulas")
|
||||||
|
|
||||||
offset := (page - 1) * pageSize
|
offset := (page - 1) * pageSize
|
||||||
err := query.Limit(pageSize).Offset(offset).Find(&results).Error
|
err := query.Limit(pageSize).Offset(offset).Find(&results).Error
|
||||||
|
|||||||
Reference in New Issue
Block a user