jingcai-php/app/Jobs/RefreshOrderJczqResult.php

96 lines
3.1 KiB
PHP
Executable File

<?php
namespace App\Jobs;
use App\Enums\BoolEnum;
use App\Enums\LottState;
use App\Enums\LottType;
use App\Events\LotteryResultEvent;
use App\Model\Config;
use App\Model\Order;
use App\Model\OrderJczqResult;
use App\Model\OrderZuhe;
use App\Model\Zq\JczqResult;
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\Arr;
use Illuminate\Support\Facades\Log;
class RefreshOrderJczqResult implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $jczqResultId;
protected $timeout = 180;
public function __construct($jczqResultId)
{
$this->jczqResultId = $jczqResultId;
$this->queue = config('queue.names.refresh_order_odds');
}
public function getData() {
return [
'$jczqResultId' => $this->jczqResultId
];
}
public function handle()
{
Log::info('RefreshOrderJczqResult, jczqResultId:'.$this->jczqResultId);
$result = JczqResult::find($this->jczqResultId);
if (!$result) {
Log::error("RefreshOrderJczqOddsListener not found resultId,", $this->getData());
return;
}
$jczqResults = OrderJczqResult::leftJoin('order', 'order.id', '=', 'order_jczq_result.order_id')
->select('order_jczq_result.id')
->where('order.lottery_state', LottState::WAIT)
->where('order_jczq_result.published', BoolEnum::NO)
->where('order_jczq_result.jczq_odds_id', $result->jczq_odds_id)
->get();
if (count($jczqResults) <= 0) {
return;
}
$jczqResultIds = $jczqResults->pluck('id')->toArray();
OrderJczqResult::select('order_jczq_result.*')
->whereIn('id', $jczqResultIds)
->chunkById(500, function ($data) {
if (count($data) <= 0) {
return;
}
// 更新发布状态
$orderResultIds = $data->pluck('id')->toArray();
OrderJczqResult::whereIn('id', $orderResultIds)
->update(['published' => BoolEnum::YES]);
// 执行计算任务
$orderIds = $data->pluck('order_id')->toArray();
foreach ($orderIds as $orderId) {
Log::info('will dispatch ComputeJczqOrderPrize, orderId:'.$orderId);
$this->dispatchComputeOrderPrize($orderId);
}
});
}
private function dispatchComputeOrderPrize($orderId)
{
$unPublishes = OrderJczqResult::where('order_id', $orderId)
->where('published', BoolEnum::NO)->count();
Log::info('will dispatch ComputeJczqOrderPrize, orderId:'.$orderId, [
'unPublishes' => $unPublishes
]);
// 如果全部已公布,则计算奖金
if ($unPublishes == 0) {
Log::info('dispatch ComputeJczqOrderPrize, orderId:'.$orderId);
ComputeJczqOrderPrize::dispatch($orderId);
}
}
}