from Mods.EditRecord.Crud import add_record from Utils.MiddlewareUtils import format_request from starlette.concurrency import iterate_in_threadpool from fastapi import Request from Context.common import auth_util, common_db import json from Utils.PrintUtils import print_error from enum import Enum class RecordTypeId(Enum): # 跟数据库record_item_type配置保持一致 company_index_main = 101 # 城投索引表 城投基本信息 company_search_main = 102 # 城投查询数据表 城投基本信息 company_base_info = 103 # 城投工商信息 城投基本信息 company_plus_property = 104 # 城投属性 城投基本信息 company_shareholder = 105 # 城投公司股东信息 城投基本信息 company_equity_penetration = 106 # 城投股权结构图 城投基本信息 company_control = 107 # 城投实控人 城投基本信息 company_credit_rating = 201 # 主体评级 城投补充信息 company_business_compose = 202 # 业务构成 城投补充信息 company_business_items = 203 # 重要项目 城投补充信息 company_finance_main = 204 # 财务报表主要指标 城投补充信息 company_restricted_assets = 205 # 受限资产 城投补充信息 company_financial_statement = 206 # 财务说明 城投补充信息 company_credit = 207 # 授信情况 城投补充信息 company_guarantee = 208 # 提供担保 城投补充信息 company_bond = 209 # 发债情况 城投补充信息 company_bank_lease = 210 # 银行借款 城投补充信息 company_trust_lease = 211 # 信托融资 城投补充信息 company_rental_lease = 212 # 租赁融资 城投补充信息 company_other_lease = 213 # 其他融资 城投补充信息 company_judicial_document = 214 # 裁判文书 城投补充信息 company_meeting_record = 301 # 历史上会记录 城投深度调研 company_conclusion = 302 # 调研结论 城投深度调研 company_score = 303 # 打分记录 城投深度调研 area_index_main = 401 # 区域索引表 区域信息 area_gdp = 402 # 区域GDP 区域信息 area_industry = 403 # 区域工业 区域信息 area_invest = 404 # 区域投资 区域信息 area_import_and_export = 405 # 区域进出口 区域信息 area_livelihood = 406 # 区域民生 区域信息 area_deposits_and_loans = 407 # 区域存贷款 区域信息 area_fiscal_revenue = 408 # 区域财政收支 区域信息 area_debt = 409 # 区域债务 区域信息 area_real_estate = 410 # 区域房地产 区域信息 area_industrial_structure = 411 # 区域产业结构 区域信息 area_major_project = 412 # 区域重大项目 区域信息 area_development_conclusion = 413 # 区域发展结论 区域信息 area_bond = 414 # 区域债券发行情况 区域信息 area_social_financing = 415 # 区域社会融资 区域信息 area_survey_conclusion = 416 # 区域调研结论 区域信息 opinion_information = 501 # 舆情资讯 其他信息 def get_update_data(before_data: dict, after_data: dict): """ 获取数据变化信息 """ update_data_before = {} update_data_after = {} for key, before_value in before_data.items(): if key in after_data: after_value = after_data[key] if before_value != after_value: update_data_before[key] = before_value update_data_after[key] = after_value return update_data_before, update_data_after async def record_middleware(req: Request, call_next): """ 编辑记录的中间件 """ class RecordTemp: need_record = False req_data = None action = None item_name = None req_url_split = req.url.path.split("/") if len(req_url_split) > 1 and req_url_split[-1] in ['delete', 'update', 'add']: convert_str = req_url_split[-2] if convert_str in RecordTypeId._member_names_: RecordTemp.need_record = True RecordTemp.req_data = await format_request(req) RecordTemp.item_name = convert_str RecordTemp.action = req_url_split[-1] res = await call_next(req) try: if RecordTemp.need_record: if res.status_code == 200: token_data = auth_util.token_data_depend(req.headers.get("Authorization")) db = common_db.get_db_i() try: item_id = None if RecordTemp.action == "add": res_body = [chunk async for chunk in res.body_iterator] res.body_iterator = iterate_in_threadpool(iter(res_body)) res_data = json.loads(res_body[0]) if "id" in res_data: item_id = res_data["id"] if "company_id" in res_data: item_id = res_data["company_id"] else: if "id" in RecordTemp.req_data.data: item_id = RecordTemp.req_data.data["id"] if "company_id" in RecordTemp.req_data.data: item_id = RecordTemp.req_data.data["company_id"] item_type_id = RecordTypeId[RecordTemp.item_name].value edit_user = token_data.email action = RecordTemp.action # if RecordTemp.action == "update": # res_body = [chunk async for chunk in res.body_iterator] # res.body_iterator = iterate_in_threadpool(iter(res_body)) # data_after = json.loads(res_body[0]) # data_after = db.query() # update_data_before, update_data_after = get_update_data(RecordTemp.req_data.data, data_after) add_record(db, item_type_id, action, edit_user, item_id, # before_data=json.dumps(update_data_before), # after_data=json.dumps(update_data_after) ) except Exception as e: print_error(e) finally: db.close() except Exception as e: print_error(e) return res