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

95 lines
2.8 KiB
PHP

<?php
namespace App\Console\Commands\Distribution;
use App\Models\DistributionPreIncome;
use App\Models\DistributionPreIncomeJob;
use App\Models\MerchantMessage;
use App\Models\Order;
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)
{
while (true) {
$page = 0;
DistributionPreIncomeJob::with('jobable')->pending()->chunkById(200, function ($jobs) use ($jobService, &$page) {
foreach ($jobs as $job) {
try {
DB::beginTransaction();
$jobService->run($job);
DB::commit();
} catch (Throwable $e) {
DB::rollBack();
report($e);
}
//发送商家端预收益进帐消息
try {
DB::beginTransaction();
switch (get_class($job->jobable)) {
case Order::class://如果是订单类型,则发送预收益消息
$order = $job->jobable;
$incomesLogs = DistributionPreIncome::where('order_id', $order->id)->get();
foreach ($incomesLogs as $log) {
MerchantMessage::createDistributionMessage($log->user_id, [
'title'=>'恭喜收入'.$log->total_revenue.'元',
'content'=>'您有新的预收益产生,共'.$log->total_revenue.'元。',
]);
}
break;
default:
break;
}
DB::commit();
} catch (Throwable $e) {
DB::rollBack();
report($e);
}
}
$page++;
});
if ($page === 0) {
sleep(60);
} elseif ($page === 1) {
sleep(30);
} else {
sleep(15);
}
}
}
}