Commit 4ee671c3 authored by 刘小敏's avatar 刘小敏

fix(share,agent): 优化分享页路径匹配及代理商分佣逻辑

parent 5bc97bd7
...@@ -13,3 +13,4 @@ composer.lock ...@@ -13,3 +13,4 @@ composer.lock
/public/uploads /public/uploads
.codebuddy/memory/2025-07-07.md .codebuddy/memory/2025-07-07.md
.codebuddy/memory/2026-07-08.md
...@@ -90,25 +90,28 @@ class Share extends Common ...@@ -90,25 +90,28 @@ class Share extends Common
} }
// custom_log('[share.log] 重复检查通过', $userId, 'share'); // custom_log('[share.log] 重复检查通过', $userId, 'share');
$memoText = '通过' . (self::FROM)[$params['from']] . '访问了'; $memoText = '通过' . (self::FROM)[$params['from']] . '访问了-';
if ($params['page'] == '/pages/index/index') { if (trim($params['page'], '\\/') == '/pages/index/index') {
$memoText .= '首页'; $memoText .= '首页';
} }
if ($params['page'] === '/pages/goods/index') { if (trim($params['page'], '\\/') === 'pages\/goods\/index') {
$memoText .= '商品'; $memoText .= '商品';
$goodsId = $params['query']['id']; $goodsId = $params['query']['id'];
} }
if ($params['page'] === '/pages/goods/groupon') { if (trim($params['page'], '\\/') === 'pages\/goods\/groupon') {
$memoText .= '拼团商品'; $memoText .= '拼团商品';
$goodsId = $params['query']['id']; $goodsId = $params['query']['id'];
} }
if ($params['page'] === '/pages/goods/seckill') { if (trim($params['page'], '\\/') === 'pages\/goods\/seckill') {
$memoText .= '秒杀商品'; $memoText .= '秒杀商品';
$goodsId = $params['query']['id']; $goodsId = $params['query']['id'];
} }
if ($params['page'] === '/pages/activity/groupon/detail') { if (trim($params['page'], '\\/') === 'pages\/activity\/groupon\/detail') {
$memoText .= '拼团活动'; $memoText .= '拼团活动';
} }
if (trim($params['page'], '\\/') === 'pages\/index\/aiGenerated') {
$memoText .= 'AI试戴详情';
}
if (!empty($goodsId)) { if (!empty($goodsId)) {
$goods = GoodsModel::find($goodsId); $goods = GoodsModel::find($goodsId);
......
...@@ -267,8 +267,8 @@ class AgentOrderCommission extends Command ...@@ -267,8 +267,8 @@ class AgentOrderCommission extends Command
// 获取订单商品信息 // 获取订单商品信息
$orderItems = $this->getOrderItems($orderId); $orderItems = $this->getOrderItems($orderId);
// 查找两级代理商 // 查找两级代理商,确保代理商申请成功后的订单才分佣
$agents = $this->findAgentChain($buyerUserId, 2); $agents = $this->findAgentChain($buyerUserId, $order['createtime'], 2);
if (empty($agents)) { if (empty($agents)) {
$this->customLog('info', "订单[{$orderSn}]跳过: 未找到有效的上级代理商, 买家[{$buyerUserId}]"); $this->customLog('info', "订单[{$orderSn}]跳过: 未找到有效的上级代理商, 买家[{$buyerUserId}]");
...@@ -284,19 +284,19 @@ class AgentOrderCommission extends Command ...@@ -284,19 +284,19 @@ class AgentOrderCommission extends Command
// 从 fa_shopro_agent 表获取该代理商的佣金比例及记录ID // 从 fa_shopro_agent 表获取该代理商的佣金比例及记录ID
$agentRecord = Db::name('shopro_agent') $agentRecord = Db::name('shopro_agent')
->where('user_id', $agentUserId) ->where('user_id', $agentUserId)
->field('id, user_id, commission_rate') ->field('id, user_id, commission_rate, createtime')
->find(); ->find();
$recordAgentId = $agentRecord['id'];
$commissionRate = floatval($agentRecord['commission_rate']);
if (!$agentRecord) { if (!$agentRecord) {
$this->customLog('warning', "订单[{$orderSn}]代理商user_id[{$agentUserId}]在shopro_agent表中不存在, 跳过"); $this->customLog('warning', "订单[{$orderSn}]代理商[{$recordAgentId}}]用户[{$agentUserId}]在shopro_agent表中不存在, 跳过");
continue; continue;
} }
$recordAgentId = $agentRecord['id'];
$commissionRate = floatval($agentRecord['commission_rate']);
if ($commissionRate <= 0) { if ($commissionRate <= 0) {
$this->customLog('warning', "订单[{$orderSn}]代理商[{$recordAgentId}]commission_rate为0或未设置, 跳过"); $this->customLog('warning', "订单[{$orderSn}]代理商[{$recordAgentId}]用户[{$agentUserId}]commission_rate为0或未设置, 跳过");
continue; continue;
} }
...@@ -304,7 +304,7 @@ class AgentOrderCommission extends Command ...@@ -304,7 +304,7 @@ class AgentOrderCommission extends Command
$commissionAmount = round($payFee * ($commissionRate / 100), 2); $commissionAmount = round($payFee * ($commissionRate / 100), 2);
if ($commissionAmount <= 0) { if ($commissionAmount <= 0) {
$this->customLog('warning', "订单[{$orderSn}]代理商[{$recordAgentId}]计算佣金为0, 跳过"); $this->customLog('warning', "订单[{$orderSn}]代理商[{$recordAgentId}]用户[{$agentUserId}]计算佣金为0, 跳过");
continue; continue;
} }
...@@ -324,9 +324,9 @@ class AgentOrderCommission extends Command ...@@ -324,9 +324,9 @@ class AgentOrderCommission extends Command
'buyer_user_id' => $buyerUserId, 'buyer_user_id' => $buyerUserId,
'receive_province_id' => $provinceInfo['province_id'] ?? 0, 'receive_province_id' => $provinceInfo['province_id'] ?? 0,
'receive_province_name' => $provinceInfo['province_name'] ?? '', 'receive_province_name' => $provinceInfo['province_name'] ?? '',
'goods_id' => $goodsInfo['goods_id'] ?? 0, // 'goods_id' => $goodsInfo['goods_id'] ?? 0,
'goods_title' => $goodsInfo['goods_title'] ?? '', // 'goods_title' => $goodsInfo['goods_title'] ?? '',
'goods_pay_price' => $goodsPayPrice, // 'goods_pay_price' => $goodsPayPrice,
'commission_rate_snapshot' => $commissionRate, 'commission_rate_snapshot' => $commissionRate,
'commission_amount' => $commissionAmount, 'commission_amount' => $commissionAmount,
'order_status' => 'completed', 'order_status' => 'completed',
...@@ -346,13 +346,12 @@ class AgentOrderCommission extends Command ...@@ -346,13 +346,12 @@ class AgentOrderCommission extends Command
$exists = Db::name('shopro_agent_order_commission') $exists = Db::name('shopro_agent_order_commission')
->where('order_id', $orderId) ->where('order_id', $orderId)
->where('agent_id', $recordAgentId) ->where('agent_id', $recordAgentId)
->where('status', 'in', [0, 1])
->lock(true) // 行级锁,进一步阻止并发 ->lock(true) // 行级锁,进一步阻止并发
->find(); ->find();
if ($exists) { if ($exists) {
Db::rollback(); Db::rollback();
$this->customLog('info', "订单[{$orderSn}]代理商[{$recordAgentId}]第{$agentLevel}级已存在佣金记录(事务内), 跳过"); $this->customLog('info', "订单[{$orderSn}]代理商[{$recordAgentId}]用户[{$agentUserId}]{$agentLevel}级已存在佣金记录(事务内), 跳过");
continue; continue;
} }
...@@ -364,7 +363,7 @@ class AgentOrderCommission extends Command ...@@ -364,7 +363,7 @@ class AgentOrderCommission extends Command
Db::rollback(); Db::rollback();
// 捕获唯一索引冲突(如有索引),说明真的重复了 // 捕获唯一索引冲突(如有索引),说明真的重复了
if (stripos($e->getMessage(), 'Duplicate entry') !== false) { if (stripos($e->getMessage(), 'Duplicate entry') !== false) {
$this->customLog('info', "订单[{$orderSn}]代理商[{$recordAgentId}]唯一索引冲突,已存在记录, 跳过"); $this->customLog('info', "订单[{$orderSn}]代理商[{$recordAgentId}]用户[{$agentUserId}]唯一索引冲突,已存在记录, 跳过");
} else { } else {
$this->customLog('error', "订单[{$orderSn}]写入异常: " . $e->getMessage()); $this->customLog('error', "订单[{$orderSn}]写入异常: " . $e->getMessage());
} }
...@@ -376,7 +375,7 @@ class AgentOrderCommission extends Command ...@@ -376,7 +375,7 @@ class AgentOrderCommission extends Command
} }
if ($inserted) { if ($inserted) {
$this->customLog('info', "订单[{$orderSn}]第{$agentLevel}级代理商[{$recordAgentId}]佣金: ¥{$commissionAmount} (订单金额: ¥{$payFee})"); $this->customLog('info', "订单[{$orderSn}]第{$agentLevel}级代理商[{$recordAgentId}]用户[{$agentUserId}]佣金: ¥{$commissionAmount} (订单金额: ¥{$payFee})");
$this->affectedAgentIds[$recordAgentId] = true; $this->affectedAgentIds[$recordAgentId] = true;
$processedCount++; $processedCount++;
} }
...@@ -406,10 +405,11 @@ class AgentOrderCommission extends Command ...@@ -406,10 +405,11 @@ class AgentOrderCommission extends Command
* 最多向上追溯两级。仅收集状态为 normal 或 freeze 的代理商。 * 最多向上追溯两级。仅收集状态为 normal 或 freeze 的代理商。
* *
* @param int $buyerUserId 买家用户ID * @param int $buyerUserId 买家用户ID
* @param int $order_create_time 订单创建时间(时间戳)
* @param int $maxLevel 最大追溯层级(默认2级) * @param int $maxLevel 最大追溯层级(默认2级)
* @return array [['user_id' => xxx, 'level' => 1], ...] * @return array [['user_id' => xxx, 'level' => 1], ...]
*/ */
protected function findAgentChain($buyerUserId, $maxLevel = 2) protected function findAgentChain($buyerUserId, int $order_create_time, $maxLevel = 2)
{ {
$agents = []; $agents = [];
$currentUserId = $buyerUserId; $currentUserId = $buyerUserId;
...@@ -428,9 +428,10 @@ class AgentOrderCommission extends Command ...@@ -428,9 +428,10 @@ class AgentOrderCommission extends Command
$parentUserId = $user['parent_user_id']; $parentUserId = $user['parent_user_id'];
// 检查上级是否为有效代理商 // 检查上级是否为有效代理商,确保代理商申请成功后的订单才分佣
$agent = Db::name('shopro_commission_agent') $agent = Db::name('shopro_agent')
->where('user_id', $parentUserId) ->where('user_id', $parentUserId)
->where('createtime', '>', $order_create_time)
->where('status', 'in', ['normal', 'freeze']) ->where('status', 'in', ['normal', 'freeze'])
->field('user_id, level') ->field('user_id, level')
->find(); ->find();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment