31 lines
815 B
Python
31 lines
815 B
Python
from fastapi import APIRouter
|
||
from pydantic import BaseModel
|
||
|
||
INDEX = "营业利润率-近三年加权平均(%)"
|
||
|
||
router = APIRouter()
|
||
|
||
|
||
class Params(BaseModel):
|
||
当年利润总额: float
|
||
当年营业收入: float
|
||
去年利润总额: float
|
||
去年营业收入: float
|
||
前年利润总额: float
|
||
前年营业收入: float
|
||
|
||
|
||
@router.post("/{}".format(INDEX))
|
||
def func(p: Params):
|
||
try:
|
||
one = p.当年利润总额 / p.当年营业收入 * 100
|
||
two = p.去年利润总额 / p.去年营业收入 * 100
|
||
three = p.前年利润总额 / p.前年营业收入 * 100
|
||
result = (one * 0.5 + two * 0.3 + three * 0.2) * 100
|
||
return round(result, 2)
|
||
|
||
except ZeroDivisionError:
|
||
return "算式无意义"
|
||
except Exception:
|
||
return "计算错误"
|