212 lines
12 KiB
Plaintext
212 lines
12 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"metadata": {},
|
|
"cell_type": "markdown",
|
|
"source": "## 履约表数据同步",
|
|
"id": "38f4d6345e9674ce"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"id": "initial_id",
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"ExecuteTime": {
|
|
"end_time": "2025-07-01T07:04:58.218775Z",
|
|
"start_time": "2025-07-01T07:04:58.156246Z"
|
|
}
|
|
},
|
|
"source": [
|
|
"# -*- coding: utf-8 -*-\n",
|
|
"import pandas as pd\n",
|
|
"import datetime\n",
|
|
"from config import Config\n",
|
|
"from api import API\n",
|
|
"import pymysql # 使用 pymysql 替代 mysql.connector\n",
|
|
"from back_ground_module import CommonModule\n",
|
|
"\n",
|
|
"start_time = datetime.datetime.now()\n",
|
|
"api_instance = API()\n",
|
|
"common_module = CommonModule()\n",
|
|
"\n",
|
|
"\n",
|
|
"class importPerforamnceData:\n",
|
|
" \"\"\"\n",
|
|
" 履约表数据支撑\n",
|
|
" \"\"\"\n",
|
|
" def __init__(self):\n",
|
|
" self.staff_id_list = None\n",
|
|
" self.performance_data_list = None\n",
|
|
" self.field_mapping = {}\n",
|
|
" self.fields()\n",
|
|
" \n",
|
|
" def load_all_data(self):\n",
|
|
" \"\"\"加载所有数据\"\"\"\n",
|
|
" payload = {\"api_key\": \"675b900991ad2491c69389ca\",\n",
|
|
" \"entry_id\": \"68637c9818bc333fc14c30ad\",# 需要修改\n",
|
|
" }\n",
|
|
" performance_data = api_instance.entry_data_list(payload)\n",
|
|
" self.performance_data_list = performance_data # 履约表\n",
|
|
"\n",
|
|
" # 获取简道云员工id\n",
|
|
" payload = {\"api_key\": \"6694d3c4fcb69ca9a111a6c4\",\n",
|
|
" \"entry_id\": \"6769204a1902c9341340a1bc\",\n",
|
|
" }\n",
|
|
" staff_id = api_instance.entry_data_list(payload)\n",
|
|
" self.staff_id_list = staff_id.get(\"data\") # api请求格式,将数据封装在data字典里\n",
|
|
" print(self.staff_id_list)\n",
|
|
"\n",
|
|
" @staticmethod\n",
|
|
" def get_staff_id(row_item, name):\n",
|
|
" \"\"\"辅助函数,用于获取员工ID\"\"\"\n",
|
|
" if str(row_item[\"_widget_1734942794144\"]) == str(name): # 检查姓名是否匹配\n",
|
|
" return row_item[\"_widget_1734942794145\"] # 返回员工ID\n",
|
|
" return None\n",
|
|
"\n",
|
|
"\n",
|
|
" def main(self):\n",
|
|
" task_start_time = datetime.datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\")\n",
|
|
" # Step1:获取履约表数据\n",
|
|
" # df = common_module.get_perforamnce_details()\n",
|
|
" # print(\"数据获取完成\")\n",
|
|
" \n",
|
|
" # Step2:清空现有数据\n",
|
|
" print(self.performance_data_list)\n",
|
|
" id_list = [item[\"_id\"] for item in self.performance_data_list]\n",
|
|
" \n",
|
|
" delete_payload ={\n",
|
|
" \"api_key\": \"675b900991ad2491c69389ca\",\n",
|
|
" \"entry_id\": \"68637c9818bc333fc14c30ad\",\n",
|
|
" \"data_ids\": id_list\n",
|
|
" }\n",
|
|
" api_instance.entry_data_batch_delete(delete_payload)\n",
|
|
" print(\"数据删除完成\")\n",
|
|
"\n",
|
|
" # Step3:将数据写入简道云中\n",
|
|
" # 日期改为utc\n",
|
|
" time_columns = ['saas开户时间', '服务期起始时间', '下单支付成功时间', '操作时间', \"下单支付成功日期\",\n",
|
|
" \"服务期结束时间\"]\n",
|
|
" \n",
|
|
" new_df = df.copy() # 复制df,以调整时间\n",
|
|
" for col in time_columns:\n",
|
|
" # 1. 转换为datetime类型(带错误处理)\n",
|
|
" # 使用.loc安全赋值\n",
|
|
" new_df[col] = pd.to_datetime(new_df[col], errors='coerce', utc=False)\n",
|
|
"\n",
|
|
" # 2. 优化后的时区转换(高效向量化操作)\n",
|
|
" new_df[col + '_date'] = (\n",
|
|
" new_df[col]\n",
|
|
" # 本地化为北京时间(东八区)\n",
|
|
" .dt.tz_localize('Asia/Shanghai', ambiguous='infer', nonexistent='NaT')\n",
|
|
" # 转换为UTC时区\n",
|
|
" .dt.tz_convert('UTC')\n",
|
|
" # 格式化为ISO8601字符串\n",
|
|
" .dt.strftime('%Y-%m-%dT%H:%M:%SZ')\n",
|
|
" )\n",
|
|
" all_data = []\n",
|
|
" for row in new_df.iterrows():\n",
|
|
" # 成员字段替换\n",
|
|
" NGV_roles = {\n",
|
|
" '运营负责人': df[\"运营负责人\"], # 运营负责人\n",
|
|
" '区域经理': df[\"区域经理\"], # 区域经理\n",
|
|
" }\n",
|
|
" for role, name in NGV_roles.items():\n",
|
|
" for row_item in self.staff_id_list:\n",
|
|
" staff_id = self.get_staff_id(row_item, name)\n",
|
|
" if staff_id:\n",
|
|
" row[role] = staff_id\n",
|
|
" break # 找到后退出循环\n",
|
|
" else:\n",
|
|
" NGV_roles[role] = None # 如果没有找到对应的员工ID\n",
|
|
" # 简道云字段替换\n",
|
|
" data_dict= self.row_to_dict(row, self.field_mapping)\n",
|
|
" all_data.append(data_dict)\n",
|
|
" \n",
|
|
" payload = {\n",
|
|
" \"api_key\": \"675b900991ad2491c69389ca\",\n",
|
|
" \"entry_id\": \"68637c9818bc333fc14c30ad\",\n",
|
|
" \"data_list\": all_data\n",
|
|
" }\n",
|
|
" api_instance.entry_data_batch_create(payload)\n",
|
|
" print(\"数据写入完成\")\n",
|
|
" common_module.send_task_status(task_start_time, \"履约表数据支撑\")\n",
|
|
" \n",
|
|
" \n",
|
|
"\n",
|
|
" @staticmethod\n",
|
|
" def row_to_dict(row, field_mapping):\n",
|
|
" \"\"\"将一行数据转换为指定格式的字典\"\"\"\n",
|
|
" result = {}\n",
|
|
" for col_name, widget_id in field_mapping.items():\n",
|
|
" if col_name in row:\n",
|
|
" value = row[col_name]\n",
|
|
" clean_value = None if pd.isna(value) else value\n",
|
|
" result[widget_id] = {\"value\": clean_value}\n",
|
|
" return result\n",
|
|
"\n",
|
|
" def fields(self):\n",
|
|
" self.field_mapping = {\n",
|
|
" '公司名称':'_widget_1751350424090',\t'门店名称':'_widget_1751350424083',\t'门店编码':'_widget_1751350424084',\t\n",
|
|
" '运营负责人':'_widget_1751350424085',\t'区域经理':'_widget_1751350424086',\t\n",
|
|
" 'saas开户时间':'_widget_1751350424088',\t'服务期起始时间':'_widget_1751350424097',\t'下单支付成功时间':'_widget_1751350424101',\t'操作时间':'_widget_1751350424110',\t'下单支付成功日期':'_widget_1751350424115',\t'服务期结束时间':'_widget_1751350424098',\n",
|
|
" '订单id':'_widget_1751350424075',\t'f6订单编号':'_widget_1751350424076',\t'宜搭的实例id':'_widget_1751350424077',\t'商品id':'_widget_1751350424078',\t'商品名称':'_widget_1751350424079',\t'发布商品类型':'_widget_1751350424080',\t'发布商品类型描述':'_widget_1751350424081',\t'门店id':'_widget_1751350424082',\t'商户中心id':'_widget_1751350424087',\t'公司id':'_widget_1751350424089',\t'产生来源':'_widget_1751350424091',\t'产生来源描述':'_widget_1751350424092',\t'类型':'_widget_1751350424093',\t'类型描述':'_widget_1751350424094',\t'服务年份':'_widget_1751350424095',\t'订单服务期第几年':'_widget_1751350424096',\t'提成业务类型':'_widget_1751350424099',\t'提成类别':'_widget_1751350424100',\t'实付金额(元)':'_widget_1751350424102',\t'系统成本价(元)':'_widget_1751350424103',\t'版本费(元)':'_widget_1751350424104',\t'服务费(元)':'_widget_1751350424105',\t'介绍人员工ID':'_widget_1751350424106',\t'介绍业绩归属人员工ID':'_widget_1751350424107',\t'处理人ID employee_id':'_widget_1751350424108',\t'业绩归属人员工ID':'_widget_1751350424109',\t'处理人是否跟进,0: 未跟进,1: 已跟进':'_widget_1751350424111',\t'满意度评分':'_widget_1751350424112',\t'评价完成时间':'_widget_1751350424113',\t'介绍人用户类型':'_widget_1751350424114',\t'培训完成时间':'_widget_1751350424116',\t'订单所处阶段':'_widget_1751350424117',\t'日分区':'_widget_1751350424118',\t\n",
|
|
" }\n",
|
|
"if __name__ == '__main__':\n",
|
|
" start = importPerforamnceData()\n",
|
|
" start.main()\n"
|
|
],
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"None\n"
|
|
]
|
|
},
|
|
{
|
|
"ename": "TypeError",
|
|
"evalue": "'NoneType' object is not iterable",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
|
|
"\u001B[1;31mTypeError\u001B[0m Traceback (most recent call last)",
|
|
"Cell \u001B[1;32mIn[9], line 137\u001B[0m\n\u001B[0;32m 135\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;18m__name__\u001B[39m \u001B[38;5;241m==\u001B[39m \u001B[38;5;124m'\u001B[39m\u001B[38;5;124m__main__\u001B[39m\u001B[38;5;124m'\u001B[39m:\n\u001B[0;32m 136\u001B[0m start \u001B[38;5;241m=\u001B[39m importPerforamnceData()\n\u001B[1;32m--> 137\u001B[0m start\u001B[38;5;241m.\u001B[39mmain()\n",
|
|
"Cell \u001B[1;32mIn[9], line 56\u001B[0m, in \u001B[0;36mimportPerforamnceData.main\u001B[1;34m(self)\u001B[0m\n\u001B[0;32m 50\u001B[0m \u001B[38;5;66;03m# Step1:获取履约表数据\u001B[39;00m\n\u001B[0;32m 51\u001B[0m \u001B[38;5;66;03m# df = common_module.get_perforamnce_details()\u001B[39;00m\n\u001B[0;32m 52\u001B[0m \u001B[38;5;66;03m# print(\"数据获取完成\")\u001B[39;00m\n\u001B[0;32m 53\u001B[0m \n\u001B[0;32m 54\u001B[0m \u001B[38;5;66;03m# Step2:清空现有数据\u001B[39;00m\n\u001B[0;32m 55\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mperformance_data_list)\n\u001B[1;32m---> 56\u001B[0m id_list \u001B[38;5;241m=\u001B[39m [item[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m_id\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;28;01mfor\u001B[39;00m item \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mperformance_data_list]\n\u001B[0;32m 58\u001B[0m delete_payload \u001B[38;5;241m=\u001B[39m{\n\u001B[0;32m 59\u001B[0m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mapi_key\u001B[39m\u001B[38;5;124m\"\u001B[39m: \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m675b900991ad2491c69389ca\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m 60\u001B[0m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mentry_id\u001B[39m\u001B[38;5;124m\"\u001B[39m: \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m68637c9818bc333fc14c30ad\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m 61\u001B[0m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mdata_ids\u001B[39m\u001B[38;5;124m\"\u001B[39m: id_list\n\u001B[0;32m 62\u001B[0m }\n\u001B[0;32m 63\u001B[0m api_instance\u001B[38;5;241m.\u001B[39mentry_data_batch_delete(delete_payload)\n",
|
|
"\u001B[1;31mTypeError\u001B[0m: 'NoneType' object is not iterable"
|
|
]
|
|
}
|
|
],
|
|
"execution_count": 9
|
|
},
|
|
{
|
|
"metadata": {},
|
|
"cell_type": "code",
|
|
"outputs": [],
|
|
"execution_count": null,
|
|
"source": "",
|
|
"id": "2c62cf325e15a3c1"
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 2
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython2",
|
|
"version": "2.7.6"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|