196 lines
7.0 KiB
PHP
Executable File
196 lines
7.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\BoolEnum;
|
|
use App\Enums\LottState;
|
|
use App\Enums\OrderType;
|
|
use App\Events\OrderWinedEvent;
|
|
use App\Model\Config;
|
|
use App\Model\Order;
|
|
use App\Model\OrderBjdcResult;
|
|
use App\Model\OrderBjdcZuhe;
|
|
use App\Model\Zq\BjdcResult;
|
|
use App\Service\BjdcService;
|
|
use App\Utils\Helps;
|
|
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 ComputeBjdcOrderPrize 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('ComputeBjdcOrderPrize::start, orderId:' . $this->orderId);
|
|
|
|
$unPublishes = OrderBjdcResult::where('order_id')->where('published', BoolEnum::NO)->count();
|
|
if ($unPublishes > 0) {
|
|
Log::error('ComputeBjdcOrderPrize has no publish match, orderId:' . $this->orderId);
|
|
return;
|
|
}
|
|
$order = Order::find($this->orderId);
|
|
if ($order->lottery_state != LottState::WAIT) {
|
|
Log::error('ComputeBjdcOrderPrize 订单状态不是待开奖状态, orderId:' . $this->orderId);
|
|
return;
|
|
}
|
|
// 刷新赔率
|
|
/** @var BjdcService $bjdcService */
|
|
$bjdcService = app(BjdcService::class);
|
|
$order->odds = $bjdcService->refreshOddsWithResult($order->odds);
|
|
$order->save();
|
|
|
|
$odds = $order->odds;
|
|
$jczqOddsIds = array_keys($odds);
|
|
$result = BjdcResult::whereIn('bjdc_odds_id', $jczqOddsIds)->get();
|
|
if (count($result) != count($jczqOddsIds)) {
|
|
Log::error('ComputeBjdcOrderPrize 开奖结果和投注信息中的id对应不上', [
|
|
'order_id' => $this->orderId,
|
|
'order_odds' => $odds,
|
|
'order_odds_ids' => $jczqOddsIds,
|
|
'result_odds_ids' => $result->pluck('bjdc_odds_id')->toArray(),
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$results = BjdcResult::whereIn('bjdc_odds_id', $jczqOddsIds)->get()->keyBy('bjdc_odds_id');
|
|
|
|
$orderZuhe = OrderBjdcZuhe::where('order_id', $this->orderId)->get();
|
|
foreach ($orderZuhe as $zuhe) {
|
|
$this->computeZuhePrize($zuhe, $order, $results);
|
|
}
|
|
|
|
$winedZuhe = OrderBjdcZuhe::where('order_id', $this->orderId)
|
|
->where('wined', BoolEnum::YES)
|
|
->get();
|
|
if (count($winedZuhe) == 0) {
|
|
$order->lottery_state = LottState::LOSS;
|
|
$order->lottery_prize = 0;
|
|
$order->lottery_tax_prize = 0;
|
|
$order->save();
|
|
|
|
$order->updateUnionOrderState(LottState::LOSS);
|
|
return;
|
|
}
|
|
|
|
$winPrize = $winedZuhe->sum('win_prize');
|
|
$winTaxPrize = $winedZuhe->sum('win_tax_prize');
|
|
|
|
$gendanBrokerage = 0;
|
|
if ($order->type == OrderType::GENDAN) {
|
|
$gendanBrokerage = $winTaxPrize* Config::fadanAllFeeLv();
|
|
}
|
|
|
|
$order->lottery_state = LottState::WIN;
|
|
$order->lottery_prize = $winPrize;
|
|
$order->lottery_tax_prize = $winTaxPrize;
|
|
$order->lottery_should_send_prize = $winTaxPrize;
|
|
$order->lottery_gendan_brokerage = 0;
|
|
$order->lottery_gendan_brokerage_all = 0;
|
|
$order->win_date = date('Ymd');
|
|
$order->save();
|
|
$order->updateUnionOrderState(LottState::WIN);
|
|
|
|
OrderWinedEvent::dispatch($order->id);
|
|
}
|
|
|
|
public function computeZuhePrize(OrderBjdcZuhe $orderZuhe, $order, $results)
|
|
{
|
|
$odds = $order->odds;
|
|
$buyOddsId = array_keys($odds);
|
|
$allWin = true;
|
|
|
|
$baseOdd = 1;
|
|
foreach ($orderZuhe->bjdc_odds_ids as $bjdc_odds_id) {
|
|
if (!in_array($bjdc_odds_id, $buyOddsId)) {
|
|
$allWin = false;
|
|
break;
|
|
}
|
|
// dump($orderZuhe->id.'-----------------------'.$bjdc_odds_id);
|
|
|
|
$buyPlayKey = Arr::get($orderZuhe->info, $bjdc_odds_id . '.play');
|
|
$buyResultKey = Arr::get($orderZuhe->info, $bjdc_odds_id . '.result');
|
|
if ($buyPlayKey === null || $buyResultKey === null) {
|
|
$allWin = false;
|
|
Log::error('ComputeBjdcOrderPrize::checkZuhePrize error, 组合中没有该比赛信息', [
|
|
'zuhe_id' => $orderZuhe->id,
|
|
'order_id' => $order->id,
|
|
'zuhe_odds_ids' => $orderZuhe->bjdc_odds_ids,
|
|
'zuhe_info' => $orderZuhe->info,
|
|
]);
|
|
break;
|
|
}
|
|
|
|
|
|
$result = Arr::get($results, $bjdc_odds_id);
|
|
if (!$result) {
|
|
$allWin = false;
|
|
Log::error('ComputeBjdcOrderPrize::checkZuhePrize error, 购买的投注信息未开奖', [
|
|
'zuhe_id' => $orderZuhe->id,
|
|
'order_id' => $order->id,
|
|
'zuhe_odds_ids' => $orderZuhe->bjdc_odds_ids,
|
|
'zuhe_info' => $orderZuhe->info,
|
|
'order_odds' => $odds,
|
|
]);
|
|
break;
|
|
}
|
|
// 3.rq_odds.win = 3.85
|
|
|
|
$buyField = str_replace('_odds', '', $buyPlayKey);
|
|
$resultOdds = $result->{$buyField.'_odds'};
|
|
if ($result->canceled()) {
|
|
$resultOdds = 1;
|
|
$baseOdd *= $resultOdds;
|
|
continue;
|
|
}
|
|
if ($resultOdds === null) {
|
|
$allWin = false;
|
|
Log::error('ComputeBjdcOrderPrize::checkZuhePrize error, 购买的投注信息在开奖结果中不存在', [
|
|
'zuhe_id' => $orderZuhe->id,
|
|
'order_id' => $order->id,
|
|
'zuhe_odds_ids' => $orderZuhe->bjdc_odds_ids,
|
|
'zuhe_info' => $orderZuhe->info,
|
|
'order_odds' => $odds,
|
|
'$resultOdds' => $resultOdds,
|
|
]);
|
|
break;
|
|
}
|
|
|
|
if ($result->{$buyField.'_field'} == $buyResultKey) {
|
|
$baseOdd *= Helps::floatFormatUnRound($resultOdds);
|
|
} else {
|
|
$allWin = false;
|
|
}
|
|
}
|
|
|
|
// 该组合没有中奖
|
|
if (!$allWin) {
|
|
return;
|
|
}
|
|
// 当前组合奖金
|
|
$perBetsPrize = $orderZuhe->bets_num * $baseOdd * Config::lotteryUnitPrice() * Config::bjdcPrizePercent() * $orderZuhe->repeat_num;
|
|
$taxPrize = Config::getAfterTaxPrizeIfOne($perBetsPrize,1 * $baseOdd * Config::lotteryUnitPrice() * Config::bjdcPrizePercent());
|
|
$winPrize = $perBetsPrize;
|
|
|
|
$orderZuhe->win_prize = $winPrize;
|
|
$orderZuhe->win_tax_prize = $taxPrize;
|
|
$orderZuhe->wined = BoolEnum::YES;
|
|
$orderZuhe->save();
|
|
}
|
|
|
|
}
|