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

469 lines
18 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\Http\Controllers\Api\Customer;
use App\Enums\LottType;
use App\Enums\SaleState;
use App\Model\Customer\Customer;
use App\Model\Lottery;
use App\Model\LotteryType;
use App\Model\Zq\CtzqSfc;
use App\Model\Zq\CtzqSfcMatch;
use App\Service\CtzqSfc14Service;
use App\Utils\Helps;
use App\Utils\ThrowException;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
class CtzqSfc14Controller extends BaseController
{
/**
* @api {GET} /api/customer/ctzq_sfc14/selling sfc14-投注列表
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {Int} lottery_type_id 彩票类型ID
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": {
* "play_types": [],
* "odds": [
* {
* "id": 4,
* "sfc_id": 471,
* "issue_num": "23081",
* "state": "selling",
* "start_time": "2023-06-28 20:00:00",
* "end_time": "2023-07-01 16:30:00",
* "prize_time": "2023-07-02 10:00:00",
* "result_info": "",
* "sale_frtn": "0.00",
* "jackpot_frtn": "0.00",
* "first_prize_num_frtn": "0.00",
* "first_prize_val_frtn": "0.00",
* "second_prize_num_frtn": "0.00",
* "second_prize_val_frtn": "0.00",
* "sale_nine": "0.00",
* "jackpot_nine": "0.00",
* "first_prize_num_nine": "0.00",
* "first_prize_val_nine": "0.00",
* "created_at": "2023-06-29 11:04:49",
* "updated_at": "2023-06-29 11:04:49",
* "deleted_at": null,
* "close_time": "2023-07-01 16:10:00",
* "matches": [
* {
* "id": 43,
* "ctzq_sfc_id": 4,
* "no": 0,
* "match_id": 432288,
* "jc_home_team_name": "新泻天鹅",
* "jc_home_team_name_full": "新泻天鹅",
* "jc_away_team_name": "广岛三箭",
* "jc_away_team_name_full": "广岛三箭",
* "is_reverse": 0,
* "result": "",
* "created_at": "2023-06-29 11:04:49",
* "updated_at": "2023-06-29 11:04:49",
* "deleted_at": null,
* "match_time_date": "2023-07-01",
* "match_time_hour": "17:00开赛",
* odds": [
* "3", // 胜
* "1", // 平
* "0" // 负
* ],
* "match": {
* "id": 11174,
* "match_id": 432288,
* "competition_id": 49,
* "competition_name": "J联赛",
* "season": "2023",
* "stage": "第19轮",
* "group_id": 0,
* "group_name": "",
* "start_time": "2023-07-01 17:00:00",
* "home_team_id": 1084,
* "home_team_name": "新泻天鹅",
* "away_team_id": "718",
* "away_team_name": "广岛三箭",
* "status": 0,
* "half_time_score": "",
* "full_time_score": "",
* "extra_time_score": "",
* "penal_score": "",
* "final_score": "",
* "win_team_id": 0,
* "man_id": "",
* "created_at": "2023-06-29 11:27:28",
* "updated_at": "2023-06-29 11:27:28",
* "deleted_at": null,
* "start_week": "周六",
* "start_time_str": "07-01 17:00",
* "competition_logo_url": "http://dt.aistat.cn/competitions/49.png",
* "home_team_logo_url": "http://dt.aistat.cn/teams/1084.png",
* "away_team_logo_url": "http://dt.aistat.cn/teams/718.png"
* }
* },
* ]
* },
* ....
* ]
* }
* }
*/
public function selling(Request $request, CtzqSfc14Service $service)
{
$lotteryTypeId = $request->input('lottery_type_id');
$odds = $service->saleLotteries($this->customer(), $lotteryTypeId);
return $this->jsonSuccess($odds);
}
/**
* @api {POST} /api/customer/ctzq_sfc14/valid sfc14-校验信息
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {Int} lottery_type_id 彩票类型ID
* @apiParam {Object} odds 投注信息
*
* @apiParamExample {json} 请求示例
* {
* "lottery_type_id":11,
* "bets_num":1,
* "odds":{
* "43":{"3":1,"1":1},
* "44":{"3":1},
* "45":{"3":1},
* "46":{"3":1},
* "47":{"3":1},
* "48":{"3":1},
* "49":{"3":1},
* "50":{"3":1},
* "51":{"3":1},
* "52":{"3":1},
* "53":{"3":1},
* "54":{"3":1},
* "55":{"1":1},
* "56":{"0":1}
* }
* }
*/
public function valid(Request $request, CtzqSfc14Service $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)
{
$lotteryTypeId = $request->input('lottery_type_id');
$lotteryType = LotteryType::where('type', LottType::CTZQ_SFC14)->find($lotteryTypeId);
ThrowException::isTrue(!$lotteryType, '无此彩种');
return $lotteryType;
}
/**
* @api {POST} /api/customer/ctzq_sfc14/compute sfc14-计算金额
* @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, CtzqSfc14Service $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_sfc14/cart sfc14-购入车信息/计算金额
* @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, CtzqSfc14Service $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);
$ctzqSfcMatchId = array_key_first($oddsData);
$ctzqJqcMatch = CtzqSfcMatch::find($ctzqSfcMatchId);
ThrowException::isTrue(!$ctzqJqcMatch, '无对应的比赛数据,无法投注');
$ctzq = CtzqSfc::where('id', $ctzqJqcMatch->ctzq_sfc_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);
$matchList = $ctzq->matches()->whereIn('id', array_keys($oddsData))->get();
foreach ($matchList as $match) {
$odds = [];
$oddArr = Arr::get($oddsData, $match->id);
foreach ($match->odds as $odd) {
$odds[] = [
'score' => $odd,
'select' => in_array($odd, $oddArr)
];
}
$match->odds_select = $odds;
}
unset($ctzq->matchs);
$ctzq->matches = $matchList;
$data = [
'title' => '足彩胜负',
'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_sfc14/order/create sfc14-创建订单/合买
* @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, CtzqSfc14Service $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()
]);
}
}