jingcai-php/app/Service/PlsService.php

644 lines
22 KiB
PHP
Executable File

<?php
namespace App\Service;
use App\Enums\BoolEnum;
use App\Enums\LottType;
use App\Enums\OrderType;
use App\Enums\PayState;
use App\Enums\PlayType;
use App\Exceptions\JingCaiException;
use App\Model\Config;
use App\Model\Customer\Customer;
use App\Model\Lottery;
use App\Model\LotteryType;
use App\Model\Order;
use App\Model\Pls;
use App\Model\Seller\Seller;
use App\Utils\Helps;
use App\Utils\ThrowException;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class PlsService implements IJingcai
{
private $shuziNum = 3;
public static function haomas()
{
return [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
];
}
public static function hezhis() {
return [
'0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27'
];
}
public static function inHezhi($he)
{
$hezhis = self::hezhis();
return in_array($he, $hezhis);
}
public static function inHaoma( $hao)
{
$haomas = self::haomas();
return in_array($hao, $haomas);
}
public function getSellingPls()
{
$pls = Pls::where('state', BoolEnum::NO)->where('enable', BoolEnum::YES)->first();
return $pls;
}
public function saleLotteries(Customer $customer, $lotteryTypeId)
{
$lotteryType = LotteryType::where('type', LottType::PLS)
->where('status', BoolEnum::YES)
->first();
ThrowException::isTrue(!$lotteryType, '不支持该彩种');
ThrowException::isTrue($lotteryType->status != BoolEnum::YES, '暂未开启该彩种');
ThrowException::isTrue($lotteryType->id != $lotteryTypeId, '彩种数据错误');
/** @var Lottery $lott */
$lott = Lottery::active()->shopAndType($customer->shop_id, $lotteryTypeId)->first();
ThrowException::isTrue(!$lott, '暂不支持该彩种');
/** @var Pls $pls */
$pls = $this->getSellingPls();
ThrowException::isTrue(!$pls, '暂未开售');
$gets = Pls::where('state', BoolEnum::YES)
->where('close_time', '>', date('Y-m-d H:i:s', time() + $lott->earlySecond()))
->orderBy('issue_num', 'desc')
->limit(10)
->get();
$result = [];
foreach ($gets as $item) {
$result[] = [
'issue_num' => $item->issue_num,
'result' => $item->result,
];
}
return [
'result' => $result,
'current' => [
'issue_num' => $pls->issue_num,
'close_time_str' => $pls->getCloseTime($lott->earlySecond())
],
];
}
public function refreshOdds($data)
{
return $data;
}
public function computePrizeInfo($data, $betsNum = 1, $passModeKeys = [])
{
$pls = $this->getSellingPls();
ThrowException::isTrue(!$pls, '暂未开售');
$zhuTotal = 0;
$money = 0;
foreach ($data as $item) {
switch ($item['play_type']) {
case PlayType::PLS_ZHIXUAN_DANSHI:
$zuhes = $this->zhixuanDanshiZuhe($item['play_odd']);
break;
case PlayType::PLS_ZHIXUAN_FUSHI:
$zuhes = $this->zhixuanFushiZuhe($item['play_odd']);
break;
case PlayType::PLS_ZHIXUAN_HEZHI:
$zuhes = $this->zhixuanHezhiZuhe($item['play_odd']);
break;
case PlayType::PLS_ZUXUAN6:
$zuhes = $this->zuxuan6Zuhe($item['play_odd']);
break;
case PlayType::PLS_ZUXUAN3:
$zuhes = $this->zuxuan3Zuhe($item['play_odd']);
break;
case PlayType::PLS_ZUXUAN_HEZHI:
$zuhes = $this->zuxuanHezhi($item['play_odd']);
break;
default:
ThrowException::run('无效玩法'.$item['play_type']);
}
$zuheShu = count($zuhes);
$zhuTotal += $zuheShu;
$money += $zuheShu * Config::lotteryUnitPrice() * $item['bets_num'];
}
return [
'zhu_num' => $zhuTotal,
'bets_num' => 0,
'expect_bets' => 0,
'money' => $money,
'prize_min' => 0,
'prize_max' => 0
];
}
// 无序
public function zuxuanHezhi($playOdd) {
$zuhes = [];
foreach ($playOdd as $he) {
$res = Helps::sumCombination($he);
foreach ($res as $item) {
$uitem = array_unique($item);
if (count($uitem) == 1) {
continue;
}
$sitem = $item;
sort($sitem);
$key = implode(',', $sitem);
if (!isset($zuhes[$key])) {
$zuhes[$key] = $sitem;
}
}
}
return array_values($zuhes);
}
public function zuxuan6Zuhe($playOdd) {
$zuhes = Helps::combination($playOdd, $this->shuziNum);
return $zuhes;
}
// 无序
public function zuxuan3Zuhe($playOdd) {
$res = Helps::combination($playOdd,2);
$zuhes = [];
foreach ($res as $item) {
foreach ($item as $hao) {
$zuhe = $item;
$zuhe[] = $hao;
sort($zuhe);
$zuhes[] = $zuhe;
}
}
return $zuhes;
}
public function zhixuanHezhiZuhe($playOdd) {
$zuhes = [];
foreach ($playOdd as $he) {
$res = Helps::sumCombination($he);
$zuhes = array_merge($zuhes, $res);
}
return $zuhes;
}
public function zhixuanFushiZuhe($playOdd) {
$list = Helps::combination($playOdd, 3);
$zuhes = [];
foreach ($list as $item) {
$zuhe = Helps::generateCombination35($item);
$zuhes = array_merge($zuhes, $zuhe);
}
return $zuhes;
}
public function zhixuanDanshiZuhe($playOdd) {
$zuhes = Helps::getRepeatCombinationData($playOdd, $this->shuziNum);
return $zuhes;
}
public function valid(Lottery $lottery, $data)
{
foreach ($data as $item) {
ThrowException::isTrue(!isset($item['play_type']), '玩法错误');
switch ($item['play_type']) {
case PlayType::PLS_ZHIXUAN_DANSHI:
$this->validZhixuanDanshi($item);
break;
case PlayType::PLS_ZHIXUAN_FUSHI:
$this->validZhixuanFushi($item);
break;
case PlayType::PLS_ZUXUAN6:
$this->validZuxuan6($item);
break;
case PlayType::PLS_ZUXUAN3:
$this->validZuxuan3($item);
break;
case PlayType::PLS_ZHIXUAN_HEZHI:
$this->validZhixuanHezhi($item);
break;
case PlayType::PLS_ZUXUAN_HEZHI:
$this->validZuxuanHezhi($item);
break;
default:
ThrowException::run('无效玩法'.$item['play_type']);
}
}
}
private function validZuxuan3($data) {
$odds = $data['play_odd'];
ThrowException::isTrue(count($odds) < 2, '至少选择2个号码');
ThrowException::isTrue(count($odds) != count(array_unique($odds)), '号码不可重复');
foreach ($odds as $hao) {
ThrowException::isTrue(!self::inHaoma($hao), '选择的号码错误');
}
}
private function validZuxuan6($data) {
$odds = $data['play_odd'];
ThrowException::isTrue(count($odds) < $this->shuziNum, '至少选择3个号码');
ThrowException::isTrue(count($odds) != count(array_unique($odds)), '号码不可重复');
foreach ($odds as $hao) {
ThrowException::isTrue(!self::inHaoma($hao), '选择的号码错误');
}
}
private function validZuxuanHezhi($data) {
$odds = $data['play_odd'];
ThrowException::isTrue(count($odds) < 1, '投注数据错误,至少选择1个');
ThrowException::isTrue(count($odds) != count(array_unique($odds)), '号码不可重复');
foreach ($odds as $hezhi) {
if ($hezhi == 0 || $hezhi == 27) {
ThrowException::run('选择的号码错误');
}
ThrowException::isTrue(!self::inHezhi($hezhi), '选择的号码错误');
}
}
private function validZhixuanHezhi($data) {
$odds = $data['play_odd'];
ThrowException::isTrue(count($odds) < 1, '投注数据错误,至少选择1个');
ThrowException::isTrue(count($odds) != count(array_unique($odds)), '号码不可重复');
foreach ($odds as $hezhi) {
ThrowException::isTrue(!self::inHezhi($hezhi), '选择的号码错误');
}
}
private function validZhixuanFushi($data) {
$odds = $data['play_odd'];
ThrowException::isTrue(count($odds) < $this->shuziNum, '投注数据错误,不少于'.$this->shuziNum.'个');
ThrowException::isTrue(count($odds) != count(array_unique($odds)), '直选复式号码不可重复');
foreach ($odds as $hao) {
ThrowException::isTrue(!self::inHaoma($hao), '选择的号码错误');
}
}
private function validZhixuanDanshi($data) {
$odds = $data['play_odd'];
ThrowException::isTrue(count($odds) != $this->shuziNum, '投注数据错误,必须为'.$this->shuziNum.'位');
foreach ($odds as $haos) {
ThrowException::isTrue(count($haos) != count(array_unique($haos)), '同位置号码不可重复');
foreach ($haos as $hao) {
ThrowException::isTrue(!self::inHaoma($hao), '选择的号码错误');
}
}
}
public function createOrder(Customer $customer, $data)
{
$lotteryTypeId = Arr::get($data, 'lottery_type_id');
$playType = Arr::get($data, 'play_type');
$type = Arr::get($data, 'type', OrderType::NORMAL);
$fadanSecret = Arr::get($data, 'type_mode', 1);
$fadanDesc = Arr::get($data, 'type_desc', '');
$betsNum = Arr::get($data, 'bets_num');
$passModeKeys = Arr::get($data, 'pass_mode_keys');
$odds = Arr::get($data, 'odds'); // 购买的场次或投注信息
$union_piece_total = intval(Arr::get($data, 'union_piece_total', 0));
$union_piece_buy = intval(Arr::get($data, 'union_piece_buy', 0));
$union_piece_keep = intval(Arr::get($data, 'union_piece_keep', 0));
$union_keep = Arr::get($data, 'union_keep', BoolEnum::NO);
$union_brokerage = intval(Arr::get($data, 'union_brokerage', 0));
$fadan_order_no = Arr::get($data, 'fadan_order_no', '');
/** @var Pls $pls */
$pls = $this->getSellingPls();
ThrowException::isTrue(!$pls, '暂未开售');
ThrowException::isTrue($type == OrderType::FADAN, '不支持发单');
ThrowException::isTrue($type == OrderType::GENDAN, '不支持跟单');
/** @var Lottery $lott */
$lott = LotteryService::getLottery($customer->shop->id, $lotteryTypeId);
throw_if(!$lott, JingCaiException::create('店铺未开通该彩种!'));
ThrowException::isTrue(date('Y-m-d H:i:s') > $pls->getCloseTime($lott->earlySecond()), '投注已截止');
$this->valid($lott, $odds);
$odds = $this->refreshOdds($odds);
$computeInfo = $this->computePrizeInfo($odds, $betsNum, $passModeKeys);
$lott->validMixMoney($computeInfo['money']);
if ($type == OrderType::UNION) {
$lott->validEnableHemai();
throw_if(!$fadanDesc, JingCaiException::create('请填写合买宣言!'));
ThrowException::isTrue($union_piece_total <= 0, '总份数不能小于0');
$piecePrice = $computeInfo['money'] / $union_piece_total;
ThrowException::isTrue($piecePrice <= 1, '每份金额必须大于1');
ThrowException::isTrue($union_piece_buy > $union_piece_total, '超过方案总金额');
if ($union_keep == BoolEnum::YES) {
$union_piece_keep = $union_piece_total - $union_piece_buy;
}
$this->canCreateUnionOrder($customer->id);
}
$pid = 0;
DB::beginTransaction();
try {
$earlyTime = $pls->getCloseTime($lott->earlySecond());
$lateTime = $earlyTime;
$order = new Order();
$order->pid = $pid;
$order->issue_num = $pls->issue_num;
$order->customer_id = $customer->id;
$order->lottery_id = $lott->id;
$order->shop_id = $customer->shop_id;
$order->lottery_type_id = $lott->lottery_type_id;
$order->order_sn = Order::makeOrderSn();
$order->play_type = '';
$order->bets_num = $computeInfo['bets_num'];
$order->bets_expect_num = $computeInfo['expect_bets'];
$order->zhu_num = $computeInfo['zhu_num'];
$order->piao_num = 0;
$order->money = $computeInfo['money'];
$order->prize_min = $computeInfo['prize_min'];
$order->prize_max = $computeInfo['prize_max'];
$order->pay_state = PayState::UNPAID;
$order->pass_mode = $passModeKeys;
$order->odds_close_time = $pls->getCloseTime(0);
$order->odds_early_close_time = $earlyTime;
$order->odds_late_close_time = $lateTime;
$order->type = $type;
$order->type_mode = $fadanSecret;
$order->type_desc = $fadanDesc;
$order->odds = $odds;
if ($type == OrderType::UNION) {
$pieceMoney = $order->money / $union_piece_total;
$order->union_piece_total = $union_piece_total;
$order->union_piece_buy = $union_piece_buy;
$order->union_piece_self = $union_piece_buy;
$order->union_piece_keep = $union_piece_keep;
$order->union_keep = $union_keep;
$order->union_brokerage = $union_brokerage;
$order->union_piece_money = $pieceMoney;
$order->union_money = $pieceMoney * ($union_piece_buy + $union_piece_keep);
}
$order->created_date = date('Ymd');
// 设置合作相关的数据
$order->setCooperateInfo($lott);
$order->save();
// 如果是合买,更新自己的pid
if ($order->type == OrderType::UNION) {
$order->pid = $order->id;
$order->save();
}
DB::commit();
return $order;
} catch (JingCaiException $e) {
DB::rollBack();
Log::error('Pls::order::create ValidateException: ' . $e);
throw $e;
} catch (\Exception $e) {
DB::rollBack();
Log::error('Pls::order::create Exception: ' . $e);
throw $e;
}
}
public function canCreateUnionOrder($customerId)
{
}
/**
* 跟单
* @param $data
* @param Customer $customer
* @return Order
* @throws \Throwable
*/
public function copyOrder(Customer $customer, Order $od, $data)
{
ThrowException::run('不支持跟单');
}
public function showOrder(Customer $customer, Order $order)
{
$order->sellings = [];
// 自购的
if ($order->customerCanSeeSellings($customer)) {
$order->sellings = $this->getOrderOdds($order);
}
$order->play_name = $order->lottery->name;
unset($order->lottery);
return $order;
}
public function getOrderOdds($order)
{
$odds = $order->odds;
foreach ($odds as &$item) {
switch ($item['play_type']) {
case PlayType::PLS_ZHIXUAN_DANSHI:
$zuhes = $this->zhixuanDanshiZuhe($item['play_odd']);
break;
case PlayType::PLS_ZHIXUAN_FUSHI:
$zuhes = $this->zhixuanFushiZuhe($item['play_odd']);
break;
case PlayType::PLS_ZHIXUAN_HEZHI:
$zuhes = $this->zhixuanHezhiZuhe($item['play_odd']);
break;
case PlayType::PLS_ZUXUAN6:
$zuhes = $this->zuxuan6Zuhe($item['play_odd']);
break;
case PlayType::PLS_ZUXUAN3:
$zuhes = $this->zuxuan3Zuhe($item['play_odd']);
break;
case PlayType::PLS_ZUXUAN_HEZHI:
$zuhes = $this->zuxuanHezhi($item['play_odd']);
break;
default:
ThrowException::run('无效玩法'.$item['play_type']);
}
$zuheShu = count($zuhes);
$money = $zuheShu * Config::lotteryUnitPrice() * $item['bets_num'];
$item['money'] = $money;
$item['zhu_num'] = $zuheShu;
}
return $odds;
}
public function sellerShowOrder(Seller $seller, Order $order)
{
$order->sellings = [];
if ($order->sellerCanSeeSellings($seller)) {
$order->sellings = $this->getOrderOdds($order);
}
$order->play_name = $order->lottery->name;
unset($order->lottery);
return $order;
}
public function chaiPiao(Order $order)
{
return [];
}
/**
* @param Pls $pls
* @param $playOdd
* @return array|null
*/
public function zhixuanDanshiWinPrize(Pls $pls, $playOdd)
{
$result = $pls->result;
$zuhes = $this->zhixuanDanshiZuhe($playOdd);
foreach ($zuhes as $zuhe) {
$sameNum = 0;
foreach ($result as $idx => $val) {
$buyVal = $zuhe[$idx];
if ($buyVal == $val) {
$sameNum++;
}
}
if ($sameNum == $this->shuziNum) {
return [
'prize' => $pls->zhixuan_prize,
'prize_after_tax' => Config::getAfterTaxPrize($pls->zhixuan_prize),
];
}
}
return null;
}
/**
* @param Pls $pls
* @param $playOdd
* @return array|null
*/
public function zhixuanFushiWinPrize(Pls $pls, $playOdd)
{
$result = $pls->result;
$zuhes = $this->zhixuanFushiZuhe($playOdd);
foreach ($zuhes as $zuhe) {
$sameNum = 0;
foreach ($result as $idx => $val) {
$buyVal = $zuhe[$idx];
if ($buyVal == $val) {
$sameNum++;
}
}
if ($sameNum == $this->shuziNum) {
return [
'prize' => $pls->zhixuan_prize,
'prize_after_tax' => Config::getAfterTaxPrize($pls->zhixuan_prize),
];
}
}
return null;
}
public function zhixuanHezhiWinPrize(Pls $pls, $playOdd)
{
foreach ($playOdd as $he) {
if ($he == $pls->hezhi) {
return [
'prize' => $pls->zhixuan_prize,
'prize_after_tax' => Config::getAfterTaxPrize($pls->zhixuan_prize),
];
}
}
return null;
}
public function zuxuan3WinPrize(Pls $pls, $playOdd)
{
$result = $pls->result;
$zuhes = $this->zuxuan3Zuhe($playOdd);
foreach ($zuhes as $zuhe) {
$win = $this->arrayEqual($result, $zuhe);
if ($win) {
return [
'prize' => $pls->zuxuan3_prize,
'prize_after_tax' => Config::getAfterTaxPrize($pls->zuxuan3_prize),
];
}
}
return null;
}
public function zuxuan6WinPrize(Pls $pls, $playOdd)
{
$result = $pls->result;
$zuhes = $this->zuxuan6Zuhe($playOdd);
foreach ($zuhes as $zuhe) {
$win = $this->arrayEqual($result, $zuhe);
if ($win) {
return [
'prize' => $pls->zuxuan6_prize,
'prize_after_tax' => Config::getAfterTaxPrize($pls->zuxuan6_prize),
];
}
}
return null;
}
public function zuxuanHezhiWinPrize(Pls $pls, $playOdd)
{
foreach ($playOdd as $he) {
if ($he == $pls->hezhi) {
return [
'prize' => $pls->zuxuan3_prize,
'prize_after_tax' => Config::getAfterTaxPrize($pls->zuxuan3_prize),
];
}
}
return null;
}
public function arrayEqual($arr1, $arr2)
{
sort($arr1);
sort($arr2);
if (count($arr1) != count($arr2)) {
return false;
}
foreach ($arr1 as $k => $v) {
if ($v != $arr2[$k]) {
return false;
}
}
return true;
}
}