897 lines
31 KiB
PHP
Executable File
897 lines
31 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\OrderBjdcSfggResult;
|
|
use App\Model\OrderBjdcSfggZuhe;
|
|
use App\Model\Seller\Seller;
|
|
use App\Model\Zq\BjdcResult;
|
|
use App\Model\Zq\BjdcSfggOdds;
|
|
use App\Model\Zq\BjdcSfggResult;
|
|
use App\Utils\Helps;
|
|
use App\Utils\ThrowException;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class BjdcSfggService implements IJingcai
|
|
{
|
|
use JingCaiTrait;
|
|
|
|
/** @var PlayTypeService playTypeService */
|
|
protected $playTypeService;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->playTypeService = app(PlayTypeService::class);
|
|
}
|
|
|
|
public function playTypeNames()
|
|
{
|
|
return [
|
|
'sfgg_odds' => '胜负过关',
|
|
|
|
];
|
|
}
|
|
|
|
public function getPlayTypeName($playType)
|
|
{
|
|
$fieldNames = $this->playTypeNames();
|
|
$result = $fieldNames[$playType];
|
|
return $result;
|
|
}
|
|
|
|
public function resultFieldNames()
|
|
{
|
|
return [
|
|
'win' => '胜',
|
|
'draw' => '平',
|
|
'loss' => '负',
|
|
'handicap' => '让球数',
|
|
|
|
];
|
|
}
|
|
|
|
public function getResultFieldName($playType, $field)
|
|
{
|
|
$fieldNames = $this->resultFieldNames();
|
|
$result = $fieldNames[$field];
|
|
if (!in_array($field, ['win', 'loss', 'draw']) || $playType != 'rq_odds') {
|
|
return $result;
|
|
}
|
|
return '让' . $result;
|
|
}
|
|
|
|
public static function generateZqPassModeInfo($oddsData, $playType)
|
|
{
|
|
$matchNum = count($oddsData);
|
|
if ($matchNum > 15) {
|
|
$matchNum = 15;
|
|
}
|
|
$multi = [];
|
|
for ($i = 3; $i <= $matchNum; $i++) {
|
|
$title = $i . '串1';
|
|
$multi[] = [
|
|
'key' => $i . '.1',
|
|
'title' => $title,
|
|
];
|
|
}
|
|
return [
|
|
'multi' => $multi,
|
|
'single' => []
|
|
];
|
|
}
|
|
|
|
public function saleLotteries(Customer $customer, $lotteryTypeId)
|
|
{
|
|
$lotteryType = LotteryType::where('type', LottType::BJDC_SFGG)
|
|
->where('status', BoolEnum::YES)
|
|
->find($lotteryTypeId);
|
|
ThrowException::isTrue(!$lotteryType, '不支持此彩种!');
|
|
|
|
$lottery = Lottery::active()
|
|
->shopAndType($customer->shop_id, $lotteryTypeId)
|
|
->first();
|
|
ThrowException::isTrue(!$lotteryType, '店铺不支持此彩种');
|
|
|
|
$odds = BjdcSfggOdds::selling()
|
|
->where('close_time', '>', date('Y-m-d H:i:s', time() + $lottery->earlySecond()))
|
|
->orderBy('play_num', 'asc')
|
|
->orderBy('issue_num', 'asc')
|
|
->get();
|
|
|
|
$result = [
|
|
'play_types' => PlayType::asBjdcSfggSelectArray(),
|
|
];
|
|
|
|
$competitions = [];
|
|
/** @var BjdcSfggOdds $odd */
|
|
foreach ($odds as $odd) {
|
|
|
|
if (!in_array($odd->bd_competition_name, $competitions)) {
|
|
$competitions[] = $odd->bd_competition_name;
|
|
}
|
|
$odd->bd_competition_name = preg_replace('/[0-9]/', '', $odd->bd_competition_name);
|
|
$realCloseTime = strtotime($odd->close_time) - $lottery->earlySecond();
|
|
$odd->close_time = date('Y-m-d H:i:s', $realCloseTime);
|
|
$odd->real_close_str = date('H:i', $realCloseTime) . '截止';
|
|
$odd->sfgg_odds = Helps::compareOdds($odd->sfgg_odds, $odd->sfgg_odds_last);
|
|
|
|
|
|
BjdcSfggOdds::unsetLastSuffixField($odd);
|
|
}
|
|
if ($competitions) {
|
|
array_unshift($competitions, '全部');
|
|
}
|
|
|
|
$result['competitions'] = $competitions;
|
|
$result['odds'] = $odds;
|
|
return $result;
|
|
}
|
|
|
|
|
|
public function refreshOddsWithResult($oddsData)
|
|
{
|
|
$ids = array_keys($oddsData);
|
|
if (!$ids) {
|
|
return $oddsData;
|
|
}
|
|
$results = BjdcSfggResult::whereIn('bjdc_sfgg_odds_id', $ids)
|
|
->get()
|
|
->keyBy('bjdc_sfgg_odds_id');
|
|
|
|
foreach ($oddsData as $id => &$oddItem) {
|
|
/** @var BjdcSfggResult $result */
|
|
$result = Arr::get($results, $id);
|
|
if (!$result) {
|
|
continue;
|
|
}
|
|
$cancel = $result->canceled();
|
|
foreach ($oddItem as $playType => &$playData) {
|
|
foreach ($playData as $playName => &$playOdds) {
|
|
if ($cancel) {
|
|
$playOdds = 1;
|
|
continue;
|
|
}
|
|
$resultPlayName = str_replace('_odds', '_field', $playType);
|
|
|
|
if ($playName == $result->{$resultPlayName}) {
|
|
$playOdds = Helps::floatFormat($result->{$playType});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return $oddsData;
|
|
}
|
|
public function refreshOdds($data)
|
|
{
|
|
$ids = array_keys($data);
|
|
throw_if(!$ids, JingCaiException::create('请选择比赛进行投注'));
|
|
|
|
$odds = BjdcSfggOdds::whereIn('id', $ids)->get();
|
|
$odds = collect($odds);
|
|
ThrowException::isTrue($odds->count() != count($ids), '比赛数据错误,请重新选择');
|
|
|
|
$result = [];
|
|
foreach ($data as $id => $val) {
|
|
$item = [];
|
|
foreach ($val as $playType => $playData) {
|
|
$oddItem = $odds->where('id', $id)->first();
|
|
foreach ($playData as $k => $v) {
|
|
$odd = Arr::get($oddItem, $playType . '.' . $k);
|
|
if (!$odd) {
|
|
Log::error('JczqService->refreshOdds数据错误', [
|
|
'data' => $data,
|
|
'key' => $playType . '.' . $k,
|
|
'val' => $odd,
|
|
'id' => $id,
|
|
]);
|
|
}
|
|
|
|
throw_if($odd === null, JingCaiException::create('数据错误,请重新选择'));
|
|
|
|
$item[$playType][$k] = Arr::get($oddItem, $playType . '.' . $k);
|
|
}
|
|
}
|
|
$result[$id] = $item;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
public function computePrizeInfo($data, $betsNum = 1, $passModeKeys = [])
|
|
{
|
|
sort($passModeKeys);
|
|
$zhuTotal = 0;
|
|
$min = 0;
|
|
$max = 0;
|
|
foreach ($passModeKeys as $key) {
|
|
|
|
$keyArr = explode('.', $key);
|
|
$change = $keyArr[0];
|
|
|
|
$prizeInfo = $this->generatePrizeOdds($data, $change);
|
|
$zhuTotal += $prizeInfo['zhu_num'];
|
|
if (!$min) {
|
|
$min = $prizeInfo['min'];
|
|
}
|
|
if ($prizeInfo['min'] < $min) {
|
|
$min = $prizeInfo['min'];
|
|
}
|
|
$max += $prizeInfo['max'];
|
|
}
|
|
return [
|
|
'zhu_num' => $zhuTotal,
|
|
'expect_bets' => 0,
|
|
'money' => Helps::floatFormat($zhuTotal * $betsNum * Config::lotteryUnitPrice()),
|
|
'prize_min' => Helps::floatFormat($min * $betsNum * Config::lotteryUnitPrice() * Config::bjdcPrizePercent()),
|
|
'prize_max' => Helps::floatFormat($max * $betsNum * Config::lotteryUnitPrice() * Config::bjdcPrizePercent())
|
|
];
|
|
}
|
|
|
|
public function generatePrizeOdds($data, $len)
|
|
{
|
|
$errRes = [
|
|
'zhu_num' => 0,
|
|
'min' => 0,
|
|
'max' => 0,
|
|
];
|
|
$fdata = $this->generateCombinationInput($data);
|
|
if (!$fdata) {
|
|
Log::error('generatePrize格式化数据失败', [
|
|
'data' => $data,
|
|
'len' => $len
|
|
]);
|
|
return $errRes;
|
|
}
|
|
$combinationData = Helps::getCombinationData($fdata, $len);
|
|
if (!$combinationData) {
|
|
Log::error('generatePrize生成组合数据失败', [
|
|
'fdata' => $fdata,
|
|
'len' => $len
|
|
]);
|
|
return $errRes;
|
|
}
|
|
|
|
$prizes = [];
|
|
$zhuTotal = count($combinationData);
|
|
foreach ($combinationData as $keyArr) {
|
|
$keyOdds = 1;
|
|
foreach ($keyArr as $key) {
|
|
$keyInfo = $this->parseJczqCombinationItem($key);
|
|
$baseItemArr = Arr::get($data, $keyInfo['id']);
|
|
$odds = Arr::get($baseItemArr, $keyInfo['play'] . '.' . $keyInfo['result'], null);
|
|
$keyOdds *= $odds;
|
|
}
|
|
$prizes[] = $keyOdds;
|
|
|
|
}
|
|
sort($prizes, SORT_ASC);
|
|
return [
|
|
'zhu_num' => $zhuTotal,
|
|
'min' => $prizes[0],
|
|
'max' => $prizes[count($prizes) - 1],
|
|
];
|
|
}
|
|
|
|
public function generateJczqCombinationItem($id, $playType, $result)
|
|
{
|
|
return sprintf('%d-%s-%s', $id, $playType, $result);
|
|
}
|
|
|
|
public function parseJczqCombinationItem($key)
|
|
{
|
|
$info = explode('-', $key);
|
|
return [
|
|
'id' => $info[0],
|
|
'play' => $info[1],
|
|
'result' => $info[2],
|
|
];
|
|
}
|
|
|
|
public function generateCombinationInput($data, $hasOdds = false)
|
|
{
|
|
$result = [];
|
|
foreach ($data as $id => $item) {
|
|
$resItem = [];
|
|
foreach ($item as $k => $itemData) {
|
|
foreach ($itemData as $kk => $kkv) {
|
|
$resKey = $this->generateJczqCombinationItem($id, $k, $kk);
|
|
if ($hasOdds) {
|
|
$resItem[$resKey] = $kkv;
|
|
} else {
|
|
$resItem[$resKey] = $resKey;
|
|
}
|
|
}
|
|
}
|
|
$result[] = $resItem;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
public function getBetsInfoFromCombinationData($data, $betsNum = 1, $dataOdd)
|
|
{
|
|
$result = [];
|
|
foreach ($data as $zuheInfo) {
|
|
$resultItem = [];
|
|
$allOdds = 1;
|
|
foreach ($zuheInfo as $key) {
|
|
$keyInfo = $this->parseJczqCombinationItem($key);
|
|
$keyInfo['odd'] = $this->getBetsOddFromCombinationData($key, $dataOdd);
|
|
$allOdds *= $keyInfo['odd'];
|
|
|
|
$resultItem['ids'][] = intval($keyInfo['id']);
|
|
$resultItem['keys'][] = $key;
|
|
$resultItem['info'][$keyInfo['id']] = $keyInfo;
|
|
$resultItem['bets_num'] = $betsNum;
|
|
}
|
|
$tempKeys = $resultItem['keys'];
|
|
sort($tempKeys);
|
|
$resultItem['unique'] = implode(';', $tempKeys);
|
|
$resultItem['all_odds'] = $allOdds;
|
|
|
|
$result[] = $resultItem;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
public function piaoMatchCanAddPlay($piao, $betsInfo)
|
|
{
|
|
foreach ($piao['bets'] as $item) {
|
|
$intersectIds = array_intersect($item['ids'], $betsInfo['ids']);
|
|
if ($intersectIds) {
|
|
|
|
foreach ($intersectIds as $id) {
|
|
if ($item['info'][$id]['play'] != $betsInfo['info'][$id]['play']) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public function piaoAddBets(&$piaos, $betsInfo)
|
|
{
|
|
$addPiao = false;
|
|
if (!$piaos) {
|
|
$piaos = [];
|
|
$betsNumAll = $betsInfo['bets_num'];
|
|
// 超出限制,添加一部分,返回剩余的
|
|
if ($betsNumAll > Config::piaoMaxBetsNum()) {
|
|
$addNum = Config::piaoMaxBetsNum();
|
|
|
|
$tempBetsInfo = $betsInfo;
|
|
$betsInfo['bets_num'] = $addNum;
|
|
$piaos[0]['bets'][] = $betsInfo;
|
|
$piaos[0]['bets_num'] = $addNum;
|
|
$tempBetsInfo['bets_num'] -= $addNum;
|
|
return $this->piaoAddBets($piaos, $tempBetsInfo);
|
|
}
|
|
|
|
$piaos[0] = [
|
|
'bets' => [$betsInfo],
|
|
'bets_num' => $betsInfo['bets_num']
|
|
];
|
|
$addPiao = true;
|
|
return $addPiao;
|
|
}
|
|
foreach ($piaos as $pk => $piao) {
|
|
|
|
if ($piaos[$pk]['bets_num'] >= Config::piaoMaxBetsNum()) {
|
|
continue;
|
|
}
|
|
|
|
// 同一场只能出现一个玩法
|
|
$can = $this->piaoMatchCanAddPlay($piao, $betsInfo);
|
|
if (!$can) {
|
|
continue;
|
|
}
|
|
|
|
$betsNumAll = $piaos[$pk]['bets_num'] + $betsInfo['bets_num'];
|
|
|
|
// 超出限制,添加一部分,返回剩余的
|
|
if ($betsNumAll > Config::piaoMaxBetsNum()) {
|
|
$addNum = Config::piaoMaxBetsNum() - $piaos[$pk]['bets_num'];
|
|
|
|
$tempBetsInfo = $betsInfo;
|
|
$betsInfo['bets_num'] = $addNum;
|
|
$piaos[$pk]['bets'][] = $betsInfo;
|
|
$piaos[$pk]['bets_num'] += $addNum;
|
|
$tempBetsInfo['bets_num'] -= $addNum;
|
|
return $this->piaoAddBets($piaos, $tempBetsInfo);
|
|
}
|
|
|
|
$addPiao = true;
|
|
$piaos[$pk]['bets'][] = $betsInfo;
|
|
$piaos[$pk]['bets_num'] += $betsInfo['bets_num'];
|
|
}
|
|
|
|
if (!$addPiao) {
|
|
$nextPk = count($piaos);
|
|
|
|
$betsNumAll = $betsInfo['bets_num'];
|
|
// 超出限制,添加一部分,返回剩余的
|
|
if ($betsNumAll > Config::piaoMaxBetsNum()) {
|
|
$addNum = Config::piaoMaxBetsNum();
|
|
$tempBetsInfo = $betsInfo;
|
|
|
|
$betsInfo['bets_num'] = $addNum;
|
|
|
|
$piaos[$nextPk]['bets'][] = $betsInfo;
|
|
$piaos[$nextPk]['bets_num'] = $addNum;
|
|
|
|
$tempBetsInfo['bets_num'] -= $addNum;
|
|
return $this->piaoAddBets($piaos, $tempBetsInfo);
|
|
}
|
|
|
|
$piaos[$nextPk] = [
|
|
'bets' => [$betsInfo],
|
|
'bets_num' => $betsInfo['bets_num']
|
|
];
|
|
}
|
|
}
|
|
|
|
public function createPiaos($betsInfos)
|
|
{
|
|
$piaos = [];
|
|
foreach ($betsInfos as $betsInfo) {
|
|
$this->piaoAddBets($piaos, $betsInfo);
|
|
}
|
|
return $piaos;
|
|
}
|
|
|
|
public function valid(Lottery $lottery, $playType, $data)
|
|
{
|
|
ThrowException::isTrue(count($data) < 3, '至少选择3场比赛');
|
|
|
|
}
|
|
|
|
/**
|
|
* 根据用户的下注数据,生成购物车中的比赛下注信息
|
|
* @param Array $matchs
|
|
* @return array
|
|
*/
|
|
public function getBjdcSfggOddsFromData(Lottery $lottery, $oddsData)
|
|
{
|
|
$jczqCount = BjdcSfggOdds::selling()->whereIn('id', array_keys($oddsData))->count();
|
|
throw_if($jczqCount != count($oddsData), JingCaiException::create('比赛信息有误'));
|
|
$odds = BjdcSfggOdds::selling()
|
|
->whereIn('id', array_keys($oddsData))
|
|
->get();
|
|
|
|
$result = [];
|
|
foreach ($odds as $odd) {
|
|
|
|
$odd->bd_competition_name = preg_replace('/[0-9]/', '', $odd->bd_competition_name);
|
|
|
|
$realCloseTime = strtotime($odd->real_close_time) - $lottery->earlySecond();
|
|
$odd->real_close_time = date('Y-m-d H:i:s', $realCloseTime);
|
|
$odd->real_close_str = date('H:i', $realCloseTime) . '截止';
|
|
|
|
unset($odd->sfgg_odds_last);
|
|
if (@$oddsData[$odd->id]['sfgg_odds']) {
|
|
$odd->sfgg_odds = Helps::copyProperty($odd->sfgg_odds, $oddsData[$odd->id]['sfgg_odds']);
|
|
} else {
|
|
Log::error('getBjdcSfggOddsFromData的数据没有sfgg_odds', $odd->toArray());
|
|
ThrowException::run('数据有误,请联系管理员');
|
|
}
|
|
$result[] = $odd;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
public function generateBetsInfo($odds, $passModeKeys, $betsNum)
|
|
{
|
|
$data = $this->generateCombinationInput($odds);
|
|
$result = [];
|
|
foreach ($passModeKeys as $key) {
|
|
$passModes = self::getPassMode($key);
|
|
foreach ($passModes as $chang => $changeGe) {
|
|
$combinationData = Helps::getCombinationData($data, $chang);
|
|
$result = array_merge($result, $combinationData);
|
|
}
|
|
}
|
|
$dataOdd = $this->generateCombinationInput($odds, true);
|
|
return $this->getBetsInfoFromCombinationData($result, $betsNum, $dataOdd);
|
|
}
|
|
|
|
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', '');
|
|
|
|
|
|
ThrowException::isTrue(!$passModeKeys, '请选择串关');
|
|
|
|
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('店铺未开通该彩种!'));
|
|
|
|
$this->valid($lott, $playType, $odds);
|
|
|
|
$odds = $this->refreshOdds($odds);
|
|
|
|
$computeInfo = $this->computePrizeInfo($odds, $betsNum, $passModeKeys);
|
|
|
|
$lott->validMixMoney($computeInfo['money']);
|
|
|
|
|
|
if ($type == OrderType::FADAN) {
|
|
throw_if(!$fadanDesc, JingCaiException::create('请填写发单宣言!'));
|
|
$this->canCreateFanDanOrder($customer->id);
|
|
|
|
ThrowException::isTrue($this->fadanValid($computeInfo['money'], $computeInfo['prize_min']), '最低奖金大于等于投注金额的1.6倍');
|
|
}
|
|
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 {
|
|
|
|
$bjdcOdds = BjdcSfggOdds::whereIn('id', array_keys($odds))->get();
|
|
$closeTimes = [];
|
|
$oddsCloseTimes = [];
|
|
$issueNum = '';
|
|
foreach ($bjdcOdds as $match) {
|
|
$realCloseTime = strtotime($match->close_time) - $lott->earlySecond();
|
|
$real_close_time = date('Y-m-d H:i:s', $realCloseTime);
|
|
$closeTimes[] = $real_close_time;
|
|
$oddsCloseTimes[] = date('Y-m-d H:i:s', strtotime($match->close_time));
|
|
if ($issueNum && $issueNum != $match->issue_num) {
|
|
ThrowException::run('无法跨期投注');
|
|
}
|
|
$issueNum = $match->issue_num;
|
|
}
|
|
sort($closeTimes);
|
|
sort($oddsCloseTimes);
|
|
$earlyTime = $closeTimes[0];
|
|
$oddsCloseTime = $oddsCloseTimes[0];
|
|
$lateTime = $closeTimes[count($closeTimes) - 1];
|
|
|
|
$this->checkCloseTimeOver($earlyTime);
|
|
|
|
$betsInfos = $this->generateBetsInfo($odds, $passModeKeys, $betsNum);
|
|
$piaos = $this->createPiaos($betsInfos);
|
|
|
|
$order = new Order();
|
|
$order->pid = $pid;
|
|
$order->customer_id = $customer->id;
|
|
$order->lottery_id = $lott->id;
|
|
$order->issue_num = $issueNum;
|
|
$order->shop_id = $customer->shop_id;
|
|
$order->lottery_type_id = $lott->lottery_type_id;
|
|
$order->order_sn = Order::makeOrderSn();
|
|
$order->play_type = $playType;
|
|
$order->bets_num = $betsNum;
|
|
$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 = $passModeKeys;
|
|
$order->odds_close_time = $oddsCloseTime;
|
|
$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();
|
|
}
|
|
|
|
// 创建组合数据
|
|
$this->createZuhe($betsInfos, $order->id, null);
|
|
|
|
$orderOdds = [];
|
|
foreach ($bjdcOdds as $match) {
|
|
|
|
$orderOdds[] = [
|
|
'order_id' => $order->id,
|
|
'bjdc_sfgg_odds_id' => $match->id,
|
|
'odds_id' => $match->odds_id,
|
|
'match_id' => $match->match_id,
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
'updated_at' => date('Y-m-d H:i:s'),
|
|
];
|
|
}
|
|
OrderBjdcSfggResult::insert($orderOdds);
|
|
|
|
DB::commit();
|
|
return $order;
|
|
} catch (JingCaiException $e) {
|
|
DB::rollBack();
|
|
Log::error('bjdc::order::create ValidateException: ' . $e);
|
|
throw $e;
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
Log::error('bjdc::order::create Exception: ' . $e);
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
private function createZuhe($betsInfo, $orderId, $optimizeZuhe = [])
|
|
{
|
|
if ($optimizeZuhe) {
|
|
foreach ($optimizeZuhe as $item) {
|
|
$orderZuhe = new OrderBjdcSfggZuhe();
|
|
$orderZuhe->order_id = $orderId;
|
|
$orderZuhe->jczq_odds_ids = $item['ids'];
|
|
$orderZuhe->info = $item['info'];
|
|
$orderZuhe->bets_num = $item['bets_num'];
|
|
$orderZuhe->repeat_num = Arr::get($item, 'repeat_num', 1);
|
|
$orderZuhe->save();
|
|
}
|
|
return;
|
|
}
|
|
foreach ($betsInfo as $item) {
|
|
$orderZuhe = new OrderBjdcSfggZuhe();
|
|
$orderZuhe->order_id = $orderId;
|
|
$orderZuhe->bjdc_sfgg_odds_ids = $item['ids'];
|
|
$orderZuhe->info = $item['info'];
|
|
$orderZuhe->bets_num = $item['bets_num'];
|
|
$orderZuhe->save();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 跟单
|
|
* @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->type_mode == 1 && ($order->type == OrderType::GENDAN || $order->type == OrderType::UNION)) {
|
|
$isTypeModel1 = true;
|
|
|
|
// 合买,是自己时,全部可见
|
|
if ($order->type == OrderType::UNION) {
|
|
if ($order->customer_id == $customer->id) {
|
|
$isTypeModel1 = false;
|
|
}
|
|
}
|
|
|
|
// 跟单:如果是自己跟自己,全部可见
|
|
if ($order->type == OrderType::GENDAN) {
|
|
if ($order->customer_id == $customer->id && $order->p_order->customer_id == $customer->id) {
|
|
$isTypeModel1 = false;
|
|
}
|
|
}
|
|
// 发单:如果是自己跟自己,全部可见
|
|
if ($order->type == OrderType::FADAN) {
|
|
if ($order->customer_id == $customer->id) {
|
|
$isTypeModel1 = false;
|
|
}
|
|
}
|
|
$order->sellings = $this->getOrderOdds($order, $isTypeModel1);
|
|
} else {
|
|
if ($order->customerCanSeeSellings($customer)) {
|
|
$order->sellings = $this->getOrderOdds($order);
|
|
}
|
|
}
|
|
if (!$order->sellings) {
|
|
$odds = $order->odds;
|
|
$jclqOddsIds = array_keys($odds);
|
|
$jclqOdds = BjdcSfggOdds::whereIn('id', $jclqOddsIds)
|
|
->where('close_time', '>', date('Y-m-d H:i:s'))
|
|
->first();
|
|
if (!$jclqOdds) {
|
|
$order->sellings = $this->getOrderOdds($order);
|
|
}
|
|
}
|
|
|
|
$order->chang_num = count($order->sellings);
|
|
$names = [];
|
|
foreach ($order->pass_mode as $val) {
|
|
$names[] = str_replace('.', '串', $val);
|
|
}
|
|
$order->pass_mode_names = $names;
|
|
|
|
$order->play_name = $order->lottery->name;
|
|
|
|
unset($order->lottery);
|
|
|
|
return $order;
|
|
}
|
|
|
|
private function getOrderOdds(Order $order, $isTypeModel1 = false)
|
|
{
|
|
/** @var Lottery $lottery */
|
|
$lottery = $order->lottery;
|
|
$earlyTime = $lottery->earlySecond();
|
|
|
|
$odds = $order->odds;
|
|
$bjdcOddsIds = array_keys($odds);
|
|
|
|
$bjdcOdds = BjdcSfggOdds::select([
|
|
'bjdc_sfgg_odds.id',
|
|
'bjdc_sfgg_odds.issue_num',
|
|
'bjdc_sfgg_odds.play_num',
|
|
'bjdc_sfgg_odds.bd_competition_name',
|
|
'bjdc_sfgg_odds.bd_competition_name_full',
|
|
'bjdc_sfgg_odds.bd_home_team_name',
|
|
'bjdc_sfgg_odds.bd_home_team_name_full',
|
|
'bjdc_sfgg_odds.bd_away_team_name',
|
|
'bjdc_sfgg_odds.bd_away_team_name_full',
|
|
DB::raw('close_time as real_close_time'),
|
|
])
|
|
->whereIn('id', $bjdcOddsIds)
|
|
->orderBy('bjdc_sfgg_odds.play_num', 'asc')
|
|
->get();
|
|
|
|
$currentDate = date('Y-m-d H:i:s');
|
|
$result = [];
|
|
foreach ($bjdcOdds as $item) {
|
|
$jczqResult = null;
|
|
if (in_array($order->lottery_state, Order::kaiJiangHouStates())) {
|
|
$jczqResult = BjdcSfggResult::where('bjdc_sfgg_odds_id', $item->id)->first();
|
|
}
|
|
$item->vs_b = $item->bd_home_team_name;
|
|
$item->vs_m = 'vs';
|
|
$item->vs_a = $item->bd_away_team_name;
|
|
$item->play_items = $this->getOrderPlayItems($order, $item, $jczqResult);
|
|
$item->half_time_score = '';
|
|
$item->full_time_score = '';
|
|
if ($jczqResult) {
|
|
$item->half_time_score = $jczqResult->half_time_score;
|
|
$item->full_time_score = $jczqResult->full_time_score;
|
|
}
|
|
$item->lottery_close_time = date('Y-m-d H:i:s', strtotime($item->real_close_time) - $earlyTime);
|
|
if ($isTypeModel1) {
|
|
if ($item->lottery_close_time > $currentDate) {
|
|
$result[] = new \stdClass();
|
|
} else {
|
|
$result[] = $item;
|
|
}
|
|
} else {
|
|
$result[] = $item;
|
|
}
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
private function getOrderPlayItems(Order $order, $bjdcOddsItem, $jczqResult)
|
|
{
|
|
$odds = $order->odds;
|
|
$buyOddsItem = Arr::get($odds, $bjdcOddsItem->id);
|
|
$playTypeNames = $this->playTypeNames();
|
|
$result = [];
|
|
|
|
foreach ($playTypeNames as $playType => $name) {
|
|
$buyPlayData = Arr::get($buyOddsItem, $playType);
|
|
if (!$buyPlayData) {
|
|
continue;
|
|
}
|
|
$resultPlayTypeItem = [];
|
|
foreach ($buyPlayData as $resultField => $odds) {
|
|
|
|
$wined = false;
|
|
|
|
$buyField = str_replace('_odds', '_field', $playType);
|
|
if ($jczqResult && $jczqResult->{$buyField} == $resultField) {
|
|
$wined = true;
|
|
}
|
|
|
|
$resultPlayTypeItem[] = [
|
|
'odds_value' => $odds,
|
|
'odds_name' => $resultField,
|
|
'name' => sprintf('%s(%s)', $this->getResultFieldName($playType, $resultField), $odds),
|
|
'wined' => $wined
|
|
];
|
|
}
|
|
$winResult = '';
|
|
$resultField = str_replace('_odds', '_name', $playType);
|
|
if ($jczqResult && $jczqResult->{$resultField}) {
|
|
$winResult = $jczqResult->{$resultField};
|
|
}
|
|
//dump($resultField,$playType,$playTypeNames,$jczqResult->toArray());
|
|
|
|
$result[] = [
|
|
'win_result' => $winResult,
|
|
'play_type' => $playType,
|
|
'play_name' => $this->getPlayTypeName($playType),
|
|
'play_items' => $resultPlayTypeItem,
|
|
];
|
|
}
|
|
// dd($result);
|
|
return $result;
|
|
}
|
|
|
|
|
|
public function sellerShowOrder(Seller $seller, Order $order)
|
|
{
|
|
$order->sellings = [];
|
|
if ($order->sellerCanSeeSellings($seller)) {
|
|
$order->sellings = $this->getOrderOdds($order);
|
|
}
|
|
$order->chang_num = count($order->sellings);
|
|
$names = [];
|
|
foreach ($order->pass_mode as $val) {
|
|
$names[] = str_replace('.', '串', $val);
|
|
}
|
|
$order->pass_mode_names = $names;
|
|
|
|
$order->play_name = $order->lottery->name;
|
|
|
|
unset($order->lottery);
|
|
|
|
return $order;
|
|
}
|
|
|
|
public function chaiPiao(Order $order)
|
|
{
|
|
|
|
$piaoList = [];
|
|
return $piaoList;
|
|
}
|
|
}
|