jingcai-php/app/Service/QxcService.php

449 lines
16 KiB
PHP
Executable File
Raw 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\Service;
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\Exceptions\JingCaiException;
use App\Model\Config;
use App\Model\Customer\Customer;
use App\Model\Dlt;
use App\Model\Lottery;
use App\Model\LotteryType;
use App\Model\Order;
use App\Model\OrderJczqResult;
use App\Model\Qxc;
use App\Model\Seller\Seller;
use App\Model\Zq\JczqOdds;
use App\Utils\Helps;
use App\Utils\ThrowException;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class QxcService implements IJingcai
{
private $shuziNum = 7;
public function __construct()
{
}
public static function qian6()
{
return [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
];
}
public static function hou1()
{
return [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14'
];
}
public static function inHaoma($index, $hao)
{
if ($index < 6) {
$haomas = self::qian6();
} else {
$haomas = self::hou1();
}
return in_array($hao, $haomas);
}
public function getSellingQxc()
{
$qxc = Qxc::where('state', BoolEnum::NO)->where('enable', BoolEnum::YES)->first();
return $qxc;
}
public function saleLotteries(Customer $customer, $lotteryTypeId)
{
$lotteryType = LotteryType::where('type', LottType::QXC)
->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 Qxc $qxc */
$qxc = $this->getSellingQxc();
ThrowException::isTrue(!$qxc, '暂未开售');
$gets = Qxc::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' => $qxc->issue_num,
'close_time_str' => $qxc->getCloseTime($lott->earlySecond())
],
];
}
public function refreshOdds($data)
{
foreach ($data as &$item) {
ThrowException::isTrue(!isset($item['play_type']), '请选择有效玩法');
$item['play_type'] = $this->getPlayType($item['play_odd']);
}
return $data;
}
public function getPlayType($playOdd) {
foreach ($playOdd as $item) {
if (count($item) >1) {
return PlayType::QXC_FUSHI;
}
}
return PlayType::QXC_DANSHI;
}
public function computePrizeInfo($data, $betsNum = 1, $passModeKeys = [])
{
$dlt = $this->getSellingQxc();
ThrowException::isTrue(!$dlt, '暂未开售');
$zhuTotal = 0;
$money = 0;
foreach ($data as $item) {
$zuhes = $this->dataToDanshi($item['play_odd']);
$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
];
}
/**
* @param $data [
* [ 0, 1,2,3 ], [7],[ 2],[3], [ 4],[5], [6]
* ]
* @return Array
*/
public function dataToDanshi($odds)
{
$zuhe = Helps::getRepeatCombinationData($odds, 7);
return $zuhe;
}
public function valid(Lottery $lottery, $data)
{
foreach ($data as $playData) {
ThrowException::isTrue(!isset($playData['play_type']), '请选择有效玩法');
ThrowException::isTrue(!isset($playData['play_odd']), '请选择数字');
ThrowException::isTrue(Arr::get($playData, 'bets_num') < 1, '请选择倍数');
$playOdd = $playData['play_odd'];
ThrowException::isTrue(count($playOdd) != $this->shuziNum, '投注数据错误,必须为'.$this->shuziNum.'位');
foreach ($playOdd as $idx => $items) {
ThrowException::isTrue(count($items) != count(array_unique($items)), '同位置数字不可重复');
foreach ($items as $hao) {
$has = self::inHaoma($idx, $hao);
ThrowException::isTrue(!$has, '选择的数字错误');
}
}
}
}
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 Qxc $qxc */
$qxc = $this->getSellingQxc();
ThrowException::isTrue(!$qxc, '暂未开售');
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') > $qxc->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 = $qxc->getCloseTime($lott->earlySecond());
$lateTime = $earlyTime;
$order = new Order();
$order->pid = $pid;
$order->issue_num = $qxc->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 = $qxc->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('qxc::order::create ValidateException: ' . $e);
throw $e;
} catch (\Exception $e) {
DB::rollBack();
Log::error('qxc::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) {
$zuhes = $this->dataToDanshi($item['play_odd']);
$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 [];
}
/**
* 第二十二条 7星彩根据投注号码与开奖号码相符情况确定相应中奖资格。具体规定如下
*
* 一等奖:投注号码的全部数字与开奖号码对应位置数字均相同,即中奖;
*
* 二等奖投注号码的前6位数字与开奖号码对应位置数字相同即中奖
*
* 三等奖投注号码前6位中的任意5个数字与开奖号码对应位置数字相同且最后一个数字与开奖号码对应位置数字相同即中奖
*
* 四等奖投注号码中任意5个数字与开奖号码对应位置数字相同即中奖
*
* 五等奖投注号码中任意4个数字与开奖号码对应位置数字相同即中奖
*
* 六等奖投注号码中任意3个数字与开奖号码对应位置数字相同或者投注号码前6位中的任意1个数字与开奖号码对应位置数字相同且最后一个数字与开奖号码对应位置数字相同或者仅最后一个数字与开奖号码对应位置数字相同即中奖。
*
* 第二十三条 当期每注投注号码只有一次中奖机会,各奖级奖金不能兼中兼得,另行设立的特别奖除外。
* @param Qxc $qxc
* @param $oddData
* @return array|null
*/
public function computeDanshiWinPrize(Qxc $qxc, $oddData)
{
$result = $qxc->result;
$qian6SameNum = 0;
$hou1SameNum = 0;
foreach ($result as $idx => $val) {
$buyVal = $oddData[$idx];
if ($buyVal == $val) {
if ($idx < 6) {
$qian6SameNum++;
} else {
$hou1SameNum++;
}
}
}
if ($qian6SameNum == 6 && $hou1SameNum == 1) {
return [
'rank' => 1,
'prize' => $qxc->base1_prize,
'prize_after_tax' => Config::getAfterTaxPrize($qxc->base1_prize),
];
}
if ($qian6SameNum == 6 && $hou1SameNum == 0) {
return [
'rank' => 2,
'prize' => $qxc->base2_prize,
'prize_after_tax' => Config::getAfterTaxPrize($qxc->base2_prize),
];
}
if ($qian6SameNum == 5 && $hou1SameNum == 1) {
return [
'rank' => 3,
'prize' => 3000,
'prize_after_tax' => Config::getAfterTaxPrize(3000),
];
}
if ($qian6SameNum + $hou1SameNum == 5 ) {
return [
'rank' => 4,
'prize' => 500,
'prize_after_tax' => Config::getAfterTaxPrize(500),
];
}
if ($qian6SameNum + $hou1SameNum == 4) {
return [
'rank' => 5,
'prize' => 30,
'prize_after_tax' => Config::getAfterTaxPrize(30),
];
}
if (($qian6SameNum + $hou1SameNum == 3) ||
$hou1SameNum == 1) {
return [
'rank' => 6,
'prize' => 5,
'prize_after_tax' => Config::getAfterTaxPrize(5),
];
}
return null;
}
}