jingcai-php/app/Jobs/ComputeJclqOrderPrize.php

256 lines
9.6 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\Lq\JclqResult;
use App\Model\Order;
use App\Model\OrderJclqResult;
use App\Model\OrderJczqResult;
use App\Model\OrderLqZuhe;
use App\Model\OrderZuhe;
use App\Model\Zq\JczqResult;
use App\Service\JclqService;
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 ComputeJclqOrderPrize 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('ComputeJclqOrderPrize::start, orderId:' . $this->orderId);
$unPublishes = OrderJclqResult::where('order_id')->where('published', BoolEnum::NO)->count();
if ($unPublishes > 0) {
Log::error('ComputeJclqOrderPrize has no publish match, orderId:' . $this->orderId);
return;
}
$order = Order::find($this->orderId);
if ($order->lottery_state != LottState::WAIT) {
Log::error('ComputeJclqOrderPrize 订单状态不是待开奖状态, orderId:' . $this->orderId);
return;
}
$odds = $order->odds;
$jclqOddsIds = array_keys($odds);
$jclqResults = JclqResult::whereIn('jclq_odds_id', $jclqOddsIds)->get();
if (count($jclqResults) != count($jclqOddsIds)) {
Log::error('ComputeJclqOrderPrize 开奖结果和投注信息中的id对应不上', [
'order_id' => $this->orderId,
'order_odds' => $odds,
'order_odds_ids' => $jclqOddsIds,
'result_odds_ids' => $jclqResults->pluck('jclq_odds_id')->toArray(),
]);
return;
}
$results = $jclqResults->keyBy('jclq_odds_id');
$orderZuhe = OrderLqZuhe::where('order_id', $this->orderId)->get();
foreach ($orderZuhe as $zuhe) {
$this->computeZuhePrize($zuhe, $order, $results);
}
$winedZuhe = OrderLqZuhe::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($order->lottery_state);
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($order->lottery_state);
OrderWinedEvent::dispatch($order->id);
}
public function computeZuhePrize(OrderLqZuhe $orderZuhe, $order, $results)
{
$odds = $order->odds;
$oddsRaw = $order->odds_raw;
$buyOddsId = array_keys($odds);
$allWin = true;
$baseOdd = 1;
foreach ($orderZuhe->jclq_odds_ids as $jclq_odds_id) {
if (!in_array($jclq_odds_id, $buyOddsId)) {
$allWin = false;
break;
}
// dump($orderZuhe->id.'-----------------------'.$jczq_odds_id);
$buyPlayKey = Arr::get($orderZuhe->info, $jclq_odds_id . '.play');
$buyResultKey = Arr::get($orderZuhe->info, $jclq_odds_id . '.result');
if ($buyPlayKey === null || $buyResultKey === null) {
$allWin = false;
Log::error('ComputeJclqOrderPrize::checkZuhePrize error, 组合中没有该比赛信息', [
'zuhe_id' => $orderZuhe->id,
'order_id' => $order->id,
'zuhe_odds_ids' => $orderZuhe->jczq_odds_ids,
'zuhe_info' => $orderZuhe->info,
]);
break;
}
// 3.rq_odds.win = 3.85
$buyOdds = Arr::get($odds, $jclq_odds_id . '.' . $buyPlayKey . '.' . $buyResultKey);
$result = Arr::get($results, $jclq_odds_id);
if (!$result) {
$allWin = false;
Log::error('ComputeJclqOrderPrize::checkZuhePrize error, 未找到中奖结果', [
'zuhe_id' => $orderZuhe->id,
'order_id' => $order->id,
'zuhe_odds_ids' => $orderZuhe->jczq_odds_ids,
'zuhe_info' => $orderZuhe->info,
'order_odds' => $odds,
'$jclq_odds_id' => $jclq_odds_id,
]);
break;
}
if ($result->canceled()) {
$buyOdds = 1;
$baseOdd *= $buyOdds;
continue;
}
if ($buyOdds === null) {
$allWin = false;
Log::error('ComputeJclqOrderPrize::checkZuhePrize error, 购买的投注信息中和组合中的数据不一致', [
'zuhe_id' => $orderZuhe->id,
'order_id' => $order->id,
'zuhe_odds_ids' => $orderZuhe->jczq_odds_ids,
'zuhe_info' => $orderZuhe->info,
'order_odds' => $odds,
]);
break;
}
$buyField = str_replace('_odds', '', $buyPlayKey);
// 大小分特殊处理
if ($buyPlayKey == 'dxf_odds') {
$buyOddsRaw = Arr::get($oddsRaw, $jclq_odds_id . '.' . $buyPlayKey);
if (!$buyOddsRaw === null) {
$allWin = false;
Log::error('ComputeJclqOrderPrize::checkZuhePrize dxf_odds error, 没有带下分数据', [
'zuhe_id' => $orderZuhe->id,
'order_id' => $order->id,
'zuhe_odds_ids' => $orderZuhe->jczq_odds_ids,
'zuhe_info' => $orderZuhe->info,
'order_odds' => $odds,
'order_odds_raw' => $oddsRaw,
]);
break;
}
$buyDxf = $buyOddsRaw['totalScore'];
$resultDxf = $result->getTotalScore();
if (bc_gt($resultDxf, $buyDxf) && $buyResultKey == 'big') {
$baseOdd *= $buyOdds;
continue;
}
if (bc_lt($resultDxf, $buyDxf) && $buyResultKey == 'small') {
$baseOdd *= $buyOdds;
continue;
}
$allWin = false;
}if ($buyPlayKey == 'rf_odds') {
$buyOddsRaw = Arr::get($oddsRaw, $jclq_odds_id . '.' . $buyPlayKey);
if (!$buyOddsRaw === null) {
$allWin = false;
Log::error('ComputeJclqOrderPrize::checkZuhePrize rf_odds error, 没有带让分数据', [
'zuhe_id' => $orderZuhe->id,
'order_id' => $order->id,
'zuhe_odds_ids' => $orderZuhe->jczq_odds_ids,
'zuhe_info' => $orderZuhe->info,
'order_odds' => $odds,
'order_odds_raw' => $oddsRaw,
]);
break;
}
$handicapScore = $buyOddsRaw['handicap'];
$homeScore = $result->home_score;
$awayScore = $result->away_score;
if ($result->is_reverse == 1) {
$homeScore = $result->away_score;
$awayScore = $result->home_score;
}
$homeWin = ($homeScore + $handicapScore) > $awayScore;
if ($homeWin && $buyResultKey == 'win') {
$baseOdd *= $buyOdds;
} else if (!$homeWin && $buyResultKey == 'loss') {
$baseOdd *= $buyOdds;
} else {
$allWin = false;
}
} else {
if ($result->{$buyField.'_field'} == $buyResultKey) {
$baseOdd *= $buyOdds;
} else {
$allWin = false;
}
}
}
// 该组合没有中奖
if (!$allWin) {
return;
}
$chuanNum = count($orderZuhe->jclq_odds_ids);
$onePrize = JclqService::getTiCaiOneBetsPrize($baseOdd * Config::lotteryUnitPrice(),$chuanNum);
$perBetsPrize = $orderZuhe->bets_num * $onePrize * $orderZuhe->repeat_num;
$taxPrize = Config::getAfterTaxPrizeIfOne($perBetsPrize, 1 * $baseOdd * Config::lotteryUnitPrice());
$winPrize = $perBetsPrize;
$orderZuhe->win_prize = $winPrize;
$orderZuhe->win_tax_prize = $taxPrize;
$orderZuhe->wined = BoolEnum::YES;
$orderZuhe->save();
}
}