103 lines
2.9 KiB
Python
103 lines
2.9 KiB
Python
import json
|
||
import requests
|
||
import time
|
||
import os
|
||
import random
|
||
|
||
from DBHelper.MongoHelper import MongoHelper
|
||
|
||
|
||
def get_data_template(template_name):
|
||
data_template_path = '/static/template/{}.json'.format(template_name)
|
||
with open(os.path.dirname(__file__) + data_template_path, "r", encoding='utf-8') as f:
|
||
data_template = json.load(f)
|
||
return data_template
|
||
|
||
|
||
def update_process_status(rid, update_data):
|
||
"""
|
||
调用更新流程接口
|
||
Parameters:
|
||
update_data dict 需要更新的数据
|
||
Returns:
|
||
result str 更新结果
|
||
"""
|
||
url = "http://api.fecribd.com/etl_tfse/company/update_process_data"
|
||
token = "dmfd7FshT!5Wng9^gcCcQV7T6FBnVgl4"
|
||
headers = {'token': token, 'Content-Type': 'application/json; charset=UTF-8'}
|
||
data = json.dumps({"rid": rid, "types": "ESG评价流程", "renew_data": update_data})
|
||
requests.post(url, headers=headers, data=data)
|
||
|
||
|
||
def get_current_year():
|
||
"""
|
||
获取当前年度
|
||
Parameters:
|
||
-
|
||
Returns:
|
||
current_year 当前年度
|
||
"""
|
||
current_year = time.strftime("%Y", time.localtime()) + "年"
|
||
return current_year
|
||
|
||
|
||
def gen_new_rid(param):
|
||
"""
|
||
生成新的评价ID,如果该ID存在,则重新生成
|
||
Parameters:
|
||
param: 生成长度
|
||
Returns:
|
||
new_id: 生成的企业ID
|
||
"""
|
||
db = MongoHelper('tfse_v0.21')
|
||
# 生成新ID
|
||
new_id = make_id(param)
|
||
# 检查新ID是否存在,如果存在则继续生成新ID
|
||
case = db.find_all_data_with_count(
|
||
'企业数据',
|
||
'评价记录',
|
||
{'评价ID': new_id}
|
||
)
|
||
while case > 0:
|
||
new_id = make_id(param)
|
||
# 新ID可使用,返回新ID
|
||
return new_id
|
||
|
||
|
||
def make_3_report_dates():
|
||
"""
|
||
生成最近三年报告期 日期
|
||
Parameters:
|
||
-
|
||
Returns:
|
||
report_dates 三年报告期
|
||
"""
|
||
current_year = int(time.strftime("%Y", time.localtime()))-1
|
||
date_01 = str(current_year) + '年'
|
||
date_02 = str(current_year-1) + '年'
|
||
date_03 = str(current_year-2) + '年'
|
||
report_dates = [date_01, date_02, date_03]
|
||
return report_dates
|
||
|
||
|
||
def make_id(num):
|
||
"""
|
||
随机生成字符串
|
||
"""
|
||
choices = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
||
salt = ''
|
||
for i in range(num):
|
||
salt += random.choice(choices)
|
||
return salt
|
||
|
||
|
||
def prepare_risk_data(cid, rid, company_name):
|
||
# 执行风险数据清洗程序
|
||
url = "http://api.fecribd.com/etl_tfse/rating/esg_risk_data"
|
||
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": rid})
|
||
requests.post(url, headers=headers, data=data)
|
||
# 风险数据清洗完成后更新流程状态
|
||
update_process_status(rid, {"评价流程.清洗风险数据": 1})
|