jingcai-php/app/Jobs/RefreshOrderCtzqJqcResult.php

90 lines
3.1 KiB
PHP
Executable File

<?php
namespace App\Jobs;
use App\Enums\BoolEnum;
use App\Model\OrderCtzqJqcResult;
use App\Model\Zq\CtzqJqc;
use App\Model\Zq\CtzqJqcMatch;
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 RefreshOrderCtzqJqcResult implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $ctzqJqcId;
public function __construct($ctzqJqcId)
{
$this->ctzqJqcId = $ctzqJqcId;
$this->queue = config('queue.names.refresh_order_odds');
}
public function getData() {
return [
'ctzqJqcId' => $this->ctzqJqcId
];
}
public function handle()
{
$ctzq = CtzqJqc::find($this->ctzqJqcId);
if (!$ctzq) {
Log::error("RefreshOrderCtzqJqcResultListener not found ctzqJqcId:". $this->ctzqJqcId);
return;
}
$matches = CtzqJqcMatch::where('ctzq_jqc_id', $ctzq->id)->get()->keyBy('id');
OrderCtzqJqcResult::select('*')
->where('ctzq_jqc_id', $ctzq->id)
->where('published', BoolEnum::NO)
->chunkById(500, function ($data) use ($matches, $ctzq) {
if (count($data) <= 0) {
return;
}
$orderIds = [];
foreach ($data as $orderResult) {
$matchResult = Arr::get($matches, $orderResult->ctzq_jqc_match_id);
if (!$matchResult) {
Log::error("RefreshOrderCtzqJqcResultListener not found ctzq_jqc_match",[
'ctzq_jqc_match_id' => $orderResult->ctzq_jqc_match_id,
'ctzq_jqc_id' => $ctzq->id,
'order_id' => $orderResult->order_id,
'OrderCtzqJqcResult_id' => $orderResult->id,
]);
continue;
}
$orderIds[$orderResult->order_id] = $orderResult->order_id;
// 更新状态
$orderResult->published = BoolEnum::YES;
$orderResult->home_result = $matchResult->home_result;
$orderResult->away_result = $matchResult->away_result;
$orderResult->save();
}
// 执行计算任务
foreach ($orderIds as $orderId) {
$this->dispatchComputeOrderPrize($orderId);
}
});
}
private function dispatchComputeOrderPrize($orderId)
{
$unPublishes = OrderCtzqJqcResult::where('order_id', $orderId)
->where('published', BoolEnum::NO)->count();
// 如果全部已公布,则计算奖金
if ($unPublishes == 0) {
Log::info('dispatch ComputeCtzqJqcOrderPrize, orderId:'.$orderId);
ComputeCtzqJqcOrderPrize::dispatch($orderId);
}
}
}