feat: 订单利润接入聚胜算页面费用结果 #987

Merged
renshen merged 1 commits from feature/renshen-order-profit-shengsuan-fees-v2 into main 2026-11-07 17:34:40 +08:00
11 changed files with 639 additions and 2 deletions

View File

@ -0,0 +1,57 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public const TABLE = 'ops_shengsuan_order_profit_rows';
public function up(): void
{
if (config('system_database.mode', 'bootstrap') === 'bootstrap') {
return;
}
$schema = Schema::connection((string) config('upstream_mirrors.connection', 'business_mirror'));
if ($schema->hasTable(self::TABLE)) {
return;
}
$schema->create(self::TABLE, function (Blueprint $table): void {
$table->bigIncrements('id');
$table->string('shop_name', 191)->default('');
$table->string('online_order_no', 64)->nullable();
$table->string('internal_order_no', 32)->nullable();
$table->string('order_key', 191)->default('');
$table->decimal('matched_package_fee', 18, 2)->nullable();
$table->decimal('matched_freight_fee', 18, 2)->nullable();
$table->decimal('matched_bill_fee', 18, 2)->nullable();
$table->decimal('generated_classification_fee', 18, 2)->nullable();
$table->decimal('generated_product_fee', 18, 2)->nullable();
$table->decimal('manual_fee', 18, 2)->nullable();
$table->decimal('order_fee', 18, 2)->nullable();
$table->decimal('promotion_fee', 18, 2)->nullable();
$table->longText('source_payload')->nullable();
$table->dateTime('synced_at')->nullable();
$table->timestamps();
$table->unique('order_key', 'shengsuan_order_key_unique');
$table->index(['shop_name', 'synced_at'], 'shengsuan_shop_synced');
$table->index(['online_order_no', 'synced_at'], 'shengsuan_online_synced');
$table->index(['internal_order_no', 'synced_at'], 'shengsuan_internal_synced');
});
}
public function down(): void
{
if (config('system_database.mode', 'bootstrap') === 'bootstrap') {
return;
}
Schema::connection((string) config('upstream_mirrors.connection', 'business_mirror'))
->dropIfExists(self::TABLE);
}
};

View File

