tfse-admin-api-v0.2/Manage/Ratings/Implements/SearchRatingApiImpl.py

611 lines
25 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
import requests
from DBHelper.MongoHelper import MongoHelper
from Manage.Ratings.Objects.CreditObject import CreditRatingResult, OperatingScore, FinanceScore, RiskScore
from Manage.Ratings.Objects.EsgObject import EsgRatingResult, SocialScore, EnvironmentalScore, GovernanceScore
from Manage.Ratings.Objects.RatingSearch import SearchRatingApi, SearchRatingResult
RATING = ["企业ID", "评价ID", "企业名称", "评价项目", "评价方式", "进行状态", "评价结果", "评价完成日期", "报告fid", "证书fid"]
class SearchRatingApiImpl(SearchRatingApi):
"""评价查询请求接口"""
db = MongoHelper("tfse_v0.21")
def conditions_search(self):
"""根据条件查询"""
search_body = self.search.make_search_body()
sort = self.sort.make_sort_body()
page_size = 10 if self.page_size > 10 else self.page_size
page_no = self.page_no
total = self.db.find_all_data_with_count(
"企业数据",
"评价记录",
search_body
)
records = self.db.find_data_by_page_with_sort(
"企业数据",
"评价记录",
search_body,
RATING,
sort,
page_size,
page_no
)
table_data = list()
if records:
for record in records:
company = SearchRatingResult()
company.cid = record['企业ID']
company.rid = record['评价ID']
company.name = record['企业名称']
company.project = record['评价项目']
company.method = record['评价方式']
company.status = record['进行状态']
company.result = record['评价结果']
company.completion_date = record['评价完成日期']
company.report_fid = record['报告fid']
company.certificate_fid = record['证书fid']
data = company.dict_to_show(columns=RATING)
table_data.append(data)
result = {
"records": table_data,
"total": total
}
return result
def rating_type(self):
"""评价类型"""
search_body = self.dict_to_show()['搜索体']
records = self.db.find_single_data(
'企业数据',
'评价记录',
search_body,
['评价项目']
)
return records
def result_search(self):
"""评价结果查询"""
search_body = self.dict_to_show()['搜索体']
records = self.rating_type()
def credit_result():
"""综信评价结果"""
result = self.db.find_single_data(
'综信评价数据',
'评价结果',
search_body,
['企业ID', '评价ID', '企业名称', '评价年度', '评价方式', '评价时间', '信用等级', '信用评分', '经营评分', '财务评分', '风险评分']
)
rating_result = CreditRatingResult()
rating_result.cid = result['企业ID']
rating_result.rid = result['评价ID']
rating_result.name = result['企业名称']
rating_result.year = result['评价年度']
rating_result.score = result['信用评分']
rating_result.level = result['信用等级']
rating_result.method = result['评价方式']
rating_result.rating_date = result['评价时间']
def operating():
"""经营评分"""
operate = OperatingScore()
operate.environment = result['经营评分']['环境']
operate.social = result['经营评分']['社会责任']
operate.governance = result['经营评分']['公司治理']
operate.total = result['经营评分']['合计']
rating_result.operating_score = operate
def finance():
"""财务评分"""
finance_score = FinanceScore()
def profit_about():
"""盈利能力"""
profit = finance_score.Profitability()
profit.roa = result['财务评分']['盈利能力']['净资产收益率']
profit.roe = result['财务评分']['盈利能力']['总资产报酬率']
profit.total = result['财务评分']['盈利能力']['合计']
return profit
def aseet_about():
"""资产质量"""
asset = finance_score.AssetQuality()
asset.total_asset_turnover = result['财务评分']['资产质量']['总资产周转率']
asset.inventory_turnover = result['财务评分']['资产质量']['存货周转率']
asset.accounts_receivable_turnover = result['财务评分']['资产质量']['应收账款周转率']
asset.total = result['财务评分']['资产质量']['合计']
return asset
def debt_about():
"""债务风险"""
debt = finance_score.DebtRisk()
debt.assets_liabilities = result['财务评分']['债务风险']['资产负债率']
debt.earned_interest_multiple = result['财务评分']['债务风险']['已获利息倍数']
debt.quick_ratio = result['财务评分']['债务风险']['速动比率']
debt.total = result['财务评分']['债务风险']['合计']
return debt
def business_about():
"""经营增长"""
business = finance_score.BusinessGrowth()
business.operating_growth_rate = result['财务评分']['经营增长']['营业增长率']
business.total_asset_growth_rate = result['财务评分']['经营增长']['总资产增长率']
business.technology_investment_ratio = result['财务评分']['经营增长']['技术投入比率']
business.total = result['财务评分']['经营增长']['合计']
return business
finance_score.profitability = profit_about()
finance_score.asset_quality = aseet_about()
finance_score.debt_risk = debt_about()
finance_score.business_growth = business_about()
finance_score.total = result['财务评分']['合计']
rating_result.finance_score = finance_score
def risk():
"""风险评分"""
risk_score = RiskScore()
risk_score.total = 0
def management():
"""经营风险"""
business = risk_score.BusinessRisk()
business.case_filing_information = result['风险评分']['经营风险']['立案信息']
business.court_announcement = result['风险评分']['经营风险']['开庭公告']
business.executed = result['风险评分']['经营风险']['被执行人']
business.court_notice = result['风险评分']['经营风险']['法院公告']
business.litigation = result['风险评分']['经营风险']['诉讼']
business.service_announcement = result['风险评分']['经营风险']['送达公告']
business.equity_pledge = result['风险评分']['经营风险']['股权出质']
business.legal_entity_change = result['风险评分']['经营风险']['法人变更']
business.members_change = result['风险评分']['经营风险']['主要成员变更']
business.total = result['风险评分']['经营风险']['合计']
risk_score.total += result['风险评分']['经营风险']['合计']
return business
def peripheral():
"""关联风险"""
associated = risk_score.AssociatedRisk()
peripheral_ = associated.PeripheralRisk()
peripheral_.high_risk = result['风险评分']['关联风险']['周边风险']['高风险']
peripheral_.warning = result['风险评分']['关联风险']['周边风险']['警示']
peripheral_.total = result['风险评分']['关联风险']['周边风险']['合计']
associated.peripheral_risk = peripheral_
associated.total = result['风险评分']['关联风险']['合计']
risk_score.total += result['风险评分']['关联风险']['合计']
return associated
risk_score.compliance_risk = result['风险评分']['合规风险']
risk_score.business_risk = management()
risk_score.associated_risk = peripheral()
risk_score.total += risk_score.compliance_risk
self.risk_score = risk_score
def __main__():
operating()
finance()
risk()
data = rating_result.dict_to_show()
return data
return __main__()
def esg_result():
"""esg评价结果"""
result = self.db.find_single_data(
'ESG评价数据',
'评价结果',
search_body,
['企业ID', '评价ID', '企业名称', '评价年度', '评价时间', '评价等级', 'ESG得分', '环境得分', '社会得分', '公司治理得分']
)
rating_result = EsgRatingResult()
rating_result.cid = result['企业ID']
rating_result.rid = result['评价ID']
rating_result.name = result['企业名称']
rating_result.year = result['评价年度']
rating_result.esg_score = result['ESG得分']
rating_result.level = result['评价等级']
rating_result.rating_date = result['评价时间']
def environment():
"""环境得分"""
score = EnvironmentalScore()
score.carbon_dioxide = result['环境得分']['单位收入二氧化碳排放']
score.energy_consumption = result['环境得分']['单位收入的能耗']
score.water_consumption = result['环境得分']['单位收入的耗水']
score.income_ratio = result['环境得分']['绿色业务收入占比(%']
score.greenhouse_gas = result['环境得分']['公司是否有温室气体减排目标']
score.energy_saving = result['环境得分']['企业是否有节能目标']
score.water_saving = result['环境得分']['企业是否有节约用水目标']
score.green_business = result['环境得分']['是否有绿色业务']
score.punish = result['环境得分']['近三年是否被环境或水务等监管部门处罚']
score.negative_effects = result['环境得分']['国家双碳目标对企业业务是否有不利影响']
score.beneficial_influence = result['环境得分']['国家双碳目标对企业业务是否有有利影响']
score.clean_energy = result['环境得分']['企业是否使用风电、光电、水电等清洁能源,是否使用清洁交通工具']
score.total = result['环境得分']['合计']
rating_result.environmental_score = score
def social():
"""社会得分"""
score = SocialScore()
score.resign = result['社会得分']['离职人数占比']
score.salary_increase = result['社会得分']['人均薪酬涨幅']
score.work_length = result['社会得分']['劳动合同中的工作时长(周)']
score.labor_disputes = result['社会得分']['劳动纠纷']
score.security_incident = result['社会得分']['安全事故']
score.provide_training = result['社会得分']['提供培训']
score.social_security = result['社会得分']['社保缴纳是否符合当地标准']
score.provident_fund = result['社会得分']['公积金缴纳是否符合当地标准']
score.physical_examination = result['社会得分']['是否提供员工体检']
score.paid_leave = result['社会得分']['是否提供带薪假期']
score.supplier = result['社会得分']['公司从前三大供货商拿货占比']
score.customer_sales = result['社会得分']['公司前3大客户销量占比']
score.repair_ratio = result['社会得分']['公司返修、退回、投诉产品比例(%']
score.poverty_alleviation = result['社会得分']['扶贫+捐赠规模(万元)']
score.judicial_risk = result['社会得分']['司法风险']
score.total = result['社会得分']['合计']
rating_result.social_score = score
def governance():
"""治理得分"""
score = GovernanceScore()
score.enterprise_nature = result['公司治理得分']['企业性质']
score.directors_board = result['公司治理得分']['公司是否设有董事会']
score.supervisory_board = result['公司治理得分']['公司是否设有监事会']
score.experience_year = result['公司治理得分']['董监高平均拥有的行业经验年数']
score.turnover_rate = result['公司治理得分']['董监高近三年离职率']
score.information_disclosure = result['公司治理得分']['公司近三年信息披露及时、可靠、完备、审计质量']
score.meetings_number = result['公司治理得分']['公司董事会近三年年均开会次数']
score.roe = result['公司治理得分']['净资产收益率']
score.audit_report = result['公司治理得分']['公司是否有审计报告']
score.total = result['公司治理得分']['合计']
rating_result.governance_score = score
def __main__():
environment()
social()
governance()
data = rating_result.dict_to_show()
return data
return __main__()
if records['评价项目'] == '综合信用评价':
res = credit_result()
else:
res = esg_result()
return res
def process_search(self):
"""评价流程"""
search_body = self.dict_to_show()['搜索体']
records = self.rating_type()
def credit_process():
"""综信评价流程"""
process = self.db.find_single_data(
'综信评价数据',
'评价流程',
search_body,
['评价流程']
)
return process
def esg_process():
"""esg评价流程"""
process = self.db.find_single_data(
'ESG评价数据',
'评价流程',
search_body,
['评价流程']
)
return process
if records['评价项目'] == '综合信用评价':
res = credit_process()
else:
res = esg_process()
return res
def detail_search(self):
"""填报数据"""
search_body = self.dict_to_show()['搜索体']
records = self.rating_type()
def credit_detail():
"""综信填报数据"""
detail = self.db.find_single_data(
'综信评价数据',
'填报数据',
search_body,
['企业ID', '企业名称', '评价年度', '行业选择', '经营问卷', '财务填报']
)
return detail
def esg_detail():
"""esg填报数据"""
detail = self.db.find_single_data(
'ESG评价数据',
'填报数据',
search_body,
['企业ID', '企业名称', '评价年度', '所属行业', '环境问卷', '社会问卷', '治理问卷']
)
return detail
if records['评价项目'] == '综合信用评价':
res = credit_detail()
else:
res = esg_detail()
return res
def risk_search(self):
"""风险数据"""
search_body = self.dict_to_show()['搜索体']
records = self.rating_type()
def credit_risk():
"""综信风险数据"""
detail = self.db.find_single_data(
'综信评价数据',
'风险数据',
search_body,
['企业ID', '关联风险', '合规风险', '经营风险']
)
return detail
def esg_risk():
"""esg风险数据"""
detail = self.db.find_single_data(
'ESG评价数据',
'风险数据',
search_body,
['企业ID', '更新日期', '严重违法', '失信人', '法律诉讼', '税收违法', '经营异常', '行政处罚', '被执行人']
)
return detail
if records['评价项目'] == '综合信用评价':
res = credit_risk()
else:
res = esg_risk()
return res
def financial_search(self):
"""财指结果"""
search_body = self.dict_to_show()['搜索体']
financial = self.db.find_all_data(
'综信评价数据',
'财指结果',
search_body,
['企业ID', '年报期', '企业名称', '净资产收益率', '存货周转率', '已获利息倍数', '应收账款周转率', '总资产周转率',
'总资产增长率', '总资产报酬率', '技术投入比率', '营业增长率', '资产负债率', '速动比率']
)
return financial
def report_search(self):
"""报告数据"""
search_body = self.dict_to_show()['搜索体']
records = self.rating_type()
def credit_report():
"""综信风险数据"""
report_data = self.db.find_single_data(
'综信评价数据',
'报告数据',
search_body,
['企业ID', '企业名称', '评价年度', '行业选择', '生成日期', '报告内容']
)
return report_data
def esg_report():
"""esg风险数据"""
report_data = self.db.find_single_data(
'ESG评价数据',
'报告数据',
search_body,
['企业ID', '企业名称', '评价年度', '生成日期', '报告内容']
)
return report_data
if records['评价项目'] == '综合信用评价':
res = credit_report()
else:
res = esg_report()
return res
def report_revise(self, text_model):
"""报告修改"""
search_body = self.dict_to_show()['搜索体']
records = self.rating_type()
def revise_credit_report():
"""综信报告修改"""
def edit():
"""更新报告数据"""
insert_data = dict()
insert_data['报告内容'] = text_model
self.db.update_single_data(
'综信评价数据',
'报告数据',
search_body,
insert_data
)
return True
def delete():
"""删除报告相关数据"""
record = self.db.find_single_data(
'企业数据',
'评价记录',
search_body,
['报告fid']
)
if not record:
return False
else:
fid = record['报告fid']
# self.db.delete_file(
# '文件',
# '综信报告',
# fid
# )
self.db.update_single_data(
'企业数据',
'评价记录',
search_body,
{'报告fid': None}
)
return True
def create():
"""生成报告"""
report_data = self.db.find_single_data(
'综信评价数据',
'报告数据',
search_body,
['评价ID', '企业ID', '企业名称', '报告内容', '生成日期', '行业选择', '评价年度']
)
report_text = dict()
report_text['text_model'] = report_data
def generate_report():
"""
调用报告生成接口
Parameters:
-
Returns:
result: type 生成结果
"""
url = 'http://api.fecribd.com/tfse_rating/report/generate_pdf_report'
token = 'X0gSlC!YE8jmr2jJr&ilcjS83j!tsoh5'
headers = {'token': token, 'Content-Type': 'application/json;charset=UTF-8'}
res_ = requests.post(url, headers=headers, data=json.dumps(report_text))
return res_
r = generate_report()
result = r.json()
if result['info'] == '生成报告成功':
self.db.update_single_data('企业数据', '评价记录', search_body, {"报告fid": result['result']['FileID']})
result['result']['FileID'] = '/file/get_company_report?file_id={}'.format(
result['result']['FileID'])
self.db.update_single_data('企业数据', '综合信用分析', search_body, {"信用分析.查看报告": result['result']['FileID']})
return result
step_1 = edit()
if step_1:
step_2 = delete()
if step_2:
step_3 = create()
return step_3
else:
return {"info": "删除失败"}
else:
return {"info": "此公司不存在报告数据"}
def revise_esg_report():
"""esg报告修改"""
return True
if records['评价项目'] == '综合信用评价':
res = revise_credit_report()
else:
res = revise_esg_report()
return res
def comprehensive_credit_search(self):
"""综合信用分析"""
search_body = self.dict_to_show()['搜索体']
data = self.db.find_single_data(
'企业数据',
'综合信用分析',
search_body,
['企业ID', '企业名称', '更新时间', '综合信用表现', '信用分析', '指标表格', '历史级别']
)
return data
def business_risk_search(self):
"""经营风险分析"""
search_body = self.dict_to_show()['搜索体']
data = self.db.find_single_data(
'企业数据',
'经营风险分析',
search_body,
['企业ID', '更新时间', '风险分数', '风险级别', '风险数量', '列入失信名单', '合规风险', '经营风险', '周边风险', '变更记录']
)
return data
def financial_elements_search(self):
"""财务要素分析"""
search_body = self.dict_to_show()['搜索体']
data = self.db.find_single_data(
'企业数据',
'财务要素分析',
search_body,
['企业ID', '更新时间', '财报期', '指标详情', '财务得分年度比较', '财务得分同行比较', '财务得分较去年变化', '财务得分较同行差异']
)
return data
def esg_evaluation_search(self):
"""esg评价分析"""
search_body = self.dict_to_show()['搜索体']
data = self.db.find_single_data(
'企业数据',
'ESG评价分析',
search_body,
['企业ID', '更新日期', 'ESG评级', '维度得分情况']
)
return data
if __name__ == '__main__':
insert = dict()
insert['企业ID'] = 'rh3XINDu'
insert['评价ID'] = 'wEtHEscX'
insert['评价流程'] = list()
step_01 = {"数据准备": ['填报数据', '风险数据']}
step_02 = {"模型打分": ['评价结果']}
step_03 = {"文档生成": ['报告数据', '综信报告', '综信证书']}
step_04 = {"数据清洗": ['ESG评价分析']}
insert['评价流程'].append(step_01)
insert['评价流程'].append(step_02)
insert['评价流程'].append(step_03)
insert['评价流程'].append(step_04)
db = MongoHelper("tfse_v0.21")
db.upsert_single_data(
'ESG评价数据',
'评价流程',
{'评价ID': 'wEtHEscX'},
insert
)