6
0
Fork 0
jiqu-library-server/app/Console/Commands/Distribution/PreIncomeSettleCommand.php

90 lines
2.7 KiB
PHP

<?php
namespace App\Console\Commands\Distribution;
use App\Models\DistributionPreIncome;
use App\Models\WalletLog;
use App\Services\WalletService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Throwable;
class PreIncomeSettleCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'distribution:pre-income-settle';
/**
* The console command description.
*
* @var string
*/
protected $description = '分销预收益结算';
/**
* Execute the console command.
*
* @param \App\Services\WalletService $walletService
* @return int
*/
public function handle(WalletService $walletService)
{
while (true) {
$page = 0;
DistributionPreIncome::processing()->chunkById(200, function ($preIncomes) use ($walletService, &$page) {
$preIncomes->load('user');
foreach ($preIncomes as $preIncome) {
try {
DB::beginTransaction();
$walletService->changeBalance(
$preIncome->user,
bcmul($preIncome->total_revenue, 100),
WalletLog::ACTION_DISTRIBUTION_PRE_INCOME,
$preIncome->remarks,
$preIncome
);
// 计算配额
$changeQuota = bcmul($preIncome->total_revenue, app_settings('distribution.quota_v2_rate', 0), 4);
$changeQuota = round($changeQuota, 3);
$preIncome->user->userInfo()->update([
'quota_v2' => DB::raw("quota_v2+{$changeQuota}"),
]);
$preIncome->user->quotaLogs()->create([
'loggable_id' => $preIncome->id,
'loggable_type' => $preIncome->getMorphClass(),
'change_quota' => $changeQuota,
'remarks' => $preIncome->type_text.'得配额',
]);
// 将预收益标记为已结算
$preIncome->update([
'completed_at' => now(),
'status' => DistributionPreIncome::STATUS_PROCESSED,
]);
DB::commit();
} catch (Throwable $e) {
DB::rollBack();
report($e);
}
}
$page++;
});
sleep(60);
}
}
}