Commit 5fc5dc60 authored by confusion's avatar confusion

修改查询返回

parent 16dc092d
...@@ -7,8 +7,8 @@ from pymongo.operations import UpdateOne ...@@ -7,8 +7,8 @@ from pymongo.operations import UpdateOne
from dependencies import get_current_user, get_fund_collect, get_bill_collect from dependencies import get_current_user, get_fund_collect, get_bill_collect
from exception.db import NotFundError from exception.db import NotFundError
from model import Response, Page, PageResponse, SortParams, FilterTime from model import Response, Page, PageResponse, SortParams, FilterTime
from model.bill import PCFBill, ExchangeBill, BillType, CreatePCFBill, CreateExchangeBill, StakingBill, \ from model.bill import PCFBillRes, ExchangeBillRes, BillType, CreatePCFBill, CreateExchangeBill, StakingBillRes, \
AdjustBill, CreateAdjustBill, UpdatePCFBill, UpdateExchangeBill, UpdateStakingBill, UpdateAdjustBill, \ AdjustBillRes, CreateAdjustBill, UpdatePCFBill, UpdateExchangeBill, UpdateStakingBill, UpdateAdjustBill, \
CreateStakingBill CreateStakingBill
from service.bill import update_bill, create_staking from service.bill import update_bill, create_staking
from tools.jwt_tools import User from tools.jwt_tools import User
...@@ -17,7 +17,7 @@ router = APIRouter() ...@@ -17,7 +17,7 @@ router = APIRouter()
@router.post('/pcf/', @router.post('/pcf/',
response_model=Response[PCFBill], response_model=Response[PCFBillRes],
tags=['新增'], tags=['新增'],
summary='添加[申购/赎回]账目', summary='添加[申购/赎回]账目',
description='添加申购赎回账目') description='添加申购赎回账目')
...@@ -41,13 +41,13 @@ async def create_pcf( ...@@ -41,13 +41,13 @@ async def create_pcf(
update = {"$push": {"assets": {"currency": item.currency, "volume": item.volume}}} update = {"$push": {"assets": {"currency": item.currency, "volume": item.volume}}}
await fund_collect.update_one(query, update) await fund_collect.update_one(query, update)
market_value = item.volume * item.price market_value = item.volume * item.price
prc = PCFBill(user_id=user.id, fund_share=market_value / fund["nav"], market_value=market_value, **item.dict()) prc = PCFBillRes(user_id=user.id, fund_share=market_value / fund["nav"], market_value=market_value, **item.dict())
await bill_collect.insert_one(prc.dict()) await bill_collect.insert_one(prc.dict())
return Response[PCFBill](data=prc.dict()) return Response[PCFBillRes](data=prc.dict())
@router.post('/exchange/', @router.post('/exchange/',
response_model=Response[ExchangeBill], response_model=Response[ExchangeBillRes],
tags=['新增'], tags=['新增'],
summary='添加置换币账目', summary='添加置换币账目',
description='添加置换币账目') description='添加置换币账目')
...@@ -78,14 +78,14 @@ async def create_exchange( ...@@ -78,14 +78,14 @@ async def create_exchange(
result = await fund_collect.bulk_write([update_input, update_output]) result = await fund_collect.bulk_write([update_input, update_output])
logger.info(result.modified_count) logger.info(result.modified_count)
input_value, output_value = item.input_volume * item.input_price, item.output_volume * item.output_price 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, exchange_bill = ExchangeBillRes(user_id=user.id, input_value=input_value, output_value=output_value,
profit=output_value - input_value, **item.dict()) profit=output_value - input_value, **item.dict())
await bill_collect.insert_one(exchange_bill.dict()) await bill_collect.insert_one(exchange_bill.dict())
return Response[ExchangeBill](data=exchange_bill.dict()) return Response[ExchangeBillRes](data=exchange_bill.dict())
@router.post('/adjust/', @router.post('/adjust/',
response_model=Response[AdjustBill], response_model=Response[AdjustBillRes],
tags=['新增'], tags=['新增'],
summary='添加调整账目', summary='添加调整账目',
description='添加调整账目') description='添加调整账目')
...@@ -106,14 +106,14 @@ async def create_adjust( ...@@ -106,14 +106,14 @@ async def create_adjust(
{"$inc": {"adjust_assets.$.volume": item.volume}} {"$inc": {"adjust_assets.$.volume": item.volume}}
) )
logger.info(f"inc_result={inc_result.modified_count}") logger.info(f"inc_result={inc_result.modified_count}")
adjust_bill = AdjustBill(user_id=user.id, **item.dict()) adjust_bill = AdjustBillRes(user_id=user.id, **item.dict())
await bill_collect.insert_one(adjust_bill.dict()) await bill_collect.insert_one(adjust_bill.dict())
response = Response[AdjustBill](data=adjust_bill.dict()) response = Response[AdjustBillRes](data=adjust_bill.dict())
return response return response
@router.post('/staking/', @router.post('/staking/',
response_model=Response[StakingBill], response_model=Response[StakingBillRes],
tags=['新增'], tags=['新增'],
summary='添加质押账目', summary='添加质押账目',
description='添加质押账目') description='添加质押账目')
...@@ -124,70 +124,13 @@ async def create_staking_api( ...@@ -124,70 +124,13 @@ async def create_staking_api(
bill_collect: AgnosticCollection = Depends(get_bill_collect), bill_collect: AgnosticCollection = Depends(get_bill_collect),
): ):
staking_bill = await create_staking(item, user.id, fund_collect, bill_collect) staking_bill = await create_staking(item, user.id, fund_collect, bill_collect)
response = Response[StakingBill](data=staking_bill.dict()) response = Response[StakingBillRes](data=staking_bill.dict())
return response return response
# @router.get('/exchange/{fund_id}/',
# response_model=PageResponse[ExchangeBill],
# summary='查询置换记录',
# description='')
# async def query_exchange(
# fund_id: str,
# page: Page = Depends(Page),
# user: User = Depends(get_current_user),
# bill_collect: AgnosticCollection = Depends(get_bill_collect),
# ):
# skip = (page.page - 1) * page.page_size
# cursor = bill_collect.find(
# {"fund_id": fund_id, "user_id": user.id, "bill_type": BillType.exchange})
# cursor = cursor.skip(skip).sort([('create_time', -1)]).limit(page.page_size)
# result = await cursor.to_list(length=None)
# response = PageResponse[ExchangeBill](data=result, **page.dict(), total=len(result))
# return response
# @router.get('/staking/{fund_id}/',
# response_model=PageResponse[StakingBill],
# summary='查询质押记录',
# description='')
# async def query_staking(
# fund_id: str,
# page: Page = Depends(Page),
# user: User = Depends(get_current_user),
# bill_collect: AgnosticCollection = Depends(get_bill_collect),
# ):
# skip = (page.page - 1) * page.page_size
# cursor = bill_collect.find(
# {"fund_id": fund_id, "user_id": user.id, "bill_type": BillType.staking})
# cursor = cursor.skip(skip).sort([('create_time', -1)]).limit(page.page_size)
# result = await cursor.to_list(length=None)
# response = PageResponse[StakingBill](data=result, **page.dict(), total=len(result))
# return response
# @router.get('/adjust/{fund_id}/',
# response_model=PageResponse[AdjustBill],
# summary='查询调整记录',
# description='')
# async def query_adjust(
# fund_id: str,
# page: Page = Depends(Page),
# user: User = Depends(get_current_user),
# bill_collect: AgnosticCollection = Depends(get_bill_collect),
# ):
# skip = (page.page - 1) * page.page_size
# cursor = bill_collect.find(
# {"fund_id": fund_id, "user_id": user.id, "bill_type": BillType.adjust})
# cursor = cursor.skip(skip).sort([('create_time', -1)]).limit(page.page_size)
# result = await cursor.to_list(length=None)
# response = PageResponse[AdjustBill](data=result, **page.dict(), total=len(result))
# return response
@router.put('/pcf/{fund_id}/', @router.put('/pcf/{fund_id}/',
tags=['更新'], tags=['更新'],
response_model=Response[PCFBill], response_model=Response[PCFBillRes],
summary='更新申购赎回记录', summary='更新申购赎回记录',
description='') description='')
async def update_pcf_bill( async def update_pcf_bill(
...@@ -205,14 +148,14 @@ async def update_pcf_bill( ...@@ -205,14 +148,14 @@ async def update_pcf_bill(
update_data=update_item, update_data=update_item,
fund_collect=fund_collect, fund_collect=fund_collect,
bill_collect=bill_collect, bill_collect=bill_collect,
res_model=PCFBill res_model=PCFBillRes
) )
return response return response
@router.put('/exchange/{fund_id}/', @router.put('/exchange/{fund_id}/',
tags=['更新'], tags=['更新'],
response_model=Response[ExchangeBill], response_model=Response[ExchangeBillRes],
summary='更新置换记录', summary='更新置换记录',
description='') description='')
async def update_exchange_bill( async def update_exchange_bill(
...@@ -230,14 +173,14 @@ async def update_exchange_bill( ...@@ -230,14 +173,14 @@ async def update_exchange_bill(
update_data=update_item, update_data=update_item,
fund_collect=fund_collect, fund_collect=fund_collect,
bill_collect=bill_collect, bill_collect=bill_collect,
res_model=ExchangeBill res_model=ExchangeBillRes
) )
return response return response
@router.put('/staking/{fund_id}/', @router.put('/staking/{fund_id}/',
tags=['更新'], tags=['更新'],
response_model=Response[StakingBill], response_model=Response[StakingBillRes],
summary='更新质押记录', summary='更新质押记录',
description='') description='')
async def update_staking_bill( async def update_staking_bill(
...@@ -255,14 +198,14 @@ async def update_staking_bill( ...@@ -255,14 +198,14 @@ async def update_staking_bill(
update_data=update_item, update_data=update_item,
fund_collect=fund_collect, fund_collect=fund_collect,
bill_collect=bill_collect, bill_collect=bill_collect,
res_model=StakingBill res_model=StakingBillRes
) )
return response return response
@router.put('/adjust/{fund_id}/', @router.put('/adjust/{fund_id}/',
tags=['更新'], tags=['更新'],
response_model=Response[AdjustBill], response_model=Response[AdjustBillRes],
summary='更新调整记录', summary='更新调整记录',
description='') description='')
async def update_adjust_bill( async def update_adjust_bill(
...@@ -280,14 +223,17 @@ async def update_adjust_bill( ...@@ -280,14 +223,17 @@ async def update_adjust_bill(
update_data=update_item, update_data=update_item,
fund_collect=fund_collect, fund_collect=fund_collect,
bill_collect=bill_collect, bill_collect=bill_collect,
res_model=AdjustBill res_model=AdjustBillRes
) )
return response return response
@router.get('/{fund_id}/', @router.get('/{fund_id}/',
tags=["查询"], tags=["查询"],
response_model=PageResponse[Union[PCFBill, ExchangeBill, StakingBill, AdjustBill]], # response_model=PageResponse[Any],
response_model=Union[
PageResponse[PCFBillRes], PageResponse[ExchangeBillRes], PageResponse[StakingBillRes], PageResponse[
AdjustBillRes]],
summary='查询账单记录', summary='查询账单记录',
description='查询账单记录') description='查询账单记录')
async def query_bill( async def query_bill(
......
...@@ -33,18 +33,17 @@ class CreatePCFBill(BaseModel): ...@@ -33,18 +33,17 @@ class CreatePCFBill(BaseModel):
bill_type: BillType = Field(..., description='账目类型') bill_type: BillType = Field(..., description='账目类型')
currency: str = Field(None, description='币种') currency: str = Field(None, description='币种')
volume: float = Field(..., description='资产数量') volume: float = Field(..., description='资产数量')
price: float = Field(None, description="价格") price: float = Field(..., description="价格")
# record_time: datetime.date = Field(default_factory=lambda: datetime.datetime.utcnow().date(), description='记录时间')
record_time: str = Field(default_factory=lambda: str(datetime.datetime.utcnow().date()), description='记录时间') record_time: str = Field(default_factory=lambda: str(datetime.datetime.utcnow().date()), description='记录时间')
remark: str = Field(default="", description="备注") remark: str = Field(default="", description="备注")
# 传入数据库类型 / 接口返回类型 # 传入数据库类型 / 接口返回类型
class PCFBill(CreatePCFBill, BaseCreateModel): class PCFBillRes(CreatePCFBill, BaseCreateModel):
"""申购赎回""" """申购赎回"""
user_id: str user_id: str
fund_share: float = Field(None, description="基金份额") fund_share: float = Field(..., description="基金份额")
market_value: float = Field(None, description="市值") market_value: float = Field(..., description="市值")
# 置换币记录 传入数据库类型 / 接口返回类型 # 置换币记录 传入数据库类型 / 接口返回类型
...@@ -60,13 +59,13 @@ class CreateExchangeBill(BaseModel): ...@@ -60,13 +59,13 @@ class CreateExchangeBill(BaseModel):
remark: str = Field(default="", description="备注") remark: str = Field(default="", description="备注")
class ExchangeBill(CreateExchangeBill, BaseCreateModel): class ExchangeBillRes(CreateExchangeBill, BaseCreateModel):
"""置换账户""" """置换账户"""
user_id: str user_id: str
input_value: float = Field(..., description="投入价值") input_value: float = Field(..., description="投入价值")
output_value: float = Field(..., description="输出价值") output_value: float = Field(..., description="输出价值")
profit: float = Field(0, description="置换产生的价差") profit: float = Field(0, description="置换产生的价差")
bill_type: BillType = Field(default=BillType.exchange, description='账目类型') bill_type: BillType = BillType.exchange
class CreateStakingBill(BaseModel): class CreateStakingBill(BaseModel):
...@@ -74,28 +73,27 @@ class CreateStakingBill(BaseModel): ...@@ -74,28 +73,27 @@ class CreateStakingBill(BaseModel):
currency: str = Field(default="ETH", description='币种') currency: str = Field(default="ETH", description='币种')
volume: float = Field(default=32, description='数量') volume: float = Field(default=32, description='数量')
pub_key: str = Field(..., description='节点key') pub_key: str = Field(..., description='节点key')
# record_time: str = Field(default_factory=lambda: str(datetime.datetime.utcnow().date()), description='记录时间') record_time: str = Field(default_factory=lambda: str(datetime.datetime.utcnow().date()), description='记录时间')
remark: str = Field(default="", description="备注") remark: str = Field(default="", description="备注")
class StakingBill(CreateStakingBill, BaseCreateModel): class StakingBillRes(CreateStakingBill, BaseCreateModel):
"""质押账目""" """质押账目"""
user_id: str user_id: str
bill_type: BillType = Field(default=BillType.staking, description='账目类型') bill_type: BillType = BillType.staking
class CreateAdjustBill(BaseModel): class CreateAdjustBill(BaseModel):
fund_id: str = Field(None, description='基金id') fund_id: str = Field(None, description='基金id')
currency: str = Field(None, description='币种') currency: str = Field('USD', description='币种')
volume: float = Field(..., description='资产数量') volume: float = Field(..., description='资产数量')
price: float = Field(None, description="价格")
record_time: str = Field(default_factory=lambda: str(datetime.datetime.utcnow().date()), description='记录时间') record_time: str = Field(default_factory=lambda: str(datetime.datetime.utcnow().date()), description='记录时间')
remark: str = Field(default="", description="备注") remark: str = Field(default="", description="备注")
class AdjustBill(CreateAdjustBill, BaseCreateModel): class AdjustBillRes(CreateAdjustBill, BaseCreateModel):
user_id: str user_id: str
bill_type: BillType = Field(default=BillType.adjust, description='账目类型') bill_type: BillType = BillType.adjust
# 更新model # 更新model
......
...@@ -6,7 +6,7 @@ from pymongo import ReturnDocument ...@@ -6,7 +6,7 @@ from pymongo import ReturnDocument
from exception.db import NotFundError from exception.db import NotFundError
from model import Response from model import Response
from model.bill import CreateStakingBill, StakingBill from model.bill import CreateStakingBill, StakingBillRes
DataT = TypeVar('DataT') DataT = TypeVar('DataT')
...@@ -27,7 +27,7 @@ async def create_staking( ...@@ -27,7 +27,7 @@ async def create_staking(
# else: # else:
# update = {"$push": {"assets": {"currency": item.currency, "volume": -item.volume}}} # update = {"$push": {"assets": {"currency": item.currency, "volume": -item.volume}}}
# await fund_collect.update_one(query, update) # await fund_collect.update_one(query, update)
staking_bill = StakingBill(user_id=user_id, **item.dict()) staking_bill = StakingBillRes(user_id=user_id, **item.dict())
await bill_collect.insert_one(staking_bill.dict()) await bill_collect.insert_one(staking_bill.dict())
return staking_bill return staking_bill
......
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