jingcai-php/app/Jobs/RefreshOrderBjdcResult.php

75 lines
2.4 KiB
PHP
Executable File

<?php
namespace App\Jobs;
use App\Enums\BoolEnum;
use App\Enums\LottState;
use App\Model\OrderBjdcResult;
use App\Model\Zq\BjdcResult;
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 RefreshOrderBjdcResult implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $bjdcResultId;
public function __construct($bjdcResultId)
{
$this->bjdcResultId = $bjdcResultId;
$this->queue = config('queue.names.refresh_order_odds');
}
public function getData() {
return [
'bjdcResultId' => $this->bjdcResultId
];
}
public function handle()
{
$result = BjdcResult::find($this->bjdcResultId);
if (!$result) {
Log::error("RefreshOrderBjdcResult not found resultId,", $this->getData());
return;
}
OrderBjdcResult::leftJoin('order', 'order.id', '=', 'order_bjdc_result.order_id')
->select('order_bjdc_result.*')
->where('order.lottery_state', LottState::WAIT)
->where('order_bjdc_result.published', BoolEnum::NO)
->where('order_bjdc_result.bjdc_odds_id', $result->bjdc_odds_id)
->chunkById(500, function ($data) {
if (count($data) <= 0) {
return;
}
// 更新发布状态
$orderResultIds = $data->pluck('id')->toArray();
OrderBjdcResult::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 = OrderBjdcResult::where('order_id', $orderId)
->where('published', BoolEnum::NO)->count();
// 如果全部已公布,则计算奖金
if ($unPublishes == 0) {
Log::info('dispatch ComputeBjdcOrderPrize, orderId:'.$orderId);
ComputeBjdcOrderPrize::dispatch($orderId);
}
}
}