31 lines
775 B
Python
31 lines
775 B
Python
"""持仓相关 DTO。
|
|
|
|
金额字段序列化为字符串,避免 JSON number 浮点精度隐患;
|
|
份额 / 盈亏比例保留 4 位,金额保留 2 位。
|
|
"""
|
|
from decimal import Decimal
|
|
|
|
from pydantic import BaseModel, field_serializer
|
|
|
|
|
|
class HoldingResp(BaseModel):
|
|
"""单条持仓记录。"""
|
|
|
|
id: int
|
|
customer_id: int
|
|
product_id: int
|
|
shares: Decimal
|
|
cost_amount: Decimal
|
|
current_value: Decimal
|
|
profit_loss: Decimal
|
|
profit_ratio: Decimal
|
|
status: str
|
|
|
|
@field_serializer("shares", "profit_ratio")
|
|
def _fmt_ratio(self, value: Decimal) -> str:
|
|
return f"{value:.4f}"
|
|
|
|
@field_serializer("cost_amount", "current_value", "profit_loss")
|
|
def _fmt_money(self, value: Decimal) -> str:
|
|
return f"{value:.2f}"
|