updateProductLogic.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. product, err := l.svcCtx.SysProductModel.FindOne(l.ctx, req.Id)
  29. if err != nil {
  30. return response.ErrNotFound("产品不存在")
  31. }
  32. product.Name = req.Name
  33. product.Remark = req.Remark
  34. if req.Status == consts.StatusEnabled || req.Status == consts.StatusDisabled {
  35. product.Status = req.Status
  36. }
  37. product.UpdateTime = time.Now().Unix()
  38. if err := l.svcCtx.SysProductModel.Update(l.ctx, product); err != nil {
  39. return err
  40. }
  41. l.svcCtx.UserDetailsLoader.CleanByProduct(l.ctx, product.Code)
  42. return nil
  43. }