简道云fastapi
This commit is contained in:
@@ -0,0 +1,304 @@
|
||||
"""
|
||||
删除相关后台任务模块
|
||||
包含删除历史维修记录、删除客户信息、删除客户车辆信息等功能
|
||||
"""
|
||||
import logging
|
||||
import traceback
|
||||
import requests
|
||||
import time
|
||||
from typing import Dict, Any, List
|
||||
from datetime import datetime
|
||||
from tqdm import tqdm
|
||||
from app.tasks.common import update_jiandaoyun, approve_workflow
|
||||
|
||||
logger = logging.getLogger('app')
|
||||
|
||||
|
||||
def delete_history_background(data: Dict[str, Any], cookies: Dict[str, str], org_id: str, org_name: str):
|
||||
"""
|
||||
删除历史维修数据后台运行函数
|
||||
:param data: 包含表单id、数据id等的字典
|
||||
:param cookies: 用户登录F6系统的cookies信息
|
||||
:param org_id: 需要删除历史维修记录的门店id
|
||||
:param org_name: 需要删除历史维修记录的门店名称
|
||||
:return: None
|
||||
"""
|
||||
url = f'https://yunxiu.f6car.cn/maintain-dump/maintainHistory/?orgid={org_id}' # 删除url
|
||||
res = requests.delete(url=url, cookies=cookies)
|
||||
res_data = res.json()
|
||||
|
||||
if res.status_code == 200 and res_data.get('code') == 200:
|
||||
results = f'{org_name} 历史维修记录已删除'
|
||||
print(results)
|
||||
logger.info(f"删除 {org_name} 历史维修记录成功")
|
||||
else:
|
||||
results = f'删除 {org_name} 历史维修记录失败: {res_data.get("message")}'
|
||||
print(results)
|
||||
logger.error(f"删除 {org_name} 历史维修记录失败: {res_data.get('message')}")
|
||||
|
||||
# 调用api回写改掉 执行明细与执行状态
|
||||
time.sleep(1)
|
||||
msg = update_jiandaoyun(data, f'{results}')
|
||||
|
||||
if msg.get('msg'):
|
||||
approve_workflow(data)
|
||||
print('表单已自动提交至下一步')
|
||||
|
||||
|
||||
def delete_customer_background(data: Dict[str, Any], cookies: Dict[str, str], json_data: List[Dict[str, Any]]):
|
||||
"""
|
||||
删除客户信息后台运行函数
|
||||
:param data: 包含表单id、数据id等字典
|
||||
:param cookies: 用户登录f6系统的cookies信息
|
||||
:param json_data: 获取到的客户信息列表,列表最大值取决url里面的值
|
||||
:return: None
|
||||
"""
|
||||
success = 0
|
||||
fail = 0
|
||||
|
||||
# 获取门店ID
|
||||
org_url = "https://yunxiu.f6car.cn/hive/org/getPageOrgGroupMembers?currentPage=1&pageSize=10&name="
|
||||
org_res = requests.get(url=org_url, cookies=cookies)
|
||||
|
||||
# 安全地获取门店ID
|
||||
org_data = org_res.json().get("data", {})
|
||||
org_list = org_data.get("list", [])
|
||||
|
||||
if not org_list or len(org_list) == 0:
|
||||
logger.error("未获取到门店信息")
|
||||
msg = update_jiandaoyun(data, '删除失败: 未获取到门店信息')
|
||||
if msg.get('msg'):
|
||||
approve_workflow(data)
|
||||
return
|
||||
|
||||
operate_org_id = org_list[0].get("orgId")
|
||||
if not operate_org_id:
|
||||
logger.error("门店ID为空")
|
||||
msg = update_jiandaoyun(data, '删除失败: 门店ID为空')
|
||||
if msg.get('msg'):
|
||||
approve_workflow(data)
|
||||
return
|
||||
|
||||
print(operate_org_id)
|
||||
# 获取会员卡列表
|
||||
card_url = f"https://yunxiu.f6car.cn/marketing/card/paging?useStationIdOwnOrgList={operate_org_id}&pageSize=100&pageNo=1"
|
||||
|
||||
card_res = requests.get(url=card_url, cookies=cookies)
|
||||
total_card = int(card_res.json().get("data").get("total"))
|
||||
print(total_card)
|
||||
total_page = total_card // 100 + (total_card % 100 > 0)
|
||||
card_list_customers = []
|
||||
for page in tqdm(range(1, total_page + 1), desc="查询会员卡"):
|
||||
card_url = (f"https://yunxiu.f6car.cn/marketing/card/paging?useStationIdOwnOrgList={operate_org_id}"
|
||||
f"&pageSize=100&pageNo={page}")
|
||||
|
||||
card_res = requests.get(url=card_url, cookies=cookies)
|
||||
card_cars_list = card_res.json().get("data").get("data")
|
||||
|
||||
for card_customer in card_cars_list:
|
||||
if card_customer.get("idCustomer") is None:
|
||||
continue
|
||||
else:
|
||||
card_list_customers.append(card_customer.get("idCustomer", None))
|
||||
time.sleep(0.2)
|
||||
|
||||
for item in tqdm(json_data, desc="删除客户"):
|
||||
id_customer = item['idCustomer']
|
||||
phone = item['cellPhone']
|
||||
consume_last_time = item['consumeLastTime']
|
||||
if consume_last_time:
|
||||
print(f'{id_customer}最近消费时间: {consume_last_time},跳过删除')
|
||||
logger.warning(f"{id_customer}最近消费时间: {consume_last_time},跳过删除")
|
||||
continue
|
||||
|
||||
if id_customer in card_list_customers:
|
||||
logger.info(f"{id_customer} 存在会员卡,跳过删除")
|
||||
fail += 1
|
||||
continue
|
||||
|
||||
try:
|
||||
url = f"https://yunxiu.f6car.cn/member/customer/{id_customer}" # 客户信息删除url
|
||||
res = requests.delete(url, cookies=cookies) # 客户信息删除
|
||||
res_data = res.json()
|
||||
if res_data.get('success'):
|
||||
success += 1
|
||||
logger.info(f"客户删除成功: ID={id_customer}, 手机号={phone}")
|
||||
else:
|
||||
fail += 1
|
||||
logger.error(f"客户删除失败: ID={id_customer}, 手机号={phone}, 错误信息: {res_data.get('message')}")
|
||||
time.sleep(0.2)
|
||||
except Exception as e:
|
||||
fail += 1
|
||||
print("删除失败,", item, id_customer, phone, e)
|
||||
logger.error(f"删除客户时发生错误: ID={id_customer}, 手机号={phone}, 错误信息: {e}")
|
||||
if success + fail < len(json_data):
|
||||
continue
|
||||
|
||||
now = datetime.now()
|
||||
if 20 <= now.hour <= 8:
|
||||
time.sleep(1)
|
||||
else:
|
||||
time.sleep(3)
|
||||
|
||||
logger.info(f"客户删除结果: 成功次数={success}, 失败次数={fail}")
|
||||
|
||||
msg = update_jiandaoyun(data, f'成功次数{success},失败次数{fail}')
|
||||
|
||||
if msg.get('msg'):
|
||||
approve_workflow(data)
|
||||
print('表单已自动提交至下一步')
|
||||
|
||||
|
||||
def delete_car_background(data: Dict[str, Any], url: str, cookies: Dict[str, str], header: Dict[str, Any],
|
||||
all_page: str):
|
||||
"""
|
||||
删除客户车辆信息后台运行函数
|
||||
:param header: 应包含账号登录的请求头
|
||||
:param url: 包含请求客户车辆信息的url
|
||||
:param all_page: 客户车辆信息的页数
|
||||
:param data: 包含表单id、数据id等的字典
|
||||
:param cookies: 登录F6系统后的请求信息
|
||||
:return: None
|
||||
"""
|
||||
print(cookies)
|
||||
success = 0
|
||||
fail = 0
|
||||
try:
|
||||
# 确保 all_page 是一个整数
|
||||
all_page = int(all_page)
|
||||
|
||||
# 获取门店ID
|
||||
org_url = "https://yunxiu.f6car.cn/hive/org/getPageOrgGroupMembers?currentPage=1&pageSize=10&name="
|
||||
org_res = requests.get(url=org_url, cookies=cookies)
|
||||
|
||||
# 安全地获取门店ID
|
||||
org_data = org_res.json().get("data", {})
|
||||
org_list = org_data.get("list", [])
|
||||
|
||||
if not org_list or len(org_list) == 0:
|
||||
logger.error("未获取到门店信息")
|
||||
msg = update_jiandaoyun(data, '删除失败: 未获取到门店信息')
|
||||
if msg.get('msg'):
|
||||
approve_workflow(data)
|
||||
return
|
||||
|
||||
operate_org_id = org_list[0].get("orgId")
|
||||
if not operate_org_id:
|
||||
logger.error("门店ID为空")
|
||||
msg = update_jiandaoyun(data, '删除失败: 门店ID为空')
|
||||
if msg.get('msg'):
|
||||
approve_workflow(data)
|
||||
return
|
||||
|
||||
print(operate_org_id)
|
||||
# 获取会员卡列表
|
||||
card_url = (
|
||||
f"https://yunxiu.f6car.cn/marketing/card/paging?useStationIdOwnOrgList={operate_org_id}&pageSize=100&pageNo=1"
|
||||
)
|
||||
card_res = requests.get(url=card_url, cookies=cookies)
|
||||
total_card = int(card_res.json().get("data").get("total"))
|
||||
print(total_card)
|
||||
total_page = total_card // 100 + (total_card % 100 > 0)
|
||||
card_list_cars = []
|
||||
for page in tqdm(range(1, total_page + 1), desc="查询会员卡"):
|
||||
card_url = (f"https://yunxiu.f6car.cn/marketing/card/paging?useStationIdOwnOrgList={operate_org_id}"
|
||||
f"&pageSize=100&pageNo={page}")
|
||||
card_res = requests.get(url=card_url, cookies=cookies)
|
||||
card_cars_list = card_res.json().get("data").get("data")
|
||||
|
||||
for card_car in card_cars_list:
|
||||
if card_car.get("cars") is None:
|
||||
continue
|
||||
for car in card_car.get("cars", []):
|
||||
card_list_cars.append(car.get("idCar", None))
|
||||
time.sleep(0.2)
|
||||
|
||||
itemlist = []
|
||||
# 使用 range() 创建一个可迭代的对象
|
||||
for page in range(1, all_page + 1):
|
||||
json_data = {
|
||||
"pageSize": 100,
|
||||
"pageNo": page
|
||||
}
|
||||
|
||||
# 获取当前页的数据
|
||||
res = requests.post(url=url, cookies=cookies, json=json_data, headers=header)
|
||||
res.raise_for_status() # 检查请求是否成功
|
||||
response_data = res.json()
|
||||
|
||||
if 'data' not in response_data or 'data' not in response_data['data']:
|
||||
print(f"警告: 页码 {page} 返回的数据格式不正确")
|
||||
continue
|
||||
|
||||
items = response_data['data']['data']
|
||||
for item in items:
|
||||
itemlist.append(item)
|
||||
|
||||
for item in tqdm(itemlist, desc="删除车辆信息"):
|
||||
car_id = item.get('tmCarInfo', {}).get('pkId')
|
||||
customer_id = item.get('tmCustomerInfo', {}).get('pkId')
|
||||
consume_last_time = item.get('tmCustomerInfo', {}).get('consumeLastTime')
|
||||
|
||||
if consume_last_time:
|
||||
logger.info(f"{customer_id}最近消费时间: {consume_last_time},跳过删除")
|
||||
fail += 1
|
||||
continue
|
||||
|
||||
if car_id in card_list_cars:
|
||||
logger.info(f"{customer_id} 存在会员卡,跳过删除")
|
||||
fail += 1
|
||||
continue
|
||||
|
||||
if not car_id or not customer_id:
|
||||
logger.info(f"页码 {page} 中缺少必要的ID信息")
|
||||
fail += 1
|
||||
continue
|
||||
|
||||
try:
|
||||
delete_url = (
|
||||
f"https://yunxiu.f6car.cn/member/car/deleteCar/{car_id}/{customer_id}"
|
||||
)
|
||||
delete_res = requests.delete(delete_url, cookies=cookies)
|
||||
delete_res.raise_for_status() # 检查删除请求是否成功
|
||||
delete_data = delete_res.json()
|
||||
|
||||
if delete_data.get('data'):
|
||||
success += 1
|
||||
else:
|
||||
fail += 1
|
||||
logger.error(
|
||||
f"删除失败: 页码 {page}, 车辆ID {car_id}, 客户ID {customer_id},"
|
||||
f" 错误信息: {delete_data.get('message', '未知错误')}")
|
||||
print(
|
||||
f"删除失败: 页码 {page}, 车辆ID {car_id}, 客户ID {customer_id},"
|
||||
f" 错误信息: {delete_data.get('message', '未知错误')}")
|
||||
|
||||
time.sleep(0.2) # 避免过快请求
|
||||
except requests.exceptions.RequestException as e:
|
||||
fail += 1
|
||||
print(f"删除失败: 页码 {page}, 车辆ID {car_id}, 客户ID {customer_id}, 错误信息: {e}")
|
||||
logger.error(f"删除失败: 页码 {page}, 车辆ID {car_id}, 客户ID {customer_id}, 错误信息: {e}")
|
||||
continue
|
||||
|
||||
now = datetime.now()
|
||||
if 20 <= now.hour <= 8:
|
||||
time.sleep(1)
|
||||
else:
|
||||
time.sleep(3)
|
||||
|
||||
print(f"完成: 成功删除 {success} 辆车, 失败 {fail} 辆车")
|
||||
logger.info(f"完成: 成功删除 {success} 辆车, 失败 {fail} 辆车")
|
||||
|
||||
except ValueError as e:
|
||||
print(f"Error converting all_page to integer: {e}")
|
||||
traceback.print_exc() # 打印堆栈跟踪信息
|
||||
except Exception as e:
|
||||
print(f"An unexpected error occurred: {e}")
|
||||
traceback.print_exc() # 打印堆栈跟踪信息
|
||||
|
||||
msg = update_jiandaoyun(data, f'成功次数{success},失败次数{fail}')
|
||||
|
||||
if msg.get('msg'):
|
||||
approve_workflow(data)
|
||||
print('表单已自动提交至下一步')
|
||||
|
||||
Reference in New Issue
Block a user