From 9f0bd4e46383e95cd442cf9c4f34972aca05ad3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=9D=E5=B7=9D?= Date: Mon, 11 Apr 2022 04:57:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=A5=E5=95=86=E4=BF=A1=E6=81=AF=20?= =?UTF-8?q?=E8=82=A1=E4=B8=9C=E4=BF=A1=E6=81=AF=20=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=B8=85=E6=B4=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CompanyData/CompanyDataImpl.py | 107 +++++++++++++++++++++++++ CompanyData/CompanyDataObj.py | 139 +++++++++++++++++++++++++++------ DBHelper/DBConfig.json | 3 +- DBHelper/MongoHelper.py | 7 +- Utils/ErrorUtil.py | 17 +++- Utils/ObjUtil.py | 2 + 6 files changed, 247 insertions(+), 28 deletions(-) diff --git a/CompanyData/CompanyDataImpl.py b/CompanyData/CompanyDataImpl.py index e69de29..df7d7eb 100644 --- a/CompanyData/CompanyDataImpl.py +++ b/CompanyData/CompanyDataImpl.py @@ -0,0 +1,107 @@ +import json +import requests + +from CompanyData.CompanyDataObj import CompanyData, BasicInfo, ShareHolder +from DBHelper.MongoHelper import MongoHelper +from Utils.ErrorUtil import LogConditionCheckFailed + + +class CompanyDataImpl(CompanyData): + """企业数据实现类""" + + db_tyc = MongoHelper("tyc") + db_tfse = MongoHelper("tfse_v0.21") + + def drag_data_from_tyc_api(self): + """拉取天眼查接口数据""" + + url = "http://api.fecribd.com/api/tyc/drag_data" + headers = {'token': "uzdq51N4!I0%HY4sCaQ!aeCSIDIVIdAM"} + data = {"企业名称": self.name} + res = requests.post(url=url, headers=headers, data=json.dumps(data)) + return True if res.status_code == 200 else False + + def prepare_company_init_data(self): + """""" + + def prepare_business_data(): + """""" + data = self.db_tyc.find_single_column( + "公司背景", + "基本信息", + {"企业名称": self.name}, + "基本信息" + ) + + basic_info = BasicInfo() + basic_info.status = data['regStatus'] + basic_info.legal_person = data['legalPersonName'] + basic_info.company_type = data['companyOrgType'] + basic_info.taxpayer_id = data['taxNumber'] + basic_info.business_scope = data['businessScope'] + basic_info.registered_capital = data['regCapital'] + basic_info.paid_capital = data['actualCapital'] + basic_info.registered_address = data['regLocation'] + basic_info.registration_authority = data['regInstitute'] + basic_info.industry = data['industry'] + basic_info.staff_size = data['staffNumRange'] + basic_info.people_insured_num = data['socialStaffNum'] + basic_info.micro_company = "是" if data['regStatus'] == 1 else "否" + + self.basic_info = basic_info + + self.db_tfse.upsert_single_data( + "企业数据", + "企业数据_更新汇总", + {"企业ID": self.cid}, + self.dict_to_save() + ) + + def prepare_share_holders(): + """""" + + self.share_holder = list() + + data = self.db_tyc.find_single_column( + "公司背景", + "企业股东", + {"企业名称": self.name}, + "企业股东" + ) + + try: + results = data['result'] + except KeyError: + raise LogConditionCheckFailed("企业股东数据异常", 200) + + for result in results: + share_holder = ShareHolder() + share_holder.name = result['name'] + share_holder.share_holder_type = "公司" if result['type'] == 1 else ("个人" if result['type'] == 2 else "其他") + + if result['capital']: + share_holder.share_holding_ratio = None if result['capital'][0]['percent'] == '' else result['capital'][0]['percent'] + share_holder.subscription_amount = None if result['capital'][0]['amomon'] == '' else result['capital'][0]['amomon'] + share_holder.subscription_date = None if result['capital'][0]['time'] == '' else result['capital'][0]['time'] + + if result['capitalActl']: + share_holder.paid_amount = [capital_actl['amomon'] for capital_actl in result['capitalActl']] + share_holder.payment_method = [capital_actl['paymet'] for capital_actl in result['capitalActl']] + share_holder.payment_time = [capital_actl['time'] for capital_actl in result['capitalActl']] + + self.share_holder.append(share_holder) + + def __main__(): + # prepare_business_data() + prepare_share_holders() + + __main__() + + +if __name__ == '__main__': + impl = CompanyDataImpl() + impl.cid = "qqqqqqqq" + impl.name = "远光软件股份有限公司" + impl.prepare_company_init_data() + + print(impl.dict_to_save()) \ No newline at end of file diff --git a/CompanyData/CompanyDataObj.py b/CompanyData/CompanyDataObj.py index 9fd03f2..c0d3d85 100644 --- a/CompanyData/CompanyDataObj.py +++ b/CompanyData/CompanyDataObj.py @@ -41,12 +41,12 @@ class ShareHolder(SpecObject): name = ValidateAttr(field='name', type=str) share_holder_type = ValidateAttr(field='share_holder_type', type=str) - share_holding_ratio = ValidateAttr(field="share_holding_ratio", type=str) - subscription_amount = ValidateAttr(field="subscription_amount", type=str) - subscription_date = ValidateAttr(field="subscription_date", type=str) - paid_amount = ValidateAttr(field="paid_amount", type=list) - payment_method = ValidateAttr(field="payment_method", type=list) - payment_time = ValidateAttr(field="payment_time", type=list) + share_holding_ratio = ValidateAttr(field="share_holding_ratio", type=str, default=None) + subscription_amount = ValidateAttr(field="subscription_amount", type=str, default=None) + subscription_date = ValidateAttr(field="subscription_date", type=str, default=None) + paid_amount = ValidateAttr(field="paid_amount", type=list, default=[]) + payment_method = ValidateAttr(field="payment_method", type=list, default=[]) + payment_time = ValidateAttr(field="payment_time", type=list, default=[]) fields_map = { "name": "股东", @@ -60,6 +60,102 @@ class ShareHolder(SpecObject): } +class MainMembers(SpecObject): + """主要成员""" + + name = ValidateAttr(field="name", type=str) + job_title = ValidateAttr(field="job_title", type=list) + + fields_map = { + "name": "姓名", + "job_title": "职务" + } + + +class BalanceSheet(SpecObject): + """资产负债表""" + accounts_receivable = ValidateAttr(field='accounts_receivable', type=float) + stock = ValidateAttr(field='stock', type=float) + total_current_assets = ValidateAttr(field='total_current_assets', type=float) + total_assets = ValidateAttr(field='total_assets', type=float) + short_loan = ValidateAttr(field='short_loan', type=float) + one_year_liabilities = ValidateAttr(field='one_year_liabilities', type=float) + total_current_liabilities = ValidateAttr(field='total_current_liabilities', type=float) + long_term_loan = ValidateAttr(field='long_term_loan', type=float) + total_liabilities = ValidateAttr(field='total_liabilities', type=float) + total_owners_equity = ValidateAttr(field='total_owners_equity', type=float) + + fields_map = { + 'accounts_receivable': '应收账款', + 'stock': '存货', + 'total_current_assets': '流动资产合计', + 'total_assets': '资产总计', + 'short_loan': '短期借款', + 'one_year_liabilities': '一年内到期非流动负债', + 'total_current_liabilities': '流动负债合计', + 'long_term_loan': '长期借款', + 'total_liabilities': '负债合计', + 'total_owners_equity': '所有者权益合计' + } + + +class ProfitSheet(SpecObject): + """利润表""" + operating_income = ValidateAttr(field='operating_income', type=float) + operating_cost = ValidateAttr(field='operating_cost', type=float) + total_profit = ValidateAttr(field='total_profit', type=float) + net_profit = ValidateAttr(field='net_profit', type=float) + + fields_map = { + 'operating_income': '营业收入', + 'operating_cost': '营业成本', + 'total_profit': '利润总额', + 'net_profit': '净利润' + } + + +class AppendixDataSheet(SpecObject): + """补充数据表""" + rd_expenses = ValidateAttr(field='rd_expenses', type=float) + interest_disbursement = ValidateAttr(field='interest_disbursement', type=float) + interest_expense = ValidateAttr(field='interest_expense', type=float) + + fields_map = { + 'rd_expenses': '研发费用异常', + 'interest_disbursement': '计入财务费的利息支出异常', + 'interest_expense': '资本化利息支出异常' + } + + +class FinancialIndex(SpecObject): + """财务指标""" + roe = ValidateAttr(field="roe", type=float) + inventory_turnover = ValidateAttr(field="inventory_turnover", type=float) + interest_multiple = ValidateAttr(field="interest_multiple", type=float) + accounts_receivable_turnover = ValidateAttr(field="accounts_receivable_turnover", type=float) + total_asset_turnover = ValidateAttr(field="total_asset_turnover", type=float) + total_asset_growth_rate = ValidateAttr(field="total_asset_growth_rate", type=float) + roa = ValidateAttr(field="roa", type=float) + technology_investment_ratio = ValidateAttr(field="technology_investment_ratio", type=float) + operating_growth_rate = ValidateAttr(field="operating_growth_rate", type=float) + assets_and_liabilities = ValidateAttr(field="assets_and_liabilities", type=float) + quick_ratio = ValidateAttr(field="quick_ratio", type=float) + + fields_map = { + "roe": "净资产收益率", + "inventory_turnover": "存货周转率", + "interest_multiple": "已获利息倍数", + "accounts_receivable_turnover": "应收账款周转率", + "total_asset_turnover": "总资产周转率", + "total_asset_growth_rate": "总资产增长率", + "roa": "总资产报酬率", + "technology_investment_ratio": "技术投入比率", + "operating_growth_rate": "营业增长率", + "assets_and_liabilities": "资产负债率", + "quick_ratio": "速动比率" + } + + class CompanyData(SpecObject): """企业数据""" @@ -69,15 +165,15 @@ class CompanyData(SpecObject): industry_l2 = ValidateAttr(field='industry_l2', type=str) basic_info = ValidateAttr(field='basic_info', type=BasicInfo) share_holder = ValidateAttr(field='share_holders', instance_list=ShareHolder) - main_members = [] - balance_sheet = {} - profit_sheet = {} - income_sheet = {} - appendix_sheet = {} - fin_index = {} - cc_rating_result = {} - esg_rating_result = {} - update_time = {} + main_members = ValidateAttr(field="main_members", type=MainMembers) + balance_sheet = ValidateAttr(field='balance_sheet', type=BalanceSheet) + profit_sheet = ValidateAttr(field='profit_sheet', type=ProfitSheet) + # income_sheet = {} + appendix_sheet = ValidateAttr(field='appendix_sheet', type=AppendixDataSheet) + fin_index = ValidateAttr(field='fin_index', type=FinancialIndex) + cc_rating_result = ValidateAttr(field='cc_rating_result', type=dict) + esg_rating_result = ValidateAttr(field='esg_rating_result', type=dict) + update_time = ValidateAttr(field='update_time', type=dict) fields_map = { "cid": "企业ID", @@ -97,13 +193,8 @@ class CompanyData(SpecObject): "update_time": "更新时间" } + def drag_data_from_tyc_api(self): + """拉取天眼查接口数据""" -if __name__ == '__main__': - company_data = CompanyData() - basic_info = BasicInfo() - share_holder = ShareHolder() - basic_info.status = "存续" - share_holder.name = '123' - company_data.basic_info = basic_info - company_data.share_holders = [share_holder, share_holder] - print(company_data.dict_to_save()) + def prepare_company_init_data(self): + """准备企业初始化数据""" diff --git a/DBHelper/DBConfig.json b/DBHelper/DBConfig.json index 67c7ddf..0e3d1de 100644 --- a/DBHelper/DBConfig.json +++ b/DBHelper/DBConfig.json @@ -1,6 +1,7 @@ { "MongoDB": { - "tfse_v0.21": "root:UTlC9cCoglD1cI1*@116.63.130.34:27021" + "tfse_v0.21": "root:UTlC9cCoglD1cI1*@116.63.130.34:27021", + "tyc": "root:gP@DwMSVd5Sh6EiH@116.63.130.34:27019" }, "Mysql": { diff --git a/DBHelper/MongoHelper.py b/DBHelper/MongoHelper.py index b1a94b3..dc25953 100644 --- a/DBHelper/MongoHelper.py +++ b/DBHelper/MongoHelper.py @@ -1,8 +1,11 @@ +import re import os import json import gridfs import pymongo + +from urllib import parse from bson import ObjectId from gridfs import GridFS @@ -18,7 +21,9 @@ class MongoHelper: with open(os.path.abspath(os.path.dirname(__file__) + '/DBConfig.json')) as f: db_configs = json.load(f) this_mongo_cfg = db_configs['MongoDB'][param] - self.client = pymongo.MongoClient('mongodb://{}'.format(this_mongo_cfg)) + m = re.match('([\s\S].*?):([\s\S].*)@([\s\S].*)', this_mongo_cfg) + parsed_mongo_config = "{}:{}@{}".format(parse.quote_plus(m.group(1)), parse.quote_plus(m.group(2)), m.group(3)) + self.client = pymongo.MongoClient('mongodb://{}'.format(parsed_mongo_config)) def find_single_column(self, param1, param2, param3, param4): """ diff --git a/Utils/ErrorUtil.py b/Utils/ErrorUtil.py index a67089b..575f26b 100644 --- a/Utils/ErrorUtil.py +++ b/Utils/ErrorUtil.py @@ -6,8 +6,8 @@ from flask import request from DBHelper.MongoHelper import MongoHelper -class ReturnConditionCheckFailed(RuntimeError): - """条件检查失败 抛出异常 接口返回失败原因和状态码""" +class CheckFailed(RuntimeError): + """检查异常""" def __init__(self, failed_info, status_code): self.failed_info = failed_info # 失败信息 @@ -39,3 +39,16 @@ class ReturnConditionCheckFailed(RuntimeError): "异常日志", info ) + + +class LogConditionCheckFailed(CheckFailed): + """直接记录检查异常""" + + def __init__(self, failed_info, status_code): + self.failed_info = failed_info # 失败信息 + self.status_code = status_code # 状态码 + self.log_error() + + +class ReturnConditionCheckFailed(CheckFailed): + """条件检查失败 抛出异常 接口返回失败原因和状态码""" diff --git a/Utils/ObjUtil.py b/Utils/ObjUtil.py index 8e9ad12..a31e258 100644 --- a/Utils/ObjUtil.py +++ b/Utils/ObjUtil.py @@ -22,6 +22,8 @@ class SpecObject(object): _dict_[self.fields_map[key]] = self.__dict__[key] else: _dict_[self.fields_map[key]] = [item.dict_to_save() for item in self.__dict__[key]] + elif self.__dict__[key] is None: + _dict_[self.fields_map[key]] = self.__dict__[key] else: _dict_[self.fields_map[key]] = self.__dict__[key].dict_to_save()