jingcai-php/app/Jobs/ComputeCtzqBqcOrderPrize.php

92 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\OrderCtzqBqcResult;
use App\Model\Zq\CtzqBqc;
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 ComputeCtzqBqcOrderPrize 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('ComputeCtzqBqcOrderPrize::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('ComputeCtzqBqcOrderPrize 订单状态不是待开奖状态, orderId:' . $this->orderId);
return;
}
$unPublishes = OrderCtzqBqcResult::where('order_id',$this->orderId)->where('published', BoolEnum::NO)->count();
if ($unPublishes > 0) {
Log::error('ComputeCtzqBqcOrderPrize has no publish match, orderId:' . $this->orderId);
return;
}
$orderResults = OrderCtzqBqcResult::where('order_id', $this->orderId)->get();
if (!$orderResults || count($orderResults) != 6) {
Log::error('ComputeCtzqBqcOrderPrize 不够6场比赛数据, orderId:' . $this->orderId);
return;
}
$win = true;
$ctzqBqcId = 0;
foreach ($orderResults as $orderResult) {
$ctzqBqcId = $orderResult->ctzq_bqc_id;
if (($orderResult->half_result != '*' && !in_array($orderResult->half_result, $orderResult->half_odds)) ||
($orderResult->whole_result != '*' && !in_array($orderResult->whole_result, $orderResult->whole_odds))) {
$win = false;
break;
}
}
$winPrize = 0;
if ($win) {
$ctzqBqc = CtzqBqc::find($ctzqBqcId);
$winPrize = $ctzqBqc->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);
}
}
}