jingcai-php/app/Jobs/RefreshOrderJclqResult.php

84 lines
2.6 KiB
PHP
Executable File

<?php
namespace App\Jobs;
use App\Enums\BoolEnum;
use App\Enums\LottState;
use App\Model\Lq\JclqResult;
use App\Model\OrderJclqResult;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class RefreshOrderJclqResult implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $jclqResultId;
public function __construct($jclqResultId)
{
$this->jclqResultId = $jclqResultId;
$this->queue = config('queue.names.refresh_order_odds');
}
public function getData() {
return [
'$jclqResultId' => $this->jclqResultId
];
}
public function handle()
{
$result = JclqResult::find($this->jclqResultId);
if (!$result) {
Log::error("RefreshOrderJclqOddsListener not found resultId,", $this->getData());
return;
}
$jclqResult = OrderJclqResult::leftJoin('order', 'order.id', '=', 'order_jclq_result.order_id')
->select('order_jclq_result.id')
->where('order.lottery_state', LottState::WAIT)
->where('order_jclq_result.published', BoolEnum::NO)
->where('order_jclq_result.jclq_odds_id', $result->jclq_odds_id)
->get();
if (count($jclqResult) <= 0) {
return;
}
$jclqResultIds = $jclqResult->pluck('id')->toArray();
OrderJclqResult::select('order_jclq_result.*')
->whereIn('id', $jclqResultIds)
->chunkById(500, function ($data) {
if (count($data) <= 0) {
return;
}
// 更新发布状态
$orderResultIds = $data->pluck('id')->toArray();
OrderJclqResult::whereIn('id', $orderResultIds)
->update(['published' => BoolEnum::YES]);
// 执行计算任务
$orderIds = $data->pluck('order_id')->toArray();
foreach ($orderIds as $orderId) {
$this->dispatchComputeOrderPrize($orderId);
}
});
}
private function dispatchComputeOrderPrize($orderId)
{
$unPublishes = OrderJclqResult::where('order_id', $orderId)
->where('published', BoolEnum::NO)->count();
// 如果全部已公布,则计算奖金
if ($unPublishes == 0) {
Log::info('dispatch ComputeJclqOrderPrize, orderId:'.$orderId);
ComputeJclqOrderPrize::dispatch($orderId);
}
}
}