package agent import ( "context" "ycc-server/app/main/model" "ycc-server/common/ctxdata" "ycc-server/common/xerr" "github.com/pkg/errors" "ycc-server/app/main/api/internal/svc" "ycc-server/app/main/api/internal/types" "github.com/zeromicro/go-zero/core/logx" ) type DeleteInviteCodeLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewDeleteInviteCodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteInviteCodeLogic { return &DeleteInviteCodeLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *DeleteInviteCodeLogic) DeleteInviteCode(req *types.DeleteInviteCodeReq) (resp *types.DeleteInviteCodeResp, err error) { // 1. 获取当前代理信息 userID, err := ctxdata.GetUidFromCtx(l.ctx) if err != nil { return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取用户信息失败, %v", err) } agent, err := l.svcCtx.AgentModel.FindOneByUserId(l.ctx, userID) if err != nil { if errors.Is(err, model.ErrNotFound) { return nil, errors.Wrapf(xerr.NewErrMsg("您不是代理"), "") } return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询代理信息失败, %v", err) } // 2. 查询邀请码是否存在且属于当前代理 inviteCode, err := l.svcCtx.AgentInviteCodeModel.FindOne(l.ctx, req.Id) if err != nil { if errors.Is(err, model.ErrNotFound) { return nil, errors.Wrapf(xerr.NewErrMsg("邀请码不存在"), "") } return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询邀请码失败, %v", err) } // 3. 验证邀请码是否属于当前代理 if !inviteCode.AgentId.Valid || inviteCode.AgentId.Int64 != agent.Id { return nil, errors.Wrapf(xerr.NewErrMsg("无权删除此邀请码"), "") } // 4. 检查邀请码是否已被使用 if inviteCode.Status == 1 { return nil, errors.Wrapf(xerr.NewErrMsg("已使用的邀请码无法删除"), "") } // 5. 执行软删除 err = l.svcCtx.AgentInviteCodeModel.DeleteSoft(l.ctx, nil, inviteCode) if err != nil { return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "删除邀请码失败, %v", err) } return &types.DeleteInviteCodeResp{}, nil }