updateProductLogic.go 2.0 KB

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