@ -42,6 +42,7 @@
| 待有效凭据后首次同步 | 普通商品当前成本价、商品类型和更新时间继续读取 `operations_replica.rpt.jst_sku`;`rpt.fys_skuCostPriceUpdate` 是一 SKU 一行的旧快照,不能充当变价历史。完整成本价变更仍读取聚水潭经典开放平台“商品操作日志查询”接口 `worklog.query`,只镜像操作内容含“采购价”的记录到 `jushuitan_cost_price_history_rows`;经典接口必须在服务器私有配置中使用同一应用的 `JUSHUITAN_PARTNER_ID / JUSHUITAN_PARTNER_KEY / JUSHUITAN_TOKEN`,不得把新开放平台的 App Key / App Secret / Access Token 混用。页面在首次同步完成前明确显示待同步或失败,不把 0 条解释为真实无历史。 |
| 本次接入 | 流程明细读取 `operations_replica/rpt.ding_Workflow + rpt.ding_WorkflowScheme` 的结构化审批主表、费用明细和付款明细,写入 `ding_process_detail_mirror`、`ding_process_payment_mirror` 与付款按明细行分摊的 `ding_process_payment_line_mirror`;主表的 `originatorDeptName` 作为发起人部门单独保留,费用明细行的“店铺”字段规范化写入 `store_name`。禁止先把明细表和付款表铺平成 CSV 后逐行求和;付款明细必须按“对应序号”关联到代表明细行,但合计始终读取独立付款事实。管理费用按分摊后的付款月份统计,跨月分笔付款不会把整张审批重复计入多月。每个纳入范围的源审批至少保留一条页面记录,没有费用金额的审批以空金额显示,不静默隐藏。首次全量先运行 `business-mirror:sync-ding-process-details --full --dry-run` 校验,再使用 `business-mirror:sync-ding-process-details --full` 发布;日常通过 `updateAt/createAt` 48 小时重叠增量同步。当前纳入日常报销、应付、线上代发、种菜、快递面单、固定资产、日常物品、样品、一次性采买、板材、工资、外贸费用和种菜充值等 14 个财务流程;发现新的明细流程编码时全量同步报错并保留上一版。 |
| 本次接入 | 财务经营报表 › 管理费用使用 `oa_process_account_mapping` 保存流程科目映射;当前快照包含原工作簿 191 个科目和补充的 `5601.11.01 货代费`、`1221.07 种菜备用金`、`5602.11.03.19 AI阿里云软件费`,共 194 个科目,其中 37 个进费比科目和 126 个分摊规则。页面按打款时间提供整月选择并默认展示上个月 OA 流程中审批状态为“已完成”、涉及付款且出纳已打款的明细,不涉及付款或出纳未打款的数据不进入汇总,按费用大类和二级汇总进费比/不进费比/未映射金额,点击二级查看三级汇总与 9 列明细模板。审批完成时间写入 `ding_process_detail_mirror.approval_finished_at`,页面月份筛选使用 `ding_process_detail_mirror.paid_at`;钉钉副本把费用明细表保存为未命名字段时,按配置中的金额列识别包含该列的未命名表格组件,避免只抓到付款明细而漏掉费用科目和金额。明细行没有科目时,使用同一审批表单表头的 `科目编码 / 科目名称` 回填;行级科目始终优先。 |
| 本次接入 | 订单利润的包材费、快递费、账单费用直接读取聚胜算订单利润页面接口 `pf.erp321.com/WebApi/PF/OrderSku/GetOrderSkuList`(支付/发货时间口径各分页抓取),导入 `ops_shengsuan_order_profit_rows`,页面按“店铺 + 线上/内部单号”原样回填,不把 0 或推算值写入缺口。抓取脚本为 `scripts/order-profit-shengsuan-sync.py`,导入命令为 `order-profit:import-shengsuan-rows`;脚本依赖本机已登录胜算的 Chrome 调试窗口,不能由服务器容器直连页面。 |
流程明细另设独立定时任务,避免与每分钟科目映射消费争用同一上游目录时被静默跳过。正式发布脚本会覆盖安装 `/etc/cron.d/caiwu-ding-process-details`:每 5 分钟尝试运行 `business-mirror:sync-ding-process-details`,页面定向刷新和一周一次的全量校准与增量同步共用 `/run/caiwu-oa-process.lock`,只在这些 OA 镜像任务之间排队,不再被店铺对账、商品同步等无关任务占用的全局上游锁阻塞;`scripts/install-ding-process-sync-cron.ps1` 仅作为手工修复入口。任务时间和镜像状态统一使用平台固定时区,审批等待时长按实际小时展示。OA 页面每小时最多把一次当前页“审批中”的审批编号写入定向刷新队列,由每分钟检查一次的批处理命令消费;其他时间直接展示最近镜像,已完成流程不进入即时刷新链路,仍由常规增量和每周全量校准处理付款等后续变化。进行中审批的钉钉接口按配置并发读取(默认 5 个、上限 10 个),单个审批失败不会丢弃同批成功结果。增量同步除跟进审批主表更新时间外,还以 `ding_WorkflowScheme` 的数字 `id` 高水位发现后补表单行,避免付款明细等新组件写入后主审批 `updateAt` 不变而被漏掉;原地改写同一组件仍由每周全量校准兜底。付款状态只按钉钉“出纳打款”节点判断,分为“出纳状态待同步 / 出纳未开始 / 出纳待打款 / 出纳退回 / 出纳已打款 / 流程终止 / 不涉及付款”。金额差异不参与付款状态判断,另以“付款差额 = 费用合计 − 已付款合计”展示。不能因为金额存在少量差异就覆盖出纳节点事实,也不能因为出纳已通过就隐藏金额差异。

