1.客户信息修改,将硬编码改为动态取值
2.新增项目批量停用、材料批量修改功能
This commit is contained in:
@@ -29,6 +29,11 @@ from app.tasks.customer_tasks import modify_customer_info_background
|
||||
# BI相关任务
|
||||
from app.tasks.bi_tasks import bi_task_background
|
||||
|
||||
from app.tasks.material_tasks import ( \
|
||||
batch_modify_materials,
|
||||
batch_disable_projects
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
# 通用功能
|
||||
'update_jiandaoyun',
|
||||
@@ -43,5 +48,7 @@ __all__ = [
|
||||
'modify_customer_info_background',
|
||||
# BI任务
|
||||
'bi_task_background',
|
||||
# 项目材料任务
|
||||
'batch_disable_projects',
|
||||
'batch_modify_materials',
|
||||
]
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
BI相关后台任务模块
|
||||
|
||||
本模块包含BI相关的后台任务,包括:
|
||||
- BI数据处理
|
||||
- BI报表生成
|
||||
- TODO BI数据处理
|
||||
|
||||
|
||||
这些任务在后台线程中执行,不会阻塞主请求。
|
||||
执行完成后会更新简道云表单并自动提交工作流。
|
||||
|
||||
+2
-3
@@ -137,8 +137,6 @@ def execute_failure_handler(data: Dict[str, Any]):
|
||||
api_instance.data_batch_create(pay_load)
|
||||
|
||||
|
||||
|
||||
|
||||
def get_operate_org_id(cookies: Dict[str, str]) -> Optional[str]:
|
||||
"""
|
||||
获取操作门店ID
|
||||
@@ -154,7 +152,7 @@ def get_operate_org_id(cookies: Dict[str, str]) -> Optional[str]:
|
||||
注意:
|
||||
如果未获取到门店信息或门店ID为空,会记录错误日志并返回 None
|
||||
"""
|
||||
org_url = "https://yunxiu.f6car.cn/hive/org/getPageOrgGroupMembers?currentPage=1&pageSize=10&name="
|
||||
org_url = "https://yunxiu.f6car.cn/hive/org/getPageOrgGroupMembers?currentPage=1&pageSize=100&name="
|
||||
|
||||
try:
|
||||
org_res = requests.get(url=org_url, cookies=cookies)
|
||||
@@ -243,3 +241,4 @@ def get_card_list(
|
||||
logger.error(f"获取会员卡列表时发生错误: {e}")
|
||||
return card_list
|
||||
|
||||
|
||||
|
||||
+165
-229
@@ -10,7 +10,7 @@ import logging
|
||||
import requests
|
||||
import pandas as pd
|
||||
import time
|
||||
import re
|
||||
import os
|
||||
from typing import Dict, Any, Optional
|
||||
from app.tasks.common import update_jiandaoyun, approve_workflow
|
||||
|
||||
@@ -20,257 +20,193 @@ logger = logging.getLogger('app')
|
||||
def modify_customer_info_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
|
||||
|
||||
|
||||
注意:
|
||||
- 根据客户手机号匹配客户信息
|
||||
- 执行完成后会自动删除上传的文件
|
||||
- 执行结果会更新到简道云表单
|
||||
"""
|
||||
df.where(pd.notnull(df), None)
|
||||
try:
|
||||
# 替换 NaN 为 None,便于后续判断
|
||||
df = df.where(pd.notnull(df), None)
|
||||
|
||||
logger.info("获取当前客户下所有客户信息")
|
||||
logger.info("开始获取当前客户下所有客户信息")
|
||||
|
||||
params = {
|
||||
'pageSize': 100,
|
||||
'pageNo': '1',
|
||||
}
|
||||
|
||||
res = requests.get(
|
||||
'https://yunxiu.f6car.cn/member/customer/listForPermission',
|
||||
params=params,
|
||||
cookies=cookies,
|
||||
)
|
||||
|
||||
total = int(res.json().get("data").get("total"))
|
||||
total_pages = (total // params["pageSize"]) + (1 if total % params["pageSize"] > 0 else 0)
|
||||
print(f"总计{total_pages}页")
|
||||
|
||||
all_customers = []
|
||||
max_retries = 10
|
||||
retry_count = 0
|
||||
for page in range(1, total_pages + 1):
|
||||
print(f"正在请求第 {page} 页...")
|
||||
params["pageNo"] = page
|
||||
|
||||
while retry_count < max_retries:
|
||||
response = requests.get(
|
||||
'https://yunxiu.f6car.cn/member/customer/listForPermission',
|
||||
params=params,
|
||||
cookies=cookies,
|
||||
timeout=10
|
||||
)
|
||||
time.sleep(1)
|
||||
if response.status_code == 200:
|
||||
suppliers = response.json().get("data", {}).get("data", [])
|
||||
all_customers.extend(suppliers)
|
||||
break
|
||||
else:
|
||||
retry_count += 1
|
||||
print(f"请求第 {page} 页失败,正在重试(第 {retry_count} 次)...")
|
||||
time.sleep(3)
|
||||
|
||||
# 获取专属运营顾问列表
|
||||
json_data = {
|
||||
'includeStopedEmployee': False,
|
||||
'pageSize': 1000,
|
||||
'filterNullUser': False,
|
||||
'keyword': '',
|
||||
'idOwnOrgList': [],
|
||||
}
|
||||
|
||||
response = requests.post(
|
||||
'https://yunxiu.f6car.cn/hive/employee/searchStaffInGroup',
|
||||
cookies=cookies,
|
||||
json=json_data,
|
||||
)
|
||||
|
||||
staff_list = response.json().get("data").get("list")
|
||||
name_to_userid = {
|
||||
emp['name']: emp['userId']
|
||||
for emp in staff_list
|
||||
if emp['userId'] is not None
|
||||
}
|
||||
df['userId'] = df['专属运营顾问'].map(name_to_userid)
|
||||
|
||||
def extract_province_city_district(address: Optional[str]) -> Dict[str, Optional[str]]:
|
||||
"""安全解析省市区信息,所有返回值都可能为None"""
|
||||
if not address:
|
||||
return {'省': None, '市': None, '区': None}
|
||||
|
||||
try:
|
||||
pattern = r'(?P<省>(?:[\u4e00-\u9fa5]+(?:省|自治区|特别行政区))?)' \
|
||||
r'(?P<市>(?:[\u4e00-\u9fa5]+(?:市|自治州|地区|盟))?)' \
|
||||
r'(?P<区>(?:[\u4e00-\u9fa5]+区|[\u4e00-\u9fa5]+县|[\u4e00-\u9fa5]+旗)?)'
|
||||
match = re.match(pattern, address.strip())
|
||||
return {k: v if v else None for k, v in match.groupdict().items()} if match else {'省': None, '市': None,
|
||||
'区': None}
|
||||
except Exception:
|
||||
return {'省': None, '市': None, '区': None}
|
||||
|
||||
def safe_get(d: Optional[Dict], *keys, default=None):
|
||||
"""多层字典安全获取值,始终返回可能为None的值"""
|
||||
if not isinstance(d, dict):
|
||||
return default
|
||||
|
||||
for key in keys:
|
||||
d = d.get(key, {})
|
||||
if not isinstance(d, dict):
|
||||
break
|
||||
return d if d != {} else default
|
||||
|
||||
def convert_to_request_data(original_data: Optional[Dict[str, Any]], df: pd.DataFrame) -> Dict[str, Any]:
|
||||
"""
|
||||
完全安全的字典转换函数
|
||||
特点:
|
||||
1. 每个字段的值都可能为None
|
||||
2. 不会因为任何字段为空而报错
|
||||
3. 不使用任何默认值,完全保留原始数据的空值状态
|
||||
"""
|
||||
customer_info = safe_get(original_data, 'data', 'customerInfo') if original_data else None
|
||||
|
||||
address_parts = extract_province_city_district(
|
||||
safe_get(customer_info, 'provinceCityAreaName') if customer_info else None
|
||||
# 分页获取全部客户
|
||||
params = {'pageSize': 100, 'pageNo': '1'}
|
||||
res = requests.get(
|
||||
'https://yunxiu.f6car.cn/member/customer/listForPermission',
|
||||
params=params,
|
||||
cookies=cookies,
|
||||
timeout=10
|
||||
)
|
||||
res.raise_for_status()
|
||||
total = int(res.json().get("data", {}).get("total", 0))
|
||||
total_pages = (total // params["pageSize"]) + (1 if total % params["pageSize"] > 0 else 0)
|
||||
logger.info(f"总计 {total_pages} 页,共 {total} 个客户")
|
||||
|
||||
cell_phone = safe_get(customer_info, 'cellPhone')
|
||||
all_customers = []
|
||||
for page in range(1, total_pages + 1):
|
||||
logger.debug(f"正在请求第 {page} 页...")
|
||||
params["pageNo"] = page
|
||||
retry_count = 0
|
||||
max_retries = 5
|
||||
while retry_count < max_retries:
|
||||
try:
|
||||
response = requests.get(
|
||||
'https://yunxiu.f6car.cn/member/customer/listForPermission',
|
||||
params=params,
|
||||
cookies=cookies,
|
||||
timeout=10
|
||||
)
|
||||
response.raise_for_status()
|
||||
page_data = response.json().get("data", {}).get("data", [])
|
||||
all_customers.extend(page_data)
|
||||
break
|
||||
except Exception as e:
|
||||
retry_count += 1
|
||||
logger.warning(f"请求第 {page} 页失败(第 {retry_count} 次重试): {e}")
|
||||
time.sleep(3)
|
||||
else:
|
||||
logger.error(f"第 {page} 页请求失败超过最大重试次数,跳过")
|
||||
|
||||
exclusive_info = None
|
||||
df_row = None
|
||||
if cell_phone and not df.empty:
|
||||
matched_rows = df[df['客户手机号'] == cell_phone]
|
||||
if not matched_rows.empty:
|
||||
df_row = matched_rows.iloc[0]
|
||||
exclusive_info = {
|
||||
'userId': df_row.get('userId'),
|
||||
'name': df_row.get('专属运营顾问')
|
||||
}
|
||||
|
||||
request_data = {
|
||||
"pkId": safe_get(customer_info, 'idCustomer'),
|
||||
"idCustomer": safe_get(customer_info, 'idCustomer'),
|
||||
"name": df_row.get('客户姓名') if df_row is not None and pd.notna(df_row.get('客户姓名')) else safe_get(
|
||||
customer_info, 'name'),
|
||||
"sex": safe_get(customer_info, 'sex'),
|
||||
"customerType": df_row.get('客户类型') if df_row is not None and pd.notna(
|
||||
df_row.get('客户类型')) else safe_get(
|
||||
customer_info, 'customerType'),
|
||||
"customerSource": safe_get(customer_info, 'customerSource'),
|
||||
"customerSourceName": df_row.get('客户来源') if df_row is not None and pd.notna(
|
||||
df_row.get('客户来源')) else safe_get(customer_info, 'customerSourceName'),
|
||||
"companyName": df_row.get('单位名称') if df_row is not None and pd.notna(
|
||||
df_row.get('单位名称')) else safe_get(
|
||||
customer_info, 'companyName'),
|
||||
"cellPhone": cell_phone,
|
||||
"wechart": safe_get(customer_info, 'wechart'),
|
||||
"qq": safe_get(customer_info, 'qq'),
|
||||
"contacts": safe_get(customer_info, 'contacts'),
|
||||
"contactTelephone": safe_get(customer_info, 'contactTelephone'),
|
||||
"province": safe_get(customer_info, 'province'),
|
||||
"city": safe_get(customer_info, 'city'),
|
||||
"area": safe_get(customer_info, 'area'),
|
||||
"street": safe_get(customer_info, 'street'),
|
||||
"address": safe_get(customer_info, 'address'),
|
||||
"detailAddress": safe_get(customer_info, 'detailAddress'),
|
||||
"pId": safe_get(customer_info, 'province'),
|
||||
"cId": safe_get(customer_info, 'city'),
|
||||
"aId": safe_get(customer_info, 'area'),
|
||||
"provinceName": address_parts.get('省'),
|
||||
"cityName": address_parts.get('市'),
|
||||
"areaName": address_parts.get('区'),
|
||||
"provinceCityAreaName": safe_get(customer_info, 'provinceCityAreaName'),
|
||||
"birthday": safe_get(customer_info, 'birthday'),
|
||||
"creationtime": safe_get(customer_info, 'creationtime'),
|
||||
"modifiedtime": safe_get(customer_info, 'modifiedtime'),
|
||||
"creator": safe_get(customer_info, 'creator'),
|
||||
"creatorName": safe_get(customer_info, 'creatorName'),
|
||||
"modifier": safe_get(customer_info, 'modifier'),
|
||||
"idOwnOrg": safe_get(customer_info, 'idOwnOrg'),
|
||||
"idOwnGroup": safe_get(customer_info, 'idOwnGroup'),
|
||||
"insuranceCompany": safe_get(customer_info, 'insuranceCompany'),
|
||||
"maritalStatus": safe_get(customer_info, 'maritalStatus'),
|
||||
"monthlyIncome": safe_get(customer_info, 'monthlyIncome'),
|
||||
"idNumber": safe_get(customer_info, 'idNumber'),
|
||||
"personHobby": safe_get(customer_info, 'personHobby'),
|
||||
"credentialsType": safe_get(customer_info, 'credentialsType'),
|
||||
"points": safe_get(customer_info, 'points'),
|
||||
"maxAccountAmount": safe_get(customer_info, 'maxAccountAmount'),
|
||||
"pointsEnable": safe_get(customer_info, 'pointsEnable'),
|
||||
"level": safe_get(customer_info, 'level'),
|
||||
"memberCardNo": safe_get(customer_info, 'memberCardNo'),
|
||||
"customerLevel": safe_get(customer_info, 'customerLevel'),
|
||||
"exclusiveConsultantId": exclusive_info['userId'] if exclusive_info else safe_get(customer_info,
|
||||
'exclusiveConsultantId'),
|
||||
"exclusiveConsultantName": exclusive_info['name'] if exclusive_info else safe_get(customer_info,
|
||||
'exclusiveConsultantName'),
|
||||
"exclusiveOrgId": safe_get(customer_info, 'exclusiveOrgId'),
|
||||
"exclusiveOrgName": safe_get(customer_info, 'exclusiveOrgName'),
|
||||
"customerMemo": df_row.get('客户备注') if df_row is not None and pd.notna(
|
||||
df_row.get('客户备注')) else safe_get(
|
||||
customer_info, 'customerMemo'),
|
||||
"isDel": safe_get(customer_info, 'isDel'),
|
||||
"idFrom": safe_get(customer_info, 'idFrom'),
|
||||
"mnemonic": safe_get(customer_info, 'mnemonic'),
|
||||
"idOrgSource": safe_get(customer_info, 'idOrgSource'),
|
||||
"firstArrivalIdSourceBill": safe_get(customer_info, 'firstArrivalIdSourceBill'),
|
||||
"lastArrivalIdSourceBill": safe_get(customer_info, 'lastArrivalIdSourceBill'),
|
||||
"customerInfoType": safe_get(customer_info, 'customerInfoType'),
|
||||
"customerInfoCompleteDate": safe_get(customer_info, 'customerInfoCompleteDate'),
|
||||
"customerInfoCompleteType": safe_get(customer_info, 'customerInfoCompleteType'),
|
||||
"xczUserId": safe_get(customer_info, 'xczUserId'),
|
||||
"xczUuid": safe_get(customer_info, 'xczUuid'),
|
||||
"idWxbCustomer": safe_get(customer_info, 'idWxbCustomer'),
|
||||
"promoteEmployeeId": safe_get(customer_info, 'promoteEmployeeId'),
|
||||
"promoteEmployeeName": safe_get(customer_info, 'promoteEmployeeName'),
|
||||
"promoteMemberId": safe_get(customer_info, 'promoteMemberId'),
|
||||
"promoteMemberName": safe_get(customer_info, 'promoteMemberName'),
|
||||
"driverExpiryDate": safe_get(customer_info, 'driverExpiryDate'),
|
||||
"crmDeleteExclusiveFlag": safe_get(customer_info, 'crmDeleteExclusiveFlag'),
|
||||
"totalObtainPoints": safe_get(customer_info, 'totalObtainPoints'),
|
||||
"totalUsedPoints": safe_get(customer_info, 'totalUsedPoints'),
|
||||
"orgName": safe_get(customer_info, 'orgName'),
|
||||
"weChatFollower": safe_get(customer_info, 'weChatFollower'),
|
||||
"pointsEnableConfig": safe_get(customer_info, 'pointsEnableConfig'),
|
||||
"personalPointsEnableConfig": safe_get(customer_info, 'personalPointsEnableConfig'),
|
||||
"pointsButtonStatus": safe_get(customer_info, 'pointsButtonStatus'),
|
||||
"tmallInstallMember": safe_get(customer_info, 'tmallInstallMember'),
|
||||
"corpId": safe_get(customer_info, 'corpId'),
|
||||
"thirdCorpId": safe_get(customer_info, 'thirdCorpId'),
|
||||
# 获取专属运营顾问列表
|
||||
json_data = {
|
||||
'includeStopedEmployee': False,
|
||||
'pageSize': 1000,
|
||||
'filterNullUser': False,
|
||||
'keyword': '',
|
||||
'idOwnOrgList': [],
|
||||
}
|
||||
staff_resp = requests.post(
|
||||
'https://yunxiu.f6car.cn/hive/employee/searchStaffInGroup',
|
||||
cookies=cookies,
|
||||
json=json_data,
|
||||
timeout=10
|
||||
)
|
||||
staff_resp.raise_for_status()
|
||||
staff_list = staff_resp.json().get("data", {}).get("list", [])
|
||||
name_to_userid = {
|
||||
emp['name']: emp['userId']
|
||||
for emp in staff_list
|
||||
if emp.get('userId') is not None
|
||||
}
|
||||
|
||||
return request_data
|
||||
# 在 df 中添加 userId 列
|
||||
df['userId'] = df['专属运营顾问'].map(name_to_userid)
|
||||
|
||||
for customer in all_customers:
|
||||
phone = customer.get("cellPhone")
|
||||
if phone in df["客户手机号"].tolist():
|
||||
print("开始修改")
|
||||
cus_id = customer.get("idCustomer", {})
|
||||
cus_response = requests.get(f'https://yunxiu.f6car.cn/member/customer/{cus_id}', cookies=cookies)
|
||||
original_data = cus_response.json()
|
||||
final_json_data = convert_to_request_data(original_data, df)
|
||||
response = requests.post(
|
||||
'https://yunxiu.f6car.cn/member/customer/modifyCustomer',
|
||||
cookies=cookies,
|
||||
json=final_json_data,
|
||||
)
|
||||
print("修改完成")
|
||||
# 字段映射:Excel 列名 -> F6 字段名
|
||||
FIELD_MAPPING = {
|
||||
'客户姓名': 'name',
|
||||
'客户类型': 'customerType',
|
||||
'客户来源': 'customerSourceName',
|
||||
'单位名称': 'companyName',
|
||||
'客户备注': 'customerMemo',
|
||||
'专属运营顾问': 'exclusiveConsultantName', # userId 单独处理
|
||||
}
|
||||
|
||||
time.sleep(1)
|
||||
def convert_to_request_data(original_data: dict, df_row: pd.Series) -> dict:
|
||||
"""以原始客户数据为基础,仅覆盖 Excel 中非空字段"""
|
||||
customer_info = original_data.get("data", {}).get("customerInfo", {}) or {}
|
||||
request_data = dict(customer_info) # 浅拷贝,足够用
|
||||
|
||||
msg = update_jiandaoyun(data, f'修改完成')
|
||||
# 覆盖指定字段
|
||||
for excel_col, f6_field in FIELD_MAPPING.items():
|
||||
value = df_row.get(excel_col)
|
||||
if pd.notna(value):
|
||||
request_data[f6_field] = str(value).strip() if isinstance(value, str) else value
|
||||
|
||||
if msg.get('msg'):
|
||||
approve_workflow(data)
|
||||
print('表单已自动提交至下一步')
|
||||
# 处理专属顾问 ID
|
||||
if pd.notna(df_row.get('userId')):
|
||||
request_data['exclusiveConsultantId'] = df_row['userId']
|
||||
|
||||
# 确保必要字段
|
||||
if 'idCustomer' in customer_info:
|
||||
request_data['pkId'] = customer_info['idCustomer']
|
||||
request_data['idCustomer'] = customer_info['idCustomer']
|
||||
|
||||
return request_data
|
||||
|
||||
# 执行批量更新
|
||||
updated_count = 0
|
||||
for customer in all_customers:
|
||||
phone = customer.get("cellPhone")
|
||||
if not phone:
|
||||
continue
|
||||
|
||||
matched_rows = df[df['客户手机号'] == phone]
|
||||
if matched_rows.empty:
|
||||
continue
|
||||
|
||||
df_row = matched_rows.iloc[0]
|
||||
cus_id = customer.get("idCustomer")
|
||||
if not cus_id:
|
||||
logger.warning(f"客户手机号 {phone} 缺少 idCustomer,跳过")
|
||||
continue
|
||||
|
||||
try:
|
||||
# 获取完整客户信息
|
||||
detail_resp = requests.get(
|
||||
f'https://yunxiu.f6car.cn/member/customer/{cus_id}',
|
||||
cookies=cookies,
|
||||
timeout=10
|
||||
)
|
||||
detail_resp.raise_for_status()
|
||||
original_data = detail_resp.json()
|
||||
|
||||
# 构造更新数据
|
||||
final_json_data = convert_to_request_data(original_data, df_row)
|
||||
|
||||
# 发送更新
|
||||
update_resp = requests.post(
|
||||
'https://yunxiu.f6car.cn/member/customer/modifyCustomer',
|
||||
cookies=cookies,
|
||||
json=final_json_data,
|
||||
timeout=10
|
||||
)
|
||||
|
||||
if update_resp.status_code == 200 and update_resp.json().get('success'):
|
||||
logger.info(f"✅ 客户 {phone} 更新成功")
|
||||
updated_count += 1
|
||||
else:
|
||||
logger.error(f"❌ 客户 {phone} 更新失败: {update_resp.text}")
|
||||
|
||||
time.sleep(1) # 避免触发限流
|
||||
|
||||
except Exception as e:
|
||||
logger.exception(f"处理客户 {phone} 时发生异常: {e}")
|
||||
|
||||
# 更新简道云状态
|
||||
msg = update_jiandaoyun(data, f'批量修改完成,共更新 {updated_count} 个客户')
|
||||
if msg.get('msg'):
|
||||
approve_workflow(data)
|
||||
logger.info('✅ 表单已自动提交至下一步')
|
||||
|
||||
except Exception as e:
|
||||
logger.exception("modify_customer_info_background 执行出错:")
|
||||
# 即使出错也尝试通知简道云
|
||||
try:
|
||||
update_jiandaoyun(data, f'执行失败: {str(e)[:200]}')
|
||||
except:
|
||||
pass
|
||||
|
||||
finally:
|
||||
# 清理临时文件
|
||||
try:
|
||||
if os.path.exists(save_path):
|
||||
os.remove(save_path)
|
||||
logger.info(f"临时文件已删除: {save_path}")
|
||||
except Exception as e:
|
||||
logger.warning(f"删除临时文件失败: {e}")
|
||||
Reference in New Issue
Block a user