| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package product
- import (
- "context"
- "errors"
- "time"
- "perms-system-server/internal/consts"
- "perms-system-server/internal/loaders"
- authHelper "perms-system-server/internal/logic/auth"
- productModel "perms-system-server/internal/model/product"
- "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,
- }
- }
- // UpdateProduct 更新产品信息。仅超管可调用,可修改产品名称、备注和启用/禁用状态。禁用产品后其成员将无法访问。
- 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("产品不存在")
- }
- prevUpdateTime := product.UpdateTime
- 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.UpdateWithOptLock(l.ctx, product, prevUpdateTime); err != nil {
- if errors.Is(err, productModel.ErrUpdateConflict) {
- return response.ErrConflict("数据已被其他操作修改,请刷新后重试")
- }
- return err
- }
- // 审计 L-R13-5 方案 A:产品禁用直接让 loadPerms 清空 Perms,UD 失效不能随请求断连丢失。
- cleanCtx, cancel := loaders.DetachCacheCleanCtx(l.ctx)
- defer cancel()
- l.svcCtx.UserDetailsLoader.CleanByProduct(cleanCtx, product.Code)
- return nil
- }
|