updateProductLogic.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. func (l *UpdateProductLogic) UpdateProduct(req *types.UpdateProductReq) error {
  25. if err := authHelper.RequireSuperAdmin(l.ctx); err != nil {
  26. return err
  27. }
  28. if len(req.Name) > 64 {
  29. return response.ErrBadRequest("产品名称长度不能超过64个字符")
  30. }
  31. if len(req.Remark) > 255 {
  32. return response.ErrBadRequest("备注长度不能超过255个字符")
  33. }
  34. product, err := l.svcCtx.SysProductModel.FindOne(l.ctx, req.Id)
  35. if err != nil {
  36. return response.ErrNotFound("产品不存在")
  37. }
  38. product.Name = req.Name
  39. product.Remark = req.Remark
  40. if req.Status == consts.StatusEnabled || req.Status == consts.StatusDisabled {
  41. product.Status = req.Status
  42. }
  43. product.UpdateTime = time.Now().Unix()
  44. if err := l.svcCtx.SysProductModel.Update(l.ctx, product); err != nil {
  45. return err
  46. }
  47. l.svcCtx.UserDetailsLoader.CleanByProduct(l.ctx, product.Code)
  48. return nil
  49. }