分账核验更新

这个提交包含在:
可乐加冰
2026-09-29 11:52:53 +08:00
父节点 fe83f69638
当前提交 0e513d7ecc
共有 3 个文件被更改,包括 95 次插入 和 30 次删除

查看文件

@@ -66,6 +66,53 @@ switch ($command) {
exit(1);
}
function tableHasColumn($table, $column)
{
static $cache = [];
$key = $table . '.' . $column;
if (array_key_exists($key, $cache)) {
return $cache[$key];
}
$full = config('database.prefix') . $table;
try {
$col = str_replace(['`', "'"], '', $column);
$rows = Db::query("SHOW COLUMNS FROM `{$full}` LIKE '{$col}'");
} catch (\Exception $e) {
$cache[$key] = false;
return false;
}
$cache[$key] = !empty($rows);
return $cache[$key];
}
/** 按订单查区域佣金(兼容无 order_id 字段的旧表结构) */
function selectRegionalRewardsByOrderId($orderId)
{
if (tableHasColumn('shopro_regional_agent_reward', 'order_id')) {
return Db::name('shopro_regional_agent_reward')->where('order_id', $orderId)->select();
}
return Db::name('shopro_regional_agent_reward')->alias('rr')
->join('shopro_regional_agent_order ro', 'ro.id = rr.regional_order_id')
->where('ro.order_id', $orderId)
->field('rr.*')
->select();
}
/** 订单是否存在分销/区域佣金(用于筛选) */
function sqlOrderHasAnyReward($orderIdExpr = 'o.id')
{
$pfx = config('database.prefix');
$dist = "EXISTS (SELECT 1 FROM {$pfx}shopro_commission_reward cr WHERE cr.order_id = {$orderIdExpr} AND cr.status >= 0)";
if (tableHasColumn('shopro_regional_agent_reward', 'order_id')) {
$reg = "EXISTS (SELECT 1 FROM {$pfx}shopro_regional_agent_reward rr WHERE rr.order_id = {$orderIdExpr} AND rr.status >= 0)";
} else {
$reg = "EXISTS (SELECT 1 FROM {$pfx}shopro_regional_agent_reward rr "
. "INNER JOIN {$pfx}shopro_regional_agent_order ro ON ro.id = rr.regional_order_id "
. "WHERE ro.order_id = {$orderIdExpr} AND rr.status >= 0)";
}
return "({$dist} OR {$reg})";
}
function splitConfigMap()
{
return Db::name('shopro_split_config')->column('value', 'name');
@@ -269,17 +316,7 @@ function fetchMissingSplitOrders($since, $limit = 20)
->where('so.channel', 'huifu')
->where('so.status', 'in', [1, 2, 3]);
})
->where(function ($q) {
$q->whereExists(function ($sq) {
$sq->name('shopro_commission_reward')->alias('cr')
->whereRaw('cr.order_id = o.id')
->where('cr.status', '>=', 0);
})->whereOr(function ($sq) {
$sq->name('shopro_regional_agent_reward')->alias('rr')
->whereRaw('rr.order_id = o.id')
->where('rr.status', '>=', 0);
});
})
->whereRaw(sqlOrderHasAnyReward('o.id'))
->field('o.id, o.order_sn, o.pay_fee, o.pay_type, o.paytime, o.transaction_id')
->order('o.id desc')
->limit($limit)
@@ -337,10 +374,10 @@ function diagnoseOrderSplitBlockers($orderId)
$rc = $recv ? $recv['receiver_account'] : '无 receiver';
$reasons[] = " 推广员 user_id={$uid} 佣金={$rw['commission']} 进件={$ob} split_receiver={$rc}";
}
$regRewards = Db::name('shopro_regional_agent_reward')
->where('order_id', $orderId)
->where('status', '>=', 0)
->select();
$regRewards = selectRegionalRewardsByOrderId($orderId);
$regRewards = array_filter($regRewards, function ($rw) {
return intval($rw['status']) >= 0;
});
foreach ($regRewards as $rw) {
$uid = $rw['agent_user_id'];
$recv = Db::name('shopro_split_receiver')
@@ -365,7 +402,9 @@ function diagnoseOrderSplitBlockers($orderId)
$walletCnt = 0;
$crIds = Db::name('shopro_commission_reward')->where('order_id', $orderId)->where('status', 1)->column('id');
$rrIds = Db::name('shopro_regional_agent_reward')->where('order_id', $orderId)->where('status', 1)->column('id');
$rrIds = array_column(array_filter(selectRegionalRewardsByOrderId($orderId), function ($r) {
return intval($r['status']) === 1;
}), 'id');
$allIds = array_merge($crIds ?: [], $rrIds ?: []);
if ($allIds) {
$walletCnt = Db::name('shopro_user_wallet_log')
@@ -458,7 +497,7 @@ function printOrderDetail($orderSn)
}
$cr = Db::name('shopro_commission_reward')->where('order_id', $oid)->select();
$rr = Db::name('shopro_regional_agent_reward')->where('order_id', $oid)->select();
$rr = selectRegionalRewardsByOrderId($oid);
echo "\n【分销/区域佣金记录(业务层)】\n";
echo "commission_reward: " . count($cr) . " 条\n";
echo "regional_reward: " . count($rr) . " 条\n";