From d76dd0c4ecd14daf079926aedc66ca715f5e38a3 Mon Sep 17 00:00:00 2001 From: P3ngSaM <61768364+P3ngSaM@users.noreply.github.com> Date: Fri, 10 Jun 2022 17:00:02 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=81=E4=B8=9A=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E9=83=A8=E5=88=86=EF=BC=88Excel=E6=96=B0=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Modules/Company/CompanyImpl.py | 229 +++++++++++++++++++++++++++++-- Modules/Company/CompanyObject.py | 24 ++++ Modules/Company/CompanyRoutes.py | 5 +- Modules/ETL/GuaranteeDataJob.py | 164 +++++++++++++++++++++- Utils/ObjUtil.py | 8 +- 5 files changed, 407 insertions(+), 23 deletions(-) diff --git a/Modules/Company/CompanyImpl.py b/Modules/Company/CompanyImpl.py index c915c2c..656b062 100644 --- a/Modules/Company/CompanyImpl.py +++ b/Modules/Company/CompanyImpl.py @@ -1,15 +1,10 @@ import json -import os -import re import time -import pandas as pd import requests import xlrd -import cpca from DBHelper.MongoHelperInstance import DB_GUA, DB_TYC -from Modules.Company.CompanyObject import BasicBusinessInfo, FinancialData, CustomerInfo, RegionalDistribution, \ - IndustryDistribution, GuaranteedBalanceDistribution, GuaranteeInfo, BankCredit +from Modules.Company.CompanyObject import BasicBusinessInfo, FinancialData, GuaranteeInfo, BankCredit from Modules.Company.CompanyUtils import CompanyUtils, ExcelSheetParser, ExcelParserUtil from Modules.Company.static.province_map import p_map from Modules.ETL.GuaranteeDataJob import RiskInfoDataJob @@ -189,7 +184,7 @@ class BasicBusinessInfoImpl(object): company_info['企业类型'] = basic_info['基本信息']['companyOrgType'] company_info['统一社会信用代码'] = basic_info['基本信息']['creditCode'] company_info['组织代码'] = basic_info['基本信息']['orgNumber'] - tuptime_newest = time.localtime(basic_info['基本信息']['estiblishTime']/1000) + tuptime_newest = time.localtime(basic_info['基本信息']['estiblishTime'] / 1000) company_info['成立日期'] = time.strftime("%Y-%m-%d", tuptime_newest) company_info['经营范围'] = basic_info['基本信息']['businessScope'] company_info['所在省份'] = [v for k, v in p_map.items() if k == basic_info['基本信息']['base']][0] @@ -269,6 +264,214 @@ class BasicBusinessInfoImpl(object): def appendix_script(data): """补充数据表""" + period = [ExcelParserUtil.trans_date_fmt_2(item['报告期'])[:4] for item in appendix_sheet] + year = ExcelParserUtil.trans_date_fmt_2(data['报告期'])[:4] + + def parameter_01(): + """当期代偿金额""" + if compensation_info: + amount = 0 + for item in compensation_info: + if ExcelParserUtil.trans_date_fmt_2(item['代偿日期'])[:4] == year: + amount += item['代偿金额(万元)'] + else: + amount = None + else: + amount = None + return amount + + def parameter_02(): + """当期代偿回收金额""" + if compensation_info: + amount = 0 + for item in compensation_info: + if ExcelParserUtil.trans_date_fmt_2(item['代偿日期'])[:4] == year: + amount += item['代偿回收金额(万元)'] + else: + amount = None + else: + amount = None + return amount + + def parameter_03(): + """当期解除担保责任余额""" + if guarantee_info: + amount = 0 + for item in guarantee_info: + if ExcelParserUtil.trans_date_fmt_2(item['担保开始日期'])[:4] == year: + amount += item['解除金额(万元)'] + else: + amount = None + else: + amount = None + return amount + + def parameter_04(): + """近三年累计代偿金额""" + if year == period[0]: + period_ = period[:3] + elif year == period[1]: + period_ = period[1:4] + else: + period_ = None + + if period_ and compensation_info: + amount = 0 + for _item in compensation_info: + if ExcelParserUtil.trans_date_fmt_2(_item['代偿日期'])[:4] in period_: + amount += _item['代偿金额(万元)'] + else: + amount = None + + return amount + + def parameter_05(): + """近三年累计代偿回收金额""" + if year == period[0]: + period_ = period[:3] + elif year == period[1]: + period_ = period[1:4] + else: + period_ = None + + if period_ and compensation_info: + amount = 0 + for _item in compensation_info: + if ExcelParserUtil.trans_date_fmt_2(_item['代偿日期'])[:4] in period_: + amount += _item['代偿回收金额(万元)'] + else: + amount = None + + return amount + + def parameter_06(): + """近三年累计解除担保责任金额""" + if year == period[0]: + period_ = period[:3] + elif year == period[1]: + period_ = period[1:4] + else: + period_ = None + + if period_ and guarantee_info: + amount = 0 + for _item in guarantee_info: + if ExcelParserUtil.trans_date_fmt_2(_item['担保开始日期'])[:4] in period_: + amount += _item['解除金额(万元)'] + else: + amount = None + + return amount + + def parameter_07(): + """最大单一行业融资担保责任余额""" + if guarantee_info: + industry = {ind['行业']: 0 for ind in guarantee_info} + for item in guarantee_info: + if ExcelParserUtil.trans_date_fmt_2(item['担保开始日期'])[:4] == year: + industry[item] += item['责任担保余额(万元)'] + amount = max(industry.values()) + else: + amount = None + return amount + + def parameter_08(): + """当期新增融资担保责任余额""" + if year == period[0]: + period_ = period[:2] + elif year == period[1]: + period_ = period[1:3] + elif year == period[2]: + period_ = period[2:4] + else: + period_ = None + + if period_ and guarantee_info: + this_year = 0 + last_year = 0 + for _item in guarantee_info: + if ExcelParserUtil.trans_date_fmt_2(_item['担保开始日期'])[:4] == period_[0]: + this_year += _item['责任担保余额(万元)'] + elif ExcelParserUtil.trans_date_fmt_2(_item['担保开始日期'])[:4] == period_[1]: + last_year += _item['责任担保余额(万元)'] + amount = this_year - last_year + else: + amount = None + + return amount + + def parameter_09(): + """融资担保责任余额""" + if guarantee_info: + amount = 0 + for item in guarantee_info: + if ExcelParserUtil.trans_date_fmt_2(item['担保开始日期'])[:4] == year and item['担保类型'] == '融资担保': + amount += item['责任担保余额(万元)'] + else: + amount = None + else: + amount = None + return amount + + def parameter_10(): + """前五大被担保企业融资担保责任余额""" + if guarantee_info: + amount_list = [] + amount = 0 + for item in guarantee_info: + if ExcelParserUtil.trans_date_fmt_2(item['担保开始日期'])[:4] == year and item['担保类型'] == '融资担保': + amount_list.append(item['责任担保余额(万元)']) + else: + amount = None + if amount_list: + five_amount = amount_list.sort(reverse=True)[:5] + amount = sum(five_amount) + else: + amount = 0 + return amount + + def parameter_11(): + """最大被担保企业融资担保责任余额""" + if guarantee_info: + amount_list = [] + amount = 0 + for item in guarantee_info: + if ExcelParserUtil.trans_date_fmt_2(item['担保开始日期'])[:4] == year and item['担保类型'] == '融资担保': + amount_list.append(item['责任担保余额(万元)']) + else: + amount = None + if amount_list: + amount = max(amount_list) + else: + amount = 0 + return amount + + def parameter_12(): + """担保在保余额""" + if guarantee_info: + amount = 0 + for item in guarantee_info: + if ExcelParserUtil.trans_date_fmt_2(item['担保开始日期'])[:4] == year: + amount += item['担保余额(万元)'] + else: + amount = None + else: + amount = None + return amount + + data['当期代偿金额'] = parameter_01() + data['当期代偿回收金额'] = parameter_02() + data['当期解除担保责任余额'] = parameter_03() + data['近三年累计代偿金额'] = parameter_04() + data['近三年累计代偿回收金额'] = parameter_05() + data['近三年累计解除担保责任金额'] = parameter_06() + data['最大单一行业融资担保责任余额'] = parameter_07() + data['当期新增融资担保责任余额'] = parameter_08() + data['融资担保责任余额'] = parameter_09() + data['前五大被担保企业融资担保责任余额'] = parameter_10() + data['最大被担保企业融资担保责任余额'] = parameter_11() + data['担保在保余额'] = parameter_12() + data.pop('报告期') fa = SpecObject.set_specify_instance( instance=financial.AppendixSheet, @@ -333,15 +536,19 @@ class BasicBusinessInfoImpl(object): bank_credit_info_list.append(bc) def risk_info(): - data_job = RiskInfoDataJob() - data_job.company_name = get_attr(company_info, ["企业名称"]) - data_job.drag() + for item in balance_sheet: + data_job = RiskInfoDataJob() + data_job.company_name = get_attr(company_info, ["企业名称"]) + data_job.cid = new_cid + data_job.report_date = ExcelParserUtil.trans_date_fmt_2(item['报告期']) + data_job.drag() def _main_(): basic_data() guarantee_data() financial_data() bank_data() + risk_info() _main_() @@ -371,3 +578,5 @@ class BasicBusinessInfoImpl(object): "银行授信", [item.fields_toggle() for item in bank_credit_info_list] ) + + diff --git a/Modules/Company/CompanyObject.py b/Modules/Company/CompanyObject.py index d49b1c7..174b32d 100644 --- a/Modules/Company/CompanyObject.py +++ b/Modules/Company/CompanyObject.py @@ -421,26 +421,50 @@ class FinancialData(SpecObject): class AppendixSheet(SpecObject): """补充数据表""" + current_compensation_amount = ValidateAttr(field="current_compensation_amount", type=[float, int], default=None) + current_compensation_recycle_amount = ValidateAttr(field="current_compensation_recycle_amount", type=[float, int], default=None) + guarantee_liability = ValidateAttr(field="guarantee_liability", type=[float, int], default=None) + accumulated_compensation_amount = ValidateAttr(field="accumulated_compensation_amount", type=[float, int], default=None) + cumulative_compensation_recovery_amount = ValidateAttr(field="cumulative_compensation_recovery_amount", type=[float, int], default=None) + cumulative_amount = ValidateAttr(field="cumulative_amount", type=[float, int], default=None) + largest_financing = ValidateAttr(field="largest_financing", type=[float, int], default=None) + financing_guarantee_liabilities = ValidateAttr(field="financing_guarantee_liabilities", type=[float, int], default=None) + financial_guarantee_responsibility_balance = ValidateAttr(field="financial_guarantee_responsibility_balance", type=[float, int], default=None) risk_weighted_assets = ValidateAttr(field="risk_weighted_assets", type=[float, int], default=None) guaranteed_risk_reserve_balance = ValidateAttr(field="guaranteed_risk_reserve_balance", type=[float, int], default=None) + top_five_financing_guarantee_liabilities = ValidateAttr(field="top_five_financing_guarantee_liabilities", type=[float, int], default=None) + maximum_enterprise_financing_guarantee = ValidateAttr(field="maximum_enterprise_financing_guarantee", type=[float, int], default=None) guarantee_compensation_reserve = ValidateAttr(field="guarantee_compensation_reserve", type=[float, int], default=None) unexpired_liability_reserve = ValidateAttr(field="unexpired_liability_reserve", type=[float, int], default=None) counter_guarantee_amount = ValidateAttr(field="counter_guarantee_amount", type=[float, int], default=None) deposit_margin = ValidateAttr(field="deposit_margin", type=[float, int], default=None) save_margin = ValidateAttr(field="save_margin", type=[float, int], default=None) guarantee_business_income = ValidateAttr(field="guarantee_business_income", type=[float, int], default=None) + guaranteed_balance = ValidateAttr(field="guaranteed_balance", type=[float, int], default=None) one_assets = ValidateAttr(field="one_assets", type=[float, int], default=None) two_assets = ValidateAttr(field="two_assets", type=[float, int], default=None) three_assets = ValidateAttr(field="three_assets", type=[float, int], default=None) fields_map = { + "current_compensation_amount": "当期代偿金额", + "current_compensation_recycle_amount": "当期代偿回收金额", + "guarantee_liability": "当期解除担保责任余额", + "accumulated_compensation_amount": "近三年累计代偿金额", + "cumulative_compensation_recovery_amount": "近三年累计代偿回收金额", + "cumulative_amount": "近三年累计解除担保责任金额", + "largest_financing": "最大单一行业融资担保责任余额", + "financing_guarantee_liabilities": "当期新增融资担保责任余额", + "financial_guarantee_responsibility_balance": "融资担保责任余额", "risk_weighted_assets": "风险加权资产", "guaranteed_risk_reserve_balance": "担保风险准备金余额", + "top_five_financing_guarantee_liabilities": "前五大被担保企业融资担保责任余额", + "maximum_enterprise_financing_guarantee": "最大被担保企业融资担保责任余额", "guarantee_compensation_reserve": "担保赔偿准备金", "unexpired_liability_reserve": "未到期责任准备金", "counter_guarantee_amount": "反担保金额", "deposit_margin": "存入保证金", "save_margin": "存出保证金", + "guaranteed_balance": "担保在保余额", "guarantee_business_income": "担保业务收入", "one_assets": "I类资产", "two_assets": "II类资产", diff --git a/Modules/Company/CompanyRoutes.py b/Modules/Company/CompanyRoutes.py index 8b1ae20..1e90970 100644 --- a/Modules/Company/CompanyRoutes.py +++ b/Modules/Company/CompanyRoutes.py @@ -1,8 +1,9 @@ from flask import Blueprint, request from Modules.AdminUser.UserAuthUtils import verify_token -from Modules.Company.CompanyImpl import BasicBusinessInfoImpl -from Utils.ErrorUtil import AttrCheckError +from Modules.Company.CompanyImpl import BasicBusinessInfoImpl, CompanyManageImpl +from Utils.ErrorUtil import AttrCheckError, APIReturnError +from Utils.RouteUtil import RouteParamsCheck company_route = Blueprint('company', __name__) diff --git a/Modules/ETL/GuaranteeDataJob.py b/Modules/ETL/GuaranteeDataJob.py index 824bdac..1666e45 100644 --- a/Modules/ETL/GuaranteeDataJob.py +++ b/Modules/ETL/GuaranteeDataJob.py @@ -1,17 +1,167 @@ +import json +import time +from datetime import datetime + +import requests + +from DBHelper.MongoHelperInstance import DB_TYC, DB_GUA from Modules.Company.CompanyObject import RiskInfo from Modules.ETL.WideETL import DataProcess, DataJob +from Utils.ErrorUtil import JustThrowError class DragRiskInfoFromTYC(DataProcess): def extract(self): - pass + url = "http://api.fecribd.com/api/tyc/basic_info" + headers = {'token': self.target.token} + data = {"企业名称": self.target.company_name} + res = requests.post(url=url, headers=headers, data=json.dumps(data)) + + if res.status_code == 200: + source_dict = dict() + + def dishonest_quantity(): + dishonest = DB_TYC.find_single_data( + "司法风险", + "失信人", + {"企业名称": self.target.company_name}, + ['失信人'] + ) + total = 0 + if dishonest['失信人']: + for item in dishonest['失信人']: + begin_date = datetime.strptime(item['publishdate'], '%Y-%m-%d').year + curr_date = datetime.strptime(self.target.report_date, '%Y-%m-%d').year + minus_date = curr_date - begin_date + if 0 <= minus_date <= 3: + total += 1 + return total + + def executed_quantity(): + executed = DB_TYC.find_single_data( + "司法风险", + "被执行人", + {"企业名称": self.target.company_name}, + ['被执行人'] + ) + total = 0 + if executed['被执行人']: + for item in executed['被执行人']: + begin_date = datetime.strptime(item['caseCreateTime'], '%Y-%m-%d').year + curr_date = datetime.strptime(self.target.report_date, '%Y-%m-%d').year + minus_date = curr_date - begin_date + if 0 <= minus_date <= 3: + total += 1 + return total + + def final_case_quantity(): + final_case = DB_TYC.find_single_data( + "司法风险", + "终本案件", + {"企业名称": self.target.company_name}, + ['终本案件'] + ) + total = 0 + if final_case['终本案件']: + for item in final_case['终本案件']['result']: + tuptime_newest = time.localtime(item['caseFinalTime']/1000) + begin_date = datetime.strptime(time.strftime("%Y-%m-%d", tuptime_newest), '%Y-%m-%d').year + curr_date = datetime.strptime(self.target.report_date, '%Y-%m-%d').year + minus_date = curr_date - begin_date + if 0 <= minus_date <= 3: + total += 1 + return total + + def consumption_restriction_quantity(): + executed = DB_TYC.find_single_data( + "司法风险", + "限制消费令", + {"企业名称": self.target.company_name}, + ['限制消费令'] + ) + total = 0 + if executed['限制消费令']: + for item in executed['限制消费令']['result']: + tuptime_newest = time.localtime(item['publishDate'] / 1000) + begin_date = datetime.strptime(time.strftime("%Y-%m-%d", tuptime_newest), '%Y-%m-%d').year + curr_date = datetime.strptime(self.target.report_date, '%Y-%m-%d').year + minus_date = curr_date - begin_date + if 0 <= minus_date <= 3: + total += 1 + return total + + def penalties_quantity(): + penalties = DB_TYC.find_single_data( + "经营风险", + "行政处罚", + {"企业名称": self.target.company_name}, + ['行政处罚'] + ) + total = 0 + if penalties['行政处罚']: + for item in penalties['行政处罚']['result']: + begin_date = datetime.strptime(item['decisionDate'], '%Y-%m-%d').year + curr_date = datetime.strptime(self.target.report_date, '%Y-%m-%d').year + minus_date = curr_date - begin_date + if 0 <= minus_date <= 3: + total += 1 + return total + + def legal_action_quantity(): + legal_action = DB_TYC.find_single_data( + "司法风险", + "诉讼", + {"企业名称": self.target.company_name}, + ['诉讼'] + ) + total = 0 + if legal_action: + for item in legal_action['诉讼']: + try: + begin_date = datetime.strptime(item['submittime'], '%Y-%m-%d').year + curr_date = datetime.strptime(self.target.report_date, '%Y-%m-%d').year + minus_date = curr_date - begin_date + condition = self.target.company_name in item['defendants'] + if 0 <= minus_date <= 3 and condition: + total += 1 + except ValueError: + continue + return total + + source_dict['失信被执行人'] = dishonest_quantity() + source_dict['被执行人'] = executed_quantity() + source_dict['终本案件'] = final_case_quantity() + source_dict['限制消费令'] = consumption_restriction_quantity() + source_dict['行政处罚'] = penalties_quantity() + source_dict['法律诉讼(被告)'] = legal_action_quantity() + + self.source = source_dict + else: + raise JustThrowError(error_info="拉取风险信息失败") def transform(self): - pass + source = self.source + target = self.target + + target.dishonest_executor = source['失信被执行人'] + target.person_to_be_executed = source['被执行人'] + target.final_case = source['终本案件'] + target.consumption_restriction_order = source['限制消费令'] + target.administrative_penalties = source['行政处罚'] + target.legal_action = source['法律诉讼(被告)'] def load(self): - pass + target = self.target + + data = target.fields_toggle() + + DB_GUA.upsert_single_data( + '企业数据', + '风险信息', + {"企业名称": data['企业名称'], "报告期": data['报告期']}, + data + ) class RiskInfoDataJob(RiskInfo): @@ -21,10 +171,10 @@ class RiskInfoDataJob(RiskInfo): data_job = DataJob( instance=self, - title="", - job_type="", - status="", - detail="" + title="拉取企业风险信息", + job_type="处理准备", + status="正常", + detail="企业名称->{};".format(self.company_name) ) data_job.processes = [ diff --git a/Utils/ObjUtil.py b/Utils/ObjUtil.py index 24c6353..eeb1a01 100644 --- a/Utils/ObjUtil.py +++ b/Utils/ObjUtil.py @@ -7,12 +7,9 @@ class SpecObject(object): def fields_toggle(self, **kwargs): """字典键值切换""" + _dict_ = dict() default_types = ['str', 'int', 'float', 'dict', 'bool', 'tuple'] - if 'fields' in kwargs: - self.__dict__ = {key: self.__dict__[key] for key in kwargs['fields']} - - _dict_ = dict() for key in self.__dict__.keys(): if key in self.fields_map.keys(): @@ -37,6 +34,9 @@ class SpecObject(object): else: _dict_[self.fields_map[key]] = self.__dict__[key].fields_toggle() + if 'fields' in kwargs: + _dict_ = {key: _dict_[key] for key in kwargs['fields']} + return _dict_ def set_instance(self, **kwargs):