|
|
@@ -7,7 +7,7 @@ from models import Recommend
|
|
|
import os
|
|
|
from pydantic import BaseModel
|
|
|
import uvicorn
|
|
|
-from utils import ReportUtils
|
|
|
+from utils import ReportUtils, FileUploadUtils
|
|
|
import requests
|
|
|
from typing import List, Dict
|
|
|
|
|
|
@@ -35,7 +35,9 @@ class RecommendRequest(BaseModel):
|
|
|
city_uuid: str # 城市id
|
|
|
product_code: str # 卷烟编码
|
|
|
recall_cust_count: int # 推荐的商户数量
|
|
|
- delivery_count: int # 投放的品规数量
|
|
|
+ delivery_count: int # 投放的品规数量
|
|
|
+ cultivacation_id: str # 投放策略id
|
|
|
+ limit_cycle_name: str # 投放周期名称
|
|
|
|
|
|
class ReportRequest(BaseModel):
|
|
|
city_uuid: str # 城市id
|
|
|
@@ -77,74 +79,40 @@ async def recommend(request: RecommendRequest, backgroundTasks: BackgroundTasks)
|
|
|
|
|
|
# 异步执行报告生成任务
|
|
|
backgroundTasks.add_task(
|
|
|
- generate_report,
|
|
|
- request.city_uuid,
|
|
|
- request.product_code,
|
|
|
- request.recall_cust_count,
|
|
|
- request.delivery_count
|
|
|
+ generate_and_upload_report,
|
|
|
+ request
|
|
|
)
|
|
|
|
|
|
return {"code": 200, "msg": "success", "data": {"recommendationInfo": request_data}}
|
|
|
|
|
|
-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)
|
|
|
-
|
|
|
-def upload_file(reports_dir):
|
|
|
- """上传报告文件"""
|
|
|
- base_url = cfgs["aliyun"]["upload_url"]
|
|
|
- files = [
|
|
|
- "卷烟信息表.xlsx",
|
|
|
- "品规商户特征关系表.xlsx",
|
|
|
- "相似卷烟表.xlsx"
|
|
|
+def generate_and_upload_report(request: RecommendRequest):
|
|
|
+ """生成并上传报告到阿里云文件数据库"""
|
|
|
+ # 生成相关报告
|
|
|
+ report_util = ReportUtils(request.city_uuid, request.product_code)
|
|
|
+ report_util.generate_all_data(request.recall_cust_count, request.delivery_count)
|
|
|
+
|
|
|
+ # 上传报告
|
|
|
+ reports_dir = os.path.join('./data/reports', request.city_uuid, request.product_code)
|
|
|
+ report_files = [
|
|
|
+ '卷烟信息表',
|
|
|
+ '品规商户特征关系表',
|
|
|
+ '相似卷烟表',
|
|
|
+ '商户售卖推荐表'
|
|
|
]
|
|
|
- # 设置请求头
|
|
|
- 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": "*/*",
|
|
|
+ file_id_map = FileUploadUtils.upload_files(reports_dir, report_files)
|
|
|
+
|
|
|
+ # 将返回的file_id保存到数据库中
|
|
|
+ data_dict = {
|
|
|
+ 'cultivacation_id': request.cultivacation_id,
|
|
|
+ 'city_uuid': request.city_uuid,
|
|
|
+ 'limit_cycle_name': request.limit_cycle_name,
|
|
|
+ 'product_code': request.product_code,
|
|
|
+ 'product_info_table': file_id_map.get('卷烟信息表'),
|
|
|
+ 'relation_table': file_id_map.get('品规商户特征关系表'),
|
|
|
+ 'similarity_product_table': file_id_map.get('相似卷烟表'),
|
|
|
+ 'recommend_table': file_id_map.get('商户售卖推荐表'),
|
|
|
}
|
|
|
- # 设置Cookie
|
|
|
- cookies = {
|
|
|
- "expires_in": "10800000",
|
|
|
- "ecp_token": "ZXlKamIyUmxJam9pTVRKbU16bGxNMlV6TkdVeE1USmpNVFkwTVdVd09HUmpOVEV4TTJSaE5tRWlMQ0p6WTI5d1pTSTZJbkJ5YjNSbFkzUmxaQ0lzSW1Oc2FXVnVkQ0k2SW1Oc2FXVnVkRjlzYjI1bmFta2lmUT09",
|
|
|
- "acw_tc": "0a031e1717502370539856352e2b3bd4c277ffe39d26a06bff018dff60ea99",
|
|
|
- "isg": "BDAwNyXyI9cQD__-0PkIodjTAfiCeRTD-vUtYyqmIw3d5bGP0Y0nUayWOe2F3syb",
|
|
|
- "dd-ztna-token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIwNTAwMzQzMDM4MjYxNDMwOTQiLCJjb3JwSWQiOiJkaW5nYzc2YTJhMzdiMjRhODYwYTRhYzVkNjk4MDg2NGQzMzUiLCJkZXB0SWRzIjoiW1s4Mzk0NzIxNjAsODM2MjQ1MTUzLDFdXSIsInJvbGVJZHMiOiJbMjEwNDk3MDY1OF0iLCJ1bmlvbklkIjoiVXFyYTB4U0VQcWNxdFNhQTFqSzRzZ2lFaUUiLCJleHAiOjE3NTA4Mjg3ODksImlhdCI6MTc1MDIyMzk4OX0.ZR8MMaqiIszgBN5-evfpRXdy2Gmcn6Wk_U_1SQirGCek4m82cM1x8NzUqyBnIT9ApB5MJk_-aD5Q1e1LM74-3GkHNRDH9GxX4mN9flLdrjL0YzJDdOWAktZBatWPW7sW4rmq_yNC0fDJZ4j8N6ZObzo4Hvj08f7wCxqWrawrttgGoEYIBcKbMJBWyj0RBSOQdCfK3NS_-6AuTdc6reAircpd9l7SgJ-ln1lcOFImjr1x4NQz7yOy3GcvY3DGtkG6ZRz5kwsajLlxOr0At_xWHFxqdV8V_WIJNV1y725cd8WGoqyZPA9xTQnPvRtyCbud8DDZ3oojohNeV9sNW5BFtA"
|
|
|
- }
|
|
|
- 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,
|
|
|
- files=files,
|
|
|
- cookies=cookies,
|
|
|
- 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)
|
|
|
+ dao.insert_report(data_dict)
|
|
|
|
|
|
@app.post("/brandcultivation/api/v1/report")
|
|
|
async def get_file_id(request: ReportRequest):
|