Files
jdy_fastapi/app/tasks/brand_tasks.py
T
2025-11-07 17:48:49 +08:00

56 lines
2.0 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):
"""
品牌批量创建后台运行函数
:param data: 包含表单id、数据id等的字典
:param cookies: 用户登录f6系统的cookies信息
:param df: 表格读取到的内容,DataFrame格式
:param save_path: 文件保存的地址
:return: 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('表单已自动提交至下一步')