添加区域数据录入接口
This commit is contained in:
parent
92c0818385
commit
938ba03892
|
@ -12,18 +12,32 @@ from Mods.RegionalEconomies.Mods.AreaLivelihood.Models import AreaLivelihood
|
|||
from Mods.RegionalEconomies.Mods.AreaRealEstate.Models import AreaRealEstate
|
||||
|
||||
|
||||
def check_if_created_then_add(db: Session, new_model, update=False):
|
||||
"""
|
||||
判断数据是否存在,存在后是否更新
|
||||
new_model:单个model数据
|
||||
"""
|
||||
query = db.query(new_model.__class__).filter_by(**new_model.to_dict())
|
||||
item = query.first()
|
||||
if not item:
|
||||
db.add(new_model)
|
||||
else:
|
||||
if update:
|
||||
query.update(**new_model.to_dict())
|
||||
|
||||
|
||||
def area_economic_data_load(db: Session, area_id: int, year: int):
|
||||
year = str(year)
|
||||
area_economic_res = yujingtong_api.get_area_economic(area_id, year)
|
||||
if not area_economic_res:
|
||||
raise HTTPException(detail="未查询到该区域数据", status_code=403)
|
||||
return False
|
||||
###
|
||||
new_area_deposits_and_loans = AreaDepositsAndLoans()
|
||||
new_area_deposits_and_loans.area_id = area_id
|
||||
new_area_deposits_and_loans.count_value = year
|
||||
new_area_deposits_and_loans.deposit = area_economic_res.SAV_DEP_BAL
|
||||
new_area_deposits_and_loans.loan = area_economic_res.LOAN_BAL
|
||||
db.add(new_area_deposits_and_loans)
|
||||
check_if_created_then_add(db, new_area_deposits_and_loans)
|
||||
###
|
||||
new_area_import_and_export = AreaImportAndExport()
|
||||
new_area_import_and_export.area_id = area_id
|
||||
|
@ -31,7 +45,7 @@ def area_economic_data_load(db: Session, area_id: int, year: int):
|
|||
new_area_import_and_export.import_and_export_volume = area_economic_res.TOT_IPT_AND_EXP
|
||||
new_area_import_and_export.export_volume = area_economic_res.TOT_EPT
|
||||
new_area_import_and_export.import_volume = area_economic_res.TOT_IPT
|
||||
db.add(new_area_deposits_and_loans)
|
||||
check_if_created_then_add(db, new_area_deposits_and_loans)
|
||||
###
|
||||
new_area_industry = AreaIndustry()
|
||||
new_area_industry.area_id = area_id
|
||||
|
@ -42,7 +56,7 @@ def area_economic_data_load(db: Session, area_id: int, year: int):
|
|||
new_area_industry.industrial_added_value = area_economic_res.GRO_VAL_IND_INC
|
||||
new_area_industry.industrial_output = area_economic_res.GRO_VAL_IND
|
||||
new_area_industry.industrial_output_growth_rate = area_economic_res.GRO_VAL_IND_RATE
|
||||
db.add(new_area_industry)
|
||||
check_if_created_then_add(db, new_area_industry)
|
||||
##
|
||||
new_area_livelihood = AreaLivelihood()
|
||||
new_area_livelihood.area_id = area_id
|
||||
|
@ -51,7 +65,7 @@ def area_economic_data_load(db: Session, area_id: int, year: int):
|
|||
new_area_livelihood.consumer_goods_retail_sales = area_economic_res.TOT_RT_SALES_OF_CONS
|
||||
new_area_livelihood.consumer_goods_retail_sales_growth_rate = area_economic_res.TOT_RT_SALES_OF_CONS_RATE
|
||||
new_area_livelihood.per_capita_disposable_income_of_urban_residents = area_economic_res.PER_CPT_DPSB_INC_IN_UB
|
||||
db.add(new_area_livelihood)
|
||||
check_if_created_then_add(db, new_area_livelihood)
|
||||
##
|
||||
new_area_real_estate = AreaRealEstate()
|
||||
new_area_real_estate.area_id = area_id
|
||||
|
@ -59,7 +73,7 @@ def area_economic_data_load(db: Session, area_id: int, year: int):
|
|||
new_area_real_estate.second_hand_housing_average_price = area_economic_res.AVE_PRI_OF_SEC_HAND_HOUSE
|
||||
new_area_real_estate.residential_land_floor_price = area_economic_res.FLO_PRI_OF_RES_LAND
|
||||
new_area_real_estate.new_house_sales_price = area_economic_res.SAL_PRI_OF_NEW_BUIL
|
||||
db.add(new_area_real_estate)
|
||||
check_if_created_then_add(db, new_area_real_estate)
|
||||
###
|
||||
new_area_gdp = AreaGdp()
|
||||
new_area_gdp.area_id = area_id
|
||||
|
@ -67,7 +81,7 @@ def area_economic_data_load(db: Session, area_id: int, year: int):
|
|||
new_area_gdp.GDP = area_economic_res.GDP
|
||||
new_area_gdp.GDP_growth_rate = area_economic_res.GDP_GRO_ANL
|
||||
new_area_gdp.GDP_per_capita = area_economic_res.PER_CPT_GDP
|
||||
db.add(new_area_gdp)
|
||||
check_if_created_then_add(db, new_area_gdp)
|
||||
###
|
||||
new_area_invest = AreaInvest()
|
||||
new_area_invest.area_id = area_id
|
||||
|
@ -75,8 +89,9 @@ def area_economic_data_load(db: Session, area_id: int, year: int):
|
|||
new_area_invest.fixed_asset_investment = area_economic_res.FIXED_AST_IVM
|
||||
new_area_invest.fixed_asset_investment_growth_rate = area_economic_res.FIXED_AST_IVM_RATE
|
||||
new_area_invest.real_estate_investment = area_economic_res.INV_REAL_EST
|
||||
db.add(new_area_invest)
|
||||
check_if_created_then_add(db, new_area_invest)
|
||||
db.commit()
|
||||
return True
|
||||
|
||||
|
||||
def area_finance_data_load(db: Session, area_id: int, year: int):
|
||||
|
@ -84,7 +99,7 @@ def area_finance_data_load(db: Session, area_id: int, year: int):
|
|||
# 区域财政
|
||||
area_finance_res = yujingtong_api.get_area_finance(area_id, year)
|
||||
if not area_finance_res:
|
||||
raise HTTPException(detail="未查询到该区域数据", status_code=403)
|
||||
return False
|
||||
new_item = AreaFiscalRevenue()
|
||||
new_item.area_id = area_id
|
||||
new_item.count_value = year
|
||||
|
@ -105,8 +120,9 @@ def area_finance_data_load(db: Session, area_id: int, year: int):
|
|||
new_item.government_fund_expenditure = area_finance_res.EXP_INTO_GOV_MNGD_FD
|
||||
new_item.state_owned_capital_operation_income = area_finance_res.BGT_RVN_FROM_STAT_CPT_OPR
|
||||
new_item.state_owned_capital_operation_expenditure = area_finance_res.EXP_FROM_STAT_CPT_OPR
|
||||
db.add(new_item)
|
||||
check_if_created_then_add(db, new_item)
|
||||
db.commit()
|
||||
return True
|
||||
|
||||
|
||||
def area_debt_data_load(db: Session, area_id: int, year: int):
|
||||
|
@ -114,7 +130,7 @@ def area_debt_data_load(db: Session, area_id: int, year: int):
|
|||
# 区域债务
|
||||
area_debt_res = yujingtong_api.get_area_debt(area_id, year)
|
||||
if not area_debt_res:
|
||||
raise HTTPException(detail="未查询到该区域数据", status_code=403)
|
||||
return False
|
||||
item = AreaDebt()
|
||||
item.area_id = area_id
|
||||
item.count_value = year
|
||||
|
@ -129,5 +145,6 @@ def area_debt_data_load(db: Session, area_id: int, year: int):
|
|||
item.broad_sense_debt_ratio = area_debt_res.GOV_AND_FNC_PLTF_DT_TO_GDP_RT
|
||||
item.debt_to_asset_ratio = area_debt_res.GOV_DT_TO_RVN_RT
|
||||
item.broad_sense_debt_to_asset_ratio = area_debt_res.GOV_AND_FNC_PLTF_DT_TO_RVN_RT
|
||||
db.add(item)
|
||||
check_if_created_then_add(db, item)
|
||||
db.commit()
|
||||
return True
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from .Mods.AreaBond import Router as AreaBondRouter
|
||||
from .Mods.AreaGdp import Router as AreaGdpRouter
|
||||
from .Mods.AreaDebt import Router as AreaDebtRouter
|
||||
|
@ -15,8 +17,9 @@ from .Mods.AreaMajorProject import Router as AreaMajorProjectRouter
|
|||
from .Mods.AreaRealEstate import Router as AreaRealEstateRouter
|
||||
from .Mods.AreaSocialFinancing import Router as AreaSocialFinancingRouter
|
||||
from .Mods.AreaSurveyConclusion import Router as AreaSurveyConclusionRouter
|
||||
|
||||
from Context.common import auth_util
|
||||
from DataLoad.AreaDataLoad import area_economic_data_load, area_finance_data_load, area_debt_data_load
|
||||
from Context.common import auth_util, common_db
|
||||
from . import Schemas
|
||||
|
||||
router = APIRouter(prefix="/regional_economies",
|
||||
dependencies=[Depends(auth_util.token_data_depend)]
|
||||
|
@ -38,3 +41,17 @@ router.include_router(AreaMajorProjectRouter.router)
|
|||
router.include_router(AreaRealEstateRouter.router)
|
||||
router.include_router(AreaSocialFinancingRouter.router)
|
||||
router.include_router(AreaSurveyConclusionRouter.router)
|
||||
|
||||
|
||||
@router.post("/area_data_auto_load", summary="区域数据自动录入",tags=["区域数据录入"])
|
||||
def area_data_auto_load(req: Schemas.AreaDataAutoLoadReq, db: Session = Depends(common_db.get_db)):
|
||||
area_id = req.area_id
|
||||
year = req.year
|
||||
area_economic_data_load_result = area_economic_data_load(db, area_id, year)
|
||||
area_finance_data_load_result = area_finance_data_load(db, area_id, year)
|
||||
area_debt_data_load_result = area_debt_data_load(db, area_id, year)
|
||||
return {
|
||||
"区域经济数据录入结果": area_economic_data_load_result,
|
||||
"区域财政数据录入结果": area_finance_data_load_result,
|
||||
"区域债务数据录入结果": area_debt_data_load_result,
|
||||
}
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class AreaDataAutoLoadReq(BaseModel):
|
||||
area_id: int
|
||||
year: int
|
Loading…
Reference in New Issue