urban-investment-research/DataLoad/AreaDataLoad.py

151 lines
7.2 KiB
Python

from fastapi import HTTPException
from sqlalchemy.orm import Session
from Context.common import yujingtong_api
from Mods.RegionalEconomies.Mods.AreaDebt.Models import AreaDebt
from Mods.RegionalEconomies.Mods.AreaDepositsAndLoans.Models import AreaDepositsAndLoans
from Mods.RegionalEconomies.Mods.AreaFiscalRevenue.Models import AreaFiscalRevenue
from Mods.RegionalEconomies.Mods.AreaGdp.Models import AreaGdp
from Mods.RegionalEconomies.Mods.AreaImportAndExport.Models import AreaImportAndExport
from Mods.RegionalEconomies.Mods.AreaIndustry.Models import AreaIndustry
from Mods.RegionalEconomies.Mods.AreaInvest.Models import AreaInvest
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:
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
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
new_area_import_and_export.count_value = year
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
check_if_created_then_add(db, new_area_deposits_and_loans)
###
new_area_industry = AreaIndustry()
new_area_industry.area_id = area_id
new_area_industry.count_value = year
new_area_industry.primary_industry = area_economic_res.PRI_IND
new_area_industry.secondary_industry = area_economic_res.SEC_IND
new_area_industry.tertiary_Industry = area_economic_res.TER_IND
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
check_if_created_then_add(db, new_area_industry)
##
new_area_livelihood = AreaLivelihood()
new_area_livelihood.area_id = area_id
new_area_livelihood.count_value = year
new_area_livelihood.population = area_economic_res.PERM_RESI
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
check_if_created_then_add(db, new_area_livelihood)
##
new_area_real_estate = AreaRealEstate()
new_area_real_estate.area_id = area_id
new_area_real_estate.count_value = year
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
check_if_created_then_add(db, new_area_real_estate)
###
new_area_gdp = AreaGdp()
new_area_gdp.area_id = area_id
new_area_gdp.count_value = year
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
check_if_created_then_add(db, new_area_gdp)
###
new_area_invest = AreaInvest()
new_area_invest.area_id = area_id
new_area_invest.count_value = year
# 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
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):
year = str(year)
# 区域财政
area_finance_res = yujingtong_api.get_area_finance(area_id, year)
if not area_finance_res:
return False
new_item = AreaFiscalRevenue()
new_item.area_id = area_id
new_item.count_value = year
new_item.general_public_budget_income = area_finance_res.RVN_IN_GEN_PUB_BGT
new_item.general_public_budget_expenditure = area_finance_res.EXPD_IN_GOV_GEN_PUB_BGT
new_item.transfer_income = area_finance_res.TRAN_INC
new_item.transfer_expenditure = area_finance_res.TRAN_EXP
# new_item.fiscal_revenue=area_finance_res.
new_item.general_bond_income = area_finance_res.GEN_BON_REV
new_item.general_bond_principal_repayment_expenditure = area_finance_res.GEN_BON_REP_EXP
new_item.special_bonds_income = area_finance_res.SPE_BOND_INC
new_item.principal_repayment_expenditure_of_special_bonds = area_finance_res.SPE_BOND_REP_EXP
new_item.tax_revenue = area_finance_res.TAX_RVN
new_item.transfer_payment_income = area_finance_res.TRAN_INC
# new_item.fiscal_expenditure=
new_item.government_fund_income = area_finance_res.RVN_INTO_GOV_MNGD_FD
new_item.land_transfer_income = area_finance_res.LAND_TRAN_INC
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
check_if_created_then_add(db, new_item)
db.commit()
return True
def area_debt_data_load(db: Session, area_id: int, year: int):
year = str(year)
# 区域债务
area_debt_res = yujingtong_api.get_area_debt(area_id, year)
if not area_debt_res:
return False
item = AreaDebt()
item.area_id = area_id
item.count_value = year
item.local_government_debt_balance = area_debt_res.OTS_DT_OF_GOV
item.local_government_debt_limit = area_debt_res.CEIL_DT_OF_GOV
item.fiscal_self_sufficiency_rate = area_debt_res.FSL_SELF_FNCG
item.general_debt_balance = area_debt_res.GEN_OTS_DT
item.special_debt_balance = area_debt_res.SPE_OTS_DT
item.general_debt_limit = area_debt_res.GEN_CEIL_DT
item.special_debt_limit = area_debt_res.SPE_CEIL_DT
item.debt_ratio = area_debt_res.GOV_DT_TO_GDP_RT
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
check_if_created_then_add(db, item)
db.commit()
return True