@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\service\partner;
|
||||
|
||||
/**
|
||||
* 创客平级 10% / 被超越 5%,只一层
|
||||
*/
|
||||
class MakerPeerReward
|
||||
{
|
||||
public function run($order, $buyerId, $chain, $diffResult, ActiveService $active)
|
||||
{
|
||||
$bonus = PartnerConfig::makerBonus();
|
||||
$peerRate = (float) PartnerConfig::get('partner_maker_peer_rate', 0.10);
|
||||
$overRate = (float) PartnerConfig::get('partner_maker_overtaken_rate', 0.05);
|
||||
$paid = $diffResult['paid'] ?? [];
|
||||
$indexByUser = [];
|
||||
foreach ($chain as $i => $node) {
|
||||
$indexByUser[$node['user_id']] = $i;
|
||||
}
|
||||
|
||||
foreach ($paid as $item) {
|
||||
$from = $indexByUser[$item['user_id']] ?? -1;
|
||||
if ($from < 0) {
|
||||
continue;
|
||||
}
|
||||
$peer = $this->findUp($chain, $from + 1, function ($node) use ($item) {
|
||||
return intval($node['level']) === intval($item['level']);
|
||||
});
|
||||
if (!$peer) {
|
||||
continue;
|
||||
}
|
||||
$levelBonus = (float) ($bonus[$item['level']] ?? 0);
|
||||
$amount = round($levelBonus * $peerRate, 2);
|
||||
if (!$active->isActive($peer['agent'] ?? $peer)) {
|
||||
PartnerRewardService::writeSkip($order, $buyerId, $peer['user_id'], 'maker_peer', 'maker', 'inactive_30d', '平级创客 30 日无自购');
|
||||
continue;
|
||||
}
|
||||
if ($amount <= 0) {
|
||||
continue;
|
||||
}
|
||||
PartnerRewardService::account($order, $buyerId, $peer['user_id'], 'maker_peer', 'maker', $amount, $levelBonus, [
|
||||
'formula' => "平级 {$levelBonus} × " . ($peerRate * 100) . "% = {$amount}",
|
||||
'from_user_id' => $item['user_id'],
|
||||
'level' => $item['level'],
|
||||
]);
|
||||
}
|
||||
|
||||
if (empty($paid)) {
|
||||
return;
|
||||
}
|
||||
$highest = $paid[count($paid) - 1];
|
||||
$from = $indexByUser[$highest['user_id']] ?? -1;
|
||||
$lower = $this->findUp($chain, $from + 1, function ($node) use ($highest) {
|
||||
return intval($node['level']) >= 2 && intval($node['level']) < intval($highest['level']);
|
||||
});
|
||||
if (!$lower) {
|
||||
return;
|
||||
}
|
||||
$amount = round((float) $highest['cap'] * $overRate, 2);
|
||||
if (!$active->isActive($lower['agent'] ?? $lower)) {
|
||||
PartnerRewardService::writeSkip($order, $buyerId, $lower['user_id'], 'maker_overtaken', 'maker', 'inactive_30d', '被超越创客 30 日无自购');
|
||||
return;
|
||||
}
|
||||
if ($amount <= 0) {
|
||||
return;
|
||||
}
|
||||
PartnerRewardService::account($order, $buyerId, $lower['user_id'], 'maker_overtaken', 'maker', $amount, $highest['cap'], [
|
||||
'formula' => "超越奖 最高水位 {$highest['cap']} × " . ($overRate * 100) . "% = {$amount}",
|
||||
'from_user_id' => $highest['user_id'],
|
||||
]);
|
||||
}
|
||||
|
||||
protected function findUp($chain, $start, $matcher)
|
||||
{
|
||||
for ($i = $start; $i < count($chain); $i++) {
|
||||
if ($matcher($chain[$i])) {
|
||||
return $chain[$i];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户