guarantee-admin-api-v0.2/Modules/ETL/GuaranteeDataJob.py

185 lines
7.4 KiB
Python
Raw Normal View History

import json
import time
from datetime import datetime
import requests
from DBHelper.MongoHelperInstance import DB_TYC, DB_GUA
2022-06-09 11:22:56 +08:00
from Modules.Company.CompanyObject import RiskInfo
from Modules.ETL.WideETL import DataProcess, DataJob
from Utils.ErrorUtil import JustThrowError
2022-06-09 11:22:56 +08:00
class DragRiskInfoFromTYC(DataProcess):
def extract(self):
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="拉取风险信息失败")
2022-06-09 11:22:56 +08:00
def transform(self):
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['法律诉讼(被告)']
2022-06-09 11:22:56 +08:00
def load(self):
target = self.target
data = target.fields_toggle()
DB_GUA.upsert_single_data(
'企业数据',
'风险信息',
{"企业名称": data['企业名称'], "报告期": data['报告期']},
data
)
2022-06-09 11:22:56 +08:00
class RiskInfoDataJob(RiskInfo):
def drag(self):
"""拉去天眼查数据接口的企业风险信息"""
data_job = DataJob(
instance=self,
title="拉取企业风险信息",
job_type="处理准备",
status="正常",
detail="企业名称->{};".format(self.company_name)
2022-06-09 11:22:56 +08:00
)
data_job.processes = [
DragRiskInfoFromTYC
]
data_job.start()