6
0
Fork 0

优化管理者津贴结算

release
李静 2022-01-15 15:49:58 +08:00
parent 7b5a10306a
commit b238b38c44
1 changed files with 32 additions and 11 deletions

View File

@ -42,8 +42,10 @@ class ManagerSubsidySettleCommand extends Command
$head = '【'.$startAt->format('Y/m/d').'-'.$endAt->format('Y/m/d').'】';
$cacheKey = $this->cacheKey($startAt, $endAt);
// 如果不是强制执行,则需检查是否已结算过
if (! $this->option('force') && Cache::has($this->cacheKey($startAt, $endAt))) {
if (! $this->option('force') && Cache::has($cacheKey)) {
return $this->warn("{$head}管理者津贴已结算");
}
@ -56,13 +58,12 @@ class ManagerSubsidySettleCommand extends Command
}
$this->info("{$head}数据写入中...");
$this->settle($startAt, $endAt);
$this->settle($startAt, $endAt, 500);
$this->info("{$head}写入成功, 总耗时: ".$this->formatDuration($tz->diffInMilliseconds(now(), false)));
$this->info("{$head}------------[结束]管理者津贴结算------------".PHP_EOL);
// 缓存31天
Cache::put($this->cacheKey($startAt, $endAt), 1, 2678400);
Cache::put($cacheKey, 1, $endAt->addMonthNoOverflow());
return 0;
}
@ -72,16 +73,30 @@ class ManagerSubsidySettleCommand extends Command
*
* @param \Illuminate\Support\Carbon $startAt
* @param \Illuminate\Support\Carbon $endAt
* @param int $count
* @return void
*/
protected function settle(Carbon $startAt, Carbon $endAt): void
protected function settle(Carbon $startAt, Carbon $endAt, int $count = 200): void
{
$feeRate = app_settings('dealer.fee_rate');
Dealer::where('is_manager', 1)->chunkById(200, function ($dealers) use ($startAt, $endAt, $feeRate) {
$time = now()->toDateTimeString();
$lastId = Cache::get(
$cacheKey = $this->cacheKey($startAt, $endAt).':last_id'
);
do {
$dealers = Dealer::where('is_manager', 1)
->forPageAfterId($count, $lastId, 'id')
->get();
$countDealers = $dealers->count();
if ($countDealers == 0) {
break;
}
$earnings = [];
$time = now()->toDateTimeString();
foreach ($dealers as $dealer) {
$salesLogs = $dealer->managerSalesLogs()
@ -96,6 +111,7 @@ class ManagerSubsidySettleCommand extends Command
foreach ($salesLogs as $log) {
$subsidy = bcmul($log->sales_volume, $log->product->manager_subsidy, 2);
$totalAmount = bcadd($totalAmount, $subsidy, 2);
if ($description !== '') {
@ -110,6 +126,7 @@ class ManagerSubsidySettleCommand extends Command
);
}
unset($salesLogs);
if (bccomp($totalAmount, '0', 2) === 1) {
// 计算手续费
@ -132,13 +149,17 @@ class ManagerSubsidySettleCommand extends Command
];
}
unset($salesLogs);
$lastId = $dealer->id;
}
DealerEarning::insert($earnings);
unset($earnings);
});
Cache::put($cacheKey, $lastId, $endAt->addMonthNoOverflow());
unset($dealers, $earnings);
} while ($countDealers == $count);
Cache::forget($cacheKey);
}
/**
@ -150,7 +171,7 @@ class ManagerSubsidySettleCommand extends Command
*/
protected function cacheKey(Carbon $startAt, Carbon $endAt)
{
$startAt->format('Ymd').'_'.$endAt->format('Ymd').'_dealer_manager_subsidy';
return $startAt->format('Ymd').'_'.$endAt->format('Ymd').'_dealer_manager_subsidy';
}
/**