41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
from fastapi import APIRouter
|
||
from pydantic import BaseModel
|
||
|
||
INDEX = "总资产报酬率_三年加权计算(%)"
|
||
|
||
router = APIRouter()
|
||
|
||
|
||
class Params(BaseModel):
|
||
当期净利润: float
|
||
当期所得税: float
|
||
当期利息: float
|
||
上期资产总计: float
|
||
当期资产总计: float
|
||
|
||
上期净利润: float
|
||
上期所得税: float
|
||
上期利息: float
|
||
上二期资产总计: float
|
||
|
||
上二期净利润: float
|
||
上二期所得税: float
|
||
上二期利息: float
|
||
上三期资产总计: float
|
||
|
||
|
||
# 计算接口
|
||
@router.post("/{}".format(INDEX))
|
||
def func(p: Params):
|
||
try:
|
||
ratio1 = (p.当期净利润 + p.当期所得税 + p.当期利息) / ((p.上期资产总计 + p.当期资产总计) / 2) * 100
|
||
ratio2 = (p.上期净利润 + p.上期所得税 + p.上期利息) / ((p.上二期资产总计 + p.上期资产总计) / 2) * 100
|
||
ratio3 = (p.上二期净利润 + p.上二期所得税 + p.上二期利息) / ((p.上三期资产总计 + p.上二期资产总计) / 2) * 100
|
||
result = ratio1 * 0.5 + ratio2 * 0.3 + ratio3 * 0.3
|
||
return round(result, 2)
|
||
|
||
except ZeroDivisionError:
|
||
return "算式无意义"
|
||
except Exception:
|
||
return "计算错误"
|