jingcai-php/app/Service/PlwService.php

378 lines
12 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\Plw;
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 PlwService implements IJingcai
{
private $shuziNum = 5;
public function __construct()
{
}
public static function haomas()
{
return [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
];
}
public static function inHaoma( $hao)
{
$haomas = self::haomas();
return in_array($hao, $haomas);
}
public function getSellingPlw()
{
$qxc = Plw::where('state', BoolEnum::NO)->where('enable', BoolEnum::YES)->first();
return $qxc;
}
public function saleLotteries(Customer $customer, $lotteryTypeId)
{
$lotteryType = LotteryType::where('type', LottType::PLW)
->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 Plw $plw */
$plw = $this->getSellingPlw();
ThrowException::isTrue(!$plw, '暂未开售');
$gets = Plw::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' => $plw->issue_num,
'close_time_str' => $plw->getCloseTime($lott->earlySecond())
],
];
}
public function refreshOdds($data)
{
foreach ($data as &$item) {
$item['play_type'] = $this->getPlayType($item['play_odd']);
}
return $data;
}
public function getPlayType($playOdd) {
foreach ($playOdd as $item) {
if (count($item) > 1) {
return PlayType::PLW_FUSHI;
}
}
return PlayType::PLW_DANSHI;
}
public function computePrizeInfo($data, $betsNum = 1, $passModeKeys = [])
{
$dlt = $this->getSellingPlw();
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, $this->shuziNum);
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 $items) {
ThrowException::isTrue(count($items) != count(array_unique($items)), '同位置数字不可重复');
foreach ($items as $hao) {
$has = self::inHaoma($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 Plw $plw */
$plw = $this->getSellingPlw();
ThrowException::isTrue(!$plw, '暂未开售');
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') > $plw->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 = $plw->getCloseTime($lott->earlySecond());
$lateTime = $earlyTime;
$order = new Order();
$order->pid = $pid;
$order->issue_num = $plw->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 = $plw->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('plw::order::create ValidateException: ' . $e);
throw $e;
} catch (\Exception $e) {
DB::rollBack();
Log::error('plw::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 [];
}
/**
* @param Plw $plw
* @param $oddData
* @return array|null
*/
public function computeDanshiWinPrize(Plw $plw, $oddData)
{
$result = $plw->result;
$sameNum = 0;
foreach ($result as $idx => $val) {
$buyVal = $oddData[$idx];
if ($buyVal == $val) {
$sameNum++;
}
}
if ($sameNum == $this->shuziNum) {
return [
'rank' => 1,
'prize' => $plw->prize,
'prize_after_tax' => Config::getAfterTaxPrize($plw->prize),
];
}
return null;
}
}