130 lines
4.3 KiB
PHP
Executable File
130 lines
4.3 KiB
PHP
Executable File
<?php
|
||
|
||
namespace App\Jobs;
|
||
|
||
|
||
use App\Enums\BoolEnum;
|
||
use App\Enums\LottState;
|
||
use App\Enums\PlayType;
|
||
use App\Events\OrderWinedEvent;
|
||
use App\Model\Order;
|
||
use App\Model\Pls;
|
||
use App\Service\PlsService;
|
||
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 ComputePlsOrderPrize implements ShouldQueue
|
||
{
|
||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||
|
||
protected $orderId;
|
||
|
||
/** @var PlsService $plsService */
|
||
protected $plsService;
|
||
|
||
public function __construct($orderId)
|
||
{
|
||
$this->plsService = app(PlsService::class);
|
||
$this->orderId = $orderId;
|
||
$this->queue = config('queue.names.compute_order_prize');
|
||
}
|
||
|
||
public function handle()
|
||
{
|
||
Log::info('ComputePlsOrderPrize::start, orderId:' . $this->orderId);
|
||
$order = Order::find($this->orderId);
|
||
if (!$order) {
|
||
Log::error('ComputePlsOrderPrize 订单状不存在, orderId:' . $this->orderId);
|
||
return;
|
||
}
|
||
if ($order->lottery_state != LottState::WAIT) {
|
||
Log::error('ComputePlsOrderPrize 订单状态不是待开奖状态, orderId:' . $this->orderId);
|
||
return;
|
||
}
|
||
|
||
$pls = Pls::where('issue_num', $order->issue_num)->where('state', BoolEnum::YES)->first();
|
||
if (!$pls) {
|
||
Log::error('ComputePlsOrderPrize 无此期号:', [
|
||
'order_id' => $this->orderId,
|
||
'issue_num' => $order->issue_num,
|
||
]);
|
||
return;
|
||
}
|
||
|
||
$prizeInfo = $this->getWinPrize($order->odds, $pls);
|
||
|
||
$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, Pls $pls) {
|
||
$win = false;
|
||
$prizeAll = 0;
|
||
$shouldSendPrize = 0;
|
||
foreach ($odds as $item) {
|
||
$betsNum = $item['bets_num']; // 倍数
|
||
$playOdd = $item['play_odd'];
|
||
|
||
$playType = $item['play_type'];
|
||
$prizeInfo = null;
|
||
|
||
switch ($playType) {
|
||
case PlayType::PLS_ZHIXUAN_DANSHI:
|
||
$prizeInfo = $this->plsService->zhixuanDanshiWinPrize($pls, $playOdd);
|
||
break;
|
||
case PlayType::PLS_ZHIXUAN_FUSHI:
|
||
$prizeInfo = $this->plsService->zhixuanFushiWinPrize($pls, $playOdd);
|
||
break;
|
||
case PlayType::PLS_ZHIXUAN_HEZHI:
|
||
$prizeInfo = $this->plsService->zhixuanHezhiWinPrize($pls, $playOdd);
|
||
break;
|
||
case PlayType::PLS_ZUXUAN3:
|
||
$prizeInfo = $this->plsService->zuxuan3WinPrize($pls, $playOdd);
|
||
break;
|
||
case PlayType::PLS_ZUXUAN6:
|
||
$prizeInfo = $this->plsService->zuxuan6WinPrize($pls, $playOdd);
|
||
break;
|
||
case PlayType::PLS_ZUXUAN_HEZHI:
|
||
$prizeInfo = $this->plsService->zuxuanHezhiWinPrize($pls, $playOdd);
|
||
break;
|
||
}
|
||
if ($prizeInfo) {
|
||
Log::warning('ComputePlsOrderPrize,wined_prize_good:', [
|
||
'bets_num' => $betsNum,
|
||
'play_odd' => $playOdd,
|
||
'play_type' => $playType,
|
||
'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
|
||
];
|
||
}
|
||
}
|