package transfercastile import ( "context" "errors" "nova_task/internal/model" "nova_task/internal/pkg/errs" "nova_task/internal/pkg/utils" "nova_task/internal/svc" "nova_task/internal/types" "github.com/zeromicro/go-zero/core/logx" ) type GetCastileBalanceLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } // 查询castile代币余额 func NewGetCastileBalanceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCastileBalanceLogic { return &GetCastileBalanceLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *GetCastileBalanceLogic) GetCastileBalance() (resp *types.UserCastileBalanceResp, err error) { //获取JWT中的UID uid := utils.GetUidUint(l.ctx) u, err := l.svcCtx.UserModel.FindOne(l.ctx, uid) if err != nil { if errors.Is(err, model.ErrNotFound) { return nil, errs.New(errs.ErrUserNotFound, "user not found") } l.Errorw("find user error", logx.Field("err", err), logx.Field("uid", uid)) return nil, errs.New(errs.ErrUserNotFound, err) } res := &types.UserCastileBalanceResp{ TotalCastile: 0, TransferAmount: 0, } cToken, err := l.svcCtx.CastileTokenModel.FindOneByEmail(l.ctx, u.Email) if err != nil { if errors.Is(err, model.ErrNotFound) { return res, nil } return nil, errs.New(errs.ErrDatabaseOperate, err) } return &types.UserCastileBalanceResp{ TotalCastile: int(cToken.Total), TransferAmount: int(cToken.Transfer), }, nil }