jingcai-php/app/Jobs/RefreshOrderBjdcSfggResult.php

77 lines
2.5 KiB
PHP
Executable File

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