Commit 114a8541 authored by 陈涛's avatar 陈涛

提交asset

parent c68a6346
from fastapi import APIRouter
from fastapi import APIRouter, Depends
from motor.core import AgnosticCollection
from dependencies import get_current_user, get_fund_collect, get_asset_collect, get_bill_collect
from model import Response
from model.asset import AssetType
from model.bill import PCFBill, ExchangeBill, BillType, CreatePCFBill
from tools.jwt_tools import User
router = APIRouter()
@router.post('/pcf/',
response_model=Response[PCFBill],
summary='添加申购赎回账目',
description='添加申购赎回账目')
async def create_pcf(
item: CreatePCFBill,
user: User = Depends(get_current_user),
fund_collect: AgnosticCollection = Depends(get_fund_collect),
asset_collect: AgnosticCollection = Depends(get_asset_collect),
bill_collect: AgnosticCollection = Depends(get_bill_collect)
):
assert item.bill_type == BillType.sub or item.bill_type == BillType.redemption, "枚举错误"
fund_asset = await asset_collect.find_one({'fund_id': item.fund_id})
assert fund_asset, "没有该资产记录"
query = {"fund_id": item.fund_id, "assets.currency": item.currency, "assets.asset_type": AssetType.vault}
update = {
"$inc": {"assets.volume": 1},
"$push": {"data": {"name": "BTC", "volume": 1}}
# "$push": {"assets": {"name": "BTC", "volume": 1}}
}
# result = mycol.update_one(query, update, upsert=True)
result = await asset_collect.update_one(query, update, upsert=True)
# 判断是否插入新数据
if result.upserted_id:
await asset_collect.update_one({}, {"$push": {"assets": {"name": "BTC", "volume": 1}}})
return Response[PCFBill](data=None)
@router.post('/exchange/',
response_model=Response[ExchangeBill],
summary='添加置换币账目',
description='添加置换币账目')
async def create_exchange(
):
pass
......@@ -31,6 +31,11 @@ async def not_fund_exception_handler(request: Request, exc: MyException):
return JSONResponse(ErrorResponse(message=str(exc), status=exc.status).dict())
@app.exception_handler(AssertionError)
async def assert_exception_handler(request: Request, exc: AssertionError):
return JSONResponse(ErrorResponse(message=str(exc), status=status.HTTP_400_BAD_REQUEST).dict())
@app.exception_handler(RequestValidationError)
async def request_validation_exception_handler(request: Request, exc: RequestValidationError):
"""
......
from enum import Enum
from typing import List
from pydantic import BaseModel, Field
from model import BaseCreateModel
class AssetType(str, Enum):
normal = "normal"
vault = "vault"
adjust = "adjust"
node = "node"
other = "other"
class BaseAssetItem(BaseModel):
class BaseCoinAssetItem(BaseModel):
fund_id: str = Field(..., description='基金id')
currency: str = Field(..., description='币种')
value: float = Field(..., description='资产数量')
asset_type: AssetType = Field(default=AssetType.normal, description='资产类型')
volume: float = Field(..., description='资产数量')
asset_type: AssetType = Field(default=AssetType.vault, description='资产类型')
# 传入数据库类型 / 接口返回类型
class NormalAsset(BaseAssetItem, BaseCreateModel):
pass
class NormalAsset(BaseCreateModel):
fund_id: str = Field(..., description='基金id')
assets: List[BaseCoinAssetItem] = Field(default=[], description='基金id')
# 传入数据库类型 / 接口返回类型
......@@ -29,12 +30,14 @@ class NormalAssetRecord(NormalAsset):
# 接口传入模型
class CreateAsset(BaseAssetItem):
class CreateAsset(BaseCoinAssetItem):
pass
class UpdateAsset(BaseModel):
value: float = Field(..., description='资产数量')
volume: float = Field(..., description='资产数量')
class Config:
orm_mode = True
......@@ -12,30 +12,57 @@ class BillType(str, Enum):
redemption = "redemption"
# 兑换
exchange = "exchange"
# 质押
staking = "staking"
# 其他
other = "other"
class BaseBillItem(BaseModel):
# 接口传入模型
# 创建申购赎回记录
class CreatePCFBill(BaseModel):
fund_id: str = Field(None, description='基金id')
email: str = Field(None, description='客户邮箱')
bill_type: BillType = Field(..., description='账目类型')
currency: str = Field(None, description='币种')
value: float = Field(..., description='资产数量')
volume: float = Field(..., description='资产数量')
price: float = Field(None, description="价格")
market_value: float = Field(None, description="市值")
# 传入数据库类型 / 接口返回类型
class NormalBill(BaseBillItem, BaseCreateModel):
fund_id: str = Field(None, description='基金id')
email: str = Field(None, description='客户邮箱')
class PCFBill(BaseCreateModel):
"""申购赎回"""
user_id: str
fund_share: float = Field(None, description="基金份额")
bill_type: BillType = Field(..., description='账目记录')
market_value: float = Field(None, description="市值")
# 置换币记录 传入数据库类型 / 接口返回类型
class ExchangeBill(BaseBillItem, BaseCreateModel):
class CreateExchangeBill(BaseModel):
fund_id: str = Field(None, description='基金id')
bill_type: BillType = Field(..., description='账目记录')
exchange_detail: List[BaseBillItem] = Field(default=[], description='置换详情')
bill_type: BillType = Field(default=BillType.exchange, description='账目类型')
input_currency: str = Field(..., description="投入币种")
input_price: float = Field(..., description="投入币种价格")
input_volume: float = Field(..., description="投入数量")
output_currency: str = Field(..., description="输出币种")
output_price: float = Field(..., description="输出币种价格")
output_volume: float = Field(..., description="输出数量")
class ExchangeBill(BaseCreateModel):
"""置换账户"""
user_id: str
input_value: float = Field(..., description="投入价值")
output_value: float = Field(..., description="输出价值")
profit: float = Field(0, description="置换产生的价差")
class StakingBill(BaseCreateModel):
"""质押账目"""
user_id: str
fund_id: str = Field(None, description='基金id')
bill_type: BillType = Field(default=BillType.staking, description='账目类型')
market_value: float = Field(None, description="市值")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment