jingcai-php/app/Jobs/RefreshOrderCtzqBqcResult.php

90 lines
3.1 KiB
PHP
Executable File

<?php
namespace App\Jobs;
use App\Enums\BoolEnum;
use App\Model\OrderCtzqBqcResult;
use App\Model\Zq\CtzqBqc;
use App\Model\Zq\CtzqBqcMatch;
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 RefreshOrderCtzqBqcResult implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $ctzqBqcId;
public function __construct($ctzqBqcId)
{
$this->ctzqBqcId = $ctzqBqcId;
$this->queue = config('queue.names.refresh_order_odds');
}
public function getData() {
return [
'ctzqBqcId' => $this->ctzqBqcId
];
}
public function handle()
{
$ctzq = CtzqBqc::find($this->ctzqBqcId);
if (!$ctzq) {
Log::error("RefreshOrderCtzqBqcResultListener not found ctzqBqcId:". $this->ctzqBqcId);
return;
}
$matches = CtzqBqcMatch::where('ctzq_bqc_id', $ctzq->id)->get()->keyBy('id');
OrderCtzqBqcResult::select('*')
->where('ctzq_bqc_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_bqc_match_id);
if (!$matchResult) {
Log::error("RefreshOrderCtzqBqcResultListener not found ctzq_bqc_match",[
'ctzq_bqc_match_id' => $orderResult->ctzqBqc_match_id,
'ctzq_bqc_id' => $ctzq->id,
'order_id' => $orderResult->order_id,
'OrderCtzqBqcResult_id' => $orderResult->id,
]);
continue;
}
$orderIds[$orderResult->order_id] = $orderResult->order_id;
// 更新状态
$orderResult->published = BoolEnum::YES;
$orderResult->half_result = $matchResult->half_result;
$orderResult->whole_result = $matchResult->whole_result;
$orderResult->save();
}
// 执行计算任务
foreach ($orderIds as $orderId) {
$this->dispatchComputeOrderPrize($orderId);
}
});
}
private function dispatchComputeOrderPrize($orderId)
{
$unPublishes = OrderCtzqBqcResult::where('order_id', $orderId)
->where('published', BoolEnum::NO)->count();
// 如果全部已公布,则计算奖金
if ($unPublishes == 0) {
Log::info('dispatch ComputeCtzqBqcOrderPrize, orderId:'.$orderId);
ComputeCtzqBqcOrderPrize::dispatch($orderId);
}
}
}