CaiWuXiTong/scripts/order-profit-shengsuan-details-sync.py
renshen d79a1eedfe feat: 成本运费异常接入完整订单商品明细 (#1004)
合并 PR #1004:补全订单商品明细并按条码识别缺成本。
2026-11-09 11:02:21 +08:00

310 lines
11 KiB
Python
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.

#!/usr/bin/env python3
"""同步聚胜算成本/运费异常订单的商品条码明细。
脚本从本地财务镜像读取异常订单,通过已登录的聚胜算 Chrome 调试窗口批量请求
订单明细接口,再导入 shared mirror。接口单批最多接受 50 个订单,脚本默认每次
并发 4 批,并逐组导入,可在中断后通过“只取缺失明细”继续。
"""
import argparse
import json
import subprocess
import sys
import time
import urllib.request
import websocket
DETAIL_API_URL = (
'https://pf1.erp321.com/WebApi/PF/OrderSku/GetPfOrderDetList'
'?uvalue=pfcweb_profit-report.order-profit'
)
class RateLimited(RuntimeError):
pass
def page_target(port: int):
with urllib.request.urlopen(f'http://127.0.0.1:{port}/json/list', timeout=5) as response:
targets = json.load(response)
pages = [
target for target in targets
if target.get('type') == 'page'
and 'ss.erp321.com' in (target.get('url') or '')
]
if not pages:
raise RuntimeError('未找到已登录的胜算页面,请先扫码登录并打开订单利润页面。')
return pages[0]
def evaluate(ws: websocket.WebSocket, expression: str):
request_id = int(time.time() * 1000) % 2_000_000_000
ws.send(json.dumps({
'id': request_id,
'method': 'Runtime.evaluate',
'params': {
'expression': expression,
'returnByValue': True,
'awaitPromise': True,
},
}))
while True:
message = json.loads(ws.recv())
if message.get('id') != request_id:
continue
if 'error' in message:
raise RuntimeError(json.dumps(message['error'], ensure_ascii=False))
result = message.get('result', {})
if 'exceptionDetails' in result:
raise RuntimeError(json.dumps(result['exceptionDetails'], ensure_ascii=False))
return result.get('result', {}).get('value')
def condition_for(ids: list[str]) -> dict:
joined = ','.join(ids)
return {
'type': 'soId',
'idType': joined,
'dateFld': 'pay_date',
'combineDate': None,
'shop': [],
'multiPriceType': 'User3',
'multiplePrice': 63,
'costTypeList': [],
'orderType': [],
'drpCoidtos': [],
'wmsCoidList': [],
'buIdList': [],
'skuIn': {'type': 'anyonesku', 'values': [], 'select': 'in'},
'skuOut': {'type': 'anyonesku', 'values': [], 'select': 'out'},
'labelIn': {'type': 'anyonelabel', 'values': [], 'select': 'in'},
'labelout': {'type': 'anyonelabel', 'values': [], 'select': 'out'},
'creatorList': [],
'cusIdList': [],
'brands': [],
'categorys': [],
'vcNames': [],
'goodsCostGroup': {
'skuCostAmountSearchType': {'value': 2, 'label': '按固定值搜索商品成本'},
'value': '',
'values': ['', ''],
},
'backGoodsType': 1,
'splitCombine': True,
'groupByType': 1,
'soId': joined,
'skuCostAmountSearchType': 2,
'skuCostAmountList': [],
'firstfreightList': [''],
'stateCityList': [],
'isOL': True,
'isExport': False,
'skugroup': 'so_id',
'isValidateDate': False,
}
def request_payload(ids: list[str]) -> dict:
return {
'uid': '',
'coid': '',
'ip': '',
'page': {'currentPage': 1, 'pageSize': 5000},
'data': {'isAds': False, 'condition': condition_for(ids)},
'ssProjectType': 'SS',
'ssClientType': 'Web',
}
def fetch_group(ws: websocket.WebSocket, groups: list[list[str]]) -> list[dict]:
payloads = [request_payload(group) for group in groups]
expression = f"""(async () => {{
const payloads = {json.dumps(payloads, ensure_ascii=False)};
return await Promise.all(payloads.map(async (payload) => {{
const response = await fetch('{DETAIL_API_URL}', {{
method: 'POST',
credentials: 'include',
headers: {{'Content-Type': 'application/json'}},
body: JSON.stringify(payload)
}});
return {{status: response.status, text: await response.text()}};
}}));
}})()"""
responses = evaluate(ws, expression)
if not isinstance(responses, list):
raise RuntimeError('聚胜算订单明细接口没有返回有效响应。')
decoded = []
for response in responses:
if response.get('status') != 200:
raise RuntimeError(f'聚胜算订单明细接口 HTTP {response.get("status")}')
payload = json.loads(response.get('text') or '{}')
if payload.get('code') == 1908:
raise RateLimited('RATE_LIMIT')
if payload.get('code') not in (0, None):
raise RuntimeError(
f'聚胜算订单明细接口错误 code={payload.get("code")} msg={payload.get("msg")}'
)
data = payload.get('data')
if not isinstance(data, dict):
raise RuntimeError('聚胜算订单明细接口缺少 data。')
decoded.append(data)
return decoded
def merge_data(items: list[dict]) -> dict:
merged = {
'ords': {'fldHeads': [], 'dataList': []},
'afters': {'fldHeads': [], 'dataList': []},
}
for data in items:
for section in ('ords', 'afters'):
source = data.get(section) or {}
if not isinstance(source, dict):
continue
if not merged[section]['fldHeads'] and isinstance(source.get('fldHeads'), list):
merged[section]['fldHeads'] = source['fldHeads']
if isinstance(source.get('dataList'), list):
merged[section]['dataList'].extend(source['dataList'])
return merged
def database_orders(args) -> list[str]:
command = [
'docker', 'exec', '-i', '-u', 'www-data', 'jiangling_app',
'php', 'artisan', 'order-profit:shengsuan-anomaly-orders',
'--from', args.begin,
'--to', args.end,
]
if not args.refresh:
command.append('--missing-details')
if args.limit > 0:
command.extend(['--limit', str(args.limit)])
process = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False)
if process.returncode != 0:
raise RuntimeError(process.stderr.decode('utf-8', 'replace').strip())
output = process.stdout.decode('utf-8', 'replace')
payload = None
for line in reversed(output.splitlines()):
line = line.strip()
if line.startswith('{'):
payload = json.loads(line)
break
if not isinstance(payload, dict):
raise RuntimeError('无法读取异常订单编号。')
orders = []
seen = set()
for order in payload.get('orders') or []:
order_no = str(order.get('order_no') or '').strip()
if order_no != '' and order_no not in seen:
seen.add(order_no)
orders.append(order_no)
return orders
def import_details(payload: dict) -> None:
process = subprocess.run(
[
'docker', 'exec', '-i', '-u', 'www-data', 'jiangling_app',
'php', 'artisan', 'order-profit:import-shengsuan-details', '--stdin',
],
input=json.dumps({'data': payload}, ensure_ascii=False).encode('utf-8'),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=False,
)
if process.returncode != 0:
message = process.stderr.decode('utf-8', 'replace').strip()
message = message or process.stdout.decode('utf-8', 'replace').strip()
raise RuntimeError(message or '订单明细导入失败。')
print(process.stdout.decode('utf-8', 'replace').strip(), flush=True)
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument('--from', dest='begin', required=True)
parser.add_argument('--to', dest='end', required=True)
parser.add_argument('--batch-size', type=int, default=50)
parser.add_argument('--concurrency', type=int, default=4)
parser.add_argument('--retries', type=int, default=8)
parser.add_argument('--delay', type=float, default=1.0)
parser.add_argument('--limit', type=int, default=0)
parser.add_argument('--debug-port', type=int, default=9333)
parser.add_argument('--refresh', action='store_true', help='刷新已有明细,不只补缺失订单')
parser.add_argument('--no-import', action='store_true')
parser.add_argument('--json-out', default='')
args = parser.parse_args()
args.batch_size = min(max(args.batch_size, 1), 50)
args.concurrency = min(max(args.concurrency, 1), 6)
args.retries = max(args.retries, 1)
orders = database_orders(args)
if not orders:
print('没有待同步的商品明细订单。', flush=True)
return 0
batches = [orders[start:start + args.batch_size] for start in range(0, len(orders), args.batch_size)]
groups = [batches[start:start + args.concurrency] for start in range(0, len(batches), args.concurrency)]
print(
f'待同步 {len(orders)} 单,共 {len(batches)} 批;每次并发 {args.concurrency} 批。',
flush=True,
)
target = page_target(args.debug_port)
ws = websocket.create_connection(target['webSocketDebuggerUrl'], timeout=240)
all_data = []
completed_orders = 0
completed_items = 0
try:
ws.send(json.dumps({'id': 0, 'method': 'Runtime.enable', 'params': {}}))
for group_index, group in enumerate(groups, start=1):
for attempt in range(1, args.retries + 1):
try:
data_items = fetch_group(ws, group)
break
except RateLimited:
wait = min(600, 60 * attempt)
print(f'第 {attempt} 次限流,等待 {wait} 秒后重试……', flush=True)
time.sleep(wait)
else:
raise RuntimeError('聚胜算订单明细接口持续限流,本次已保留此前导入结果。')
merged = merge_data(data_items)
item_count = len((merged.get('ords') or {}).get('dataList') or [])
completed_orders += sum(len(batch) for batch in group)
completed_items += item_count
all_data.append(merged)
if not args.no_import:
import_details(merged)
print(
f'[{group_index}/{len(groups)}] 已请求 {completed_orders}/{len(orders)} 单,'
f'返回销售商品 {completed_items} 行。',
flush=True,
)
if args.delay > 0 and group_index < len(groups):
time.sleep(args.delay)
finally:
ws.close()
if args.json_out:
with open(args.json_out, 'w') as handle:
handle.write(json.dumps({'responses': all_data}, ensure_ascii=False))
print(f'明细 JSON:{args.json_out}', flush=True)
return 0
if __name__ == '__main__':
try:
raise SystemExit(main())
except KeyboardInterrupt:
print('已中止;已导入的批次会保留,可重新运行继续补缺失明细。', file=sys.stderr)
raise SystemExit(130)
except Exception as error:
print(f'明细同步失败:{error}', file=sys.stderr)
raise SystemExit(1)