jingcai-php/app/Jobs/ComputeQxcOrderPrize.php

109 lines
3.5 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Jobs;
use App\Enums\BoolEnum;
use App\Enums\LottState;
use App\Events\OrderWinedEvent;
use App\Model\Order;
use App\Model\Qxc;
use App\Service\QxcService;
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 ComputeQxcOrderPrize implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $orderId;
/** @var QxcService $qxcService */
protected $qxcService;
public function __construct($orderId)
{
$this->qxcService = app(QxcService::class);
$this->orderId = $orderId;
$this->queue = config('queue.names.compute_order_prize');
}
public function handle()
{
Log::info('ComputeQxcOrderPrize::start, orderId:' . $this->orderId);
$order = Order::find($this->orderId);
if (!$order) {
Log::error('ComputeQxcOrderPrize 订单状不存在, orderId:' . $this->orderId);
return;
}
if ($order->lottery_state != LottState::WAIT) {
Log::error('ComputeQxcOrderPrize 订单状态不是待开奖状态, orderId:' . $this->orderId);
return;
}
$qxc = Qxc::where('issue_num', $order->issue_num)->where('state', BoolEnum::YES)->first();
if (!$qxc) {
Log::error('ComputeQxcOrderPrize 无此期号:', [
'order_id' => $this->orderId,
'issue_num' => $order->issue_num,
]);
return;
}
$prizeInfo = $this->getWinPrize($order->odds, $qxc);
$win = $prizeInfo['win'];
$winPrize = $prizeInfo['prize'];
$winTaxPrize = $prizeInfo['should_send_prize'];
$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);
}
}
public function getWinPrize($odds, Qxc $qxc) {
$win = false;
$prizeAll = 0;
$shouldSendPrize = 0;
foreach ($odds as $item) {
$betsNum = $item['bets_num']; // 倍数
$playOdd = $item['play_odd'];
$zuheList = $this->qxcService->dataToDanshi($playOdd);
foreach ($zuheList as $zuhe) {
$prizeInfo = $this->qxcService->computeDanshiWinPrize($qxc, $zuhe);
if ($prizeInfo) {
Log::warning('ComputeQxcOrderPrizewined_prize_good:', [
'bets_num' => $betsNum,
'play_odd' => $item['play_odd'],
'danshi_play_odd' => $zuhe,
'prize_info' => $prizeInfo,
'order_id' => $this->orderId
]);
$win = true;
$shouldSendPrize += $prizeInfo['prize_after_tax'] * $item['bets_num'];
$prizeAll += $prizeInfo['prize']*$item['bets_num'];
}
}
}
return [
'win' => $win,
'prize' => $prizeAll,
'should_send_prize' => $shouldSendPrize
];
}
}