View File

@ -0,0 +1,79 @@
<?php
namespace Modules\EcommerceFinance\Console;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Modules\EcommerceFinance\Services\ShengsuanOrderProfitRowMapper;
class ImportShengsuanOrderProfitRows extends Command
{
protected $signature = 'order-profit:import-shengsuan-rows
{--file= : 聚胜算订单利润接口返回的 JSON 文件路径}
{--stdin : 从标准输入读取 JSON,忽略 --file}';
protected $description = '把聚胜算订单利润页面的完整结果导入共享镜像,供订单利润页按订单原样展示';
public function handle(ShengsuanOrderProfitRowMapper $mapper): int
{
$path = trim((string) $this->option('file'));
$fromStdin = (bool) $this->option('stdin');
try {
$raw = $fromStdin
? stream_get_contents(STDIN)
: (($path !== '' && is_file($path)) ? file_get_contents($path) : '');
} catch (\Throwable) {
$raw = '';
}
if ($raw === '') {
$this->error('未读取到聚胜算订单利润 JSON。');
return self::FAILURE;
}
try {
$decoded = json_decode($raw, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException) {
$this->error('聚胜算订单利润 JSON 无法解析。');
return self::FAILURE;
}
$rows = $mapper->rows($decoded);
if ($rows === []) {
$this->warn('JSON 中没有订单利润数据行。');
return self::FAILURE;
}
$connection = DB::connection((string) config('upstream_mirrors.connection', 'business_mirror'));
$connection->beginTransaction();
try {
foreach (array_chunk($rows, 200) as $chunk) {
$connection->table($mapper->table())->upsert(
$chunk,
['order_key'],
[
'shop_name', 'online_order_no', 'internal_order_no',
'matched_package_fee', 'matched_freight_fee', 'matched_bill_fee',
'generated_classification_fee', 'generated_product_fee', 'manual_fee',
'order_fee', 'promotion_fee', 'source_payload', 'synced_at', 'updated_at',
],
);
}
$connection->commit();
} catch (\Throwable $exception) {
$connection->rollBack();
report($exception);
$this->error('订单利润页面结果导入失败,已回滚。');
return self::FAILURE;
}
$this->info(sprintf('订单利润页面结果导入完成:%d 条。', count($rows)));
return self::SUCCESS;
}
}

View File

@ -5,6 +5,7 @@ namespace Modules\EcommerceFinance\Providers;
use Illuminate\Support\ServiceProvider as Provider;
use Modules\EcommerceFinance\Console\ProcessDingRefreshRequests;
use Modules\EcommerceFinance\Console\DiagnoseJushuitanPackage;
use Modules\EcommerceFinance\Console\ImportShengsuanOrderProfitRows;
use Modules\EcommerceFinance\Console\SyncJushuitanShipping;
use Modules\EcommerceFinance\Services\JianSunSnapshotRefresher;
use Modules\EcommerceFinance\Services\OperationsReplicaSnapshotRefresher;
@ -23,6 +24,7 @@ class Main extends Provider
SyncJushuitanShipping::class,
ProcessDingRefreshRequests::class,
DiagnoseJushuitanPackage::class,
ImportShengsuanOrderProfitRows::class,
]);
}
}

View File

