86 lines
3.9 KiB
Python
86 lines
3.9 KiB
Python
|
# flask_app/routes/get_deviation.py
|
||
|
|
||
|
from flask import Blueprint, request, jsonify, Response, stream_with_context, g
|
||
|
import json
|
||
|
import os
|
||
|
from flask_app.main.download import download_file
|
||
|
from flask_app.general.post_processing import outer_post_processing
|
||
|
from flask_app.general.接口_技术偏离表 import get_tech_and_business_deviation
|
||
|
|
||
|
from flask_app.logger_setup import CSTFormatter
|
||
|
|
||
|
from flask_app.routes.utils import generate_deviation_response, validate_request
|
||
|
|
||
|
get_deviation_bp = Blueprint('get_deviation', __name__)
|
||
|
@get_deviation_bp.route('/get_deviation', methods=['POST'])
|
||
|
def get_deviation():
|
||
|
logger = g.logger
|
||
|
unique_id = g.unique_id
|
||
|
validation = validate_request()
|
||
|
|
||
|
if isinstance(validation, tuple) and len(validation) == 2 and isinstance(validation[0], str):
|
||
|
file_url, zb_type = validation
|
||
|
else:
|
||
|
return validation # 错误响应
|
||
|
|
||
|
try:
|
||
|
logger.info("开始解析 URL: " + file_url)
|
||
|
if zb_type != 2:
|
||
|
logger.error(f"无效的 zb_type: {zb_type}. 期望 zb_type: 2")
|
||
|
return jsonify({
|
||
|
'error': 'Invalid zb_type',
|
||
|
'message': '此端点仅支持 zb_type 2 (采购需求)'
|
||
|
}), 400
|
||
|
else:
|
||
|
tech_deviation, tech_star_deviation, business_deviation, business_star_deviation, zigefuhe_deviation = download_and_process_file_for_deviation(
|
||
|
file_url, unique_id)
|
||
|
if tech_deviation is None:
|
||
|
return jsonify({'error': 'File processing failed'}), 500
|
||
|
tech_deviation_response, tech_deviation_star_response, zigefuhe_deviation_response, shangwu_deviation_response, shangwu_star_deviation_response = generate_deviation_response(
|
||
|
tech_deviation, tech_star_deviation, business_deviation, business_star_deviation, zigefuhe_deviation,
|
||
|
logger)
|
||
|
|
||
|
final_response = {
|
||
|
'message': 'processed successfully',
|
||
|
'filename': 'END',
|
||
|
'data': 'END'
|
||
|
}
|
||
|
|
||
|
@stream_with_context
|
||
|
def generate():
|
||
|
yield f"data: {json.dumps(tech_deviation_response, ensure_ascii=False)}\n\n"
|
||
|
yield f"data: {json.dumps(tech_deviation_star_response, ensure_ascii=False)}\n\n"
|
||
|
yield f"data: {json.dumps(zigefuhe_deviation_response, ensure_ascii=False)}\n\n"
|
||
|
yield f"data: {json.dumps(shangwu_deviation_response, ensure_ascii=False)}\n\n"
|
||
|
yield f"data: {json.dumps(shangwu_star_deviation_response, ensure_ascii=False)}\n\n"
|
||
|
yield f"data: {json.dumps(final_response, ensure_ascii=False)}\n\n"
|
||
|
|
||
|
return Response(generate(), mimetype='text/event-stream')
|
||
|
except Exception as e:
|
||
|
logger.error('发生异常: ' + str(e))
|
||
|
return jsonify({'error': str(e)}), 500
|
||
|
|
||
|
|
||
|
def download_and_process_file_for_deviation(file_url, unique_id):
|
||
|
"""
|
||
|
下载并处理采购需求文件。
|
||
|
|
||
|
参数:
|
||
|
file_url (str): 文件的URL地址。
|
||
|
|
||
|
返回:
|
||
|
tuple: (tech_deviation, tech_star_deviation, business_deviation, business_star_deviation, zigefuhe_deviation)
|
||
|
"""
|
||
|
logger = g.logger
|
||
|
output_folder = g.output_folder
|
||
|
filename = "ztbfile"
|
||
|
downloaded_filename = os.path.join(output_folder, filename)
|
||
|
downloaded_filepath, file_type = download_file(file_url, downloaded_filename)
|
||
|
if downloaded_filepath is None or file_type == 4:
|
||
|
logger.error("Unsupported file type or failed to download file")
|
||
|
return None, None, None, None, None
|
||
|
logger.info("Local file path: " + downloaded_filepath)
|
||
|
tech_deviation, tech_star_deviation, business_deviation, business_star_deviation, zigefuhe_deviation = get_tech_and_business_deviation(
|
||
|
downloaded_filepath, file_type, unique_id, output_folder)
|
||
|
return tech_deviation, tech_star_deviation, business_deviation, business_star_deviation, zigefuhe_deviation
|