2022-02-08 17:07:20 +08:00
|
|
|
|
import os
|
2021-12-03 14:15:24 +08:00
|
|
|
|
import copy
|
2021-12-15 10:55:20 +08:00
|
|
|
|
import json
|
2022-02-08 17:07:20 +08:00
|
|
|
|
import random
|
2021-12-03 14:15:24 +08:00
|
|
|
|
import time
|
2022-02-08 17:07:20 +08:00
|
|
|
|
|
2021-12-15 10:55:20 +08:00
|
|
|
|
import requests
|
2021-12-03 14:15:24 +08:00
|
|
|
|
|
2022-02-15 11:04:55 +08:00
|
|
|
|
from input.input_db import *
|
2022-01-27 17:01:38 +08:00
|
|
|
|
from user.user_db import FIND_USER_INFO
|
|
|
|
|
from user.user_impl import check_verified
|
2021-12-03 14:15:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_current_year():
|
|
|
|
|
"""
|
|
|
|
|
获取当前年度
|
|
|
|
|
Parameters:
|
|
|
|
|
-
|
|
|
|
|
Returns:
|
|
|
|
|
current_year 当前年度
|
|
|
|
|
"""
|
|
|
|
|
current_year = time.strftime("%Y", time.localtime()) + "年"
|
|
|
|
|
return current_year
|
|
|
|
|
|
2021-12-02 17:20:48 +08:00
|
|
|
|
|
2021-12-03 14:15:24 +08:00
|
|
|
|
def make_3_report_dates():
|
|
|
|
|
"""
|
|
|
|
|
生成最近三年报告期 日期
|
|
|
|
|
Parameters:
|
|
|
|
|
-
|
|
|
|
|
Returns:
|
|
|
|
|
report_dates 三年报告期
|
|
|
|
|
"""
|
2022-01-04 10:55:55 +08:00
|
|
|
|
current_year = int(time.strftime("%Y", time.localtime()))-1
|
2021-12-03 14:15:24 +08:00
|
|
|
|
date_01 = str(current_year) + '-12-31'
|
|
|
|
|
date_02 = str(current_year-1) + '-12-31'
|
|
|
|
|
date_03 = str(current_year-2) + '-12-31'
|
|
|
|
|
report_dates = [date_01, date_02, date_03]
|
|
|
|
|
return report_dates
|
2021-12-02 17:20:48 +08:00
|
|
|
|
|
2021-12-03 14:15:24 +08:00
|
|
|
|
|
|
|
|
|
def gen_new_rid():
|
|
|
|
|
"""
|
|
|
|
|
生成新的评价ID,如果该ID存在,则重新生成
|
|
|
|
|
Parameters:
|
|
|
|
|
-
|
|
|
|
|
Returns:
|
|
|
|
|
new_id: 生成的企业ID
|
|
|
|
|
"""
|
|
|
|
|
# 生成新ID
|
|
|
|
|
new_id = make_id()
|
|
|
|
|
# 检查新ID是否存在,如果存在则继续生成新ID
|
|
|
|
|
case = find_rating_records({"评价ID": new_id}) is []
|
|
|
|
|
while case:
|
|
|
|
|
new_id = make_id()
|
|
|
|
|
# 新ID可使用,返回新ID
|
|
|
|
|
return new_id
|
2021-12-02 17:20:48 +08:00
|
|
|
|
|
|
|
|
|
|
2021-12-06 16:17:10 +08:00
|
|
|
|
def get_company_type(cid):
|
|
|
|
|
"""
|
|
|
|
|
获取企业填报的企业类型
|
|
|
|
|
Parameters:
|
|
|
|
|
cid: 企业ID
|
|
|
|
|
Returns:
|
|
|
|
|
industry
|
|
|
|
|
若没有查询到行业 返回空值
|
|
|
|
|
"""
|
|
|
|
|
# 根据企业ID获取企业当年填报的企业类型
|
|
|
|
|
choose_industry = find_rating_inputs({"企业ID": cid, "评价年度": get_current_year()})[0]['行业选择']
|
|
|
|
|
|
|
|
|
|
if not len(choose_industry) > 1:
|
|
|
|
|
return None
|
|
|
|
|
else:
|
|
|
|
|
return choose_industry[0]
|
|
|
|
|
|
|
|
|
|
|
2021-12-02 17:20:48 +08:00
|
|
|
|
def rating_records_by_cid(cid):
|
|
|
|
|
"""
|
|
|
|
|
查询一家企业的所有评价记录
|
|
|
|
|
Parameters:
|
|
|
|
|
cid: 企业ID
|
|
|
|
|
Returns:
|
|
|
|
|
records: 企业的评价记录
|
|
|
|
|
"""
|
2022-01-07 11:24:18 +08:00
|
|
|
|
report_link = '/file/get_company_report?file_id={}'
|
2022-01-13 14:14:55 +08:00
|
|
|
|
certification_link = "/file/get_certification?file_id={}"
|
2021-12-02 17:20:48 +08:00
|
|
|
|
records = find_rating_records({"企业ID": cid})
|
2022-01-06 13:51:00 +08:00
|
|
|
|
for record in records:
|
|
|
|
|
if records is not None:
|
2022-01-07 12:10:15 +08:00
|
|
|
|
record['报告'] = report_link.format(record['报告fid']) if record['报告fid'] is not None else None
|
2022-01-13 14:14:55 +08:00
|
|
|
|
record['证书'] = certification_link.format(record['证书fid']) if record['证书fid'] is not None else None
|
2022-01-06 13:51:00 +08:00
|
|
|
|
record.pop('企业ID')
|
|
|
|
|
record.pop('报告fid')
|
|
|
|
|
record.pop('证书fid')
|
2021-12-02 17:20:48 +08:00
|
|
|
|
return records
|
2021-12-03 14:15:24 +08:00
|
|
|
|
|
|
|
|
|
|
2021-12-07 17:17:26 +08:00
|
|
|
|
def start_general_rating_script(cid):
|
2021-12-03 14:15:24 +08:00
|
|
|
|
"""
|
|
|
|
|
开始进行综合评价的数据填报
|
|
|
|
|
Parameters:
|
|
|
|
|
cid: 企业ID
|
|
|
|
|
Returns:
|
|
|
|
|
record 填报信息
|
|
|
|
|
"""
|
|
|
|
|
# 检查是否已认证
|
|
|
|
|
if not check_verified(cid):
|
|
|
|
|
return "该企业未认证", {}
|
|
|
|
|
|
|
|
|
|
# 根据企业ID和当前年度查询是否存在当年的填报记录
|
2021-12-03 17:11:23 +08:00
|
|
|
|
exist_input_form = find_rating_inputs({"企业ID": cid, "评价年度": get_current_year()})
|
2021-12-03 14:15:24 +08:00
|
|
|
|
|
|
|
|
|
# 若存在填报记录,根据评价ID查询评价记录该评价是否完成
|
2021-12-03 17:11:23 +08:00
|
|
|
|
if exist_input_form:
|
2021-12-03 14:15:24 +08:00
|
|
|
|
# 已完成 本年度不能再填报
|
2021-12-03 17:11:23 +08:00
|
|
|
|
if find_rating_records({"评价ID": exist_input_form[0]["评价ID"]})[0]['进行状态'] == "完成":
|
2021-12-03 14:15:24 +08:00
|
|
|
|
return "本年度已评价", {}
|
|
|
|
|
# 未完成 返回已经填报保存的数据
|
|
|
|
|
else:
|
2021-12-03 17:11:23 +08:00
|
|
|
|
exist_input_form[0].pop('评价ID')
|
|
|
|
|
exist_input_form[0].pop('企业ID')
|
|
|
|
|
return "继续填报", exist_input_form[0]
|
2021-12-03 14:15:24 +08:00
|
|
|
|
|
|
|
|
|
# 不存在填报记录 新生成填报记录、评价记录 返回空填报记录对象
|
|
|
|
|
else:
|
2021-12-03 17:11:23 +08:00
|
|
|
|
# 新生成填报ID
|
2021-12-03 14:15:24 +08:00
|
|
|
|
new_rid = gen_new_rid()
|
2021-12-03 17:11:23 +08:00
|
|
|
|
# 生成报告期
|
2021-12-03 14:15:24 +08:00
|
|
|
|
report_dates = make_3_report_dates()
|
|
|
|
|
|
|
|
|
|
# 新填报记录
|
2022-01-27 17:01:38 +08:00
|
|
|
|
company_name = FIND_USER_INFO({"企业ID": cid})[0]['企业名称']
|
2021-12-15 15:28:23 +08:00
|
|
|
|
|
2022-02-08 17:07:20 +08:00
|
|
|
|
input_json = '/static/template/input.json'
|
|
|
|
|
with open(os.path.dirname(__file__)+input_json, "r", encoding='utf-8') as f:
|
|
|
|
|
new_input = json.load(f)
|
|
|
|
|
|
2021-12-03 14:15:24 +08:00
|
|
|
|
new_input['评价ID'] = new_rid
|
|
|
|
|
new_input['企业ID'] = cid
|
2021-12-15 15:28:23 +08:00
|
|
|
|
new_input['企业名称'] = company_name
|
2021-12-03 14:15:24 +08:00
|
|
|
|
new_input['评价年度'] = get_current_year()
|
|
|
|
|
for sheets in ['资产负债表', '利润表', '补充数据表']:
|
|
|
|
|
for i in range(3):
|
|
|
|
|
new_input['财务填报'][sheets][i]['报告期'] = report_dates[i]
|
|
|
|
|
|
|
|
|
|
# 生成新评价记录
|
2022-02-08 17:07:20 +08:00
|
|
|
|
rating_json = '/static/template/rating.json'
|
|
|
|
|
with open(os.path.dirname(__file__)+rating_json, "r", encoding='utf-8') as f:
|
|
|
|
|
rating = json.load(f)
|
2021-12-03 14:15:24 +08:00
|
|
|
|
rating['评价ID'] = new_rid
|
|
|
|
|
rating['企业ID'] = cid
|
|
|
|
|
rating['评价项目'] = "综合信用评价"
|
|
|
|
|
rating['评价方式'] = "企业申报"
|
|
|
|
|
rating['进行状态'] = "进行"
|
|
|
|
|
|
|
|
|
|
# 深拷贝新填报记录,不然保存后会生成_id
|
|
|
|
|
return_input = copy.deepcopy(new_input)
|
|
|
|
|
# 插入新填报记录
|
|
|
|
|
insert_general_input(new_input)
|
|
|
|
|
# 插入新评价记录
|
|
|
|
|
insert_rating_records(rating)
|
|
|
|
|
|
2021-12-15 15:28:23 +08:00
|
|
|
|
# 执行风险数据清洗程序
|
2022-03-02 16:01:35 +08:00
|
|
|
|
url = "http://api.fecribd.com/etl_tfse/rating/risk_data"
|
2021-12-15 15:28:23 +08:00
|
|
|
|
token = "dmfd7FshT!5Wng9^gcCcQV7T6FBnVgl4"
|
|
|
|
|
headers = {'token': token, 'Content-Type': 'application/json; charset=UTF-8'}
|
|
|
|
|
data = json.dumps({"company_id": cid, "company_name": company_name, "evaluation_id": new_rid})
|
|
|
|
|
requests.post(url, headers=headers, data=data)
|
|
|
|
|
|
2021-12-03 14:15:24 +08:00
|
|
|
|
return "开始填报", return_input
|
2021-12-03 17:11:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def save_general_input_form(cid, input_form):
|
|
|
|
|
"""
|
|
|
|
|
保存综合信用评价填报
|
|
|
|
|
Parameters:
|
|
|
|
|
cid: 企业ID
|
|
|
|
|
input_form: 填报表单
|
|
|
|
|
Returns:
|
|
|
|
|
res: desc
|
|
|
|
|
"""
|
|
|
|
|
rid = find_rating_records({"企业ID": cid, "进行状态": "进行"})[0]['评价ID']
|
|
|
|
|
update_general_input(rid, input_form)
|
2021-12-06 15:44:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def choose_industry_script():
|
|
|
|
|
"""
|
|
|
|
|
选择行业
|
|
|
|
|
Parameters:
|
|
|
|
|
-
|
|
|
|
|
Returns:
|
2021-12-15 10:55:20 +08:00
|
|
|
|
-
|
2021-12-06 15:44:52 +08:00
|
|
|
|
"""
|
2022-02-08 17:07:20 +08:00
|
|
|
|
industry_json = '/static/template/industry.json'
|
|
|
|
|
with open(os.path.dirname(__file__)+industry_json, "r", encoding='utf-8') as f:
|
|
|
|
|
data = json.load(f)
|
2021-12-06 15:44:52 +08:00
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
2021-12-06 16:17:10 +08:00
|
|
|
|
def questionnaire_script(industry):
|
2021-12-06 15:44:52 +08:00
|
|
|
|
"""
|
2021-12-06 16:17:10 +08:00
|
|
|
|
问卷模板
|
2021-12-06 15:44:52 +08:00
|
|
|
|
Parameters:
|
2021-12-06 16:17:10 +08:00
|
|
|
|
industry: 行业
|
2021-12-06 15:44:52 +08:00
|
|
|
|
Returns:
|
2021-12-06 16:17:10 +08:00
|
|
|
|
data: 问卷内容
|
2021-12-06 15:44:52 +08:00
|
|
|
|
"""
|
2022-01-06 12:38:01 +08:00
|
|
|
|
try:
|
2022-02-08 17:07:20 +08:00
|
|
|
|
questionnaire_json = '/static/questionnaire/{}.json'.format(industry)
|
|
|
|
|
with open(os.path.dirname(__file__)+questionnaire_json, "r", encoding='utf-8') as f:
|
|
|
|
|
data = json.load(f)
|
2022-01-06 12:38:01 +08:00
|
|
|
|
return data
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
data = [
|
|
|
|
|
{"label": "环境", "children": []},
|
|
|
|
|
{"label": "社会责任", "children": []},
|
|
|
|
|
{"label": "公司治理", "children": []}
|
|
|
|
|
]
|
|
|
|
|
return data
|
2021-12-15 10:55:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def exec_general_rating_scripts(cid):
|
|
|
|
|
"""
|
|
|
|
|
Notes
|
|
|
|
|
Parameters:
|
|
|
|
|
-
|
|
|
|
|
Returns:
|
|
|
|
|
-
|
|
|
|
|
"""
|
|
|
|
|
# pre
|
|
|
|
|
record = find_rating_records({"企业ID": cid, "进行状态": "进行"})
|
|
|
|
|
|
|
|
|
|
if not record:
|
2021-12-30 14:24:16 +08:00
|
|
|
|
return False, "没有正在进行的评价"
|
2021-12-15 10:55:20 +08:00
|
|
|
|
|
|
|
|
|
# Parameters
|
|
|
|
|
rid = record[0]["评价ID"]
|
|
|
|
|
inputs = find_rating_inputs({"评价ID": rid})[0]
|
|
|
|
|
risks = {"风险数据": find_risk_data(rid)[0]}
|
|
|
|
|
risks["风险数据"].pop('企业ID')
|
|
|
|
|
risks["风险数据"].pop('评价ID')
|
|
|
|
|
rating_inputs = {**inputs, **risks}
|
|
|
|
|
|
2021-12-24 16:59:37 +08:00
|
|
|
|
# 打分模型
|
|
|
|
|
def general_model():
|
2022-03-02 16:01:35 +08:00
|
|
|
|
url = "http://api.fecribd.com/tfse_rating/rating/general_model"
|
2021-12-24 16:59:37 +08:00
|
|
|
|
token = "X0gSlC!YE8jmr2jJr&ilcjS83j!tsoh5"
|
|
|
|
|
headers = {'token': token, "Content-Type": "application/json;charset=UTF-8"}
|
|
|
|
|
data = json.dumps({"input_data": rating_inputs})
|
|
|
|
|
res = requests.post(url, headers=headers, data=data)
|
|
|
|
|
if res.status_code == 200:
|
|
|
|
|
update_data = {
|
|
|
|
|
"评价结果": json.loads(res.text)['result'],
|
2021-12-30 14:24:16 +08:00
|
|
|
|
"评价时间": time.strftime("%Y-%m-%d", time.localtime())
|
2021-12-24 16:59:37 +08:00
|
|
|
|
}
|
|
|
|
|
update_rating_records({"评价ID": rid}, update_data)
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
# 生成报告
|
|
|
|
|
def gen_report():
|
2022-03-02 16:01:35 +08:00
|
|
|
|
url = "http://api.fecribd.com/tfse_rating/report/gen_general_rating_report"
|
2021-12-28 10:47:22 +08:00
|
|
|
|
token = "X0gSlC!YE8jmr2jJr&ilcjS83j!tsoh5"
|
|
|
|
|
headers = {'token': token, "Content-Type": "application/json;charset=UTF-8"}
|
|
|
|
|
data = {
|
|
|
|
|
"company": rating_inputs['企业名称'],
|
|
|
|
|
"cid": rating_inputs['企业ID'],
|
|
|
|
|
"rid": rating_inputs['评价ID']
|
|
|
|
|
}
|
2021-12-31 03:21:48 +08:00
|
|
|
|
res = requests.post(url, headers=headers, data=json.dumps(data))
|
2021-12-28 10:47:22 +08:00
|
|
|
|
if res.status_code == 200:
|
|
|
|
|
update_data = {
|
|
|
|
|
"报告fid": json.loads(res.text)['result']['FileID']
|
|
|
|
|
}
|
|
|
|
|
update_rating_records({"评价ID": rid}, update_data)
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
return False
|
2021-12-27 16:50:50 +08:00
|
|
|
|
|
|
|
|
|
# 生成证书
|
|
|
|
|
def gen_certified():
|
2022-03-02 16:01:35 +08:00
|
|
|
|
url = "http://api.fecribd.com/tfse_rating/certificate/generate_certificate"
|
2022-01-13 14:14:55 +08:00
|
|
|
|
token = "X0gSlC!YE8jmr2jJr&ilcjS83j!tsoh5"
|
|
|
|
|
headers = {'token': token, "Content-Type": "application/json;charset=UTF-8"}
|
|
|
|
|
data = {
|
|
|
|
|
"company": rating_inputs['企业名称'],
|
|
|
|
|
"cid": rating_inputs['企业ID'],
|
|
|
|
|
"rid": rating_inputs['评价ID']
|
|
|
|
|
}
|
|
|
|
|
res = requests.post(url, headers=headers, data=json.dumps(data))
|
|
|
|
|
if res.status_code == 200:
|
|
|
|
|
update_data = {
|
|
|
|
|
"证书fid": json.loads(res.text)['result']['FileID']
|
|
|
|
|
}
|
|
|
|
|
update_rating_records({"评价ID": rid}, update_data)
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
return False
|
2021-12-27 16:50:50 +08:00
|
|
|
|
|
|
|
|
|
# 生成企业主页数据
|
|
|
|
|
def company_index():
|
2022-03-02 16:01:35 +08:00
|
|
|
|
url = "http://api.fecribd.com/etl_tfse/company/company_index_etl"
|
2021-12-28 10:47:22 +08:00
|
|
|
|
token = "dmfd7FshT!5Wng9^gcCcQV7T6FBnVgl4"
|
|
|
|
|
headers = {'token': token, "Content-Type": "application/json;charset=UTF-8"}
|
|
|
|
|
data = {
|
|
|
|
|
"rid": rating_inputs['评价ID']
|
|
|
|
|
}
|
2021-12-31 03:21:48 +08:00
|
|
|
|
res = requests.post(url, headers=headers, data=json.dumps(data))
|
2021-12-28 10:47:22 +08:00
|
|
|
|
if res.status_code == 200:
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
if not general_model():
|
|
|
|
|
return False, "打分失败"
|
|
|
|
|
|
|
|
|
|
if not gen_report():
|
2022-01-13 14:14:55 +08:00
|
|
|
|
return False, "生成报告失败"
|
|
|
|
|
|
|
|
|
|
if not gen_certified():
|
|
|
|
|
return False, "生成证书失败"
|
2021-12-28 10:47:22 +08:00
|
|
|
|
|
|
|
|
|
if not company_index():
|
2022-01-13 14:14:55 +08:00
|
|
|
|
return False, "准备主页数据失败"
|
2021-12-28 10:47:22 +08:00
|
|
|
|
|
2021-12-30 14:24:16 +08:00
|
|
|
|
update_rating_records({"评价ID": rid}, {"进行状态": "完成"})
|
|
|
|
|
|
2021-12-28 10:47:22 +08:00
|
|
|
|
return True, "执行成功"
|
2022-02-08 17:07:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def make_id(num):
|
|
|
|
|
"""
|
|
|
|
|
随机生成字符串
|
|
|
|
|
"""
|
|
|
|
|
choices = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
|
|
|
|
salt = ''
|
|
|
|
|
for i in range(num):
|
|
|
|
|
salt += random.choice(choices)
|
|
|
|
|
return salt
|