jingcai-php/app/Http/Controllers/Api/Customer/CtzqJqcController.php

457 lines
17 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
/**
* @createtime 2023/4/26
* @author wild
* @copyright PhpStorm
*/
namespace App\Http\Controllers\Api\Customer;
use App\Enums\PayType;
use App\Enums\SaleState;
use App\Model\Customer\Customer;
use App\Model\Lottery;
use App\Model\LotteryType;
use App\Model\Zq\CtzqJqc;
use App\Model\Zq\CtzqJqcMatch;
use App\Service\CtzqJqcService;
use App\Utils\Helps;
use App\Utils\ThrowException;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
class CtzqJqcController extends BaseController
{
/**
* @api {GET} /api/customer/ctzq_jqc/selling 4场进球彩-投注列表
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {Int} lottery_type_id 彩票类型ID
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": {
* "play_types": [ // 选择时候需要切换url
* {
* "name": "4场进球",
* "play_type": "ctzq_jqc" // 用这个替换
* },
* {
* "name": "6场半全场",
* "play_type": "ctzq_bqc"
* }
* ],
* "odds": [
* {
* "id": 1,
* "ret_id": 1,
* "type": "ctzq_jqc",
* "issue_num": "20007", // 期号
* "state": "selling", // noSelling 未开售, selling: 销售中, stopSelling: 停售, hasLottery: 已开奖, delayedLottery: 延迟开奖, cancel: 取消)
* "start_time": "2023-04-26 23:20:10", // 开售时间
* "end_time": "2023-04-27 23:20:10", // 停售时间
* "prize_time": "2023-04-27 23:20:10",
* "result_info": "",
* "sales": "0.0000",
* "jackpot": "0.0000",
* "first_prize_num": 0,
* "first_prize_value": 0,
* "created_at": null,
* "updated_at": null,
* "deleted_at": null,
* "competition_name": "xxxx", // 赛事
* "match_time_date": "2023-04-26", // 比赛开始日期
* "match_time_hour": "23:20开赛", // 比赛开始小时
* "close_time": "2023-04-27 23:00:10",
* "matches": [
* {
* "id": 19,
* "ctzq_jqc_id": 1,
* "no": 0,
* "match_id": 10001, // 比赛id
* "jc_home_team_name": "jc_home_team_name+1", // 主队
* "jc_home_team_name_full": "jc_home_team_name_full+1",
* "jc_away_team_name": "jc_away_team_name+1", // 客队
* "jc_away_team_name_full": "jc_away_team_name_full+1",
* "is_reverse": 1,
* "home_result": "1",
* "away_result": "1",
* "created_at": "2023-05-03 20:19:42",
* "updated_at": "2023-05-03 20:19:42",
* "deleted_at": null,
* "home_odds": [ // 主队
* "0",
* "1",
* "2",
* "3+"
* ],
* "away_odds": [ // 客队
* "0",
* "1",
* "2",
* "3+"
* ]
* }
* ]
* },
* ]
* }
* }
*/
public function selling(Request $request, CtzqJqcService $service)
{
$lotteryTypeId = $request->input('lottery_type_id');
$odds = $service->saleLotteries($this->customer(), $lotteryTypeId);
return $this->jsonSuccess($odds);
}
/**
* @api {POST} /api/customer/ctzq_jqc/valid 4场进球彩-校验信息
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {Int} lottery_type_id 彩票类型ID
* @apiParam {Object} odds 投注信息
*
* @apiParamExample {json} 请求示例
* {
* "lottery_type_id":3, // 彩种类型id
* "issue_num":20007, // 期号
* "odds":{
* "1":{ // odds id
* "home_odds":[1], // 主队得分
* "away_odds":[1] // 客队得分
* },
* ....
* }
* }
*/
public function valid(Request $request, CtzqJqcService $service)
{
$lotteryType = $this->getLotteryType($request);
$lottery = Lottery::active()->where('shop_id', $this->customerShopId())
->where('lottery_type_id', $lotteryType->id)
->first();
ThrowException::isTrue(!$lottery, '不支持该彩种');
$allData = $request->all();
$allData['odds'] = $service->getOddsFromRequestData($allData['odds']);
$service->valid($lottery, $allData);
return $this->jsonSuccess([]);
}
private function getLotteryType(Request $request)
{
$uri = $request->getUri();
preg_match('/(ctzq_[a-z]{3})/i', $uri, $mat);
$type = $mat[0];
$lotteryTypeId = $request->input('lottery_type_id');
$lotteryType = LotteryType::where('type', $type)->find($lotteryTypeId);
ThrowException::isTrue(!$lotteryType, '无此彩种');
return $lotteryType;
}
/**
* @api {POST} /api/customer/ctzq_jqc/compute 4场进球彩-计算金额
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {Int} lottery_type_id 彩票类型ID
* @apiParam {Object} odds 投注信息
* @apiParamExample {json} 请求示例
* {
* "lottery_type_id":4,
* "issue_num":20008,
* "bets_num":5,
* "odds":{
* "5":{ // odds id
* "home_odds":[1,2], // 主队投注
* "away_odds":[1] // 客队投注
* },
* }
* }
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": {
* "money": 111,// 价格
* }
* }
* }
*/
public function compute(Request $request, CtzqJqcService $service)
{
$oddsData = $request->input('odds');
$betsNum = intval($request->input('bets_num'));
if (!$oddsData) {
return $this->jsonFailed('请选择比赛');
}
ThrowException::isTrue($betsNum < 1, '倍数不能小于1');
$lotteryType = $this->getLotteryType($request);
$lott = Lottery::active()->shopAndType($this->customerShopId(), $lotteryType->id)->first();
ThrowException::isTrue(!$lott, '暂不支持该彩种');
$allData = $request->all();
$allData['odds'] = $service->getOddsFromRequestData($allData['odds']);
$service->valid($lott, $allData);
$moneyInfo = $service->computePrizeInfo( $allData['odds'], $betsNum);
return $this->jsonSuccess($moneyInfo);
}
/**
* @api {POST} /api/customer/ctzq_jqc/cart 4场进球彩-购入车信息/计算金额
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {Int} lottery_type_id 彩票类型ID
* @apiParam {Object} odds 投注信息
* @apiParamExample {json} 请求示例
* {
* "lottery_type_id":4,
* "issue_num":20008,
* "bets_num":5,
* "odds":{
* "5":{ // odds id
* "home_odds":[1,2], // 主队投注
* "away_odds":[1] // 客队投注
* },
* }
* }
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": {
* "has_fadan": true,// 是否支持发单
* "has_union": false, // 是否支持合买
* "money": 4, // 总投注金额
* "bets_num": 1, // 投注倍数
* "zhu_num": 1, // 投注数
* "odds": [{
* "id": 1,
* "ret_id": 1,
* "type": "ctzq_jqc",
* "issue_num": "20007",
* "state": "selling",
* "start_time": "2023-04-26 23:20:10",
* "end_time": "2023-04-27 23:20:10",
* "prize_time": "2023-04-27 23:20:10",
* "result_info": "",
* "sales": "0.0000",
* "jackpot": "0.0000",
* "first_prize_num": 0,
* "first_prize_value": 0,
* "created_at": null,
* "updated_at": null,
* "deleted_at": null,
* "competition_name": "xxxx",
* "match_time_date": "2023-04-26",
* "match_time_hour": "23:20开赛",
* "close_time": "2023-04-27 23:00:10",
* "close_time_str": "2023-04-27 23:00:10截止请尽快提交投注", // 截止时间
* "matches": [
* {
* "id": 1,
* "ctzq_jqc_id": 1,
* "no": 1,
* "match_id": 2000,
* "jc_home_team_name": "jc_home_team_name+0", // 主队
* "jc_home_team_name_full": "jc_home_team_name_full+0",
* "jc_away_team_name": "jc_away_team_name+0", // 客队
* "jc_away_team_name_full": "jc_away_team_name_full+0",
* "is_reverse": 0,
* "result": -1,
* "created_at": "2023-04-26T15:26:31.000000Z",
* "updated_at": "2023-04-26T15:26:31.000000Z",
* "deleted_at": null,
* "home_odds_select": [ // 主队投注分数
* {
* "score": "0", // 分数
* "select": false // 未选择
* },
* {
* "score": "1",
* "select": true // 已选择
* },
* {
* "score": "2",
* "select": true
* },
* {
* "score": "3+",
* "select": false
* }
* ],
* "away_odds_select": [ // 客队投注分数
* {
* "score": "0",
* "select": false
* },
* {
* "score": "1",
* "select": true
* },
* {
* "score": "2",
* "select": false
* },
* {
* "score": "3+",
* "select": false
* }
* ]
* },
* ...
* ]
* }]
* }
* }
*/
public function cart(Request $request, CtzqJqcService $service)
{
$oddsData = $request->input('odds');
$betsNum = intval($request->input('bets_num'));
if (!$oddsData) {
return $this->jsonFailed('请选择比赛');
}
ThrowException::isTrue($betsNum < 1, '倍数不能小于1');
$lotteryType = $this->getLotteryType($request);
$lott = Lottery::active()->shopAndType($this->customerShopId(), $lotteryType->id)->first();
ThrowException::isTrue(!$lott, '暂不支持该彩种');
$allData = $request->all();
$allData['odds'] = $service->getOddsFromRequestData($allData['odds']);
$oddsData = $allData['odds'];
$service->valid($lott, $allData);
$moneyInfo = $service->computePrizeInfo($oddsData, $betsNum);
$ctzqJqcMatchId = array_key_first($oddsData);
$ctzqJqcMatch = CtzqJqcMatch::find($ctzqJqcMatchId);
ThrowException::isTrue(!$ctzqJqcMatch, '无对应的比赛数据,无法投注');
$ctzq = CtzqJqc::where('id', $ctzqJqcMatch->ctzq_jqc_id)->where('state', SaleState::Selling)->first();
ThrowException::isTrue(!$ctzq, '已结束投注');
$ctzq->match_time_date = date('Y-m-d', strtotime($ctzq->start_time));
$ctzq->match_time_hour = date('H:i', strtotime($ctzq->start_time)) . '开赛';
$ctzq->close_time = $ctzq->getCloseTime($lott->earlySecond());
$ctzq->close_time_str = sprintf('%s截止请尽快提交投注', $ctzq->close_time);
foreach ($ctzq->matches as $match) {
$homeOdds = [];
$awayOdds = [];
$oddArr = Arr::get($oddsData, sprintf('%d.home_odds', $match->id));
foreach ($match->home_odds as $odd) {
$homeOdds[] = [
'score' => $odd,
'select' => in_array($odd, $oddArr)
];
}
$oddArr = Arr::get($oddsData, sprintf('%d.away_odds', $match->id));
foreach ($match->away_odds as $odd) {
$awayOdds[] = [
'score' => $odd,
'select' => in_array($odd, $oddArr)
];
}
$match->home_odds_select = $homeOdds;
$match->away_odds_select = $awayOdds;
}
$data = [
'title' => '4场进球彩',
'money' => $moneyInfo['money'],
'zhu_num' => $moneyInfo['zhu_num'],
'bets_num' => $betsNum,
'close_time_str' => $ctzq->close_time_str,
'odds' => [$ctzq],
'has_fadan' => false,
'has_union' => $lott->enableHemai(),
'prize_optimize' => $lott->enableOptimizePrize(),
];
return $this->jsonSuccess($data);
}
/**
* @api {POST} /api/customer/ctzq_jqc/order/create 4场进球彩-创建订单/合买
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {Int} lottery_type_id 彩票类型ID
* @apiParam {Object} odds 投注信息
* @apiParamExample {json} 请求示例
* {
* "lottery_type_id":4,
* "issue_num":20008,
* "bets_num":5,
* "odds":{
* "5":{ // odds id
* "home_odds":[1,2], // 主队投注
* "away_odds":[1] // 客队投注
* },
* }
* }
*
* @apiSuccessExample {json} 返回结果
* {
* "code":200,
* "message":"",
* "data":{
* "order_sn":"P2023033000000010", // 订单号
* "balance":"0.00", // 余额
* "money":0, // 订单总金额
* "need_pay_money":0, // 需要支付的金额
* "username":"发达", // 用户名
* "lottery":"jc足球混合投注", // 投注彩票信息
* "pay_types":[ // 支付方式
* {
* "pay_type":"alipay",
* "name":"支付宝"
* }
* ]
* }
* }
*/
public function orderCreate(Request $request, CtzqJqcService $service)
{
$data = $request->all();
$lotteryType = $this->getLotteryType($request);
ThrowException::isTrue(!$lotteryType, '暂不支持该彩种');
/** @var Customer $customer */
$customer = Customer::find($this->customerId());
if (!$this->getPayTypes()) {
ThrowException::isTrue($customer->balanceActive()<=0, '该店铺暂未开启支付');
}
$data['odds'] = $service->getOddsFromRequestData($data['odds']);
$order = $service->createOrder($this->customer(), $data);
$payMoney = $order->getPayMoney();
/** @var Customer $customer */
$customer = Customer::find($this->customerId());
$balance = $customer->balanceActive();
return $this->jsonSuccess([
'order_sn' => $order->order_sn,
'balance' => $balance,
'money' => $payMoney,
'need_pay_money' => Helps::floatFormat($balance >= $payMoney ? 0 : $payMoney - $balance),
'username' => $this->customer()->name,
'lottery' => $order->lotteryName(),
'pay_types' => $this->getPayTypes()
]);
}
}