@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\service\partner;
|
||||
|
||||
use app\admin\model\shopro\commission\Agent as AgentModel;
|
||||
|
||||
/**
|
||||
* 马豆日结 1:1 GMV 均分
|
||||
*/
|
||||
class MadowService
|
||||
{
|
||||
public function share($settleDate, $gmv, $userIds)
|
||||
{
|
||||
$count = count($userIds);
|
||||
$pool = (float) $gmv;
|
||||
if ($pool <= 0) {
|
||||
return ['pool' => 0, 'count' => $count, 'each' => 0];
|
||||
}
|
||||
if ($count <= 0) {
|
||||
PartnerRewardService::write([
|
||||
'order_id' => PartnerRewardService::dailyOrderId($settleDate),
|
||||
'buyer_id' => 0,
|
||||
'user_id' => 0,
|
||||
'role' => 'system',
|
||||
'reward_type' => 'madow_share',
|
||||
'amount' => 0,
|
||||
'madow' => $pool,
|
||||
'base_amount' => $pool,
|
||||
'settle_date' => $settleDate,
|
||||
'ext' => ['formula' => "分母为0,马豆池 {$pool} 进平台", 'skip_reason' => ''],
|
||||
'status' => 1,
|
||||
]);
|
||||
return ['pool' => $pool, 'count' => 0, 'each' => 0];
|
||||
}
|
||||
|
||||
$each = function_exists('bcdiv') ? bcdiv((string) $pool, (string) $count, 8) : number_format($pool / $count, 8, '.', '');
|
||||
$distributed = 0;
|
||||
foreach ($userIds as $userId) {
|
||||
AgentModel::where('user_id', $userId)->setInc('madow', $each);
|
||||
PartnerRewardService::write([
|
||||
'order_id' => PartnerRewardService::dailyOrderId($settleDate),
|
||||
'buyer_id' => 0,
|
||||
'user_id' => $userId,
|
||||
'role' => 'taster',
|
||||
'reward_type' => 'madow_share',
|
||||
'amount' => 0,
|
||||
'madow' => $each,
|
||||
'base_amount' => $pool,
|
||||
'settle_date' => $settleDate,
|
||||
'ext' => ['formula' => "GMV {$pool} / {$count} = {$each}", 'share_user_count' => $count, 'gmv' => $pool],
|
||||
'status' => 1,
|
||||
]);
|
||||
$distributed = function_exists('bcadd') ? bcadd((string) $distributed, (string) $each, 8) : ($distributed + $each);
|
||||
}
|
||||
$tail = function_exists('bcsub') ? bcsub((string) $pool, (string) $distributed, 8) : round($pool - $distributed, 8);
|
||||
if ((float) $tail > 0) {
|
||||
PartnerRewardService::write([
|
||||
'order_id' => PartnerRewardService::dailyOrderId($settleDate),
|
||||
'buyer_id' => 0,
|
||||
'user_id' => 0,
|
||||
'role' => 'system',
|
||||
'reward_type' => 'madow_share',
|
||||
'amount' => 0,
|
||||
'madow' => $tail,
|
||||
'base_amount' => $pool,
|
||||
'settle_date' => $settleDate,
|
||||
'ext' => ['formula' => "尾差 {$tail} 进平台"],
|
||||
'status' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
return ['pool' => $pool, 'count' => $count, 'each' => $each];
|
||||
}
|
||||
|
||||
public function rollback($settleDate)
|
||||
{
|
||||
$rows = \app\admin\model\shopro\partner\Reward::where('settle_date', $settleDate)
|
||||
->where('reward_type', 'madow_share')
|
||||
->where('status', 1)
|
||||
->where('user_id', '>', 0)
|
||||
->select();
|
||||
foreach ($rows as $row) {
|
||||
$agent = AgentModel::where('user_id', $row->user_id)->find();
|
||||
if ($agent && (float) $row->madow > 0) {
|
||||
$agent->madow = max(0, (float) $agent->madow - (float) $row->madow);
|
||||
$agent->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户