优化管理津贴结算
parent
3bf9034940
commit
7b5a10306a
|
|
@ -4,12 +4,12 @@ namespace App\Console\Commands\Dealer;
|
||||||
|
|
||||||
use App\Enums\DealerEarningStatus;
|
use App\Enums\DealerEarningStatus;
|
||||||
use App\Enums\DealerEarningType;
|
use App\Enums\DealerEarningType;
|
||||||
|
use App\Enums\DealerLvl;
|
||||||
|
use App\Models\Dealer;
|
||||||
use App\Models\DealerEarning;
|
use App\Models\DealerEarning;
|
||||||
use App\Models\DealerManageSubsidyLog;
|
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Illuminate\Support\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
|
|
||||||
class ManageSubsidySettleCommand extends Command
|
class ManageSubsidySettleCommand extends Command
|
||||||
{
|
{
|
||||||
|
|
@ -54,7 +54,7 @@ class ManageSubsidySettleCommand extends Command
|
||||||
$endAt = $tz->copy()->setDay(19)->endOfDay();
|
$endAt = $tz->copy()->setDay(19)->endOfDay();
|
||||||
} elseif ($tz->day >= 5) {
|
} elseif ($tz->day >= 5) {
|
||||||
$startAt = $tz->copy()->subMonthNoOverflow()->set('day', 20)->startOfDay();
|
$startAt = $tz->copy()->subMonthNoOverflow()->set('day', 20)->startOfDay();
|
||||||
$endAt = $tz->copy()->set('day', 4)->endOfDay();
|
$endAt = $tz->copy()->set('day', 24)->endOfDay();
|
||||||
} else {
|
} else {
|
||||||
$startAt = $tz->copy()->subMonthNoOverflow()->setDay(5)->startOfDay();
|
$startAt = $tz->copy()->subMonthNoOverflow()->setDay(5)->startOfDay();
|
||||||
$endAt = $startAt->copy()->subMonthNoOverflow()->setDay(19)->endOfDay();
|
$endAt = $startAt->copy()->subMonthNoOverflow()->setDay(19)->endOfDay();
|
||||||
|
|
@ -81,7 +81,7 @@ class ManageSubsidySettleCommand extends Command
|
||||||
|
|
||||||
$this->info("{$head}------------[结束]管理津贴结算------------".PHP_EOL);
|
$this->info("{$head}------------[结束]管理津贴结算------------".PHP_EOL);
|
||||||
|
|
||||||
Cache::put($this->cacheKey($startAt, $endAt), 1, 24*3600*22);
|
Cache::put($this->cacheKey($startAt, $endAt), 1, $endAt->tomorrow());
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
@ -91,45 +91,68 @@ class ManageSubsidySettleCommand extends Command
|
||||||
*
|
*
|
||||||
* @param \Illuminate\Support\Carbon $startAt
|
* @param \Illuminate\Support\Carbon $startAt
|
||||||
* @param \Illuminate\Support\Carbon $endAt
|
* @param \Illuminate\Support\Carbon $endAt
|
||||||
|
* @param int $count
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function settle(Carbon $startAt, Carbon $endAt): void
|
protected function settle(Carbon $startAt, Carbon $endAt, $count = 500): void
|
||||||
{
|
{
|
||||||
$subsidies = DealerManageSubsidyLog::select('user_id', DB::raw('sum(total_amount) as total_amount'))
|
|
||||||
->whereBetween('order_completed_at', [$startAt, $endAt])
|
|
||||||
->groupBy('user_id')
|
|
||||||
->get();
|
|
||||||
|
|
||||||
// 手续费比例(百分比)
|
|
||||||
$time = now()->toDateTimeString();
|
|
||||||
$feeRate = app_settings('dealer.fee_rate');
|
$feeRate = app_settings('dealer.fee_rate');
|
||||||
|
|
||||||
$data = [];
|
$lastId = Cache::get(
|
||||||
foreach ($subsidies as $key => $subsidy) {
|
$key = $this->cacheKey($startAt, $endAt).':last_id'
|
||||||
if ($key > 0 && $key%500 === 0) {
|
);
|
||||||
DealerEarning::insert($data);
|
|
||||||
|
|
||||||
$data = [];
|
do {
|
||||||
$time = now()->toDateTimeString();
|
$dealers = Dealer::where('lvl', '>=', DealerLvl::Contracted)
|
||||||
|
->forPageAfterId($count, $lastId, 'id')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$countDealers = $dealers->count();
|
||||||
|
|
||||||
|
if ($countDealers == 0) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$fee = bcmul(bcdiv($feeRate, '100', 10), $subsidy->total_amount, 3);
|
$earnings = [];
|
||||||
|
$time = now()->toDateTimeString();
|
||||||
|
|
||||||
$data[] = [
|
foreach ($dealers as $dealer) {
|
||||||
'user_id' => $subsidy->user_id,
|
$totalAmount = $dealer->manageSubsidyLogs()
|
||||||
'total_amount' => $subsidy->total_amount,
|
->whereBetween('order_completed_at', [$startAt, $endAt])
|
||||||
'fee' => $fee,
|
->sum('total_amount');
|
||||||
'fee_rate' => $feeRate,
|
|
||||||
'type' => DealerEarningType::ManageSubsidy,
|
|
||||||
'start_at' => $startAt,
|
|
||||||
'end_at' => $endAt,
|
|
||||||
'status' => DealerEarningStatus::Pending,
|
|
||||||
'created_at' => $time,
|
|
||||||
'updated_at' => $time,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
DealerEarning::insert($data);
|
if (bccomp($totalAmount, '0', 2) === 1) {
|
||||||
|
// 计算手续费
|
||||||
|
$fee = bcmul($totalAmount, bcdiv($feeRate, '100', 10), 2);
|
||||||
|
|
||||||
|
$earnings[] = [
|
||||||
|
'user_id' => $dealer->user_id,
|
||||||
|
'total_amount' => $totalAmount,
|
||||||
|
'fee' => $fee,
|
||||||
|
'fee_rate' => $feeRate,
|
||||||
|
'type' => DealerEarningType::ManageSubsidy,
|
||||||
|
'start_at' => $startAt,
|
||||||
|
'end_at' => $endAt,
|
||||||
|
'lvl' => $dealer->lvl,
|
||||||
|
'is_manager' => $dealer->is_manager,
|
||||||
|
'status' => DealerEarningStatus::Pending,
|
||||||
|
'description' => '管理津贴',
|
||||||
|
'created_at' => $time,
|
||||||
|
'updated_at' => $time,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$lastId = $dealer->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
DealerEarning::insert($earnings);
|
||||||
|
|
||||||
|
Cache::put($key, $lastId, $endAt);
|
||||||
|
|
||||||
|
unset($dealers, $earnings);
|
||||||
|
} while ($countDealers == $count);
|
||||||
|
|
||||||
|
Cache::forget($key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,14 @@ class Dealer extends Model
|
||||||
return $this->hasMany(DealerManagerSalesLog::class, 'user_id', 'user_id');
|
return $this->hasMany(DealerManagerSalesLog::class, 'user_id', 'user_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 属于此经销商的管理者津贴
|
||||||
|
*/
|
||||||
|
public function manageSubsidyLogs()
|
||||||
|
{
|
||||||
|
return $this->hasMany(DealerManageSubsidyLog::class, 'user_id', 'user_id');
|
||||||
|
}
|
||||||
|
|
||||||
public function getLvlTextAttribute()
|
public function getLvlTextAttribute()
|
||||||
{
|
{
|
||||||
return $this->lvl->text();
|
return $this->lvl->text();
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ class CreateDealerEarningsTable extends Migration
|
||||||
$table->timestamp('completed_at')->nullable()->comment('完成时间');
|
$table->timestamp('completed_at')->nullable()->comment('完成时间');
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
|
|
||||||
$table->unique(['user_id', 'start_at', 'end_at']);
|
$table->unique(['user_id', 'type', 'start_at', 'end_at']);
|
||||||
$table->index(['start_at', 'end_at']);
|
$table->index(['start_at', 'end_at']);
|
||||||
$table->index('type');
|
$table->index('type');
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue