Commit ad124ae7 authored by 陈涛's avatar 陈涛

提交创建置换,申购赎回

parent cfb90ebf
from fastapi import APIRouter, Depends
from motor.core import AgnosticCollection
from pymongo.operations import UpdateOne
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 model.bill import PCFBill, ExchangeBill, BillType, CreatePCFBill, CreateExchangeBill
from tools.jwt_tools import User
router = APIRouter()
......@@ -18,26 +19,24 @@ 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)
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)
query = {"id": item.fund_id}
fund = await fund_collect.find_one(query)
filter_asset = list(filter(lambda x: x["currency"] == item.currency, fund["assets"]))
if filter_asset:
inc = item.volume if item.bill_type == BillType.sub else -item.volume
assert filter_asset[0]["volume"] + inc >= 0, "余额不足"
update = {"$inc": {"assets.$.volume": inc}}
await fund_collect.update_one({**query, "assets.currency": item.currency}, update)
else:
update = {"$push": {"assets": {"currency": item.currency, "volume": item.volume}}}
await fund_collect.update_one(query, update)
market_value = item.volume * item.price
prc = PCFBill(user_id=user.id, fund_share=market_value / fund["nav"], market_value=market_value, **item.dict())
await bill_collect.insert_one(prc.dict())
return Response[PCFBill](data=prc.dict())
@router.post('/exchange/',
......@@ -45,6 +44,32 @@ async def create_pcf(
summary='添加置换币账目',
description='添加置换币账目')
async def create_exchange(
item: CreateExchangeBill,
user: User = Depends(get_current_user),
fund_collect: AgnosticCollection = Depends(get_fund_collect),
bill_collect: AgnosticCollection = Depends(get_bill_collect),
):
pass
fund = await fund_collect.find_one({"id": item.fund_id})
filter_asset = list(filter(lambda x: x["currency"] == item.input_currency, fund["assets"]))
assert filter_asset, f"{item.input_currency}余额不足"
input_asset = filter_asset[0]
assert input_asset["volume"] >= item.input_volume, f"{item.input_currency}余额不足"
update_input = UpdateOne(
{"id": item.fund_id, "assets.currency": item.input_currency},
{"$inc": {"assets.$.volume": -item.input_volume}}
)
output_filter = list(filter(lambda x: x["currency"] == item.output_currency, fund["assets"]))
update_output = UpdateOne(
{"id": item.fund_id, "assets.currency": item.output_currency},
{"$inc": {"assets.$.volume": item.output_volume}}
) if output_filter else UpdateOne(
{"id": item.fund_id},
{"$push": {"assets": {"currency": item.output_currency, "volume": item.output_volume}}}
)
result = await fund_collect.bulk_write([update_input, update_output])
print(result.modified_count)
input_value, output_value = item.input_volume * item.input_price, item.output_volume * item.output_price
exchange_bill = ExchangeBill(user_id=user.id, input_value=input_value, output_value=output_value,
profit=output_value - input_value, **item.dict())
await bill_collect.insert_one(exchange_bill.dict())
return Response[ExchangeBill](data=exchange_bill.dict())
......@@ -31,7 +31,7 @@ class CreatePCFBill(BaseModel):
# 传入数据库类型 / 接口返回类型
class PCFBill(BaseCreateModel):
class PCFBill(CreatePCFBill, BaseCreateModel):
"""申购赎回"""
user_id: str
fund_share: float = Field(None, description="基金份额")
......@@ -43,7 +43,6 @@ class PCFBill(BaseCreateModel):
class CreateExchangeBill(BaseModel):
fund_id: str = Field(None, description='基金id')
bill_type: BillType = Field(default=BillType.exchange, description='账目类型')
input_currency: str = Field(..., description="投入币种")
input_price: float = Field(..., description="投入币种价格")
input_volume: float = Field(..., description="投入数量")
......@@ -52,12 +51,13 @@ class CreateExchangeBill(BaseModel):
output_volume: float = Field(..., description="输出数量")
class ExchangeBill(BaseCreateModel):
class ExchangeBill(CreateExchangeBill, BaseCreateModel):
"""置换账户"""
user_id: str
input_value: float = Field(..., description="投入价值")
output_value: float = Field(..., description="输出价值")
profit: float = Field(0, description="置换产生的价差")
bill_type: BillType = Field(default=BillType.exchange, description='账目类型')
class StakingBill(BaseCreateModel):
......
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