uint/uint64全部改为uint32
This commit is contained in:
@@ -44,11 +44,11 @@ type PlanRepository interface {
|
||||
// ListPlans 获取计划列表,支持过滤和分页
|
||||
ListPlans(ctx context.Context, opts ListPlansOptions, page, pageSize int) ([]models.Plan, int64, error)
|
||||
// GetBasicPlanByID 根据ID获取计划的基本信息,不包含子计划和任务详情
|
||||
GetBasicPlanByID(ctx context.Context, id uint) (*models.Plan, error)
|
||||
GetBasicPlanByID(ctx context.Context, id uint32) (*models.Plan, error)
|
||||
// GetPlanByID 根据ID获取计划,包含子计划和任务详情
|
||||
GetPlanByID(ctx context.Context, id uint) (*models.Plan, error)
|
||||
GetPlanByID(ctx context.Context, id uint32) (*models.Plan, error)
|
||||
// GetPlansByIDs 根据ID列表获取计划,不包含子计划和任务详情
|
||||
GetPlansByIDs(ctx context.Context, ids []uint) ([]models.Plan, error)
|
||||
GetPlansByIDs(ctx context.Context, ids []uint32) ([]models.Plan, error)
|
||||
// GetSystemPlanByName 根据计划名称获取系统计划,包含子计划和任务详情
|
||||
GetSystemPlanByName(ctx context.Context, planName models.PlanName) (*models.Plan, error)
|
||||
// CreatePlan 创建一个新的计划
|
||||
@@ -60,35 +60,35 @@ type PlanRepository interface {
|
||||
// UpdatePlan 更新计划的所有字段
|
||||
UpdatePlan(ctx context.Context, plan *models.Plan) error
|
||||
// UpdatePlanStatus 更新指定计划的状态
|
||||
UpdatePlanStatus(ctx context.Context, id uint, status models.PlanStatus) error
|
||||
UpdatePlanStatus(ctx context.Context, id uint32, status models.PlanStatus) error
|
||||
// UpdateExecuteCount 更新指定计划的执行计数
|
||||
UpdateExecuteCount(ctx context.Context, id uint, count uint) error
|
||||
UpdateExecuteCount(ctx context.Context, id uint32, count uint32) error
|
||||
// DeletePlan 根据ID删除计划,同时删除其关联的任务(非子任务)或子计划关联
|
||||
DeletePlan(ctx context.Context, id uint) error
|
||||
DeletePlan(ctx context.Context, id uint32) error
|
||||
// FlattenPlanTasks 递归展开计划,返回按执行顺序排列的所有任务列表
|
||||
FlattenPlanTasks(ctx context.Context, planID uint) ([]models.Task, error)
|
||||
FlattenPlanTasks(ctx context.Context, planID uint32) ([]models.Task, error)
|
||||
// DeleteTask 根据ID删除任务
|
||||
DeleteTask(ctx context.Context, id int) error
|
||||
// FindTaskByID 根据ID获取任务的基本信息
|
||||
FindTaskByID(ctx context.Context, id int) (*models.Task, error)
|
||||
// FindPlanAnalysisTaskByParamsPlanID 根据Parameters中的ParamsPlanID字段值查找TaskPlanAnalysis类型的Task
|
||||
FindPlanAnalysisTaskByParamsPlanID(ctx context.Context, paramsPlanID uint) (*models.Task, error)
|
||||
FindPlanAnalysisTaskByParamsPlanID(ctx context.Context, paramsPlanID uint32) (*models.Task, error)
|
||||
// FindRunnablePlans 获取所有应执行的计划
|
||||
FindRunnablePlans(ctx context.Context) ([]*models.Plan, error)
|
||||
// FindInactivePlans 获取所有已禁用或已停止的计划
|
||||
FindInactivePlans(ctx context.Context) ([]*models.Plan, error)
|
||||
// FindPlanAnalysisTaskByPlanID 根据 PlanID 找到其关联的 'plan_analysis' 任务
|
||||
FindPlanAnalysisTaskByPlanID(ctx context.Context, planID uint) (*models.Task, error)
|
||||
FindPlanAnalysisTaskByPlanID(ctx context.Context, planID uint32) (*models.Task, error)
|
||||
// CreatePlanAnalysisTask 创建一个 plan_analysis 类型的任务并返回它
|
||||
CreatePlanAnalysisTask(ctx context.Context, plan *models.Plan) (*models.Task, error)
|
||||
// FindPlansWithPendingTasks 查找所有正在执行的计划
|
||||
FindPlansWithPendingTasks(ctx context.Context) ([]*models.Plan, error)
|
||||
// StopPlanTransactionally 停止一个计划的执行,包括更新状态、移除待执行任务和更新执行日志
|
||||
StopPlanTransactionally(ctx context.Context, planID uint) error
|
||||
StopPlanTransactionally(ctx context.Context, planID uint32) error
|
||||
// UpdatePlanStateAfterExecution 更新计划执行后的状态(计数和状态)
|
||||
UpdatePlanStateAfterExecution(ctx context.Context, planID uint, newCount uint, newStatus models.PlanStatus) error
|
||||
UpdatePlanStateAfterExecution(ctx context.Context, planID uint32, newCount uint32, newStatus models.PlanStatus) error
|
||||
// ListTasksByDeviceID 根据设备ID获取关联任务列表
|
||||
ListTasksByDeviceID(ctx context.Context, deviceID uint) ([]*models.Task, error)
|
||||
ListTasksByDeviceID(ctx context.Context, deviceID uint32) ([]*models.Task, error)
|
||||
}
|
||||
|
||||
// gormPlanRepository 是 PlanRepository 的 GORM 实现
|
||||
@@ -140,7 +140,7 @@ func (r *gormPlanRepository) ListPlans(ctx context.Context, opts ListPlansOption
|
||||
}
|
||||
|
||||
// GetBasicPlanByID 根据ID获取计划的基本信息,不包含子计划和任务详情
|
||||
func (r *gormPlanRepository) GetBasicPlanByID(ctx context.Context, id uint) (*models.Plan, error) {
|
||||
func (r *gormPlanRepository) GetBasicPlanByID(ctx context.Context, id uint32) (*models.Plan, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "GetBasicPlanByID")
|
||||
var plan models.Plan
|
||||
// GORM 默认不会加载关联,除非使用 Preload,所以直接 First 即可满足要求
|
||||
@@ -152,7 +152,7 @@ func (r *gormPlanRepository) GetBasicPlanByID(ctx context.Context, id uint) (*mo
|
||||
}
|
||||
|
||||
// GetPlansByIDs 根据ID列表获取计划,不包含子计划和任务详情
|
||||
func (r *gormPlanRepository) GetPlansByIDs(ctx context.Context, ids []uint) ([]models.Plan, error) {
|
||||
func (r *gormPlanRepository) GetPlansByIDs(ctx context.Context, ids []uint32) ([]models.Plan, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "GetPlansByIDs")
|
||||
var plans []models.Plan
|
||||
if len(ids) == 0 {
|
||||
@@ -183,7 +183,7 @@ func (r *gormPlanRepository) GetSystemPlanByName(ctx context.Context, planName m
|
||||
}
|
||||
|
||||
// GetPlanByID 根据ID获取计划,包含子计划和任务详情
|
||||
func (r *gormPlanRepository) GetPlanByID(ctx context.Context, id uint) (*models.Plan, error) {
|
||||
func (r *gormPlanRepository) GetPlanByID(ctx context.Context, id uint32) (*models.Plan, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "GetPlanByID")
|
||||
var plan models.Plan
|
||||
|
||||
@@ -254,7 +254,7 @@ func (r *gormPlanRepository) CreatePlanTx(ctx context.Context, tx *gorm.DB, plan
|
||||
|
||||
// 如果是子计划类型,验证所有子计划是否存在且ID不为0
|
||||
if plan.ContentType == models.PlanContentTypeSubPlans {
|
||||
childIDsToValidate := make(map[uint]bool)
|
||||
childIDsToValidate := make(map[uint32]bool)
|
||||
for _, subPlanLink := range plan.SubPlans {
|
||||
if subPlanLink.ChildPlanID == 0 {
|
||||
return ErrSubPlanIDIsZeroOnCreate
|
||||
@@ -262,7 +262,7 @@ func (r *gormPlanRepository) CreatePlanTx(ctx context.Context, tx *gorm.DB, plan
|
||||
childIDsToValidate[subPlanLink.ChildPlanID] = true
|
||||
}
|
||||
|
||||
var ids []uint
|
||||
var ids []uint32
|
||||
for id := range childIDsToValidate {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
@@ -334,8 +334,8 @@ func (r *gormPlanRepository) validatePlanTree(ctx context.Context, tx *gorm.DB,
|
||||
}
|
||||
|
||||
// 2. 递归验证所有子节点,并检测循环引用
|
||||
allIDs := make(map[uint]bool)
|
||||
recursionStack := make(map[uint]bool)
|
||||
allIDs := make(map[uint32]bool)
|
||||
recursionStack := make(map[uint32]bool)
|
||||
if err := validateNodeAndDetectCycles(plan, allIDs, recursionStack); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -346,7 +346,7 @@ func (r *gormPlanRepository) validatePlanTree(ctx context.Context, tx *gorm.DB,
|
||||
}
|
||||
|
||||
// 4. 一次性数据库存在性校验
|
||||
var idsToCheck []uint
|
||||
var idsToCheck []uint32
|
||||
for id := range allIDs {
|
||||
idsToCheck = append(idsToCheck, id)
|
||||
}
|
||||
@@ -364,7 +364,7 @@ func (r *gormPlanRepository) validatePlanTree(ctx context.Context, tx *gorm.DB,
|
||||
}
|
||||
|
||||
// validateNodeAndDetectCycles 递归地验证节点有效性并检测循环引用
|
||||
func validateNodeAndDetectCycles(plan *models.Plan, allIDs, recursionStack map[uint]bool) error {
|
||||
func validateNodeAndDetectCycles(plan *models.Plan, allIDs, recursionStack map[uint32]bool) error {
|
||||
if plan == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -469,7 +469,7 @@ func (r *gormPlanRepository) reconcileSubPlans(ctx context.Context, tx *gorm.DB,
|
||||
return err
|
||||
}
|
||||
|
||||
existingLinkMap := make(map[uint]bool)
|
||||
existingLinkMap := make(map[uint32]bool)
|
||||
for _, link := range existingLinks {
|
||||
existingLinkMap[link.ID] = true
|
||||
}
|
||||
@@ -489,7 +489,7 @@ func (r *gormPlanRepository) reconcileSubPlans(ctx context.Context, tx *gorm.DB,
|
||||
}
|
||||
}
|
||||
|
||||
var linksToDelete []uint
|
||||
var linksToDelete []uint32
|
||||
for id := range existingLinkMap {
|
||||
linksToDelete = append(linksToDelete, id)
|
||||
}
|
||||
@@ -503,7 +503,7 @@ func (r *gormPlanRepository) reconcileSubPlans(ctx context.Context, tx *gorm.DB,
|
||||
}
|
||||
|
||||
// DeletePlan 根据ID删除计划,同时删除其关联的任务(非子任务)或子计划关联
|
||||
func (r *gormPlanRepository) DeletePlan(ctx context.Context, id uint) error {
|
||||
func (r *gormPlanRepository) DeletePlan(ctx context.Context, id uint32) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "DeletePlan")
|
||||
return r.db.WithContext(repoCtx).Transaction(func(tx *gorm.DB) error {
|
||||
// 1. 检查该计划是否是其他计划的子计划
|
||||
@@ -545,7 +545,7 @@ func (r *gormPlanRepository) DeletePlan(ctx context.Context, id uint) error {
|
||||
}
|
||||
|
||||
// FlattenPlanTasks 递归展开计划,返回按执行顺序排列的所有任务列表
|
||||
func (r *gormPlanRepository) FlattenPlanTasks(ctx context.Context, planID uint) ([]models.Task, error) {
|
||||
func (r *gormPlanRepository) FlattenPlanTasks(ctx context.Context, planID uint32) ([]models.Task, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "FlattenPlanTasks")
|
||||
plan, err := r.GetPlanByID(repoCtx, planID)
|
||||
if err != nil {
|
||||
@@ -645,7 +645,7 @@ func (r *gormPlanRepository) deleteTasksTx(ctx context.Context, tx *gorm.DB, ids
|
||||
}
|
||||
|
||||
// FindPlanAnalysisTaskByParamsPlanID 根据Parameters中的ParamsPlanID字段值查找TaskPlanAnalysis类型的Task
|
||||
func (r *gormPlanRepository) FindPlanAnalysisTaskByParamsPlanID(ctx context.Context, paramsPlanID uint) (*models.Task, error) {
|
||||
func (r *gormPlanRepository) FindPlanAnalysisTaskByParamsPlanID(ctx context.Context, paramsPlanID uint32) (*models.Task, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindPlanAnalysisTaskByParamsPlanID")
|
||||
return r.findPlanAnalysisTask(repoCtx, r.db, paramsPlanID)
|
||||
}
|
||||
@@ -722,7 +722,7 @@ func (r *gormPlanRepository) FindInactivePlans(ctx context.Context) ([]*models.P
|
||||
|
||||
// findPlanAnalysisTask 是一个内部使用的、更高效的查找方法
|
||||
// 关键修改:通过查询 parameters JSON 字段来查找
|
||||
func (r *gormPlanRepository) findPlanAnalysisTask(ctx context.Context, tx *gorm.DB, planID uint) (*models.Task, error) {
|
||||
func (r *gormPlanRepository) findPlanAnalysisTask(ctx context.Context, tx *gorm.DB, planID uint32) (*models.Task, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "findPlanAnalysisTask")
|
||||
var task models.Task
|
||||
err := tx.WithContext(repoCtx).Where("type = ? AND parameters->>'plan_id' = ?", models.TaskPlanAnalysis, fmt.Sprintf("%d", planID)).First(&task).Error
|
||||
@@ -734,7 +734,7 @@ func (r *gormPlanRepository) findPlanAnalysisTask(ctx context.Context, tx *gorm.
|
||||
|
||||
// FindPlanAnalysisTaskByPlanID 是暴露给外部的公共方法
|
||||
// 关键修改:通过查询 parameters JSON 字段来查找
|
||||
func (r *gormPlanRepository) FindPlanAnalysisTaskByPlanID(ctx context.Context, planID uint) (*models.Task, error) {
|
||||
func (r *gormPlanRepository) FindPlanAnalysisTaskByPlanID(ctx context.Context, planID uint32) (*models.Task, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindPlanAnalysisTaskByPlanID")
|
||||
return r.findPlanAnalysisTask(repoCtx, r.db, planID)
|
||||
}
|
||||
@@ -769,7 +769,7 @@ func (r *gormPlanRepository) FindPlansWithPendingTasks(ctx context.Context) ([]*
|
||||
}
|
||||
|
||||
// StopPlanTransactionally 停止一个计划的执行,包括更新状态、移除待执行任务和更新执行日志。
|
||||
func (r *gormPlanRepository) StopPlanTransactionally(ctx context.Context, planID uint) error {
|
||||
func (r *gormPlanRepository) StopPlanTransactionally(ctx context.Context, planID uint32) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "StopPlanTransactionally")
|
||||
return r.db.WithContext(repoCtx).Transaction(func(tx *gorm.DB) error {
|
||||
// 使用事务创建新的仓库实例,确保所有操作都在同一个事务中
|
||||
@@ -800,7 +800,7 @@ func (r *gormPlanRepository) StopPlanTransactionally(ctx context.Context, planID
|
||||
}
|
||||
|
||||
if len(taskLogs) > 0 {
|
||||
var taskLogIDs []uint
|
||||
var taskLogIDs []uint32
|
||||
for _, tl := range taskLogs {
|
||||
taskLogIDs = append(taskLogIDs, tl.ID)
|
||||
}
|
||||
@@ -817,7 +817,7 @@ func (r *gormPlanRepository) StopPlanTransactionally(ctx context.Context, planID
|
||||
}
|
||||
|
||||
if len(pendingTasks) > 0 {
|
||||
var pendingTaskIDs []uint
|
||||
var pendingTaskIDs []uint32
|
||||
for _, pt := range pendingTasks {
|
||||
pendingTaskIDs = append(pendingTaskIDs, pt.ID)
|
||||
}
|
||||
@@ -837,7 +837,7 @@ func (r *gormPlanRepository) StopPlanTransactionally(ctx context.Context, planID
|
||||
}
|
||||
|
||||
// UpdatePlanStatus 更新指定计划的状态
|
||||
func (r *gormPlanRepository) UpdatePlanStatus(ctx context.Context, id uint, status models.PlanStatus) error {
|
||||
func (r *gormPlanRepository) UpdatePlanStatus(ctx context.Context, id uint32, status models.PlanStatus) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "UpdatePlanStatus")
|
||||
result := r.db.WithContext(repoCtx).Model(&models.Plan{}).Where("id = ?", id).Update("status", status)
|
||||
if result.Error != nil {
|
||||
@@ -849,7 +849,7 @@ func (r *gormPlanRepository) UpdatePlanStatus(ctx context.Context, id uint, stat
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *gormPlanRepository) UpdatePlanStateAfterExecution(ctx context.Context, planID uint, newCount uint, newStatus models.PlanStatus) error {
|
||||
func (r *gormPlanRepository) UpdatePlanStateAfterExecution(ctx context.Context, planID uint32, newCount uint32, newStatus models.PlanStatus) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "UpdatePlanStateAfterExecution")
|
||||
return r.db.WithContext(repoCtx).Model(&models.Plan{}).Where("id = ?", planID).Updates(map[string]interface{}{
|
||||
"execute_count": newCount,
|
||||
@@ -858,7 +858,7 @@ func (r *gormPlanRepository) UpdatePlanStateAfterExecution(ctx context.Context,
|
||||
}
|
||||
|
||||
// UpdateExecuteCount 更新指定计划的执行计数
|
||||
func (r *gormPlanRepository) UpdateExecuteCount(ctx context.Context, id uint, count uint) error {
|
||||
func (r *gormPlanRepository) UpdateExecuteCount(ctx context.Context, id uint32, count uint32) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "UpdateExecuteCount")
|
||||
result := r.db.WithContext(repoCtx).Model(&models.Plan{}).Where("id = ?", id).Update("execute_count", count)
|
||||
if result.Error != nil {
|
||||
@@ -881,7 +881,7 @@ func (r *gormPlanRepository) FindTaskByID(ctx context.Context, id int) (*models.
|
||||
return &task, nil
|
||||
}
|
||||
|
||||
func (r *gormPlanRepository) ListTasksByDeviceID(ctx context.Context, deviceID uint) ([]*models.Task, error) {
|
||||
func (r *gormPlanRepository) ListTasksByDeviceID(ctx context.Context, deviceID uint32) ([]*models.Task, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListTasksByDeviceID")
|
||||
tasks := []*models.Task{}
|
||||
// 使用 Joins 方法来连接 tasks 表和 device_tasks 关联表,
|
||||
|
||||
Reference in New Issue
Block a user