updateProductLogic.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package product
  2. import (
  3. "context"
  4. "errors"
  5. "time"
  6. "perms-system-server/internal/consts"
  7. "perms-system-server/internal/loaders"
  8. authHelper "perms-system-server/internal/logic/auth"
  9. productModel "perms-system-server/internal/model/product"
  10. "perms-system-server/internal/response"
  11. "perms-system-server/internal/svc"
  12. "perms-system-server/internal/types"
  13. "github.com/zeromicro/go-zero/core/logx"
  14. )
  15. type UpdateProductLogic struct {
  16. logx.Logger
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. }
  20. func NewUpdateProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateProductLogic {
  21. return &UpdateProductLogic{
  22. Logger: logx.WithContext(ctx),
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. }
  26. }
  27. // UpdateProduct 更新产品信息。仅超管可调用,可修改产品名称、备注和启用/禁用状态。禁用产品后其成员将无法访问。
  28. func (l *UpdateProductLogic) UpdateProduct(req *types.UpdateProductReq) error {
  29. if err := authHelper.RequireSuperAdmin(l.ctx); err != nil {
  30. return err
  31. }
  32. if len(req.Name) > 64 {
  33. return response.ErrBadRequest("产品名称长度不能超过64个字符")
  34. }
  35. if len(req.Remark) > 255 {
  36. return response.ErrBadRequest("备注长度不能超过255个字符")
  37. }
  38. product, err := l.svcCtx.SysProductModel.FindOne(l.ctx, req.Id)
  39. if err != nil {
  40. return response.ErrNotFound("产品不存在")
  41. }
  42. prevUpdateTime := product.UpdateTime
  43. product.Name = req.Name
  44. product.Remark = req.Remark
  45. if req.Status != 0 {
  46. if req.Status != consts.StatusEnabled && req.Status != consts.StatusDisabled {
  47. return response.ErrBadRequest("状态值无效,仅支持 1(启用) 和 2(禁用)")
  48. }
  49. product.Status = req.Status
  50. }
  51. product.UpdateTime = time.Now().Unix()
  52. if err := l.svcCtx.SysProductModel.UpdateWithOptLock(l.ctx, product, prevUpdateTime); err != nil {
  53. if errors.Is(err, productModel.ErrUpdateConflict) {
  54. return response.ErrConflict("数据已被其他操作修改,请刷新后重试")
  55. }
  56. return err
  57. }
  58. // 审计 L-R13-5 方案 A:产品禁用直接让 loadPerms 清空 Perms,UD 失效不能随请求断连丢失。
  59. cleanCtx, cancel := loaders.DetachCacheCleanCtx(l.ctx)
  60. defer cancel()
  61. l.svcCtx.UserDetailsLoader.CleanByProduct(cleanCtx, product.Code)
  62. return nil
  63. }