文件
Henglianshutong/addons/shopro/service/partner/PeerShareReward.php

94 行
3.2 KiB
PHP

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
namespace addons\shopro\service\partner;
/**
* 酒道馆 / 区县协同奖 10% 逐代递减级联,最低发放不能少于 0.10 元截断
*/
class PeerShareReward
{
public function run($order, $buyerId, $chain, $teamResult, ActiveService $active)
{
$venueRate = (float) PartnerConfig::get('partner_venue_peer_rate', 0.10);
$countyRate = (float) PartnerConfig::get('partner_county_peer_rate', 0.10);
if (!empty($teamResult['venue'])) {
$this->peer($order, $buyerId, $chain, $teamResult['venue'], 'is_venue', 'venue_peer', 'venue', $venueRate, $active, '酒道馆');
}
if (!empty($teamResult['county'])) {
$this->peer($order, $buyerId, $chain, $teamResult['county'], 'is_county', 'county_peer', 'county', $countyRate, $active, '区县');
}
}
protected function peer($order, $buyerId, $chain, $from, $flag, $type, $role, $rate, ActiveService $active, $label)
{
$minAmount = (float) PartnerConfig::get('partner_peer_min_amount', 0.10);
if ($minAmount <= 0) {
$minAmount = 0.10;
}
$maxGenerations = intval(PartnerConfig::get("partner_{$role}_peer_max_generations", 50));
if ($maxGenerations <= 0) {
$maxGenerations = 50;
}
$currentFrom = intval($from['index']);
$currentBase = (float) $from['amount'];
$sourceUserId = $from['user_id'];
for ($gen = 1; $gen <= $maxGenerations; $gen++) {
$peerIdx = -1;
for ($i = $currentFrom + 1; $i < count($chain); $i++) {
if (intval($chain[$i][$flag] ?? 0)) {
$peerIdx = $i;
break;
}
}
if ($peerIdx < 0) {
break;
}
$peer = $chain[$peerIdx];
$amount = round($currentBase * $rate, 2);
if ($amount < $minAmount) {
break; // 收益少于 0.10 元,上级平级不再拿,严格截断
}
if (!$active->isActive($peer['agent'] ?? $peer)) {
PartnerRewardService::writeSkip(
$order,
$buyerId,
$peer['user_id'],
$type,
$role,
'inactive_30d',
"{$label}协同(第{$gen}代) 30 日无自购"
);
} else {
PartnerRewardService::account(
$order,
$buyerId,
$peer['user_id'],
$type,
$role,
$amount,
$currentBase,
[
'generation' => $gen,
'formula' => "{$label}协同(第{$gen}代) {$currentBase} × " . ($rate * 100) . "% = {$amount}",
'from_user_id' => $sourceUserId,
]
);
}
$currentFrom = $peerIdx;
$currentBase = $amount;
$sourceUserId = $peer['user_id'];
if ($currentBase < $minAmount) {
break;
}
}
}
}