60 lines
1.5 KiB
PHP
60 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands\Distribution;
|
|
|
|
use App\Exceptions\BizException;
|
|
use App\Models\DistributionPreIncomeJob;
|
|
use App\Services\DistributionPreIncomeJobService;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
|
|
class PreIncomeJobCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'distribution:pre-income-job';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = '分销预收益任务';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @param \App\Services\DistributionPreIncomeJobService $jobService
|
|
* @return int
|
|
*/
|
|
public function handle(DistributionPreIncomeJobService $jobService)
|
|
{
|
|
DistributionPreIncomeJob::with('jobable')->pending()->chunkById(200, function ($jobs) use ($jobService) {
|
|
foreach ($jobs as $job) {
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$jobService->run($job);
|
|
|
|
DB::commit();
|
|
} catch (Throwable $e) {
|
|
DB::rollBack();
|
|
|
|
report($e);
|
|
|
|
if ($e instanceof BizException) {
|
|
$job->update([
|
|
'status' => DistributionPreIncomeJob::STATUS_FAILED,
|
|
'failed_reason' => $e->getMessage(),
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|