You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

deleteresourcespeclogic.go 2.0 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package core
  2. import (
  3. "context"
  4. "github.com/pkg/errors"
  5. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
  6. "gorm.io/gorm"
  7. "time"
  8. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
  9. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type DeleteResourceSpecLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewDeleteResourceSpecLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteResourceSpecLogic {
  18. return &DeleteResourceSpecLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx,
  22. }
  23. }
  24. func (l *DeleteResourceSpecLogic) DeleteResourceSpec(req *types.DeletePathId) (resp *types.CommonResp, err error) {
  25. // 初始化事务
  26. tx := l.svcCtx.DbEngin.Begin()
  27. defer func() {
  28. if r := recover(); r != nil {
  29. tx.Rollback()
  30. panic(r)
  31. }
  32. }()
  33. // 1. 检查主资源规格是否存在
  34. var mainSpec models.TResourceSpec
  35. if err := tx.Where("id = ? AND deleted_at IS NULL", req.Id).
  36. First(&mainSpec).
  37. Error; err != nil {
  38. tx.Rollback()
  39. if errors.Is(err, gorm.ErrRecordNotFound) {
  40. return nil, errors.Errorf("资源规格不存在 (ID: %s)", req.Id)
  41. }
  42. return nil, errors.Wrapf(err, "查询资源规格失败 (ID: %s)", req.Id)
  43. }
  44. // 2. 删除主资源规格(软删除)
  45. if err := tx.Model(&models.TResourceSpec{}).
  46. Where("id = ?", req.Id).
  47. Update("deleted_at", time.Now()).
  48. Error; err != nil {
  49. tx.Rollback()
  50. return nil, errors.Wrapf(err, "删除主资源规格失败 (ID: %s)", req.Id)
  51. }
  52. // 3. 删除关联的基础资源规格(软删除)
  53. if err := tx.Model(&models.TBaseResourceSpec{}).
  54. Where("resource_spec_id = ?", req.Id).
  55. Update("deleted_at", time.Now()).
  56. Error; err != nil {
  57. tx.Rollback()
  58. return nil, errors.Wrapf(err, "删除基础资源规格失败 (ID: %s)", req.Id)
  59. }
  60. // 提交事务
  61. if err := tx.Commit().Error; err != nil {
  62. return nil, errors.Wrap(err, "提交事务失败")
  63. }
  64. return resp, nil
  65. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.