@ -8,6 +8,7 @@ use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Modules\EcommerceFinance\Services\GoodsProfitAfsLookupSchema;
use Modules\EcommerceFinance\Services\ShengsuanOrderProfitRowMapper;
/**
* 聚水潭「订单利润」页面数据,按订单聚合商品利润镜像明细。
@ -20,6 +21,8 @@ class OrderProfitRepository
{
private const TABLE = GoodsProfitAfsLookupSchema::TABLE;
private const FEE_TABLE = ShengsuanOrderProfitRowMapper::TABLE;
private const NUMERIC_FIELDS = [
'product_quantity', 'product_amount', 'product_cost',
'actual_shipped_quantity', 'actual_shipped_amount', 'actual_shipped_cost',
@ -110,6 +113,7 @@ class OrderProfitRepository
'initial_completed_at' => $state->initial_completed_at ?? null,
'last_succeeded_at' => $state->last_succeeded_at ?? null,
'last_error_code' => $state->last_error_code ?? null,
'last_fees_synced_at' => $this->lastFeesSyncedAt(),
];
}
@ -236,6 +240,7 @@ class OrderProfitRepository
}
$rows = array_slice($rows, 0, $limit);
$this->hydrateItems($rows, $columns);
$this->hydrateMatchedFees($rows);
return ['rows' => $rows, 'has_more' => $hasMore];
}
@ -492,6 +497,70 @@ class OrderProfitRepository
unset($row);
}
/**
* 三个自动匹配费用来自聚胜算订单利润页面结果,不在商品利润镜像内。
* 只允许用页面接口的原始值覆盖,缺失时仍保持 null,不用 0 或推算值代替。
*
* @param list<array<string,mixed>> $rows
*/
private function hydrateMatchedFees(array &$rows): void
{
if ($rows === []) {
return;
}
if (! Schema::connection($this->connectionName())->hasTable(self::FEE_TABLE)) {
return;
}
$keys = [];
foreach ($rows as $row) {
$shop = trim((string) ($row['shop_name'] ?? ''));
$order = trim((string) ($row['order_key'] ?? ''));
if ($shop !== '' && $order !== '') {
$keys[$shop . '|' . $order] = true;
}
}
if ($keys === []) {
return;
}
$fees = [];
foreach ($this->connection()->table(self::FEE_TABLE)
->whereIn('order_key', array_keys($keys))
->get([
'shop_name', 'order_key', 'matched_package_fee',
'matched_freight_fee', 'matched_bill_fee',
]) as $record) {
$key = trim((string) $record->order_key);
$fees[$key] = [
$record->matched_package_fee,
$record->matched_freight_fee,
$record->matched_bill_fee,
];
}
foreach ($rows as &$row) {
$key = trim((string) ($row['shop_name'] ?? '')) . '|' . trim((string) ($row['order_key'] ?? ''));
if (! isset($fees[$key])) {
continue;
}
[$package, $freight, $bill] = $fees[$key];
$row['matched_package_fee'] = $package === null ? null : (float) $package;
$row['matched_freight_fee'] = $freight === null ? null : (float) $freight;
$row['matched_bill_fee'] = $bill === null ? null : (float) $bill;
}
unset($row);
}
private function lastFeesSyncedAt(): ?string
{
try {
return $this->connection()->table(self::FEE_TABLE)->max('synced_at');
} catch (\Throwable) {
return null;
}
}
/** @param list<string> $fields */
private function sumExpression(array $columns, array $fields): string
{

View File

@ -0,0 +1,110 @@
<?php
namespace Modules\EcommerceFinance\Services;
class ShengsuanOrderProfitRowMapper
{
public const TABLE = 'ops_shengsuan_order_profit_rows';
private const FEE_FIELDS = [
'包材费(自动匹配)' => 'matched_package_fee',
'快递费(自动匹配)' => 'matched_freight_fee',
'账单费用(自动匹配)' => 'matched_bill_fee',
'分类单成本生成费用' => 'generated_classification_fee',
'商品成本生成费用' => 'generated_product_fee',
'人工维护' => 'manual_fee',
'订单费用' => 'order_fee',
'其中:推广费' => 'promotion_fee',
];
public function table(): string
{
return self::TABLE;
}
/**
* @param array<string,mixed> $decoded
* @return list<array<string,mixed>>
*/
public function rows(array $decoded): array
{
$data = $decoded['data'] ?? $decoded;
if (! is_array($data)) {
return [];
}
$heads = is_array($data['fldHeads'] ?? null) ? $data['fldHeads'] : [];
$dataList = is_array($data['dataList'] ?? null) ? $data['dataList'] : [];
$fields = [];
foreach (['店铺名称' => 'shop_name', '线上单号' => 'online_order_no', '内部单号' => 'internal_order_no']
+ self::FEE_FIELDS as $title => $field) {
$fields[$field] = $this->fieldForTitle($heads, $title);
}
$rows = [];
$now = now();
foreach ($dataList as $record) {
if (! is_array($record)) {
continue;
}
$shopName = trim((string) $this->value($record, $fields['shop_name'] ?? null));
$onlineOrder = trim((string) $this->value($record, $fields['online_order_no'] ?? null));
$internalOrder = trim((string) $this->value($record, $fields['internal_order_no'] ?? null));
$orderNo = $onlineOrder !== '' ? $onlineOrder : $internalOrder;
if ($shopName === '' || $orderNo === '') {
continue;
}
$row = [
'shop_name' => $shopName,
'online_order_no' => $onlineOrder !== '' ? $onlineOrder : null,
'internal_order_no' => $internalOrder !== '' ? $internalOrder : null,
'order_key' => $shopName . '|' . $orderNo,
'source_payload' => json_encode($record, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
'synced_at' => $now,
'created_at' => $now,
'updated_at' => $now,
];
foreach (self::FEE_FIELDS as $title => $column) {
$row[$column] = $this->decimal($this->value($record, $fields[$column] ?? null));
}
$rows[] = $row;
}
return $rows;
}
/**
* @param list<array<string,mixed>> $heads
*/
private function fieldForTitle(array $heads, string $title): ?string
{
foreach ($heads as $head) {
if (trim((string) ($head['title'] ?? '')) === $title) {
$field = trim((string) ($head['fld'] ?? ''));
return $field === '' ? null : $field;
}
}
return null;
}
/** @param array<string,mixed> $record */
private function value(array $record, ?string $field): mixed
{
return $field === null ? null : ($record[$field] ?? null);
}
private function decimal(mixed $value): ?float
{
if ($value === null || $value === '') {
return null;
}
return is_numeric($value) ? round((float) $value, 2) : null;
}
}

View File

@ -173,7 +173,7 @@
<div class="order-profit-note">
<span class="material-icons-outlined" aria-hidden="true">info</span>
<span>商品成本、退款、退货类型和退平台补贴来自聚水潭订单利润共享镜像;包材费、快递费、账单费用是聚胜算订单利润页面的自动匹配结果,当前商品利润明细镜像不含该结果,不能从现有字段推算,未取得页面结果时显示“—”。</span>
<span>商品成本、退款、退货类型和退平台补贴来自聚水潭订单利润共享镜像;包材费、快递费、账单费用直接读取聚胜算订单利润页面结果,未同步到该订单时显示“—”。</span>
</div>
@if ($orderProfitError ?? null)
@ -188,6 +188,7 @@
<div class="order-profit-sync">
数据状态:{{ ($status['ready'] ?? false) ? '共享镜像已就绪' : '共享镜像同步中' }}
@if (!empty($status['last_succeeded_at'])) · 最近同步 {{ $status['last_succeeded_at'] }} @endif
@if (!empty($status['last_fees_synced_at'])) · 胜算费用同步 {{ $status['last_fees_synced_at'] }} @endif
</div>
@endif
<div class="upstream-data-table-scroll">

View File

@ -0,0 +1,224 @@
#!/usr/bin/env python3
"""从聚胜算订单利润页面接口抓取页面结果并导入财务系统。
使用前需要本机有已登录聚胜算的 Chrome 调试窗口(remote debugging port 9333),
并且本机 Docker 容器 jiangling_app 运行财务系统。
示例:
# 指定订单
python3 scripts/order-profit-shengsuan-sync.py --orders 5127581702031023636
# 按支付时间拉 7 天
python3 scripts/order-profit-shengsuan-sync.py --date-fld pay_date \
--from 2026-11-01 --to 2026-11-07
# 只打印 JSON,不导入
python3 scripts/order-profit-shengsuan-sync.py --orders 5127581702031023636 --no-import
"""
import argparse
import json
import subprocess
import sys
import time
import urllib.request
import websocket
API_URL = 'https://pf.erp321.com/WebApi/PF/OrderSku/GetOrderSkuList?uvalue=pfcweb_profit-report.order-profit'
CHROME_LIST_URL = 'http://127.0.0.1:9333/json/list'
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, await_promise: bool = False):
payload = {
'id': 1,
'method': 'Runtime.evaluate',
'params': {
'expression': expression,
'returnByValue': True,
'awaitPromise': await_promise,
},
}
ws.send(json.dumps(payload))
while True:
message = json.loads(ws.recv())
if message.get('id') == 1:
if 'error' in message:
raise RuntimeError(json.dumps(message['error'], ensure_ascii=False))
if 'exceptionDetails' in message.get('result', {}):
raise RuntimeError(json.dumps(message['result']['exceptionDetails'], ensure_ascii=False))
return message.get('result', {}).get('result', {}).get('value')
def fetch_page(ws: websocket.WebSocket, condition: dict, current_page: int, page_size: int):
request = {
'uid': '', 'coid': '', 'ip': '',
'page': {'currentPage': current_page, 'pageSize': page_size},
'data': {'condition': condition, 'isAds': False},
'ssProjectType': 'SS',
'ssClientType': 'Web',
}
expression = f"""(async () => {{
const res = await fetch('{API_URL}', {{
method: 'POST', credentials: 'include',
headers: {{'Content-Type': 'application/json'}},
body: JSON.stringify({json.dumps(request, ensure_ascii=False)})
}});
const text = await res.text();
return {{status: res.status, text}};
}})()"""
result = evaluate(ws, expression, await_promise=True)
if result is None or result.get('status') != 200:
raise RuntimeError(f'接口返回异常:{result}')
decoded = json.loads(result['text'])
if decoded.get('code') == 1908:
raise RuntimeError('RATE_LIMIT')
if decoded.get('code') not in (0, None):
raise RuntimeError(f"接口业务错误 code={decoded.get('code')} msg={decoded.get('msg')}")
return decoded
def condition_from_args(args):
base = {
'type': 'soId',
'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,
'skuCostAmountSearchType': 2,
'skuCostAmountList': [],
'firstfreightList': [''],
'stateCityList': [],
'isOL': True,
'skugroup': 'so_id',
'isValidateDate': False,
'isCrossBorder': False,
}
if args.orders:
base['type'] = 'soId'
base['idType'] = ','.join(args.orders)
base['dateFld'] = 'pay_date'
return base
base['dateFld'] = args.date_fld
base['type'] = 'oid' if args.date_fld == 'send_date' else 'soId'
base['groupByType'] = 1 if base['type'] == 'soId' else 2
base['combineDate'] = None
base['beginDate'] = args.begin
base['endDate'] = args.end
if args.shop:
base['shop'] = [{'value': shop, 'label': shop} for shop in args.shop]
return base
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument('--orders', nargs='*', default=[])
parser.add_argument('--date-fld', choices=['pay_date', 'send_date', 'order_date'], default='pay_date')
parser.add_argument('--from', dest='begin', default=None)
parser.add_argument('--to', dest='end', default=None)
parser.add_argument('--shop', nargs='*', default=[])
parser.add_argument('--page-size', type=int, default=5000)
parser.add_argument('--max-pages', type=int, default=0)
parser.add_argument('--page-delay', type=float, default=0.8)
parser.add_argument('--debug-port', type=int, default=9333)
parser.add_argument('--no-import', action='store_true')
parser.add_argument('--json-out', default='/tmp/order-profit-shengsuan.json')
args = parser.parse_args()
if not args.orders and (not args.begin or not args.end):
parser.error('订单模式需要 --orders,日期模式需要 --from 和 --to')
target = page_target(args.debug_port)
ws = websocket.create_connection(target['webSocketDebuggerUrl'], timeout=180)
try:
ws.send(json.dumps({'id': 0, 'method': 'Runtime.enable', 'params': {}}))
condition = condition_from_args(args)
current = 1
collected = []
heads = None
total = None
while True:
page = None
for attempt in range(1, 8):
try:
page = fetch_page(ws, condition, current, args.page_size)
break
except RuntimeError as exception:
if str(exception) != 'RATE_LIMIT':
raise
wait = min(300, 60 * attempt)
print(f'第 {attempt} 次限流,等待 {wait} 秒后重试……', flush=True)
time.sleep(wait)
if page is None:
raise RuntimeError('聚胜算接口持续限流,本次未完成。')
data = page.get('data') or {}
if heads is None:
heads = data.get('fldHeads') or []
collected.extend(data.get('dataList') or [])
page_info = page.get('page') or {}
total = page_info.get('count', len(collected))
pages = page_info.get('pages', 1)
if current >= pages or len(collected) >= total:
break
if args.max_pages and current >= args.max_pages:
break
current += 1
time.sleep(args.page_delay)
payload = {'data': {'fldHeads': heads, 'dataList': collected}, 'page': {'count': total}}
text = json.dumps(payload, ensure_ascii=False)
with open(args.json_out, 'w') as handle:
handle.write(text)
print(f'抓取 {len(collected)} 条,JSON:{args.json_out}')
if not args.no_import:
process = subprocess.run(
['docker', 'exec', '-i', '-u', 'www-data', 'jiangling_app',
'php', '-d', 'memory_limit=2G', 'artisan',
'order-profit:import-shengsuan-rows', '--stdin'],
input=text.encode('utf-8'),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=False,
)
sys.stdout.write(process.stdout.decode('utf-8', 'replace'))
if process.returncode != 0:
sys.stderr.write(process.stderr.decode('utf-8', 'replace'))
return process.returncode
finally:
ws.close()
return 0
if __name__ == '__main__':
raise SystemExit(main())

View File

@ -127,7 +127,7 @@ class OrderProfitPageTest extends FeatureTestCase
->assertSeeText('75.00')
->assertSeeText('20.00')
->assertSeeText('仅退款')
->assertSeeText('包材费、快递费、账单费用是聚胜算订单利润页面的自动匹配结果')
->assertSeeText('包材费、快递费、账单费用直接读取聚胜算订单利润页面结果')
->assertSeeText('743208893')
->assertSeeTextInOrder(['内部单号', '明细', '订单时间'])
->assertSee('data-finance-fixed="true"', false)

View File

@ -8,6 +8,7 @@ use Illuminate\Support\Facades\Schema;
use Modules\EcommerceFinance\Repositories\GoodsProfitAfsRepository;
use Modules\EcommerceFinance\Repositories\OrderProfitRepository;
use Modules\EcommerceFinance\Services\GoodsProfitAfsLookupSchema;
use Modules\EcommerceFinance\Services\ShengsuanOrderProfitRowMapper;
use Tests\TestCase;
class OrderProfitRepositoryTest extends TestCase
@ -59,6 +60,16 @@ class OrderProfitRepositoryTest extends TestCase
$table->dateTime('last_succeeded_at')->nullable();
$table->string('last_error_code')->nullable();
});
$schema->create(ShengsuanOrderProfitRowMapper::TABLE, function (Blueprint $table): void {
$table->bigIncrements('id');
$table->string('shop_name', 191)->default('');
$table->string('order_key', 191)->default('');
$table->decimal('matched_package_fee', 18, 2)->nullable();
$table->decimal('matched_freight_fee', 18, 2)->nullable();
$table->decimal('matched_bill_fee', 18, 2)->nullable();
$table->dateTime('synced_at')->nullable();
$table->timestamps();
});
$db = DB::connection('business_mirror');
$db->table('business_mirror_table_states')->insert([
@ -272,4 +283,36 @@ class OrderProfitRepositoryTest extends TestCase
$this->assertSame('26092142570188', $second['rows'][0]['online_order_no']);
$this->assertFalse($second['has_more']);
}
public function test_matched_fees_come_from_shengsuan_page_rows(): void
{
DB::connection('business_mirror')->table(ShengsuanOrderProfitRowMapper::TABLE)->insert([
'shop_name' => '唯品会MP-卓禾家具特卖旗舰店',
'order_key' => '唯品会MP-卓禾家具特卖旗舰店|26092142570188',
'matched_package_fee' => 0,
'matched_freight_fee' => 10.5,
'matched_bill_fee' => -0.5,
'synced_at' => now(),
'created_at' => now(),
'updated_at' => now(),
]);
$repository = app(OrderProfitRepository::class);
$result = $repository->paginate([
'date_field' => 'order_time',
'from' => '2026-09-01',
'to' => '2026-09-30',
'order_no' => '26092142570188',
'shop_name' => '',
'sku_id' => '',
], 1, 25);
$this->assertSame(
'唯品会MP-卓禾家具特卖旗舰店|26092142570188',
$result['rows'][0]['shop_name'] . '|' . $result['rows'][0]['order_key'],
);
$this->assertSame(0.0, $result['rows'][0]['matched_package_fee']);
$this->assertSame(10.5, $result['rows'][0]['matched_freight_fee']);
$this->assertSame(-0.5, $result['rows'][0]['matched_bill_fee']);
}
}

