Commit 4e2c7e47 authored by 陈涛's avatar 陈涛

增加establishment_time

parent a1e83a1e
from typing import Union
from loguru import logger from loguru import logger
from fastapi import APIRouter, Depends from fastapi import APIRouter, Depends
from motor.core import AgnosticCollection from motor.core import AgnosticCollection
from pydantic import BaseModel
from pymongo.operations import UpdateOne 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 from model import Response, Page, PageResponse
from model.bill import PCFBill, ExchangeBill, BillType, CreatePCFBill, CreateExchangeBill, StakingBill, CreateStaking from model.bill import PCFBill, ExchangeBill, BillType, CreatePCFBill, CreateExchangeBill, StakingBill, CreateStaking, \
AdjustBill, CreateAdjustBill
from tools.jwt_tools import User from tools.jwt_tools import User
router = APIRouter() router = APIRouter()
...@@ -26,8 +24,9 @@ async def create_pcf( ...@@ -26,8 +24,9 @@ async def create_pcf(
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, "枚举错误" assert item.bill_type == BillType.sub or item.bill_type == BillType.redemption, "枚举错误"
query = {"id": item.fund_id} query = {"id": item.fund_id, "user_id": user.id}
fund = await fund_collect.find_one(query) fund = await fund_collect.find_one(query)
assert fund, NotFundError()
filter_asset = list(filter(lambda x: x["currency"] == item.currency, fund["assets"])) filter_asset = list(filter(lambda x: x["currency"] == item.currency, fund["assets"]))
if filter_asset: if filter_asset:
inc = item.volume if item.bill_type == BillType.sub else -item.volume inc = item.volume if item.bill_type == BillType.sub else -item.volume
...@@ -53,7 +52,7 @@ async def create_exchange( ...@@ -53,7 +52,7 @@ async def create_exchange(
fund_collect: AgnosticCollection = Depends(get_fund_collect), fund_collect: AgnosticCollection = Depends(get_fund_collect),
bill_collect: AgnosticCollection = Depends(get_bill_collect), bill_collect: AgnosticCollection = Depends(get_bill_collect),
): ):
fund = await fund_collect.find_one({"id": item.fund_id}) fund = await fund_collect.find_one({"id": item.fund_id, "user_id": user.id})
assert fund, NotFundError() assert fund, NotFundError()
filter_asset = list(filter(lambda x: x["currency"] == item.input_currency, fund["assets"])) filter_asset = list(filter(lambda x: x["currency"] == item.input_currency, fund["assets"]))
assert filter_asset, f"{item.input_currency}余额不足" assert filter_asset, f"{item.input_currency}余额不足"
...@@ -80,24 +79,30 @@ async def create_exchange( ...@@ -80,24 +79,30 @@ async def create_exchange(
return Response[ExchangeBill](data=exchange_bill.dict()) return Response[ExchangeBill](data=exchange_bill.dict())
@router.get('/pcf/{fund_id}/', @router.post('/adjust/',
response_model=PageResponse[PCFBill], response_model=Response[AdjustBill],
summary='查询申购赎回记录', summary='添加调整账目',
description='') description='添加调整账目')
async def query_bill( async def create_adjust(
fund_id: str, item: CreateAdjustBill,
bill_type: BillType,
page: Page = Depends(Page),
user: User = Depends(get_current_user), user: User = Depends(get_current_user),
fund_collect: AgnosticCollection = Depends(get_fund_collect),
bill_collect: AgnosticCollection = Depends(get_bill_collect), bill_collect: AgnosticCollection = Depends(get_bill_collect),
): ):
skip = (page.page - 1) * page.page_size query = {"id": item.fund_id, "user_id": user.id}
cursor = bill_collect.find( result = await fund_collect.update_one(
{"fund_id": fund_id, "user_id": user.id, "bill_type": {"$in": [BillType.sub, BillType.redemption]}}) {**query, "adjust_assets": {"$not": {"$elemMatch": {"currency": item.currency}}}},
cursor = cursor.skip(skip).limit(page.page_size) {"$push": {"adjust_assets": {"currency": item.currency, "volume": item.volume}}}
result = await cursor.to_list(length=None) )
if result.modified_count == 0:
response = PageResponse[PCFBill](data=result, **page.dict(), total=len(result)) inc_result = await fund_collect.update_one(
{**query, "adjust_assets.currency": item.currency},
{"$inc": {"adjust_assets.$.volume": item.volume}}
)
logger.info(f"inc_result={inc_result.modified_count}")
adjust_bill = AdjustBill(user_id=user.id, **item.dict())
await bill_collect.insert_one(adjust_bill.dict())
response = Response[AdjustBill](data=adjust_bill.dict())
return response return response
...@@ -105,7 +110,7 @@ async def query_bill( ...@@ -105,7 +110,7 @@ async def query_bill(
response_model=PageResponse[ExchangeBill], response_model=PageResponse[ExchangeBill],
summary='查询置换记录', summary='查询置换记录',
description='') description='')
async def query_exchange( async def query_exchange_bill(
fund_id: str, fund_id: str,
page: Page = Depends(Page), page: Page = Depends(Page),
user: User = Depends(get_current_user), user: User = Depends(get_current_user),
...@@ -156,3 +161,35 @@ async def query_staking( ...@@ -156,3 +161,35 @@ async def query_staking(
result = await cursor.to_list(length=None) result = await cursor.to_list(length=None)
response = PageResponse[StakingBill](data=result, **page.dict(), total=len(result)) response = PageResponse[StakingBill](data=result, **page.dict(), total=len(result))
return response 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).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('/{fund_id}/',
# response_model=PageResponse[AdjustBill],
# summary='更新调整记录',
# description='')
# async def update_bill(
# bill_id: str,
# update_item: str,
# user: User = Depends(get_current_user),
# bill_collect: AgnosticCollection = Depends(get_bill_collect),
# ):
# pass
\ No newline at end of file
from enum import Enum from enum import Enum
from typing import List from typing import List, Optional
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
from model import BaseCreateModel from model import BaseCreateModel
...@@ -71,10 +71,43 @@ class StakingBill(BaseCreateModel): ...@@ -71,10 +71,43 @@ class StakingBill(BaseCreateModel):
bill_type: BillType = Field(default=BillType.staking, description='账目类型') bill_type: BillType = Field(default=BillType.staking, description='账目类型')
class CreateAdjustBill(BaseCreateModel): class CreateAdjustBill(BaseModel):
pass fund_id: str = Field(None, description='基金id')
currency: str = Field(None, description='币种')
volume: float = Field(..., description='资产数量')
price: float = Field(None, description="价格")
class AdjustBill(BaseCreateModel): class AdjustBill(CreateAdjustBill, BaseCreateModel):
user_id: str user_id: str
bill_type: BillType = Field(default=BillType.adjust, description='账目类型') bill_type: BillType = Field(default=BillType.adjust, description='账目类型')
# 更新model
class UpdatePCFBill(BaseModel):
email: Optional[str] = Field(None, description='客户邮箱')
currency: Optional[str] = Field(None, description='币种')
volume: Optional[float] = Field(..., description='资产数量')
price: Optional[float] = Field(None, description="价格")
fund_share: Optional[float] = Field(None, description="基金份额")
market_value: Optional[float] = Field(None, description="市值")
class UpdateExchangeBill(BaseModel):
input_currency: Optional[str] = Field(..., description="投入币种")
input_price: Optional[float] = Field(..., description="投入币种价格")
input_volume: Optional[float] = Field(..., description="投入数量")
output_currency: Optional[str] = Field(..., description="输出币种")
output_price: Optional[float] = Field(..., description="输出币种价格")
output_volume: Optional[float] = Field(..., description="输出数量")
class UpdateStakingBill(BaseModel):
currency: Optional[str] = Field(default="ETH", description='币种')
volume: Optional[float] = Field(..., description='数量')
class UpdateAdjustBill(BaseModel):
currency: Optional[str] = Field(None, description='币种')
volume: Optional[float] = Field(..., description='资产数量')
price: Optional[float] = Field(None, description="价格")
import datetime
from enum import Enum from enum import Enum
from typing import List, Optional from typing import List, Optional
...@@ -24,6 +25,7 @@ class BaseFundItem(BaseModel): ...@@ -24,6 +25,7 @@ class BaseFundItem(BaseModel):
fund_status: FundStatus = Field(default=FundStatus.active, description='基金状态') fund_status: FundStatus = Field(default=FundStatus.active, description='基金状态')
base_coin: str = Field(default='USD', description='基准币种') base_coin: str = Field(default='USD', description='基准币种')
base_nav: float = Field(default=1, description='初始净值') base_nav: float = Field(default=1, description='初始净值')
establishment_time: int = Field(default_factory=lambda: int(datetime.datetime.utcnow().timestamp()), description="成立日期")
settlement_time: str = Field(default='08:00', description='结算时间') settlement_time: str = Field(default='08:00', description='结算时间')
...@@ -43,6 +45,7 @@ class UpdateFund(BaseModel): ...@@ -43,6 +45,7 @@ class UpdateFund(BaseModel):
nav: Optional[float] = Field(None, description='当前净值') nav: Optional[float] = Field(None, description='当前净值')
base_nav: Optional[float] = Field(None, description='初始净值') base_nav: Optional[float] = Field(None, description='初始净值')
settlement_time: Optional[str] = Field(None, description='结算时间') settlement_time: Optional[str] = Field(None, description='结算时间')
establishment_time: Optional[int] = Field(None, description="成立日期")
class Config: class Config:
orm_mode = True orm_mode = True
......
...@@ -53,7 +53,7 @@ async def get_identify_key(): ...@@ -53,7 +53,7 @@ async def get_identify_key():
public_key = rsa.from_jwk(json.dumps(key_data)) public_key = rsa.from_jwk(json.dumps(key_data))
settings.public_key = public_key settings.public_key = public_key
settings.algorithms = key_data['alg'] settings.algorithms = key_data['alg']
logger.info('公钥获取成功') logger.info(f"公钥获取成功, public_key={public_key}, algorithms={key_data['alg']}")
def decode_token(token): def decode_token(token):
......
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