diff --git a/app.py b/app.py index 8502642..9a5f0f7 100644 --- a/app.py +++ b/app.py @@ -5,6 +5,7 @@ from TestManage.routes import test_route from company.routes import company_route from rating.routes import rating_route from user.routes import user_route +from macro.routes import macro_route app = Flask(__name__) CORS(app, supports_credentials=True) @@ -15,6 +16,7 @@ app.register_blueprint(test_route, url_prefix='/admin/test') app.register_blueprint(user_route, url_prefix='/admin/user') app.register_blueprint(company_route, url_prefix='/admin/company') app.register_blueprint(rating_route, url_prefix='/admin/rating') +app.register_blueprint(macro_route, url_prefix='/admin/marco') if __name__ == '__main__': app.run() diff --git a/macro/Macro.py b/macro/Macro.py new file mode 100644 index 0000000..1605584 --- /dev/null +++ b/macro/Macro.py @@ -0,0 +1,42 @@ +from macro.scripts import * + + +class Macro: + + def __init__(self): + self.industry = None + + def upload_macroscopic_report(self, file): + """ + 宏观报告上传 + Parameters: + file: 宏观PDF + Returns: + result: 生成结果 + """ + result = upload_macroscopic_report_script(self.industry, file) + return result + + def search_macroscopic_report(self, time, skip, limit): + """ + 宏观报告上传 + Parameters: + time: 上传日期 + skip: 默认0 + limit: 每页条数 + Returns: + result: 查询结果 + """ + result = search_macroscopic_report_script(self.industry, time, skip, limit) + return result + + def upload_macroscopic_data(self, req): + """ + 宏观数据上传 + Parameters: + req: 上传数据 + Returns: + result: 查询结果 + """ + result = upload_macroscopic_data_script(req) + return result diff --git a/macro/__init__.py b/macro/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/macro/db.py b/macro/db.py new file mode 100644 index 0000000..d80f594 --- /dev/null +++ b/macro/db.py @@ -0,0 +1,156 @@ +import re + +import pymongo +from bson import ObjectId +from gridfs import GridFS + +client = pymongo.MongoClient('mongodb://{}:{}@{}:{}'.format('root', 'sromitdTW569kC#M', '116.63.130.34', 27018)) + + +def find_macroscopic_report_data(industry): + """ + 查询宏观报告数据 + Parameters: + industry: 行业名称 + Returns: + record: 报告数据 + """ + collection = client['宏观']['行业报告'] + data = collection.find({"行业名称": industry}, {'_id': False}) + record = list(data) + return record + + +def insert_macroscopic_report_data(insert_data): + """ + 插入宏观报告数据 + Parameters: + insert_data: 需要保存的数据 + Returns: + result: 保存结果 + """ + collection = client['宏观']['行业报告'] + res = collection.insert_one(insert_data) + info = res.acknowledged + return info + + +def update_macroscopic_report_data(industry, update_time, fid): + """ + 更新宏观报告数据id + Parameters: + industry: 行业分类 + update_time: 上传日期 + fid: 文件id + Returns: + result: 更新结果 + """ + collection = client['宏观']['行业报告'] + res = collection.update_one({"行业名称": industry, '上传日期': update_time}, {"$set": {"报告fid": fid}}) + info = res.raw_result['updatedExisting'] + return info + + +def upload_report(file_name, files): + """ + 根据名称保存该企业报告 + Parameters: + file_name: 文件名 + files: pdf文件 + Returns: + result: 保存结果 + """ + try: + database = '宏观' + collection = '宏观报告PDF' + fs = GridFS(client[database], collection) + pdf_id = fs.put(data=files.read(), content_type='application/pdf', filename=file_name) + return str(pdf_id) + except Exception: + return False + + +def delete_macroscopic_pdf(fid): + """ + 根据id删除宏观报告 + Parameters: + fid: 评价ID + Returns: + record: 删除结果 + """ + database = '模型' + collection = '宏观报告PDF' + fs = GridFS(client[database], collection) + fs.delete(ObjectId(fid)) + + +def find_macroscopic_report_records(industry, time, skip, limit): + """ + 根据id删除宏观报告 + Parameters: + industry: 行业名称 + time: 上传日期 + skip: 页码,默认从0开始 + limit: 每页条数 + Returns: + record: 查询结果 + """ + collection = client['宏观']['行业报告'] + skip = (skip - 1) * limit + # 参数都不为空 + if industry and time: + find = {"行业名称": industry, "上传日期": re.compile(time)} + data = collection.find(find, {'_id': False}).skip(skip).limit(limit) + count = collection.find(find).count() + record = list(data) + result = { + "total": count, + "data": record + } + return result + elif len(industry) == 0 and time: + find = {"上传日期": re.compile(time)} + data = collection.find(find, {'_id': False}).skip(skip).limit(limit) + count = collection.find(find).count() + record = list(data) + result = { + "total": count, + "data": record + } + return result + elif len(time) == 0 and industry: + find = {"行业名称": industry} + data = collection.find(find, {'_id': False}).skip(skip).limit(limit) + count = collection.find(find).count() + record = list(data) + result = { + "total": count, + "data": record + } + return result + else: + data = collection.find({}, {'_id': False}).skip(skip).limit(limit) + count = collection.find().count() + record = list(data) + result = { + "total": count, + "data": record + } + return result + + +def upload_industry_macroscopic_data(req): + """ + 上传行业宏观数据 + Parameters: + req: 上传的数据 + Returns: + record: 上传结果 + """ + collection = client['宏观']['行业数据'] + find = {"一级行业": req['一级行业'], "二级行业": req['二级行业'], "统计时间": req['统计时间'], "统计字段名称": req['统计字段名称']} + res = collection.update_one(find, {"$set": req}, upsert=True) + if res.raw_result['n'] == 1: + return True + else: + return False diff --git a/macro/routes.py b/macro/routes.py new file mode 100644 index 0000000..7cf7882 --- /dev/null +++ b/macro/routes.py @@ -0,0 +1,81 @@ +from flask import Blueprint, request +from common.security.APIAuth import api_secret +from macro.Macro import Macro + +macro_route = Blueprint('macro', __name__) + + +@macro_route.route('/upload_macroscopic_report', methods=['POST']) +@api_secret +def upload_report(): + """ + 根据行业名称上传行业宏观报告 + Parameters: + - + Returns: + info: 上传结果 + """ + try: + macro = Macro() + macro.industry = request.form['industry'] + file = request.files['file'] + result = macro.upload_macroscopic_report(file) + if result: + return {"info": "上传成功", "result": {"报告fid": result}}, 200 + else: + return {"info": "上传失败"}, 210 + except KeyError: + return {"info": "键值错误"}, 400 + except TypeError: + return {"info": "参数错误"}, 400 + + +@macro_route.route('/search_macroscopic_report', methods=['POST']) +@api_secret +def search_report(): + """ + 根据行业名称查询行业宏观报告 + Parameters: + - + Returns: + info: 查询结果 + """ + try: + macro = Macro() + macro.industry = request.json['industry'] + time = request.json['time'] + skip = request.json['skip'] + limit = request.json['limit'] + result = macro.search_macroscopic_report(time, skip, limit) + if result: + return {"info": "查询成功", "result": result}, 200 + else: + return {"info": "查询失败"}, 210 + except KeyError: + return {"info": "键值错误"}, 400 + except TypeError: + return {"info": "参数错误"}, 400 + + +@macro_route.route('/upload_macro_data', methods=['POST']) +@api_secret +def upload_data(): + """ + 上传行业宏观数据 + Parameters: + - + Returns: + info: 上传结果 + """ + try: + macro = Macro() + req = request.json + result = macro.upload_macroscopic_data(req) + if result: + return {"info": "上传成功"}, 200 + else: + return {"info": "上传失败"}, 210 + except KeyError: + return {"info": "键值错误"}, 400 + except TypeError: + return {"info": "参数错误"}, 400 \ No newline at end of file diff --git a/macro/scripts.py b/macro/scripts.py new file mode 100644 index 0000000..cb91442 --- /dev/null +++ b/macro/scripts.py @@ -0,0 +1,101 @@ +import datetime + +import pandas as pd + +from macro.db import * + + +def upload_macroscopic_report_script(industry, file): + """ + 根据行业保存PDF宏观报告 + Parameters: + industry: 行业名称 + file: pdf文件 + Returns: + result: 生成结果 + """ + + def save_report(): + """ + 保存PDF宏观报告 + Parameters: + - + Returns: + result: 生成结果 + """ + file_name = industry + '宏观报告' + file_id = upload_report(file_name, file) + return file_id + + # 保存新宏观报告pdf文件 + new_fid = save_report() + # 根据行业名称查询此行业是否存在宏观报告 + macrosc = find_macroscopic_report_data(industry) + if macrosc: + update_time = datetime.datetime.now().strftime('%Y-%m-%d') + # 使用新fid更新此条数据 + upload_res = update_macroscopic_report_data(industry, update_time, new_fid) + if upload_res: + return new_fid + else: + return False + else: + # 不存在此条数据,就插入一条新数据 + insert_data = dict() + insert_data['行业名称'] = industry + insert_data['报告名称'] = industry + '宏观报告' + insert_data['报告fid'] = new_fid + insert_data['上传日期'] = datetime.datetime.now().strftime('%Y-%m-%d') + save_res = insert_macroscopic_report_data(insert_data) + if save_res: + return new_fid + else: + return False + + +def search_macroscopic_report_script(industry, time, skip, limit): + """ + 根据行业或上传日期查询PDF宏观报告 + Parameters: + industry: 行业名称 + time: 上传日期 + skip: 页码 + limit: 每页条数 + Returns: + result: 查询结果 + """ + result = find_macroscopic_report_records(industry, time, skip, limit) + return result + + +def upload_macroscopic_data_script(req): + """ + 上传行业宏观数据 + Parameters: + req: 上传数据 + Returns: + result: 查询结果 + """ + result = upload_industry_macroscopic_data(req) + return result + + +def get_macro_data(): + df = pd.read_excel('./static/C13-02.xlsx') + for index, row in df.iterrows(): + num = len(row) + for i in range(num-1): + req = { + "一级行业": "制造业", + "二级行业": row[0], + "数据": row[i + 1], + "统计字段名称": row.keys()[i + 1], + "统计周期": "年度", + "统计时间": "2020年", + "数据单位": "亿元" + } + upload_industry_macroscopic_data(req) + + +if __name__ == '__main__': + get_macro_data() \ No newline at end of file diff --git a/macro/static/C13-02.xlsx b/macro/static/C13-02.xlsx new file mode 100644 index 0000000..d0619dd Binary files /dev/null and b/macro/static/C13-02.xlsx differ diff --git a/rating/Rating.py b/rating/Rating.py index 2ac544e..d6b0f77 100644 --- a/rating/Rating.py +++ b/rating/Rating.py @@ -78,6 +78,3 @@ class Rating: result = report_create_script(self.rid) return result - def upload_macroscopic_report(self, file): - result = upload_macroscopic_report_script(self.industry, file) - return result diff --git a/rating/db.py b/rating/db.py index b18020e..f026671 100644 --- a/rating/db.py +++ b/rating/db.py @@ -1,5 +1,4 @@ import re - import pymongo from bson import ObjectId from gridfs import GridFS @@ -147,79 +146,3 @@ def find_report_data(rid): data = collection.find({"评价ID": rid}, {'_id': False}) record = list(data) return record - - -def find_macroscopic_report_data(industry): - """ - 查询宏观报告数据 - Parameters: - industry: 行业名称 - Returns: - record: 报告数据 - """ - collection = client['宏观']['行业报告'] - data = collection.find({"行业名称": industry}, {'_id': False}) - record = list(data) - return record - - -def insert_macroscopic_report_data(insert_data): - """ - 插入宏观报告数据 - Parameters: - insert_data: 需要保存的数据 - Returns: - result: 保存结果 - """ - collection = client['宏观']['行业报告'] - res = collection.insert_one(insert_data) - info = res.acknowledged - return info - - -def update_macroscopic_report_data(industry, update_time, fid): - """ - 更新宏观报告数据id - Parameters: - industry: 行业分类 - update_time: 上传日期 - fid: 文件id - Returns: - result: 更新结果 - """ - collection = client['宏观']['行业报告'] - res = collection.update_one({"行业名称": industry, '上传日期': update_time}, {"$set": {"报告fid": fid}}) - info = res.raw_result['updatedExisting'] - return info - - -def upload_report(file_name, files): - """ - 根据名称保存该企业报告 - """ - try: - database = '宏观' - collection = '宏观报告PDF' - fs = GridFS(client[database], collection) - pdf_id = fs.put(data=files.read(), content_type='application/pdf', filename=file_name) - return str(pdf_id) - except Exception: - return False - - -def delete_macroscopic_pdf(fid): - """ - 根据id删除宏观报告 - Parameters: - fid: 评价ID - Returns: - record: 删除结果 - """ - database = '模型' - collection = '宏观报告PDF' - fs = GridFS(client[database], collection) - fs.delete(ObjectId(fid)) - - -if __name__ == '__main__': - delete_report_pdf('61de994660a518e91fbd0192') \ No newline at end of file diff --git a/rating/routes.py b/rating/routes.py index a4775ff..aff2bdb 100644 --- a/rating/routes.py +++ b/rating/routes.py @@ -1,7 +1,6 @@ from flask import Blueprint, request from common.security.APIAuth import api_secret from rating.Rating import Rating - rating_route = Blueprint('rating', __name__) @@ -130,28 +129,3 @@ def report_edit_save_delete(): return {"info": "键值错误"}, 400 except TypeError: return {"info": "参数错误"}, 400 - - -@rating_route.route('/upload_macroscopic_report', methods=['POST']) -@api_secret -def upload_report(): - """ - 根据行业名称上传行业宏观报告 - Parameters: - - - Returns: - info: 上传结果 - """ - try: - rating = Rating() - rating.industry = request.form['industry'] - file = request.files['file'] - result = rating.upload_macroscopic_report(file) - if result: - return {"info": "上传成功", "result": {"报告fid": result}}, 200 - else: - return {"info": "上传失败"}, 210 - except KeyError: - return {"info": "键值错误"}, 400 - except TypeError: - return {"info": "参数错误"}, 400 diff --git a/rating/scripts.py b/rating/scripts.py index 3199816..18a3177 100644 --- a/rating/scripts.py +++ b/rating/scripts.py @@ -1,8 +1,5 @@ -import datetime import json - import requests - from rating.db import * @@ -125,52 +122,3 @@ def report_create_script(rid): if result['info'] == '生成报告成功': update_evaluation_records(rid, {"报告fid": result['result']['FileID']}) return result - - -def upload_macroscopic_report_script(industry, file): - """ - 根据行业保存PDF宏观报告 - Parameters: - industry: 行业名称 - file: pdf文件 - Returns: - result: 生成结果 - """ - - def save_report(): - """ - 保存PDF宏观报告 - Parameters: - - - Returns: - result: 生成结果 - """ - file_name = industry + '宏观报告' - file_id = upload_report(file_name, file) - return file_id - - # 保存新宏观报告pdf文件 - new_fid = save_report() - # 根据行业名称查询此行业是否存在宏观报告 - macrosc = find_macroscopic_report_data(industry) - if macrosc: - update_time = datetime.datetime.now().strftime('%Y-%m-%d') - # 使用新fid更新此条数据 - upload_res = update_macroscopic_report_data(industry, update_time, new_fid) - if upload_res: - return new_fid - else: - return False - else: - # 不存在此条数据,就插入一条新数据 - insert_data = dict() - insert_data['行业名称'] = industry - insert_data['报告名称'] = industry + '宏观报告' - insert_data['报告fid'] = new_fid - insert_data['上传日期'] = datetime.datetime.now().strftime('%Y-%m-%d') - save_res = insert_macroscopic_report_data(insert_data) - if save_res: - return new_fid - else: - return False -