| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package product
- import (
- "context"
- "time"
- "perms-system-server/internal/consts"
- authHelper "perms-system-server/internal/logic/auth"
- "perms-system-server/internal/response"
- "perms-system-server/internal/svc"
- "perms-system-server/internal/types"
- "github.com/zeromicro/go-zero/core/logx"
- )
- type UpdateProductLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- }
- func NewUpdateProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateProductLogic {
- return &UpdateProductLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx,
- }
- }
- func (l *UpdateProductLogic) UpdateProduct(req *types.UpdateProductReq) error {
- if err := authHelper.RequireSuperAdmin(l.ctx); err != nil {
- return err
- }
- if len(req.Name) > 64 {
- return response.ErrBadRequest("产品名称长度不能超过64个字符")
- }
- if len(req.Remark) > 255 {
- return response.ErrBadRequest("备注长度不能超过255个字符")
- }
- product, err := l.svcCtx.SysProductModel.FindOne(l.ctx, req.Id)
- if err != nil {
- return response.ErrNotFound("产品不存在")
- }
- product.Name = req.Name
- product.Remark = req.Remark
- if req.Status != 0 {
- if req.Status != consts.StatusEnabled && req.Status != consts.StatusDisabled {
- return response.ErrBadRequest("状态值无效,仅支持 1(启用) 和 2(禁用)")
- }
- product.Status = req.Status
- }
- product.UpdateTime = time.Now().Unix()
- if err := l.svcCtx.SysProductModel.Update(l.ctx, product); err != nil {
- return err
- }
- l.svcCtx.UserDetailsLoader.CleanByProduct(l.ctx, product.Code)
- return nil
- }
|