updateProductLogic.go 1.7 KB

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