jingcai-php/app/Jobs/RefreshOrderCtzqSfc9Result.php

89 lines
3.0 KiB
PHP
Executable File

<?php
namespace App\Jobs;
use App\Enums\BoolEnum;
use App\Enums\LottType;
use App\Model\OrderCtzqSfcResult;
use App\Model\Zq\CtzqSfc;
use App\Model\Zq\CtzqSfcMatch;
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 RefreshOrderCtzqSfc9Result implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $ctzqSfcId;
public function __construct($ctzqSfcId)
{
$this->ctzqSfcId = $ctzqSfcId;
$this->queue = config('queue.names.refresh_order_odds');
}
public function getData() {
return [
'ctzqSfcId' => $this->ctzqSfcId
];
}
public function handle()
{
$ctzq = CtzqSfc::find($this->ctzqSfcId);
if (!$ctzq) {
Log::error("RefreshOrderCtzqSfc9Result not found ctzqSfcId:". $this->ctzqSfcId);
return;
}
$matches = CtzqSfcMatch::where('ctzq_sfc_id', $ctzq->id)->get()->keyBy('id');
OrderCtzqSfcResult::select('*')
->where('ctzq_sfc_id', $ctzq->id)
->where('lottery_type', LottType::CTZQ_SFC9)
->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_sfc_match_id);
if (!$matchResult) {
Log::error("RefreshOrderCtzqSfc9Result not found ctzq_jqc_match",[
'ctzq_sfc_match_id' => $orderResult->ctzq_sfc_match_id,
'ctzq_sfc_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->result = $matchResult->result;
$orderResult->save();
}
// 执行计算任务
foreach ($orderIds as $orderId) {
$this->dispatchComputeOrderPrize($orderId);
}
});
}
private function dispatchComputeOrderPrize($orderId)
{
$unPublishes = OrderCtzqSfcResult::where('order_id', $orderId)
->where('published', BoolEnum::NO)->count();
// 如果全部已公布,则计算奖金
if ($unPublishes == 0) {
Log::info('dispatch ComputeCtzqSfc9OrderPrize, orderId:'.$orderId);
ComputeCtzqSfc9OrderPrize::dispatch($orderId);
}
}
}