jingcai-php/app/Jobs/ComputePlwOrderPrize.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\Plw;
use App\Service\PlwService;
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 ComputePlwOrderPrize implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $orderId;
/** @var PlwService $plwService */
protected $plwService;
public function __construct($orderId)
{
$this->plwService = app(PlwService::class);
$this->orderId = $orderId;
$this->queue = config('queue.names.compute_order_prize');
}
public function handle()
{
Log::info('ComputePlwOrderPrize::start, orderId:' . $this->orderId);
$order = Order::find($this->orderId);
if (!$order) {
Log::error('ComputePlwOrderPrize 订单状不存在, orderId:' . $this->orderId);
return;
}
if ($order->lottery_state != LottState::WAIT) {
Log::error('ComputePlwOrderPrize 订单状态不是待开奖状态, orderId:' . $this->orderId);
return;
}
$plw = Plw::where('issue_num', $order->issue_num)->where('state', BoolEnum::YES)->first();
if (!$plw) {
Log::error('ComputePlwOrderPrize 无此期号:', [
'order_id' => $this->orderId,
'issue_num' => $order->issue_num,
]);
return;
}
$prizeInfo = $this->getWinPrize($order->odds, $plw);
$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, Plw $plw) {
$win = false;
$prizeAll = 0;
$shouldSendPrize = 0;
foreach ($odds as $item) {
$betsNum = $item['bets_num']; // 倍数
$playOdd = $item['play_odd'];
$zuheList = $this->plwService->dataToDanshi($playOdd);
foreach ($zuheList as $zuhe) {
$prizeInfo = $this->plwService->computeDanshiWinPrize($plw, $zuhe);
if ($prizeInfo) {
Log::warning('ComputePlwOrderPrizewined_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
];
}
}