jingcai-php/app/Jobs/ComputeCtzqJqcOrderPrize.php

94 lines
3.0 KiB
PHP
Executable File

<?php
namespace App\Jobs;
use App\Enums\BoolEnum;
use App\Enums\LottState;
use App\Events\OrderWinedEvent;
use App\Model\Config;
use App\Model\Order;
use App\Model\OrderCtzqJqcResult;
use App\Model\Zq\CtzqJqc;
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 ComputeCtzqJqcOrderPrize implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $orderId;
public function __construct($orderId)
{
$this->orderId = $orderId;
$this->queue = config('queue.names.compute_order_prize');
}
public function handle()
{
Log::info('ComputeCtzqJqcOrderPrize::start, orderId:' . $this->orderId);
$order = Order::find($this->orderId);
if (!$order) {
Log::error('ComputeCtzqBqcOrderPrize 订单状不存在, orderId:' . $this->orderId);
return;
}
if ($order->lottery_state != LottState::WAIT) {
Log::error('ComputeCtzqJqcOrderPrize 订单状态不是待开奖状态, orderId:' . $this->orderId);
return;
}
$unPublishes = OrderCtzqJqcResult::where('order_id', $this->orderId)->where('published', BoolEnum::NO)->count();
if ($unPublishes > 0) {
Log::error('ComputeCtzqJqcOrderPrize has no publish match, orderId:' . $this->orderId);
return;
}
$orderResults = OrderCtzqJqcResult::where('order_id', $this->orderId)->get();
if (!$orderResults || count($orderResults) != 4) {
Log::error('ComputeCtzqJqcOrderPrize 不够4场比赛数据, orderId:' . $this->orderId);
return;
}
$win = true;
$ctzqJqcId = 0;
foreach ($orderResults as $orderResult) {
$ctzqJqcId = $orderResult->ctzq_jqc_id;
if (($orderResult->home_result != '*' && !in_array($orderResult->home_result, $orderResult->home_odds)) ||
$orderResult->away_result != '*' && !in_array($orderResult->away_result, $orderResult->away_odds)) {
$win = false;
break;
}
}
$winPrize = 0;
if ($win) {
$ctzqJqc = CtzqJqc::find($ctzqJqcId);
$winPrize = $ctzqJqc->first_prize_value;
}
$winTaxPrize = Config::getAfterTaxPrize($winPrize);
$winPrize = $winPrize * $order->bets_num;
$winTaxPrize = $winTaxPrize * $order->bets_num;
$order->lottery_state = $win ? LottState::WIN : LottState::LOSS;
$order->lottery_prize = $winPrize;
$order->lottery_tax_prize = $winTaxPrize;
$order->lottery_should_send_prize = $winTaxPrize;
if ($win) {
$order->win_date = date('Ymd');
}
$order->save();
$order->updateUnionOrderState($order->lottery_state);
if ($win) {
OrderWinedEvent::dispatch($order->id);
}
}
}