View File

@ -0,0 +1,51 @@
<?php
namespace Tests\Unit\EcommerceFinance;
use Modules\EcommerceFinance\Services\ShengsuanOrderProfitRowMapper;
use Tests\TestCase;
class ShengsuanOrderProfitRowMapperTest extends TestCase
{
public function test_maps_page_headers_and_keeps_the_three_matched_fees(): void
{
$mapper = new ShengsuanOrderProfitRowMapper();
$rows = $mapper->rows([
'data' => [
'fldHeads' => [
['fld' => 'c_1', 'title' => '店铺名称'],
['fld' => 'c_2', 'title' => '线上单号'],
['fld' => 'c_3', 'title' => '内部单号'],
['fld' => 'c_4', 'title' => '包材费(自动匹配)'],
['fld' => 'c_5', 'title' => '快递费(自动匹配)'],
['fld' => 'c_6', 'title' => '账单费用(自动匹配)'],
['fld' => 'c_7', 'title' => '人工维护'],
['fld' => 'c_8', 'title' => '订单费用'],
],
'dataList' => [[
'c_1' => '天猫-卓禾家具旗舰店',
'c_2' => '5127581702031023636',
'c_3' => '10097143',
'c_4' => 0,
'c_5' => 7.05,
'c_6' => -0.5,
'c_7' => 3.6,
'c_8' => 10.15,
]],
],
]);
$this->assertCount(1, $rows);
$row = $rows[0];
$this->assertSame('天猫-卓禾家具旗舰店', $row['shop_name']);
$this->assertSame('5127581702031023636', $row['online_order_no']);
$this->assertSame('10097143', $row['internal_order_no']);
$this->assertSame('天猫-卓禾家具旗舰店|5127581702031023636', $row['order_key']);
$this->assertSame(0.0, $row['matched_package_fee']);
$this->assertSame(7.05, $row['matched_freight_fee']);
$this->assertSame(-0.5, $row['matched_bill_fee']);
$this->assertSame(3.6, $row['manual_fee']);
$this->assertSame(10.15, $row['order_fee']);
$this->assertArrayHasKey('source_payload', $row);
}
}