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.

editresourcespeclogic.go 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package core
  2. import (
  3. "context"
  4. "github.com/pkg/errors"
  5. "github.com/zeromicro/go-zero/core/logx"
  6. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
  7. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
  8. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
  9. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
  10. "gorm.io/gorm"
  11. )
  12. type EditResourceSpecLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewEditResourceSpecLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EditResourceSpecLogic {
  18. return &EditResourceSpecLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx,
  22. }
  23. }
  24. func (l *EditResourceSpecLogic) EditResourceSpec(req *types.EditResourceReq) (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. } else if err != nil {
  32. tx.Rollback()
  33. }
  34. }()
  35. // 1. 验证资源规格存在
  36. var existing models.TResourceSpec
  37. if err = tx.Model(&models.TResourceSpec{}).
  38. Where("id = ? AND deleted_at IS NULL", req.Id).
  39. First(&existing).
  40. Error; err != nil {
  41. if errors.Is(err, gorm.ErrRecordNotFound) {
  42. return nil, errors.Errorf("资源规格不存在 (ID: %d)", req.Id)
  43. }
  44. return nil, errors.Wrapf(err, "查询资源规格失败 (ID: %d)", req.Id)
  45. }
  46. // 2. 参数校验
  47. if err = validateRequestParams(req); err != nil {
  48. return nil, err
  49. }
  50. // 3. 转换参数
  51. statusInt := utils.StringToInt64(req.Status)
  52. costPerUnit := utils.StringToFloat64(req.CostPerUnit)
  53. // 4. 更新主资源规格
  54. if err = updateMainResourceSpec(tx, req.Id, statusInt, req.CostType, costPerUnit, req.UserId); err != nil {
  55. return nil, err
  56. }
  57. // 5. 更新子资源规格
  58. if err = updateSubResources(tx, req); err != nil {
  59. return nil, err
  60. }
  61. // 提交事务
  62. if err = tx.Commit().Error; err != nil {
  63. return nil, errors.Wrap(err, "提交事务失败")
  64. }
  65. // 返回成功响应
  66. return resp, nil
  67. }
  68. // validateRequestParams 验证请求参数合法性
  69. func validateRequestParams(req *types.EditResourceReq) error {
  70. // 状态校验
  71. if req.Status != "0" && req.Status != "1" {
  72. return errors.Errorf("资源规格状态不合法 (ID: %d)", req.Id)
  73. }
  74. // 计费类型校验
  75. validCostTypes := map[string]struct{}{
  76. "hourly": {},
  77. "daily": {},
  78. "monthly": {},
  79. "perUse": {},
  80. }
  81. if _, ok := validCostTypes[req.CostType]; !ok {
  82. return errors.Errorf("资源规格计费类型不合法 (ID: %d)", req.Id)
  83. }
  84. return nil
  85. }
  86. // updateMainResourceSpec 更新主资源规格
  87. func updateMainResourceSpec(tx *gorm.DB, id int64, status int64, costType string, costPerUnit float64, userId int64) error {
  88. return tx.Model(&models.TResourceSpec{}).
  89. Where("id = ?", id).
  90. Updates(map[string]interface{}{
  91. "status": status,
  92. "cost_type": costType,
  93. "cost_per_unit": costPerUnit,
  94. "user_id": userId,
  95. }).
  96. Error
  97. }
  98. // updateSubResources 更新子资源规格
  99. func updateSubResources(tx *gorm.DB, req *types.EditResourceReq) error {
  100. // 定义更新操作集合
  101. updateOperations := []struct {
  102. Value string
  103. Unit string
  104. SpecType string
  105. SpecName string
  106. }{
  107. {req.CpuValue, req.CpuUnit, "CPU", ""},
  108. {req.MemoryValue, req.MemoryUnit, "MEMORY", "RAM"},
  109. {req.StorageValue, req.StorageUnit, "STORAGE", ""},
  110. }
  111. // 批量执行更新操作
  112. for _, op := range updateOperations {
  113. if op.Value == "" && op.Unit == "" {
  114. continue
  115. }
  116. if err := updateBaseResourceSpec(tx, req.Id, op.SpecType, op.SpecName, op.Value, op.Unit); err != nil {
  117. return errors.Wrapf(err, "更新%s规格失败 (ID: %d)", op.SpecType, req.Id)
  118. }
  119. }
  120. return nil
  121. }
  122. // updateBaseResourceSpec 通用基础资源规格更新函数
  123. func updateBaseResourceSpec(tx *gorm.DB, specID int64, specType string, specName string, value, unit string) error {
  124. updates := make(map[string]interface{})
  125. if value != "" {
  126. updates["total_value"] = value
  127. }
  128. if unit != "" {
  129. updates["total_unit"] = unit
  130. }
  131. if len(updates) == 0 {
  132. return nil
  133. }
  134. query := tx.Model(&models.TBaseResourceSpec{}).
  135. Where("resource_spec_id = ? AND type = ?", specID, specType)
  136. if specName != "" {
  137. query = query.Where("name = ?", specName)
  138. }
  139. if err := query.Updates(updates).Error; err != nil {
  140. return errors.Wrapf(err, "更新%s规格失败", specType)
  141. }
  142. return nil
  143. }

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.