| @@ -2,7 +2,10 @@ package core | |||||
| import ( | import ( | ||||
| "context" | "context" | ||||
| "github.com/pkg/errors" | |||||
| "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models" | "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models" | ||||
| "gorm.io/gorm" | |||||
| "time" | |||||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" | "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" | ||||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" | "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" | ||||
| @@ -25,11 +28,49 @@ func NewDeleteResourceSpecLogic(ctx context.Context, svcCtx *svc.ServiceContext) | |||||
| } | } | ||||
| func (l *DeleteResourceSpecLogic) DeleteResourceSpec(req *types.DeletePathId) (resp *types.CommonResp, err error) { | func (l *DeleteResourceSpecLogic) DeleteResourceSpec(req *types.DeletePathId) (resp *types.CommonResp, err error) { | ||||
| db := l.svcCtx.DbEngin.Model(&models.TResourceSpec{}).Table("t_resource_spec") | |||||
| err = db.Delete(&models.TResourceSpec{}, req.Id).Error | |||||
| if err != nil { | |||||
| return nil, err | |||||
| // 初始化事务 | |||||
| tx := l.svcCtx.DbEngin.Begin() | |||||
| defer func() { | |||||
| if r := recover(); r != nil { | |||||
| tx.Rollback() | |||||
| panic(r) | |||||
| } | |||||
| }() | |||||
| // 1. 检查主资源规格是否存在 | |||||
| var mainSpec models.TResourceSpec | |||||
| if err := tx.Where("id = ? AND deleted_at IS NULL", req.Id). | |||||
| First(&mainSpec). | |||||
| Error; err != nil { | |||||
| tx.Rollback() | |||||
| if errors.Is(err, gorm.ErrRecordNotFound) { | |||||
| return nil, errors.Errorf("资源规格不存在 (ID: %s)", req.Id) | |||||
| } | |||||
| return nil, errors.Wrapf(err, "查询资源规格失败 (ID: %s)", req.Id) | |||||
| } | |||||
| // 2. 删除主资源规格(软删除) | |||||
| if err := tx.Model(&models.TResourceSpec{}). | |||||
| Where("id = ?", req.Id). | |||||
| Update("deleted_at", time.Now()). | |||||
| Error; err != nil { | |||||
| tx.Rollback() | |||||
| return nil, errors.Wrapf(err, "删除主资源规格失败 (ID: %s)", req.Id) | |||||
| } | |||||
| // 3. 删除关联的基础资源规格(软删除) | |||||
| if err := tx.Model(&models.TBaseResourceSpec{}). | |||||
| Where("resource_spec_id = ?", req.Id). | |||||
| Update("deleted_at", time.Now()). | |||||
| Error; err != nil { | |||||
| tx.Rollback() | |||||
| return nil, errors.Wrapf(err, "删除基础资源规格失败 (ID: %s)", req.Id) | |||||
| } | |||||
| // 提交事务 | |||||
| if err := tx.Commit().Error; err != nil { | |||||
| return nil, errors.Wrap(err, "提交事务失败") | |||||
| } | } | ||||
| return | |||||
| return resp, nil | |||||
| } | } | ||||