38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
"""持仓服务:查询当前客户持仓(路由层只编排,不碰数据/逻辑)。"""
|
|
from __future__ import annotations
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from model.sys_user import SysUser
|
|
from repositories.fin_holdings import FinHoldingsRepo
|
|
from schemas.holdings import HoldingResp
|
|
from utils.exceptions import ForbiddenError
|
|
|
|
_HOLDING_STATUS = "持有中"
|
|
|
|
|
|
async def get_holdings(db: AsyncSession, user: SysUser) -> list[HoldingResp]:
|
|
"""查询当前客户的在持持仓(status=持有中)。
|
|
|
|
- 仅客户账号可查(员工共用 sys_user,但无持仓);
|
|
- 无持仓返回空列表,不报错。
|
|
"""
|
|
if user.user_type != "CUSTOMER":
|
|
raise ForbiddenError("仅客户账号可查询持仓")
|
|
|
|
holdings = await FinHoldingsRepo(db).list_by_customer(user.id, _HOLDING_STATUS)
|
|
return [
|
|
HoldingResp(
|
|
id=h.id,
|
|
customer_id=h.customer_id,
|
|
product_id=h.product_id,
|
|
shares=h.shares,
|
|
cost_amount=h.cost_amount,
|
|
current_value=h.current_value,
|
|
profit_loss=h.profit_loss,
|
|
profit_ratio=h.profit_ratio,
|
|
status=h.status,
|
|
)
|
|
for h in holdings
|
|
]
|