191 行
7.3 KiB
PHP
191 行
7.3 KiB
PHP
<?php
|
||
|
||
namespace addons\shopro\service\partner;
|
||
|
||
use app\admin\model\shopro\commission\Agent as AgentModel;
|
||
|
||
/**
|
||
* dmepa日结 1:1 GMV 均分
|
||
*/
|
||
class MadowService
|
||
{
|
||
public function share($settleDate, $gmv, $userIds)
|
||
{
|
||
// 幂等防重:检查该结算日期是否已分发过madow
|
||
$existing = \think\Db::name('shopro_partner_reward')
|
||
->where('settle_date', $settleDate)
|
||
->where('reward_type', 'madow_share')
|
||
->find();
|
||
if ($existing) {
|
||
$pool = (float) $gmv;
|
||
$count = count($userIds);
|
||
$each = $existing['madow'] ?? 0;
|
||
return ['pool' => $pool, 'count' => $count, 'each' => $each];
|
||
}
|
||
|
||
$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,dmepa池 {$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;
|
||
$dailyOrderId = PartnerRewardService::dailyOrderId($settleDate);
|
||
$now = time();
|
||
$chunks = array_chunk($userIds, 500);
|
||
|
||
foreach ($chunks as $chunk) {
|
||
AgentModel::whereIn('user_id', $chunk)->setInc('madow', $each);
|
||
$batchRows = [];
|
||
foreach ($chunk as $userId) {
|
||
$batchRows[] = [
|
||
'order_id' => $dailyOrderId,
|
||
'buyer_id' => 0,
|
||
'user_id' => $userId,
|
||
'role' => 'taster',
|
||
'reward_type' => 'madow_share',
|
||
'amount' => 0,
|
||
'madow' => $each,
|
||
'base_amount' => $pool,
|
||
'settle_date' => $settleDate,
|
||
'ext' => json_encode(['formula' => "GMV {$pool} / {$count} = {$each}", 'share_user_count' => $count, 'gmv' => $pool], JSON_UNESCAPED_UNICODE),
|
||
'status' => 1,
|
||
'createtime' => $now,
|
||
'updatetime' => $now,
|
||
];
|
||
$distributed = function_exists('bcadd') ? bcadd((string) $distributed, (string) $each, 8) : ($distributed + $each);
|
||
}
|
||
\think\Db::name('shopro_partner_reward')->insertAll($batchRows);
|
||
}
|
||
$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];
|
||
}
|
||
|
||
/**
|
||
* 临时分钟级dmepa结算均分
|
||
*/
|
||
public function shareMinute($gmv, $userIds, $orderIds = [])
|
||
{
|
||
$count = count($userIds);
|
||
$pool = (float) $gmv;
|
||
if ($pool <= 0) {
|
||
return ['pool' => 0, 'count' => $count, 'each' => 0];
|
||
}
|
||
|
||
$minId = (int) \think\Db::name('shopro_partner_reward')->where('order_id', '<', 0)->min('order_id');
|
||
$virtualOrderId = min(-100000, $minId - 1);
|
||
|
||
if ($count <= 0) {
|
||
PartnerRewardService::write([
|
||
'order_id' => $virtualOrderId,
|
||
'buyer_id' => 0,
|
||
'user_id' => 0,
|
||
'role' => 'system',
|
||
'reward_type' => 'madow_share',
|
||
'amount' => 0,
|
||
'madow' => $pool,
|
||
'base_amount' => $pool,
|
||
'settle_date' => null,
|
||
'ext' => ['formula' => "分母为0,dmepa池 {$pool} 进平台", 'orders' => $orderIds],
|
||
'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;
|
||
$now = time();
|
||
$chunks = array_chunk($userIds, 500);
|
||
|
||
foreach ($chunks as $chunk) {
|
||
AgentModel::whereIn('user_id', $chunk)->setInc('madow', $each);
|
||
$batchRows = [];
|
||
foreach ($chunk as $userId) {
|
||
$batchRows[] = [
|
||
'order_id' => $virtualOrderId,
|
||
'buyer_id' => 0,
|
||
'user_id' => $userId,
|
||
'role' => 'taster',
|
||
'reward_type' => 'madow_share',
|
||
'amount' => 0,
|
||
'madow' => $each,
|
||
'base_amount' => $pool,
|
||
'settle_date' => null,
|
||
'ext' => json_encode(['formula' => "GMV {$pool} / {$count} = {$each}", 'share_user_count' => $count, 'gmv' => $pool, 'orders' => $orderIds], JSON_UNESCAPED_UNICODE),
|
||
'status' => 1,
|
||
'createtime' => $now,
|
||
'updatetime' => $now,
|
||
];
|
||
$distributed = function_exists('bcadd') ? bcadd((string) $distributed, (string) $each, 8) : ($distributed + $each);
|
||
}
|
||
\think\Db::name('shopro_partner_reward')->insertAll($batchRows);
|
||
}
|
||
|
||
$tail = function_exists('bcsub') ? bcsub((string) $pool, (string) $distributed, 8) : round($pool - $distributed, 8);
|
||
if ((float) $tail > 0) {
|
||
PartnerRewardService::write([
|
||
'order_id' => $virtualOrderId,
|
||
'buyer_id' => 0,
|
||
'user_id' => 0,
|
||
'role' => 'system',
|
||
'reward_type' => 'madow_share',
|
||
'amount' => 0,
|
||
'madow' => $tail,
|
||
'base_amount' => $pool,
|
||
'settle_date' => null,
|
||
'ext' => ['formula' => "尾差 {$tail} 进平台", 'orders' => $orderIds],
|
||
'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) {
|
||
if ((float) $row->madow > 0) {
|
||
AgentModel::where('user_id', $row->user_id)->setDec('madow', $row->madow);
|
||
}
|
||
}
|
||
}
|
||
}
|