jingcai-php/app/Jobs/ComputeDltOrderPrize.php

157 lines
5.7 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\Enums\LottType;
use App\Enums\OrderType;
use App\Enums\PayState;
use App\Enums\PlayType;
use App\Events\OrderWinedEvent;
use App\Model\Config;
use App\Model\Dlt;
use App\Model\LotteryType;
use App\Model\Order;
use App\Model\OrderJczqResult;
use App\Model\OrderZuhe;
use App\Model\Zq\JczqResult;
use App\Service\DltService;
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\Arr;
use Illuminate\Support\Facades\Log;
class ComputeDltOrderPrize implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $orderId;
/** @var DltService $dltService */
protected $dltService;
public function __construct($orderId)
{
$this->dltService = app(DltService::class);
$this->orderId = $orderId;
$this->queue = config('queue.names.compute_order_prize');
}
public function handle()
{
Log::info('ComputeDltOrderPrize::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('ComputeDltOrderPrize 订单状态不是待开奖状态, orderId:' . $this->orderId);
return;
}
$dlt = Dlt::where('issue_num', $order->issue_num)->where('state', BoolEnum::YES)->first();
if (!$dlt) {
Log::error('ComputeDltOrderPrize 无此期号:', [
'order_id' => $this->orderId,
'issue_num' => $order->issue_num,
]);
return;
}
$prizeInfo = $this->getWinPrize($order->odds, $dlt);
$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, Dlt $dlt) {
$win = false;
$prizeAll = 0;
$shouldSendPrize = 0;
foreach ($odds as $item) {
$enableAdd = Arr::get($item, 'enable_add', 0);
if ($item['play_type'] == PlayType::DLT_DANSHI) {
$prizeInfo = $this->dltService->danshiWinPrize($dlt, $item['play_odd'], $enableAdd);
if ($prizeInfo) {
Log::warning('ComputeDltOrderPrizewined_prize_good:', [
'play_type' => $item['play_type'],
'play_odd' => $item['play_odd'],
'prize_info' => $prizeInfo,
'order_id' => $this->orderId
]);
$win = true;
$shouldSendPrize += $prizeInfo['prize_after_tax'] * $item['bets_num'];
$prizeAll += $prizeInfo['prize']*$item['bets_num'];
}
continue;
}
if ($item['play_type'] == PlayType::DLT_FUSHI) {
$danshiList = $this->dltService->fushiToDanshi($item);
foreach ($danshiList as $danshiPlayOdd) {
$prizeInfo3 = $this->dltService->danshiWinPrize($dlt, $danshiPlayOdd, $enableAdd);
if ($prizeInfo3) {
Log::warning('ComputeDltOrderPrizewined_prize_good:', [
'play_type' => $item['play_type'],
'play_odd' => $item['play_odd'],
'danshi_play_odd' => $danshiPlayOdd,
'prize_info' => $prizeInfo3,
'order_id' => $this->orderId
]);
$win = true;
$shouldSendPrize += $prizeInfo3['prize_after_tax'] * $item['bets_num'];
$prizeAll += $prizeInfo3['prize']*$item['bets_num'];
}
}
continue;
}
if ($item['play_type'] == PlayType::DLT_DANTUO) {
$danshiList = $this->dltService->dantuoToDanshi($item);
foreach ($danshiList as $danshiPlayOdd) {
$prizeInfo2 = $this->dltService->danshiWinPrize($dlt, $danshiPlayOdd, $enableAdd);
if ($prizeInfo2) {
Log::warning('ComputeDltOrderPrizewined_prize_good:', [
'play_type' => $item['play_type'],
'play_odd' => $item['play_odd'],
'danshi_play_odd' => $danshiPlayOdd,
'prize_info' => $prizeInfo2,
'order_id' => $this->orderId
]);
$win = true;
$shouldSendPrize += $prizeInfo2['prize_after_tax'] * $item['bets_num'];
$prizeAll += $prizeInfo2['prize']*$item['bets_num'];
}
}
continue;
}
}
return [
'win' => $win,
'prize' => $prizeAll,
'should_send_prize' => $shouldSendPrize
];
}
}