|
|
@@ -1,3 +1,4 @@
|
|
|
+from config
|
|
|
from fastapi import FastAPI, Request, status, BackgroundTasks
|
|
|
from fastapi.exceptions import RequestValidationError
|
|
|
from fastapi.responses import JSONResponse
|
|
|
@@ -7,6 +8,7 @@ import os
|
|
|
from pydantic import BaseModel
|
|
|
import uvicorn
|
|
|
from utils import ReportUtils
|
|
|
+import requests
|
|
|
from typing import List, Dict
|
|
|
|
|
|
app = FastAPI()
|
|
|
@@ -81,53 +83,63 @@ def generate_report(city_uuid, product_id, recall_count, delivery_count):
|
|
|
"""生成报告"""
|
|
|
report_util = ReportUtils(city_uuid, product_id)
|
|
|
report_util.generate_all_data(recall_count, delivery_count)
|
|
|
+ repots_dir = os.path.join('./data/reports', city_uuid, product_id)
|
|
|
+ upload_file(repots_dir)
|
|
|
|
|
|
-REPORT_FILES = [
|
|
|
- "卷烟信息表.xlsx",
|
|
|
- "品规商户特征关系表.xlsx",
|
|
|
- "商户售卖推荐表.xlsx",
|
|
|
- "相似卷烟表.xlsx"
|
|
|
-]
|
|
|
-
|
|
|
-@app.get("/brandcultivation/api/v1/download_report")
|
|
|
-async def get_report_files(city_uuid: str, product_code: str) -> JSONResponse:
|
|
|
- report_dir = os.path.join("./data/reports", city_uuid, product_code)
|
|
|
-
|
|
|
- # 检查报告是否存在
|
|
|
- if not os.path.exists(report_dir):
|
|
|
- return JSONResponse(
|
|
|
- status_code=200,
|
|
|
- content={
|
|
|
- "code": 200,
|
|
|
- "msg": "report directory not found",
|
|
|
- "data": {"reportDownloadInfo": "该品规报告还未生成!"}
|
|
|
- }
|
|
|
- )
|
|
|
-
|
|
|
- # 收集所有存在的文件
|
|
|
- available_files:List[Dict[str, str]] = []
|
|
|
- base_url = "http://127.0.0.1:7960"
|
|
|
-
|
|
|
- for filename in REPORT_FILES:
|
|
|
- file_path = os.path.join(report_dir, filename)
|
|
|
- if os.path.exists(file_path):
|
|
|
- file_url = f"{base_url}/brandcultivation/api/v1/download_file/{city_uuid}/{product_code}/{filename}"
|
|
|
- available_files.append({
|
|
|
- "filename": filename,
|
|
|
- "download_url": file_url
|
|
|
- })
|
|
|
-
|
|
|
- return JSONResponse(
|
|
|
- status_code=200,
|
|
|
- content={
|
|
|
- "code": 200,
|
|
|
- "msg": "success",
|
|
|
- "data": {
|
|
|
- "reportDownloadInfo": available_files
|
|
|
- }
|
|
|
- }
|
|
|
- )
|
|
|
+def upload_file(reports_dir):
|
|
|
+ """上传报告文件"""
|
|
|
+ base_url = "https://10-79-117-86-8p1kxyomtjwgt3.ztna-dingtalk.com/screen/mapi/file/fileUpload"
|
|
|
+ files = [
|
|
|
+ "卷烟信息表.xlsx",
|
|
|
+ "品规商户特征关系表.xlsx",
|
|
|
+ "相似卷烟表.xlsx"
|
|
|
+ ]
|
|
|
+ # 设置请求头
|
|
|
+ headers = {
|
|
|
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
|
|
|
+ "Accept": "*/*",
|
|
|
+ }
|
|
|
+ # 设置Cookie
|
|
|
+ cookies = {
|
|
|
+ "ecp_token": "ZXlKamIyUmxJam9pTURZMVltUXpZbUV5T0dWaFl6ZzRPREJqTkRjNU5ERXpaV0k0T1dRd1pERWlMQ0p6WTI5d1pTSTZJbkJ5YjNSbFkzUmxaQ0lzSW1Oc2FXVnVkQ0k2SW1Oc2FXVnVkRjlzYjI1bmFta2lmUT09",
|
|
|
+ "acw_tc": "0a03176c17452016143048775e73c8e9556c4c3ee2dbc80f405a9314a5d0e5",
|
|
|
+ "cna": "09T8H5zH8SoCASeqHG9RyLbu",
|
|
|
+ "isg": "BDAwNyXyI9cQD__-0PkIodjTAfiCeRTD-vUtYyqmIw3d5bGP0Y0nUayWOe2F3syb",
|
|
|
+ "dd-ztna-token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIwNTAwMzQzMDM4MjYxNDMwOTQiLCJjb3JwSWQiOiJkaW5nYzc2YTJhMzdiMjRhODYwYTRhYzVkNjk4MDg2NGQzMzUiLCJkZXB0SWRzIjoiW1s4Mzk0NzIxNjAsODM2MjQ1MTUzLDFdXSIsInJvbGVJZHMiOiJbMjEwNDk3MDY1OF0iLCJ1bmlvbklkIjoiVXFyYTB4U0VQcWNxdFNhQTFqSzRzZ2lFaUUiLCJleHAiOjE3NDU4MDY0MjIsImlhdCI6MTc0NTIwMTYyMn0.S0K3UWF0fBP5fF6TQaUkxe8I7piSQjrXqG3DdviqTzdO9Y0J7wsydcRvfp-OuHUkXbY92diIBGRHBr5Bb0eYgmCZ946Q8CHk12WXWvJmMDPrQ4i2C1J7W7uzUuPYJiWQLGQd40iCxNKfZIGq2ML-6r1i_k0iA7L_f6En1e9b5gLIs0GaluUwhCTTpAHBsmb-FquQER2Um9igARbMT6aaNrJzQbcPPWntQ3Pz_65PgjtTUMiFtBZW-YeAm2iB9JtYo_SJUsoX0d_oYo-6D4IfgZtkYKZZxbLj5rWnhoaJHm5acELAO8otQNZYQra0PfRIaKTT1vNkr1L5rShIxZk3yQ"
|
|
|
+ }
|
|
|
+ files_id = {}
|
|
|
+ for file in files:
|
|
|
+ file_path = os.path.join(reports_dir, file)
|
|
|
+ try:
|
|
|
+ with open(file_path, 'rb') as f:
|
|
|
+ files = {'file': (os.path.basename(file_path), f)}
|
|
|
+
|
|
|
+ response = requests.post(
|
|
|
+ base_url,
|
|
|
+ headers=headers,
|
|
|
+ cookies=cookies,
|
|
|
+ files=files,
|
|
|
+ verify=True
|
|
|
+ )
|
|
|
+
|
|
|
+ if response.json()["success"]:
|
|
|
+ file_id = response.json()["data"]["file_info"]["fileid"]
|
|
|
+ files_id[os.path.basename(file_path).split('.')[0]] = file_id
|
|
|
+ except requests.exceptions.RequestException as e:
|
|
|
+ print("请求出错:", e)
|
|
|
+ except Exception as e:
|
|
|
+ print("发生错误:", e)
|
|
|
|
|
|
+ files_id_str = ""
|
|
|
+ if files_id:
|
|
|
+ for filename, file_id in files_id.items():
|
|
|
+ files_id_str += f"{filename}, {file_id}\n"
|
|
|
+ else:
|
|
|
+ files_id_str = "failed"
|
|
|
+ with open(os.path.join(reports_dir, "files_id.txt"), 'w', encoding="utf-8") as file:
|
|
|
+ file.write(files_id_str)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
- uvicorn.run(app, host="0.0.0.0", port=7960)
|
|
|
+ uvicorn.run(app, host="0.0.0.0", port=7960)
|
|
|
+ # report_dir = "./data/reports/00000000000000000000000011445301/440298"
|
|
|
+ # upload_file(report_dir)
|