jingcai-php/app/Service/GuanService.php

336 lines
10 KiB
PHP
Executable File

<?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\Enums\SaleState;
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\OrderGuanResult;
use App\Model\OrderJczqResult;
use App\Model\Seller\Seller;
use App\Model\Zq\JczqGuan;
use App\Model\Zq\JczqGuanOdds;
use App\Model\Zq\JczqGuanYaOdds;
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 GuanService implements IJingcai
{
public function __construct()
{
}
public function getSellingGuan()
{
return JczqGuan::where('type', 'guan')->where('sales_state', SaleState::Selling)->first();
}
public function saleLotteries(Customer $customer, $lotteryTypeId)
{
$lotteryType = LotteryType::where('type', LottType::GUAN)
->where('status', BoolEnum::YES)
->find($lotteryTypeId);
ThrowException::isTrue(!$lotteryType, '不支持此彩种');
/** @var Lottery $lott */
$lott = Lottery::active()->shopAndType($customer->shop_id, $lotteryTypeId)->first();
ThrowException::isTrue(!$lott, '暂不支持该彩种');
/** @var JczqGuan $guan */
$guan = $this->getSellingGuan();
ThrowException::isTrue(!$guan, '暂未开售');
unset($guan->options);
$guan->odds;
return $guan;
}
public function refreshOdds($data)
{
$result = [];
if (!$data) {
return [];
}
$odds = JczqGuanOdds::whereIn('id', array_keys($data))->pluck('odds', 'id')->toArray();
if (count($data) != count($odds)) {
ThrowException::run('投注信息有误');
}
// $first = JczqGuanOdds::whereIn('id', array_keys($data))->where('order_state', SaleState::StopSelling)->first();
// ThrowException::isTrue($first, '请选择销售中的比赛');
foreach ($data as $id => $item) {
if (Arr::get($odds, $id) === null) {
Log::error('冠军投注信息有误:', [
'data' => $data,
'odds' => $odds,
'error_id' => $id,
]);
ThrowException::run('投注信息有误:' . $id);
}
$result[$id] = Arr::get($odds, $id);
}
return $result;
}
public function computePrizeInfo($data, $betsNum = 1, $passModeKeys = [])
{
$guan = $this->getSellingGuan();
ThrowException::isTrue(!$guan, '暂未开售');
$zhuTotal = count($data);
$money = count($data) * $betsNum * Config::lotteryUnitPrice();
$maxOdds = 0;
foreach ($data as $id => $odd) {
$maxOdds = $odd > $maxOdds ? $odd : $maxOdds;
}
$prize = $betsNum * Config::lotteryUnitPrice() * $maxOdds;
return [
'zhu_num' => $zhuTotal,
'bets_num' => $betsNum,
'expect_bets' => 0,
'money' => $money,
'prize_min' => Helps::floatFormat($prize),
'prize_max' => Helps::floatFormat($prize)
];
}
public function valid(Lottery $lottery, $data)
{
}
public function createPiaos($data)
{
return [];
}
public function createOrder(Customer $customer, $data)
{
$lotteryTypeId = Arr::get($data, 'lottery_type_id');
$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');
$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));
/** @var JczqGuan $guan */
$guan = $this->getSellingGuan();
ThrowException::isTrue(!$guan, '暂未开售');
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') > $guan->getCloseTime($lott->earlySecond()), '投注已截止!');
$this->valid($lott, $odds);
$odds = $this->refreshOdds($odds);
$first = JczqGuanOdds::whereIn('id', array_keys($odds))->where('order_state', SaleState::StopSelling)->first();
ThrowException::isTrue($first, '请选择销售中的比赛');
$computeInfo = $this->computePrizeInfo($odds, $betsNum);
$oddsRaw = $odds;
$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 = $guan->getCloseTime($lott->earlySecond());
$lateTime = $earlyTime;
$piaos = $this->createPiaos($odds);
$order = new Order();
$order->pid = $pid;
$order->issue_num = $guan->id;
$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 = count($piaos);
$order->money = $computeInfo['money'];
$order->prize_min = $computeInfo['prize_min'];
$order->prize_max = $computeInfo['prize_max'];
$order->pay_state = PayState::UNPAID;
$order->pass_mode = [];
$order->odds_close_time = $guan->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;
$order->odds_raw = $oddsRaw;
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();
$this->saveOrderResult($order);
// 如果是合买,更新自己的pid
if ($order->type == OrderType::UNION) {
$order->pid = $order->id;
$order->save();
}
DB::commit();
return $order;
} catch (JingCaiException $e) {
DB::rollBack();
Log::error('dlt::order::create ValidateException: ' . $e);
throw $e;
} catch (\Exception $e) {
DB::rollBack();
Log::error('dlt::order::create Exception: ' . $e);
throw $e;
}
}
public function saveOrderResult($order)
{
$odds = $order->odds;
$guanOdds = JczqGuanOdds::whereIn('id', array_keys($odds))->get();
foreach ($guanOdds as $item) {
$result = new OrderGuanResult();
$result->order_id = $order->id;
$result->odds_id = $item->odds_id;
$result->jczq_guan_odds_id = $item->id;
$result->published = BoolEnum::NO;
$result->save();
}
}
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;
$result = JczqGuanOdds::whereIn('id', array_keys($odds))->get();
return $result;
}
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 [];
}
}