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

623 lines
22 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\BoolEnum;
use App\Enums\LottType;
use App\Enums\OrderType;
use App\Enums\PayType;
use App\Exceptions\JingCaiException;
use App\Model\Customer\Customer;
use App\Model\Lottery;
use App\Model\LotteryType;
use App\Model\Order;
use App\Model\Zq\JczqOdds;
use App\Service\JczqService;
use App\Service\JingcaiRule;
use App\Utils\Helps;
use App\Utils\Result;
use App\Utils\ThrowException;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
class JczqController extends BaseController
{
/**
* @api {GET} /api/customer/jczq/selling 竞彩足球-投注列表
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {Int} lottery_type_id 彩票类型ID
*
* @apiSuccessExample {json} 返回结果
* {
* "play_types":[ // 玩法
* {
* "name":"混合投注",
* "play_type":"mixed"
* },
* {
* "name":"胜平负/让球",
* "play_type":"spf"
* },
* {
* "name":"全场比分",
* "play_type":"bf"
* },
* {
* "name":"总进球",
* "play_type":"jq"
* },
* {
* "name":"半全场",
* "play_type":"bqc"
* },
* {
* "name":"单场决胜",
* "play_type":"single"
* }
* ],
* "competitions":[ // 赛事
* "全部",
* "adsf离开家"
* ],
* "odds":[
* {
* "id":3,
* "lottery_id":1,
* "odds_id":1,
* "match_id":1,
* "issue_num":"2022-01-01/周一",
* "play_num":21, // 场次
* "jc_competition_name":"adsf离开家", // 竞彩网显示赛事名称(简称)
* "jc_competition_name_full":"发三分大赛",
* "jc_home_team_name":"发顺丰", // 竞彩网显示主队名称(简称)
* "jc_home_team_name_full":"发送",
* "jc_away_team_name":"fda", // 竞彩网显示客队名称(简称)
* "jc_away_team_name_full":"fda",
* "sale_state":"selling", // 竞彩官方销售状态selling: 销售中, stopSelling: 停售)
* "is_reverse":0,
* "real_close_time":null,
* "order_state":"selling",
* "spf_odds":{ // 胜平负赔率
* "win":2.85, // 主胜赔率
* "win_updown":0, // 主胜赔率 上升1下降-1无变化0
* "draw":2.75, // 平局赔率
* "draw_updown":0,
* "loss":2.25, // 客胜赔率
* "loss_updown":-1,
* "single":0, // 竞彩单关标识, 0: 串关, 1: 单关
* "single_updown":0
* },
* "rq_odds":{ // 让球胜平负赔率
* "win":2.85, // 主胜赔率
* "win_updown":1,
* "draw":2.75, // 平局赔率
* "draw_updown":0,
* "loss":2.25, // 客胜赔率
* "loss_updown":0,
* "single":0,
* "single_updown":0,
* "handicap":-1, // 让球数
* "handicap_updown":0
* },
* "bf_odds":{ // 比分赔率
* "dx":100, //平局其它比分赔率
* "dx_updown":0,
* "lx":50, //客胜其它比分赔率
* "lx_updown":0,
* "wx":15, //主胜其它比分赔率
* "wx_updown":0,
* "dh0a0":27, //平局0:0赔率
* "dh0a0_updown":0,
* "dh1a1":8.5,
* "dh1a1_updown":0,
* "dh2a2":11,
* "dh2a2_updown":0,
* "dh3a3":30,
* "dh3a3_updown":0,
* "lh0a1":20, //客胜0:1赔率
* "lh0a1_updown":0,
* "lh0a2":27,
* "lh0a2_updown":0,
* "lh0a3":50,
* "lh0a3_updown":0,
* "lh0a4":120,
* "lh0a4_updown":0,
* "lh0a5":400,
* "lh0a5_updown":0,
* "lh1a2":11,
* "lh1a2_updown":0,
* "lh1a3":27,
* "lh1a3_updown":0,
* "lh1a4":70,
* "lh1a4_updown":0,
* "lh1a5":200,
* "lh1a5_updown":0,
* "lh2a3":21,
* "lh2a3_updown":0,
* "lh2a4":60,
* "lh2a4_updown":0,
* "lh2a5":200,
* "lh2a5_updown":0,
* "wh1a0":11, //主胜1:0赔率
* "wh1a0_updown":0,
* "wh2a0":10.5,
* "wh2a0_updown":0,
* "wh2a1":7.25,
* "wh2a1_updown":0,
* "wh3a0":13,
* "wh3a0_updown":0,
* "wh3a1":10.5,
* "wh3a1_updown":0,
* "wh3a2":13,
* "wh3a2_updown":0,
* "wh4a0":22,
* "wh4a0_updown":0,
* "wh4a1":18,
* "wh4a1_updown":0,
* "wh4a2":25,
* "wh4a2_updown":0,
* "wh5a0":39,
* "wh5a0_updown":0,
* "wh5a1":34,
* "wh5a1_updown":0,
* "wh5a2":50,
* "wh5a2_updown":0,
* "single":1,
* "single_updown":0
* },
* "jq_odds":{ //总进球数赔率
* "g0":14, //进0球赔率
* "g0_updown":0,
* "g1":5.2,
* "g1_updown":0,
* "g2":3.5,
* "g2_updown":0,
* "g3":3.2,
* "g3_updown":0,
* "g4":4.6,
* "g4_updown":0,
* "g5":8,
* "g5_updown":0,
* "g6":15,
* "g6_updown":0,
* "g7m":23, //进7+球赔率
* "g7m_updown":0,
* "single":1,
* "single_updown":0
* },
* "bqc_odds":{ //半全场胜平负赔率
* "dd":4.15, //平平赔率
* "dd_updown":0,
* "dl":5.65, //平负赔率
* "dl_updown":0,
* "dw":5.2, //平胜赔率
* "dw_updown":0,
* "ld":15, //负平赔率
* "ld_updown":0,
* "ll":4.15, //负负赔率
* "ll_updown":0,
* "lw":24, //负胜赔率
* "lw_updown":0,
* "wd":15, //胜平赔率
* "wd_updown":0,
* "wl":26, //胜负赔率
* "wl_updown":0,
* "ww":4, //胜胜赔率
* "ww_updown":0,
* "single":1,
* "single_updown":0
* },
* "real_close_str":"00:00截止", //截止时间
* "issue_num_week":"周一",
* "issue_num_day":"2022-01-01"
* }
* ]
*}
*/
public function selling(Request $request, JczqService $jczqService)
{
$lotteryTypeId = $request->input('lottery_type_id');
$odds = $jczqService->saleLotteries($this->customer(), $lotteryTypeId);
return $this->jsonSuccess($odds);
}
/**
* @api {POST} /api/customer/jczq/valid 竞彩足球-验证组合方案是否可用
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {Int} lottery_type_id 彩种类型id
* @apiParam {String} play_type 玩法
* @apiParam {Object} odds 组合投注信息
* @apiParamExample {json} 请求示例
* {
* "lottery_type_id": 1, // 彩种类型id
* "play_type": "bf_odds", // 玩法
* "odds": {
* 1: { // 彩票赔率数据id
* "spf_odds": { // 玩法spf,bf,bqc胜平负比分半全场
* "win": 2.3 // 投注的值
* },
* "bf_odds": {
* "win": 2.3
* },
* ...
* },
* ...
* }
* }
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 410,
* "message": "不允许组合方案", // 验证结果信息只要code不是200就是未通过验证
* "data": null
* }
*/
public function valid(Request $request, JczqService $jczqService)
{
$lotteryTypeId = $request->input('lottery_type_id');
$playType = $request->input('play_type');
$data = $request->input('odds');
$lotteryType = LotteryType::find($lotteryTypeId);
if (!$lotteryType) {
return $this->jsonFailed('非法数据');
}
$lottery = Lottery::active()->where('shop_id', $this->customerShopId())
->where('lottery_type_id', $lotteryTypeId)
->first();
throw_if(!$lottery, JingCaiException::create('无允许的组合方案', Result::FAILED));
$jczqService->valid($lottery, $playType, $data);
return $this->jsonSuccess([]);
}
/**
* @api {POST} /api/customer/jczq/compute 竞彩足球-计算奖金和投注金额
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {Int} lottery_type_id 彩种类型id
* @apiParam {Int} bets_num 倍数
* @apiParam {Array} pass_mode_keys m串n的key
* @apiParam {Object} odds 组合投注信息
* @apiParam {String} play_type 玩法
* @apiParamExample {json} 请求示例
* {
* "lottery_type_id": 1, // 彩种类型id
* "playgames": {
* 1: { // 彩票赔率数据id
* "spf_odds": { // 玩法spf,bf,bqc胜平负比分半全场
* "win": 2.3 // 投注的值
* },
* "bf_odds": {
* "win": 2.3
* },
* ...
* },
* ...
* }
* }
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": {
* "money": 24, // 投注金额
* "prize_min": 200, // 最低中奖金额
* "prize_max": 400 // 最高中奖金额
* }
* }
*/
public function compute(Request $request, JczqService $jczqService)
{
$odds = $request->input('odds');
$betsNum = $request->input('bets_num');
$passModeKeys = $request->input('pass_mode_keys'); // 例如['2.1']
$playType = $request->input('play_type');
if (!$odds) {
return $this->jsonFailed('请选择比赛');
}
$odds = $jczqService->refreshOdds($odds);
$moneyInfo = $jczqService->computePrizeInfo($odds, $betsNum, $passModeKeys);
return $this->jsonSuccess($moneyInfo);
}
/**
* @api {POST} /api/customer/jczq/cart 竞彩足球-购入车信息
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {Int} lottery_type_id 彩种类型id
* @apiParam {Int} bets_num 倍数
* @apiParam {String} play_type 玩法
* @apiParam {Object} odds 组合投注信息
* @apiParamExample {json} 请求示例
* {
* "lottery_type_id": 1, // 彩种类型id
* "playgames": {
* 1: { // 彩票赔率数据id
* "spf_odds": { // 玩法spf,bf,bqc胜平负比分半全场
* "win": 2.3 // 投注的值
* },
* "bf_odds": {
* "win": 2.3
* },
* ...
* },
* ...
* }
* }
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": {
* "has_fadan": true,// 是否支持发单
* "has_union": false, // 是否支持合买
* "money": 12, // 投注金额
* "prize_min": 100, // 最低奖金
* "prize_max": 200, // 最高奖金
* "close_time_str": "3214:323 32:32:32截止请尽快提交投注", // 提示信息
* "pass_mode":
* {
* "multi": [], // 多选
* "single": [ // 单选
* {
* "key": "2.1",
* "title": "2串1"
* }
* ]
* }
* ,
* "odds": [ // 购买界面所需信息,字段参考【竞彩足球-投注列表】
* {
* "id": 3,
* "lottery_type_id": 1,
* "odds_id": 1,
* "match_id": 1,
* "issue_num": "2022-01-01/周一",
* "play_num": 21,
* "jc_competition_name": "adsf离开家",
* "jc_competition_name_full": "发三分大赛",
* "jc_home_team_name": "发顺丰",
* "jc_home_team_name_full": "发送",
* "jc_away_team_name": "fda",
* "jc_away_team_name_full": "fda",
* "sale_state": "selling",
* "is_reverse": 0,
* "real_close_time": "1969-12-31 23:50:00",
* "order_state": "selling",
* "spf_odds": {
* "win": 2.85,
* "loss": 2.25
* },
* "rq_odds": {
* "win": 2.85
* },
* "created_at": null,
* "updated_at": null,
* "deleted_at": null,
* "real_close_str": "23:50截止",
* "issue_num_week": "周一",
* "issue_num_day": "2022-01-01"
* },
* ...
* ]
* }
* }
*/
public function cart(Request $request,
JczqService $jczqService)
{
$typeId = $request->input('lottery_type_id');
$oddsData = $request->input('odds');
$betsNum = $request->input('bets_num');
$playType = $request->input('play_type');
if (!$oddsData) {
return $this->jsonFailed('请选择比赛');
}
$lott = Lottery::active()->shopAndType($this->customerShopId(), $typeId)->first();
ThrowException::isTrue(!$lott, '暂不支持该彩种');
$jczqService->valid($lott, $playType, $oddsData);
$oddsNum = count($oddsData);
$passMode = JczqService::generateZqPassModeInfo($oddsData, $playType);
if ($lott->mn == BoolEnum::NO) {
$passMode['single'] = [];
}
$oddsData = $jczqService->refreshOdds($oddsData);
$moneyInfo = $jczqService->computePrizeInfo($oddsData, $betsNum, [$oddsNum . '.1']);
$resultMatch = $jczqService->getJczqOddsFromData($lott, $oddsData);
$earlyCloseTime = null;
foreach ($resultMatch as $match) {
if (!isset($earlyCloseTime)) {
$earlyCloseTime = $match->real_close_time;
}
if ($earlyCloseTime > $match->real_close_time) {
$earlyCloseTime = $match->real_close_time;
}
}
$data = [
'title' => '竞彩足球',
'money' => $moneyInfo['money'],
'prize_min' => $moneyInfo['prize_min'],
'prize_max' => $moneyInfo['prize_max'],
'close_time_str' => sprintf('%s截止请尽快提交投注', $earlyCloseTime),
'pass_mode' => $passMode,
'odds' => $resultMatch,
'has_fadan' => true,
'has_union' => $lott->enableHemai(),
'prize_optimize' => $lott->enableOptimizePrize(),
];
return $this->jsonSuccess($data);
}
/**
* @api {POST} /api/customer/jczq/order/create 竞彩足球-创建订单/发单/合买
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {Int} lottery_type_id 彩种类型id
* @apiParam {Int} bets_num 倍数
* @apiParam {String} play_type 玩法
* @apiParam {String} pass_mode_keys 过关方式
* @apiParam {Int} type 1普通投注2发单;3合买4跟单
* @apiParam {String} [fadan_order_no] 跟单的订单no(只有在type=4的时候生效)
* @apiParam {Int} [type_mode] 1截止后公开2公开;3认购可见
* @apiParam {String} [type_desc] 发单/合买描述,发单/合买时必填
* @apiParam {Int} [union_piece_total] 总份数,合买时必填
* @apiParam {Int} [union_piece_buy] 购买份数,合买时必填
* @apiParam {Int} [union_piece_keep] 保底份数,合买时必填
* @apiParam {Int} [union_keep] 是否全额保底:1是,0否合买时必填
* @apiParam {Int} [union_brokerage] 佣金,合买时必填
* @apiParam {Object} odds 组合投注信息
* @apiParamExample {json} 请求示例
* {
* "lottery_type_id":1, // 彩种
* "bets_num":2, // 倍数
* "play_type":"all", // 玩法
* "pass_mode_keys":["2.1"], // 过关方式
* "odds":{ // 赔率/组合投注信息
* "1":{ // 彩票赔率数据id
* "spf_odds":{
* "win":2.3,
* "loss":3.1
* },
* "rq_odds":{
* "win":1.2
* }
* },
* ...
* }
* }
*
* @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, JczqService $service)
{
$data = $request->all();
$lotteryTypeId = Arr::get($data, 'lottery_type_id');
$lotteryType = LotteryType::find($lotteryTypeId);
ThrowException::isTrue(!$lotteryType, '暂不支持该彩种');
/** @var Customer $customer */
$customer = Customer::find($this->customerId());
if (!$this->getPayTypes()) {
ThrowException::isTrue($customer->balanceActive()<=0, '该店铺暂未开启支付');
}
$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()
]);
}
/**
* @api {POST} /api/customer/jczq/order/optimize_prize 竞彩足球-奖金优化
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {Int} optimize_type 1平均优化2搏热3搏冷
* @apiParam {Int} money 购买钱数
* @apiParam {Array} pass_mode_keys 过关方式
* @apiParam {Object} odds 组合投注信息
* @apiParamExample {json} 请求示例
* {
* "bets_num":2, // 倍数
* "pass_mode_keys":["2.1"], // 过关方式
* "odds":{ // 赔率/组合投注信息
* "1":{ // 彩票赔率数据id
* "spf_odds":{
* "win":2.3,
* "loss":3.1
* },
* "rq_odds":{
* "win":1.2
* }
* },
* ...
* }
* }
*
* @apiSuccessExample {json} 返回结果
* {
* }
*/
public function optimizePrize(Request $request, JczqService $service)
{
$optimizeType = $request->input('optimize_type');
$oddsData = $request->input('odds');
$money = $request->input('money');
$passModeKeys = $request->input('pass_mode_keys');
if (!$oddsData) {
return $this->jsonFailed('请选择比赛');
}
$lottType = LotteryType::where('type', LottType::JCZQ)->first();
$lott = Lottery::where('shop_id', $this->customerShopId())
->where('lottery_type_id', $lottType->id)
->first();
$lott->validEnableOptimizePrize();
ThrowException::isTrue(!$passModeKeys, '请选择串关方式');
$oddsData = $service->refreshOdds($oddsData);
$ids = array_keys($oddsData);
$odds = JczqOdds::whereIn('id', $ids)->get();
$zuhe = $service->generateBetsInfo($oddsData, $passModeKeys, 1);
$optimizeZuhe = $service->optimizePrize($zuhe, $money, $optimizeType);
return $this->jsonSuccess([
'zuhe' => $optimizeZuhe,
'odds_data' => $odds,
]);
}
}