logistics/自动化跟单11.ipynb

770 lines
29 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-16T14:28:31.802210Z",
"start_time": "2025-05-16T14:28:31.520519Z"
}
},
"outputs": [],
"source": [
"import pandas as pd \n",
"import requests \n",
"from login_for_cookie import Vc\n",
"import pendulum\n",
"import json\n",
"from utils/gtools import MySQLconnect \n",
"from lxml import etree\n",
"from pathlib import Path\n",
"from datetime import datetime\n",
"cookie = Vc(user='robot1',pswd= 'xi%26SiH4LbJ')\n",
"headers = {\"Cookie\":cookie}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-16T10:32:48.135119Z",
"start_time": "2025-05-16T10:32:48.017568Z"
}
},
"outputs": [],
"source": [
"def get_fs(row):\n",
" data = row[\"采购单号\"]\n",
" buy_url = \"http://cp.maso.hk/index.php?main=store_in_receive\"\n",
"\n",
" payload = {\n",
" \"s_pid\": 2867159,\n",
" \"s_suborderid\": \"\",\n",
" \"pdt_standard_id\": \"\",\n",
" \"s_store\": -1,\n",
" \"s_status[0]\": 0,\n",
" \"s_sort\": \"desc\",\n",
" \"Submit\": \"查询\",\n",
" \"synergy_sign\": -1,\n",
" \"start_dep\": \"\",\n",
" \"start_user\": \"\",\n",
" \"to_dep\": \"\",\n",
" \"to_user\": \"\",\n",
" \"swebid\": 0,\n",
" \"local_pdt_id\": 0,\n",
" \"sadd_time\": \"\",\n",
" \"sadd_time_end\": \"\",\n",
" \"sreal_express_no\": \"\",\n",
" \"sweb_id\": \"\",\n",
" \"sof\": 0,\n",
" \"soe\": 0,\n",
" \"sod\": \"\",\n",
" \"tsid\": -1,\n",
" \"tsdid\": -1,\n",
" \"tsst\": -1,\n",
" \"order_cate\": -1,\n",
" \"error_status\": -1,\n",
" \"web_uid\": -1,\n",
" \"team_id\": -1,\n",
" \"handle\": -1,\n",
" \"handle_uid\": 0,\n",
" \"handle_time_start\": \"\",\n",
" \"handle_time_end\": \"\",\n",
" \"shiping_time_start\": \"\",\n",
" \"shiping_time_end\": \"\",\n",
" \"trade_time_start\": \"\",\n",
" \"trade_time_end\": \"\",\n",
" \"order_express_fee\": -1,\n",
" \"web_type\": -1,\n",
" \"shipping_type\": -1,\n",
" \"maintain_add_time_s\": \"\",\n",
" \"maintain_add_time_e\": \"\",\n",
" \"maintain_comfirm_time_s\": \"\",\n",
" \"maintain_comfirm_time_e\": \"\",\n",
" \"dispense_user_id\": \"\",\n",
" \"export_page\": 1\n",
" }\n",
" payload[\"s_pid\"] = data\n",
" resp = requests.post(url = buy_url ,headers=headers , data=payload)\n",
" buy_df_list = pd.read_html(resp.text,match=\"所属网站ID\")\n",
" buy_df = buy_df_list[1]\n",
" buy_df.columns = buy_df.loc[0,:]\n",
" trade_id = buy_df[\"交易ID\"][1]\n",
" #交易明细里头\n",
" trade_url = f\"http://cp.maso.hk/index.php?main=store_tradelist_info&trade_id={trade_id}\"\n",
" resp2 = requests.get(url = trade_url ,headers=headers )\n",
" df_list2 = pd.read_html(resp2.text,match=\"下单发货时间\")\n",
" df2 = df_list2[0]\n",
" df2.columns = df2.loc[0,:]\n",
" 下单发货时间 = int(df2.iloc[2,1][0].replace(\"--\",\"0\"))\n",
" 维护发货时间 = int(df2.iloc[2,3].replace(\"--\",\"0\"))\n",
" 支付时间 = pendulum.parse(df2.iloc[3,3])\n",
" 最大发货时间 = 支付时间.add(days=max(下单发货时间,维护发货时间))\n",
" #交易列表\n",
" trade_list_url = f\"http://cp.maso.hk/index.php?main=store_in_receive&navlist=trade_list&s_trade_id={trade_id}\"\n",
" resp3 = requests.get(url = trade_list_url ,headers=headers )\n",
" df_list3 = pd.read_html(resp3.text,match=\"涨幅\")\n",
" df3 = df_list3[0]\n",
" df3.columns = df3.loc[0,:]\n",
" 交易平台 = df3[\"交易平台\"][1]\n",
" 交易号 = df3[\"交易号\"][1]\n",
" 交易平台订单号 = df3[\"交易平台订单号\"][1]\n",
" \n",
" return pd.Series([trade_id,下单发货时间, 维护发货时间,支付时间,最大发货时间,交易平台,交易号,交易平台订单号])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-16T10:38:08.275276Z",
"start_time": "2025-05-16T10:34:21.240914Z"
}
},
"outputs": [],
"source": [
"resultdf = pd.read_excel(r\"跟单测试源文件.xlsx\")[[\"跟单任务ID\", \"采购单号\"]]\n",
"# 扩展 get_fs\n",
"fs_cols = resultdf.apply(lambda x: get_fs(x), axis=1, result_type=\"expand\")\n",
"fs_cols.columns = [\"交易ID\",'下单发货时间', '维护发货时间','支付时间','最大发货时间','交易平台','交易号','交易平台订单号']\n",
"resultdf = pd.concat([resultdf, fs_cols], axis=1)\n",
"# 处理日期\n",
"for col in ['支付时间', '最大发货时间']:\n",
" resultdf[col] = pd.to_datetime(resultdf[col], errors='coerce')\n",
" resultdf[col] = resultdf[col].dt.strftime('%Y-%m-%d')\n",
"resultdf['pay_date'] = resultdf['支付时间']\n",
"# POST\n",
"data = resultdf.to_dict(orient='records')\n",
"headers = {\"Content-Type\": \"application/json; charset=utf-8\"}\n",
"url = \"http://192.168.100.44/rpaapi/followup\" \n",
"response = requests.post(url, headers=headers, data=json.dumps(data))\n",
"print(\"状态码:\", response.status_code)\n",
"print(\"响应内容:\", response.json())\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-16T10:39:03.196709Z",
"start_time": "2025-05-16T10:38:50.994916Z"
}
},
"outputs": [],
"source": [
"mdm = MySQLconnect(\"mdm\")\n",
"#对数据库进行创建TASK这个应该放在后端\n",
"def create_first_task():\n",
" all_sql = \"select * from tw_order_follow_up WHERE platform = '淘天' AND length(trade_platform_order_num) >=12\"\n",
" df = pd.read_sql(all_sql, mdm.engine())\n",
" for idx,row in df.iterrows():\n",
" trade_id = row[\"trade_id\"]\n",
" max_logis_date_mmdd =row[\"max_logis_date\"]\n",
" mmdd = f\"{max_logis_date_mmdd.month}月{max_logis_date_mmdd.day}日\"\n",
" send_text = f\"平台交易号:{trade_id},你好,请问最快什么时候发货,{mmdd}前可以发货吗?\"\n",
" task_url = \"http://192.168.100.44/rpaapi/taskcreate\" \n",
" headers = {\n",
" \"Content-Type\": \"application/json; charset=utf-8\", # 指定 UTF-8 编码\n",
" \"Other-Header\": \"value\"\n",
" }\n",
"\n",
" response = requests.post(url=task_url, headers=headers, data=json.dumps({\"task_type\":\"跟单\",\"type_id\":row[\"cpmaso_task_id\"],\"send_content\":send_text,\"level\":\"level1\"}))\n",
"create_first_task()\n",
"\n",
"#这里是写到影刀里面的\n",
"def get_tw_url(task_id):\n",
" url = f\"http://192.168.100.44/rpaapi/get_url/{task_id}\"\n",
" response = requests.get(url=url).json()\n",
" return response\n",
"\n",
"def finish_task(task_id,task_status,session_id):\n",
" task_url = \"http://192.168.100.44/rpaapi/taskfinish\" \n",
" response = requests.post(url=task_url, headers=headers, data=json.dumps({\"task_id\":task_id,\"task_status\":task_status,\"session_id\":session_id}))\n",
" return response.json()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#以下内容也全都写到影刀里面\n",
"file = Path(r\"C:\\Users\\Admin\\Desktop\\taoworldtask.txt\")\n",
"def html_to_db(html): # -> list[dict]\n",
" tree = etree.HTML(html)\n",
" container = tree.xpath(\"//*[contains(@class, 'IMChat--ww_container--rcKMqZZ')]\")[0]\n",
" shopname = container.xpath(\"//*[contains(@class, 'IMChat--ww_header--qyQJ4Lp')]/text()\")[0].strip()\n",
" message_item_lines = container.xpath(\"//*[contains(@class, 'message-item-lin')]\")\n",
"\n",
" results = []\n",
" for msg in message_item_lines:\n",
" message_id = msg.get('id', '')\n",
" \n",
" # 昵称\n",
" nick_x = msg.xpath(\".//div[contains(@class, 'nick')]/text()\")\n",
" nick = nick_x[0].strip() if nick_x else ''\n",
" \n",
" # 时间\n",
" time_x = msg.xpath(\".//div[contains(@class, 'time')]/text()\")\n",
" time_str = time_x[0].strip() if time_x else ''\n",
" try:\n",
" time = datetime.strptime(time_str, \"%Y-%m-%d %H:%M:%S\") if time_str else None\n",
" except ValueError:\n",
" time = None\n",
"\n",
" # 内容或图片\n",
" content_x = msg.xpath(\".//pre[contains(@class, 'edit')]/text()\")\n",
" content = ','.join(content_x).strip() if content_x else ''\n",
" img_x = msg.xpath(\".//img[contains(@class, 'imui-msg-img')]/@src\")\n",
" if img_x:\n",
" content = img_x[0] # 可考虑记录 chat_type='image'\n",
"\n",
" result = {\n",
" 'id': message_id,\n",
" 'nick': nick,\n",
" 'time': time.strftime(\"%Y-%m-%d %H:%M:%S\") if time else None,\n",
" 'content': content\n",
" }\n",
" results.append(result)\n",
" return {\n",
" \"shopname\": shopname,\n",
" \"messages\": results\n",
" }\n",
"\n",
"def upload_msg(msg_list, account, create_chat_id=''):\n",
" url = \"http://192.168.100.44/rpaapi/upload_msg\"\n",
"\n",
" payload = {\n",
" \"shopname\": msg_list[\"shopname\"],\n",
" \"messages\": msg_list[\"messages\"],\n",
" \"account\": account,\n",
" \"creata_chat_id\": create_chat_id # 如果为空,表示接收方;否则为发送者创建会话用\n",
" }\n",
"\n",
" headers = {\n",
" \"Content-Type\": \"application/json\"\n",
" }\n",
"\n",
" try:\n",
" resp = requests.post(url, headers=headers, data=json.dumps(payload))\n",
" if resp.status_code == 200:\n",
" print(\"上传成功\")\n",
" else:\n",
" print(f\"上传失败: {resp.status_code} - {resp.text}\")\n",
" except Exception as e:\n",
" print(f\"请求异常: {e}\")\n",
"\n",
"msg_data = html_to_db(file.read_text(encoding='utf-8'))\n",
"msg_data1 = msg_data.copy()\n",
"msg_data1[\"messages\"] = msg_data[\"messages\"][:10]\n",
"upload_msg(msg_data1, account=\"t-2217567810350-0\", create_chat_id=\"3469772970637.PNM\")\n",
"msg_data2 = msg_data.copy()\n",
"msg_data2[\"messages\"] = msg_data[\"messages\"][5:]\n",
"upload_msg(msg_data2, account=\"t-2217567810350-0\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from http import HTTPStatus\n",
"from dashscope import Application\n",
"response = Application.call(\n",
" # 若没有配置环境变量可用百炼API Key将下行替换为api_key=\"sk-xxx\"。但不建议在生产环境中直接将API Key硬编码到代码中以减少API Key泄露风险。\n",
" api_key=\"sk-17d17a164fb44ff99d88647bc6a1d551\",\n",
" app_id='83af9a2c0cf444da9a1860c91a1963ec',# 替换为实际的应用 ID\n",
" prompt='你是谁?')\n",
"if response.status_code != HTTPStatus.OK:\n",
" print(f'request_id={response.request_id}')\n",
" print(f'code={response.status_code}')\n",
" print(f'message={response.message}')\n",
" print(f'请参考文档https://help.aliyun.com/zh/model-studio/developer-reference/error-code')\n",
"else:\n",
" print(response.output.text)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-14T17:20:59.901784Z",
"start_time": "2025-05-14T17:20:59.797072Z"
}
},
"outputs": [],
"source": [
"import os\n",
"from http import HTTPStatus\n",
"from dashscope import Application\n",
"mdm = MySQLconnect(\"mdm\")\n",
"sql = \"\"\"SELECT * FROM `tw_session_detail` WHERE session_id = 111\"\"\"\n",
"chatdf = pd.read_sql(sql, mdm.engine())\n",
"rows_as_dict_list = chatdf.to_dict(orient='records')\n",
"rows_as_dict_list"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-14T17:21:12.453964Z",
"start_time": "2025-05-14T17:21:03.052705Z"
}
},
"outputs": [],
"source": [
"import os\n",
"from http import HTTPStatus\n",
"from dashscope import Application\n",
"response = Application.call(\n",
" # 若没有配置环境变量可用百炼API Key将下行替换为api_key=\"sk-xxx\"。但不建议在生产环境中直接将API Key硬编码到代码中以减少API Key泄露风险。\n",
" api_key=\"sk-17d17a164fb44ff99d88647bc6a1d551\",\n",
" app_id='83af9a2c0cf444da9a1860c91a1963ec',# 替换为实际的应用 ID\n",
" prompt=str(rows_as_dict_list))\n",
"if response.status_code != HTTPStatus.OK:\n",
" print(f'request_id={response.request_id}')\n",
" print(f'code={response.status_code}')\n",
" print(f'message={response.message}')\n",
" print(f'请参考文档https://help.aliyun.com/zh/model-studio/developer-reference/error-code')\n",
"else:\n",
" print(json.loads(response.output.text))\n",
" #上传数据库\n",
" #"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"################################################################## 上传聊天记录"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-16T14:28:17.306768Z",
"start_time": "2025-05-16T14:28:17.294156Z"
},
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-16T15:38:46.563968Z",
"start_time": "2025-05-16T15:38:44.315926Z"
},
"collapsed": false
},
"outputs": [],
"source": [
"import os\n",
"import pandas as pd\n",
"from datetime import datetime\n",
"from http import HTTPStatus\n",
"from dashscope import Application\n",
"from utils.gtools import MySQLconnect\n",
"import json\n",
"mdm = MySQLconnect('mdm')\n",
"import requests"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def upload_to_cpmaso(id,date,file,process_remark='',is_return=0,is_refund=0,is_find_goods=0):\n",
" url = \"http://192.168.100.44/rpaapi/upload_system_appointed_time\"\n",
" data = {\"id\":id,\"date\":date,\"content\":file,\"process_remark\":process_remark,\"is_refund\":is_refund,\"is_return\":is_return,\"is_find_goods\":is_find_goods} # id是任务idcontent是txt文件内容remark是备注\n",
" headers = {\"Content-Type\": \"application/json\"}\n",
" response = requests.post(url=url,json=data,headers=headers)\n",
" return response"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from http import HTTPStatus\n",
"from dashscope import Application\n",
"def aliyun(prompt):\n",
" response = Application.call(\n",
" # 若没有配置环境变量可用百炼API Key将下行替换为api_key=\"sk-xxx\"。但不建议在生产环境中直接将API Key硬编码到代码中以减少API Key泄露风险。\n",
" api_key=\"sk-17d17a164fb44ff99d88647bc6a1d551\",\n",
" app_id='83af9a2c0cf444da9a1860c91a1963ec',# 替换为实际的应用 ID\n",
" prompt=prompt)\n",
" api_data = json.loads(response.output.text)\n",
" return api_data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-16T15:38:58.960899Z",
"start_time": "2025-05-16T15:38:58.940364Z"
},
"collapsed": false
},
"outputs": [],
"source": [
"sql = \"\"\"SELECT * FROM `tw_order_follow_up` where platform = '淘天' AND upload_cpmaso = 0\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-16T15:39:01.808655Z",
"start_time": "2025-05-16T15:39:01.716950Z"
},
"collapsed": false
},
"outputs": [],
"source": [
"df = pd.read_sql(sql,mdm.engine())\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-15T15:26:13.035452Z",
"start_time": "2025-05-15T15:26:13.013689Z"
},
"collapsed": false
},
"outputs": [],
"source": [
"def update_follow_up(task_id,time='null',task_status=0,refund=0,has_image=1,clearly_time='0',one_more_time=0):\n",
" sql = f\"\"\"\n",
" UPDATE `tw_order_follow_up`\n",
" SET agreed_delivery_time = {time},task_status = {task_status},need_refund = {refund},\n",
" has_image = {has_image},clearly_specify_the_delivery_time = {clearly_time},one_more_time = {one_more_time}\n",
" WHERE cpmaso_task_id = \"{task_id}\"\n",
" \"\"\"\n",
" connect = mdm.connect()\n",
" connect.cursor().execute(sql)\n",
" connect.commit()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-16T15:45:35.889562Z",
"start_time": "2025-05-16T15:41:16.724883Z"
},
"collapsed": false
},
"outputs": [],
"source": [
"from datetime import datetime \n",
"#id,date,file,remake='',is_return=0,is_refund=0,is_find_goods=0):\n",
"for idx,row in df.iterrows():\n",
" #如果已发货,就上传记录\n",
" if row[\"task_status\"] == 5:\n",
" resp=upload_to_cpmaso(id=row[\"cpmaso_task_id\"],\n",
" date = datetime.now().strftime(\"%Y-%m-%d\"),\n",
" file = \"\",\n",
" process_remark = \"已发货\")\n",
" print(resp)\n",
" url = 'http://192.168.100.44/rpaapi/upload_followup'\n",
" data = {\n",
" \"task_id\" : row[\"cpmaso_task_id\"],\n",
" \"task_status\":row[\"task_status\"],\n",
" \"upload_cpmaso\" : 1\n",
" }\n",
" headers = {\"Content-Type\": \"application/json\"}\n",
" response = requests.post(url=url,json=data,headers=headers)\n",
" print(1)\n",
" elif row[\"task_status\"] == 0:\n",
" chat_sql = f\"\"\"SELECT\n",
" t3.* \n",
" FROM\n",
" `tw_order_follow_up` t1 LEFT JOIN tw_auto_task t2 on t1.last_task_id = t2.id\n",
" LEFT JOIN tw_session_detail t3 on t3.session_id = t2.send_session\n",
" WHERE\n",
" cpmaso_task_id = {row[\"cpmaso_task_id\"]} \"\"\"\n",
" chatdf = pd.read_sql(chat_sql, mdm.engine())\n",
" \n",
" \n",
" if len(chatdf) == 0 or chatdf[\"id\"][0] is None:\n",
" continue\n",
" else:\n",
" last_match_idx = chatdf[chatdf['chat_content'].str.contains('平台交易号', na=False)].index[-1]\n",
" # # 获取该行及之后所有行\n",
" chatdf = chatdf.loc[last_match_idx:]\n",
" if len(chatdf) == 1:\n",
" resp=upload_to_cpmaso(id=row[\"cpmaso_task_id\"],\n",
" date =\"\",\n",
" file = str(chatdf.to_dict(orient='records')),\n",
" process_remark = \"买家未回复\")\n",
" continue\n",
" elif len(chatdf) >= 2:\n",
" pass\n",
" \n",
" rows_as_dict_list = chatdf.to_dict(orient='records')\n",
" prompt=str(rows_as_dict_list)\n",
" chat_list = [row['chat_content'] for row in rows_as_dict_list]\n",
" api_data= aliyun(prompt)\n",
" print(chat_list)\n",
" deliver_date = api_data['result']['约定发货信息']['发货时间'] #发货时间\n",
" answer_result = api_data[\"result\"]['回复结果'] # 回复结果\n",
" is_return = api_data['result']['是否需要退款']\n",
" is_deliver = api_data['result']['是否已发货'] \n",
" is_ask = api_data['result']['是否需要追问']\n",
" time = datetime.strptime(deliver_date, \"%Y/%m/%d\").date().strftime(\"%Y-%m-%d\") if deliver_date else \"\"\n",
" need_refund = 1 if is_return == \"是\" else 0\n",
" clearly_time = 1 if answer_result == '明确发货时间' else 2 if answer_result == '无法明确发货时间' else 3\n",
" one_more_time = 1 if is_ask == '是' else 2\n",
" has_image = 1 if answer_result == '内含图片' else 0\n",
" task_status = 5 if answer_result == '回复已发货' else 4 if has_image == 1 else 1 if clearly_time == 2 else 2 if clearly_time == 1 else 1\n",
" task_status = 4 if need_refund == 1 else task_status\n",
" print(api_data)\n",
"\n",
" resp=upload_to_cpmaso(id=row[\"cpmaso_task_id\"],\n",
" date = time,\n",
" file = str(chatdf.to_dict(orient='records')),\n",
" process_remark = answer_result)\n",
" \n",
" url = 'http://192.168.100.44/rpaapi/upload_followup'\n",
" data = {\n",
" \"task_id\" : row[\"cpmaso_task_id\"],\n",
" \"time\":time,\n",
" \"task_status\":task_status,\n",
" \"refund\": need_refund,\n",
" \"has_image\":has_image,\n",
" \"clearly_time\":clearly_time,\n",
" \"one_more_time\":one_more_time,\n",
" \"upload_cpmaso\" : 1\n",
" }\n",
"\n",
" headers = {\"Content-Type\": \"application/json\"}\n",
" response = requests.post(url=url,json=data,headers=headers)\n",
" print(1)\n",
" for j in range(len(rows_as_dict_list)):\n",
" id = rows_as_dict_list[j]['id']\n",
" url = 'http://192.168.100.44/rpaapi/read'\n",
" data = {\"chat_id\":id}\n",
" headers = {\"Content-Type\": \"application/json\"}\n",
" requests.post(url=url,json=data,headers=headers)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-16T15:00:09.917941Z",
"start_time": "2025-05-16T15:00:09.796011Z"
},
"collapsed": false
},
"outputs": [],
"source": [
"def aliyun(rows_as_dict_list):\n",
" response = Application.call(\n",
" # 若没有配置环境变量可用百炼API Key将下行替换为api_key=\"sk-xxx\"。但不建议在生产环境中直接将API Key硬编码到代码中以减少API Key泄露风险。\n",
" api_key=\"sk-17d17a164fb44ff99d88647bc6a1d551\",\n",
" app_id='83af9a2c0cf444da9a1860c91a1963ec',# 替换为实际的应用 ID\n",
" prompt=str(rows_as_dict_list))\n",
" api_data = json.loads(response.output.text)\n",
" return api_data\n",
" deliver_date = api_data['result']['约定发货信息']['发货时间'] #发货时间\n",
" answer_result = api_data[\"result\"]['回复结果'] # 回复结果\n",
" is_return = api_data['result']['是否需要退款']\n",
" is_deliver = api_data['result']['是否已发货']\n",
" is_ask = api_data['result']['是否需要追问']\n",
" time = datetime.strptime(deliver_date, \"%Y/%m/%d\").date() if deliver_date else None\n",
" need_refund = 1 if is_return == \"是\" else 0\n",
" clearly_time = 1 if answer_result == '明确发货时间' else 2 if answer_result == '无法明确发货时间' else 3\n",
" one_more_time = 1 if is_ask == '是' else 2\n",
" has_image = 1 if answer_result == '内含图片' else 0\n",
" task_status = 5 if answer_result == '回复已发货' else 4 if has_image == 1 else 1 if clearly_time == 2 else 2 if clearly_time == 1 else 1\n",
" task_status = 4 if need_refund == 1 else task_status\n",
" print(api_data)\n",
"\n",
" url = 'http://192.168.100.44/rpaapi/upload_followup'\n",
" data = {\n",
" \"task_id\" : df.iloc[i,1],\"time\":time,\n",
" \"task_status\":task_status,\n",
" \"refund\": need_refund,\n",
" \"has_image\":has_image,\n",
" \"clearly_time\":clearly_time,\n",
" \"one_more_time\":one_more_time\n",
" }\n",
" headers = {\"Content-Type\": \"application/json\"}\n",
" response = requests.post(url=url,json=data,headers=headers)\n",
" print(1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-16T15:07:00.762532Z",
"start_time": "2025-05-16T15:07:00.737861Z"
},
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"############################################################# 聊天记录上传系统"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-15T17:01:48.937072Z",
"start_time": "2025-05-15T17:01:47.491710Z"
},
"collapsed": false
},
"outputs": [],
"source": [
"sql = \"\"\"SELECT cpmaso_task_id,agreed_delivery_time,task_status,need_refund,clearly_specify_the_delivery_time,one_more_time\n",
"from tw_order_follow_up\n",
"where platform = '淘天'\n",
"and DATE(create_date) = DATE(CONVERT_TZ(NOW(), '+00:00', '+08:00'))\n",
"and task_status != 0\n",
"and task_status != 5\n",
"and task_status != 4\"\"\"\n",
"df = pd.read_sql(sql,mdm.engine())\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-15T17:51:28.209124Z",
"start_time": "2025-05-15T17:51:28.204284Z"
},
"collapsed": false
},
"outputs": [],
"source": [
"def upload_to_cpmaso(id,date,file,remake='',is_return=0,is_refund=0,is_find_goods=0):\n",
" url = \"http://192.168.100.44/rpaapi/upload_system_appointed_time\"\n",
" data = {\"id\":id,\"date\":date,\"content\":file,\"remark\":remake,\"is_refund\":is_refund,\"is_return\":is_return,\"is_find_goods\":is_find_goods} # id是任务idcontent是txt文件内容remark是备注\n",
" headers = {\"Content-Type\": \"application/json\"}\n",
" response = requests.post(url=url,json=data,headers=headers)\n",
" print(response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-15T18:01:35.681048Z",
"start_time": "2025-05-15T18:01:24.070530Z"
},
"collapsed": false
},
"outputs": [],
"source": [
"for i in range(len(df)):\n",
" type_id = str(df.iloc[i]['cpmaso_task_id'])\n",
" is_refund = str(df.iloc[i]['need_refund'])\n",
" if df.iloc[i]['agreed_delivery_time']: # 如果时间不为空\n",
" date = str(df.iloc[i]['agreed_delivery_time'])\n",
" remake= ''\n",
" else:\n",
" date = ''\n",
" remake = '没有明确时间'\n",
" msg_sql = f\"\"\"SELECT b.*\n",
" FROM tw_auto_task a\n",
" left join tw_session_detail b on a.send_session = b.session_id\n",
" where a.type_id = {type_id}\"\"\"\n",
" msg_df = pd.read_sql(msg_sql,mdm.engine())\n",
" msg_df['chat_date'] = msg_df['chat_date'].dt.strftime('%Y-%m-%d %H:%M:%S')\n",
" msg_file = msg_df.to_dict(orient='records')\n",
" upload_to_cpmaso(id=type_id,date=date,file=msg_file,is_refund=is_refund,remake=remake)\n",
" print(msg_file)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}