72 lines
2.6 KiB
Python
72 lines
2.6 KiB
Python
"""
|
|
品牌相关后台任务模块
|
|
|
|
本模块包含品牌相关的后台任务,包括:
|
|
- 品牌批量创建
|
|
|
|
这些任务在后台线程中执行,不会阻塞主请求。
|
|
"""
|
|
import logging
|
|
import os
|
|
import requests
|
|
import pandas as pd
|
|
from typing import Dict, Any
|
|
from tqdm import tqdm
|
|
from app.tasks.common import update_jiandaoyun, approve_workflow
|
|
|
|
logger = logging.getLogger('app')
|
|
|
|
|
|
def create_brand_background(data: Dict[str, Any], cookies: Dict[str, str], df: pd.DataFrame, save_path: str):
|
|
"""
|
|
品牌批量创建后台任务
|
|
|
|
在后台线程中批量创建品牌,从 Excel 文件中读取品牌名称并创建。
|
|
执行完成后会更新简道云表单并自动提交工作流。
|
|
|
|
Args:
|
|
data: 包含表单ID(api_key)、表单ID(entry_id)、数据ID(data_id)的字典
|
|
cookies: 用户登录 F6 系统的 cookies 信息
|
|
df: Excel 文件读取的内容,DataFrame 格式,第一列为品牌名称
|
|
save_path: Excel 文件保存的地址,执行完成后会删除此文件
|
|
|
|
Returns:
|
|
None
|
|
|
|
注意:
|
|
- 无效的品牌名(None、空字符串)会被跳过
|
|
- 执行完成后会自动删除上传的文件
|
|
- 执行结果会更新到简道云表单
|
|
"""
|
|
df = df.where(pd.notnull(df), None)
|
|
# 定义请求URL
|
|
url = 'https://yunxiu.f6car.cn/camaro/brand/getOrCreate'
|
|
# 遍历DataFrame中的每一行数据
|
|
results = []
|
|
for index, row in tqdm(df.iterrows(), total=df.shape[0], desc="创建品牌"):
|
|
brand_name = row[df.columns[0]]
|
|
|
|
if brand_name is None or pd.isna(brand_name) or str(brand_name).strip() == '':
|
|
results.append({f'{brand_name}': '无效品牌名', '状态': '跳过'})
|
|
logger.warning(f"跳过无效品牌名: {brand_name}")
|
|
continue
|
|
|
|
try:
|
|
response = requests.post(url, cookies=cookies, json={"brandName": brand_name})
|
|
response.raise_for_status() # 抛出HTTP错误
|
|
results.append({'品牌名': brand_name, '状态': '创建成功'})
|
|
except requests.exceptions.RequestException as e:
|
|
results.append({'品牌名': brand_name, '状态': f'创建失败: {str(e)}'})
|
|
print({'msg': '已执行', 'msg_details': f'{results}'})
|
|
logger.info(f"品牌创建结果: {results}")
|
|
os.remove(save_path)
|
|
print(f'{save_path}已删除')
|
|
print(data)
|
|
# 调用api回写改掉 执行明细与执行状态
|
|
msg = update_jiandaoyun(data, f'{results}')
|
|
|
|
if msg.get('msg'):
|
|
approve_workflow(data)
|
|
print('表单已自动提交至下一步')
|
|
|