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

1069 lines
40 KiB
PHP
Executable File
Raw Permalink 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\LottState;
use App\Enums\OrderType;
use App\Enums\PayState;
use App\Enums\PayType;
use App\Exceptions\JingCaiException;
use App\Model\Config;
use App\Model\Customer\Customer;
use App\Model\Customer\CustomerFollow;
use App\Model\Customer\CustomerRanking;
use App\Model\Lottery;
use App\Model\Order;
use App\Service\LotteryService;
use App\Utils\Helps;
use App\Utils\ThrowException;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
class OrderController extends BaseController
{
const FILTER_HIT_RATE = 'hit_rate'; // 命中率
const FILTER_HOT = 'hot'; // 跟单人气
const FILTER_MONEY = 'money'; // 自购金额
const FILTER_FOLLOW = 'follow'; // 我的关注
/**
* @api {GET} /api/customer/order/da_shen_list 跟单-一站成名等
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": {
* "lian_hong": [ // 连红榜
* {
* "id": 1,
* "name": "我是大神1",
* "nickname": "我是大神1",
* "avatar": "",
* "red_num": 1
* }
* ],
* "cheng_ming": [ // 一站成名
* {
* "id": 1,
* "name": "我是大神1",
* "nickname": "我是大神1",
* "avatar": "",
* "red_num": 1
* }
* ],
* "sheng_lv": [ // 胜率榜
* {
* "id": 1,
* "name": "我是大神1",
* "nickname": "我是大神1",
* "avatar": "",
* "red_num": 1
* }
* ]
* }
* }
*/
public function daShenList()
{
ThrowException::isTrue(!Helps::genDanEnable(), '系统维护中...');
$cdate = date('Ymd');
$fames = CustomerRanking::with('customer:id,name,nickname,avatar,remark')
->where('cdate', $cdate)
->where('rank_fame', 1)
->orderBy('last_prize', 'desc')
->limit(4)
->get();
$hitRate = CustomerRanking::with('customer:id,name,nickname,avatar,remark')
->where('cdate', $cdate)
->where('rank_hit_rate', 1)
->orderBy('wined_max7', 'desc')
->limit(4)
->get();
$prifit = CustomerRanking::with('customer:id,name,nickname,avatar,remark')
->where('cdate', $cdate)
->where('rank_profit', 1)
->orderBy('last_prize', 'desc')
->limit(4)
->get();
$data = [
'lian_hong' => $hitRate,
'cheng_ming' => $fames,
'sheng_lv' => $prifit,
];
$this->setGendanNum($data);
return $this->jsonSuccess($data);
}
protected function setGendanNum(&$data) {
$values = array_values($data);
$customerRankings = collect();
foreach ($values as $item) {
$customerRankings = $customerRankings->merge($item);
}
$customerIds = $customerRankings->pluck('customer_id')->toArray();
$allowGendanNum = [];
if ($customerIds) {
$allowGendanNum = Order::select([
'customer_id',
DB::raw('count(*) as c')
])
->where('lottery_state', LottState::WAIT)
->where('type', OrderType::FADAN)
->whereIn('customer_id', $customerIds)
->where('pay_state', PayState::SUCCESS)
->where('odds_early_close_time', '>', date('Y-m-d H:i:s'))
->groupBy('customer_id')
->pluck('c', 'customer_id')
->toArray();
}
foreach ($data as $k => &$items) {
foreach ($items as &$item) {
$item->gendan_num = Arr::get($allowGendanNum, $item->customer_id, 0);
}
}
}
/**
* @api {GET} /api/customer/order/ranking_list 排名详情
*/
public function rankingList(Request $request)
{
$rankTypes = [
'rank_fame' => 'last_prize',
'rank_hit_rate' => 'wined_max7',
'rank_profit' => 'profit_lv7'
];
// rank_type: rank_fame, rank_hit_rate,rank_profit
$rankType = $request->input('rank_type');
$sortField = Arr::get($rankTypes, $rankType);
if (!$rankType || !$sortField) {
return $this->jsonSuccess([]);
}
$list = CustomerRanking::with('customer:id,name,nickname,avatar,remark')
->where('cdate', date('Ymd'))
->where($rankType, 1)
->orderBy($sortField, 'desc')
->orderBy('wined_last7', 'desc')
->limit(10)
->get();
$customerIdList = [];
foreach ($list as $item) {
$customerIdList[] = $item->customer_id;
}
$followed = CustomerFollow::whereIn('customer_id', $customerIdList)
->where('follower_id', $this->customerId())
->pluck('follower_id', 'customer_id')
->toArray();
foreach ($list as $item) {
$item->followed = Arr::get($followed, $item->customer_id) ? true : false;
}
return $this->jsonSuccess([
'data' => $list,
'total' => 0
]);
}
// {GET} /api/customer/order/fadan_customers 跟单-大厅列表
public function fadanCustomers(Request $request) {
$nickname = $request->input('nickname');
if (!$nickname) {
return $this->jsonSuccess();
}
$customers = Order::leftJoin('customer', 'order.customer_id', 'customer.id')
->select([
'customer.id',
'customer.name',
'customer.nickname',
'customer.avatar',
'customer.fans_num',
'customer.fadan_num',
'customer.follower_num',
DB::raw('count(*) as c')
])
->where('order.type', OrderType::FADAN)
// ->where('customer.follower_num', '>', 0)
->whereLike('customer.nickname', $nickname)
->groupBy('customer.id')
->paging();
$customerIdList = [];
foreach ($customers as $item) {
$customerIdList[] = $item->id;
}
$rankings = CustomerRanking::where('customer_id', $customerIdList)
->where('cdate', date('Ymd'))
->get()
->keyBy('customer_id');
foreach ($customers as $item) {
$item->ranking = Arr::get($rankings, $item->id);
}
return $this->jsonSuccess($customers);
}
/**
* @api {POST} /api/customer/order/copies 跟单-大厅列表
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {String} filter hit_rate:命中率;hot: 跟单人气;money: 自购金额;follow: 我的关注
* @apiParam {String} [nickname] 彩民昵称
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": {
* "current_page": 1,
* "data": [
* {
* "nickname": "", // 用户昵称
* "name": "发达", // 用户名称
* "win_alway_num": 0, // 连胜次数
* "avatar": "", // 用户头像
* "id": 1,
* "customer_id": 1,
* "lottery_id": 1,
* "money": 24, // 自购金额
* "pid": 0,
* "odds_early_close_time": "2023-04-05 15:48:38", // 结束时间
* "play_type": "mixed",
* "pass_mode": [ // 玩法
* "2.1"
* ],
* "copy_num": 0,
* "play_type_name": "混合投注", // 玩法名称
* "buy_type_name": "自购", // 购买类型
* "pass_mode_name": [
* "2串1"
* ],
* "lottery": {
* "id": 1,
* "name": "竞彩足球", // 彩种名称
* "description": "出票,最低投注0元,停售前0分钟截止投注"
* }
* },
* ],
* "from": 1,
* "per_page": 20,
* "to": 2,
* "total": 2
* }
* }
*/
public function copies(Request $request)
{
ThrowException::isTrue(!Helps::genDanEnable(), '系统维护中...');
$size = $request->input('size');
$filter = $request->input('filter');
$nickname = $request->input('nickname');
$customerIds = [];
if ($nickname) {
$customerIds = Customer::whereLike('nickname', $nickname)->pluck('id')->toArray();
if (!$customerIds) {
return $this->jsonSuccess();
}
}
$query = Order::with([
'lottery:id,name',
])
->leftJoin('customer', 'order.customer_id', '=', 'customer.id')
->select([
'customer.nickname', 'customer.name', 'customer.win_alway_num', 'customer.avatar',
'order.id', 'order.order_sn', 'order.customer_id', 'order.lottery_id', 'order.money', 'order.pid', 'order.odds_early_close_time', 'order.play_type', 'order.pass_mode', 'order.gendan_num', 'order.type'
])
->where('type', OrderType::FADAN)
->where('order.pay_state', PayState::SUCCESS)
->where('odds_early_close_time', '>', date('Y-m-d H:i:s'))
// ->whereIn('order.lottery_state', [LottState::PENDING, LottState::WAIT, LottState::DRAFT]);
->where('order.lottery_state', LottState::WAIT);
if ($nickname && $customerIds) {
$query->whereIn('customer_id', $customerIds);
}
// TODO:: 我的关注
if ($filter == self::FILTER_FOLLOW) {
$follows = CustomerFollow::where('follower_id', $this->customerId())->get()->toArray();
if (!$follows) {
return $this->jsonSuccess([
'data' => [],
'total' => 0,
'per_page' => 20
]);
}
$query->whereIn('customer_id', array_column($follows, 'customer_id'));
}
if ($filter == self::FILTER_HIT_RATE) {
$query->orderBy('customer.win_alway_num', 'desc')
->orderBy('customer.win_hit_rate', 'desc');
} else if ($filter == self::FILTER_HOT) {
$query->orderBy('order.gendan_num', 'desc');
} else if ($filter == self::FILTER_MONEY) {
$query->orderBy('order.money', 'desc');
}
$orders = $query->paging($size);
foreach ($orders as $item) {
$item->avatar = Helps::resizeImage($item->avatar);
}
return $this->jsonSuccess($orders);
}
/**
* @api {POST} /api/customer/order/copy 跟单创建
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {String} order_sn 订单编号
* @apiParam {Float} money 金额
* @apiParam {Int} bets_num 倍数
* @apiParamExample {json} 请求示例
* {
* "id":1, // 订单id
* "money":2, // 金额
* "bets_num":2,
* }
*
* @apiSuccessExample {json} 返回结果
* {
* "code":200,
* "message":"",
* "data":{
* "order_sn":"P2023033000000010", // 订单号
* "balance":"0.00", // 余额
* "money":0, // 订单金额
* "username":"发达", // 用户名
* "lottery":"jc足球混合投注", // 投注彩票信息
* "pay_types":[ // 支付方式
* {
* "pay_type":"alipay",
* "name":"支付宝"
* }
* ]
* }
* }
*/
public function copy(Request $request)
{
$orderSn = $request->input('order_sn');
throw_if(!$orderSn, JingCaiException::create('id不能为空!'));
$betsNum = $request->input('bets_num');
throw_if($betsNum < 1 || !is_integer($betsNum), JingCaiException::create('投注倍数有误'));
/** @var Order $od */
$od = Order::sn($orderSn)->first();
ThrowException::isTrue(!$od, '所跟订单不存在');
ThrowException::isTrue($od->type != OrderType::FADAN, '请在跟单大厅中跟单');
// ThrowException::isTrue(!$od->canCopy(), '已无法跟单,请选择大厅中的其他跟单');
ThrowException::isTrue($od->customer_id == $this->customerId(), '无法跟单自己的发单');
ThrowException::isTrue($od->lottery_state != LottState::WAIT, '订单状态有误');
ThrowException::isTrue(date('Y-m-d H:i:s') > $od->odds_early_close_time, '所跟订单已结束投注');
/** @var Order $gendan */
$gendan = Order::where('pid', $od->id)->where('customer_id', $this->customerId())->first();
if ($gendan) {
// if ($gendan->pay_state == PayState::SUCCESS) {
// if (in_array($gendan->lottery_state, $gendan->gendanLotteryState())) {
// ThrowException::run('无法重复跟单');
// }
// }
if ($gendan->pay_state == PayState::UNPAID) {
$gendan->where('id', $gendan->id)->where('pay_state', PayState::SUCCESS)->delete();
}
}
$service = LotteryService::getJingcaiServiceByOrder($od);
$order = $service->copyOrder($this->customer(), $od, $request->all());
ThrowException::isTrue(!$order, '不支持跟单');
$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 {GET} /api/customer/order/show 订单详情(普通投注和发单)
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {String} order_sn 订单编号
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": {
* "lottery_type": {
* "id": 1,
* "type": "jczq", //jczq:竞彩足球jclq:竞彩篮球ctzq_jqc4:场进球彩ctzq_bqc6:场半全场(根据此字段判断订单详情中的投注信息)
* "name": "jc足球"
* }
* "customer":{ // 投注人
* "id":2,
* "name":"",
* "nickname":"", // 投注人名
* "level_name":"储备组长",
* "level_score_group":"5000",
* "lottery_state_name":"",
* "client_type_name":"未知"
* },
* "id": 2,
* "pid": 0,
* "lottery_is_wined": true, // true中奖false未中奖
* "order_sn": "P2023040900000002",
* "customer_id": 1,
* "lottery_id": 1,
* "shop_id": 0,
* "lottery_type_id": 0,
* "type": 1,
* "copy_num": 0, // 跟单数
* "bets_num": 2, // 倍数
* "piao_num": 0, //
* "money": 24, // 购买金额
* "prize_min": "11.04", // 最低奖金
* "prize_max": "142.56", // 最高奖金
* "pass_mode": [
* "2.1"
* ],
* "play_type": "mixed",
* "pay_type": "",
* "pay_state": "success",
* "pay_at": "2023-04-09 03:41:06",
* "odds_early_close_time": "2023-04-05 15:38:38",
* "odds_late_close_time": "2023-04-06 15:38:38",
* "lottery_state": 5,
* "lottery_prize": "0.00", // 中奖金额
* "lottery_tax_prize": "0.00", // 中奖金额
* "type_mode": 1, 1截止后公开2公开;3认购可见
* "type_desc": "浪费大家发的啦",
* "created_at": "2023-04-09T03:32:51.000000Z",
* "updated_at": "2023-04-09T03:41:06.000000Z",
* "deleted_at": null,
* "sellings": [ // 4场进球彩/6场半全场
* {
* "no": "01", // 序号
* "team_name": "jc_home_team_name+1", // 球队名
* "odds": [ // 购买的比分
* 1,
* 2
* ]
* },
* ],
* "sellings": [ // 竞彩篮球,竞彩足球
* {
* "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",
* "id": 3,
* "order_id": 2,
* "jczq_odds_id": 3,
* "odds_id": 1,
* "match_id": 1,
* "odds_result": 0,
* "issue_num_week": "周一",
* "issue_num_day": "2022-01-01",
* "vs_b": "发顺丰",
* "vs_m": "vs",
* "vs_a": "fda",
* "half_time_score": "fda", // 足球:半场比分
* "full_time_score": "fda", // 足球:全场比分
* "home_score": "fda", // 篮球: 主队得分
* "away_score": "fda", // 篮球: 客队得分
* "play_items": [ // 每种玩法和对应的投注项
* {
* "win_result": "",
* "play_name": "胜平负", // 玩法
* "play_items": [
* {
* name: "胜", // 投注项
* wined: true, // 是否中
* }
* ]
* }
* ]
* },
*
* ],
* "chang_num": 2,
* "pass_mode_names": [ // 串关
* "2x1"
* ],
* "play_name": "竞彩足球", // 彩种
* "play_type_name": "混合投注", // 玩法
* "buy_type_name": "自购",
* "pass_mode_name": [
* "2串1"
* ],
* "materials": [],
* "gendan_info": { // 跟单信息
* "brokerage":1, // 跟单佣金
* "count_gendan":1, // 跟单人数
* "count_default_show":10, // 默认人数
* "list":[
* {
* "id":16,
* "pid":13,
* "customer_id":2,
* "money":20, // 跟单金额
* "lottery_state":5,
* "lottery_tax_prize":"0.00", // 税后奖金
* "play_type_name":"",
* "buy_type_name":"",
* "pass_mode_name":[
*
* ],
* "lottery_estimate_send_prize":"0.00",
* "gendan_brokerage":"0.00",
* "customer":{ // 投注人
* "id":2,
* "name":"",
* "nickname":"", // 投注人名
* "level_name":"储备组长",
* "level_score_group":"5000",
* "lottery_state_name":"",
* "client_type_name":"未知"
* },
* "p_order":{ // 发单
* "id":13,
* "order_sn":"P2023050500000004",
* "customer_id":1,
* "play_type_name":"",
* "buy_type_name":"",
* "pass_mode_name":[
*
* ],
* "lottery_estimate_send_prize":null,
* "gendan_brokerage":null,
* "customer":{ // 发单人
* "id":1,
* "nickname":"",
* "name":"发达",
* "level_name":"储备组长",
* "level_score_group":"5000",
* "lottery_state_name":"",
* "client_type_name":"未知"
* }
* }
* }
* ]
* }
* }
* }
*
*/
public function show(Request $request)
{
$orderSn = $request->input('order_sn');
/** @var Order $order */
$order = Order::with([
'materials:id,order_id,path', 'receiver:id,name',
'lotteryType:id,type,name',
'customer:id,nickname,name,avatar'
])->sn($orderSn)->first();
ThrowException::isTrue(!$order, '订单不存在');
$order->withShuziCai();
if ($order->type == OrderType::UNION) {
if ($order->id != $order->pid) {
$order = Order::with([
'materials:id,path,order_id', 'receiver:id,name',
'lotteryType:id,type,name',
'customer:id,nickname,name,avatar'
])->where('id', $order->pid)->first();
}
}
$order->p_order = null;
if ($order->pid > 0) {
$pOrder = Order::with('customer:id,nickname,name')
->select('id', 'customer_id')
->find($order->pid);
$order->p_order = $pOrder;
$order->p_order->fadan_lv = Config::fadanAllFeeLv() * 100 . '%';
}
$jingcaiService = LotteryService::getJingcaiServiceByOrder($order);
$order->gendan_info = $order->getGendanInfo();
$order->union_info = $order->getUnionInfo();
$orderData = $jingcaiService->showOrder($this->customer(), $order);
$closeTime = $order->getMatchStartTime();
$orderData->all_closed = true;
if (($orderData->type == OrderType::FADAN && $this->customerId()!= $orderData->customer_id) || $orderData->type == OrderType::GENDAN || ($orderData->type == OrderType::UNION && $this->customerId() != $orderData->customer_id)) {
if ($closeTime >= time()) {
unset($orderData->materials);
$orderData->materials = [];
$orderData->all_closed = false;
}
if ($orderData->type_mode != 1) {
$orderData->all_closed = true;
}
}
return $this->jsonSuccess($orderData);
}
/**
* @api {GET} /api/customer/order/gendan_list 订单详情-跟列列表分页
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {String} order_sn 订单编号
*
* @apiSuccessExample {json} 返回结果,参考订单详情中的gendan列表数据
* {}
*/
public function gendanList(Request $request) {
$customerId = $this->customerId();
$orderSn = $request->input('order_sn');
$size = $request->input('size');
ThrowException::isTrue(!$orderSn, '缺少参数');
$order = Order::where('order_sn', $orderSn)->first();
ThrowException::isTrue(!$order, '参数无效');
$query = Order::with([
'customer:id,name,nickname',
'pOrder:id,order_sn,customer_id', 'pOrder.customer:id,nickname,name'
])
->select(['id', 'pid', 'customer_id', 'money', 'lottery_state', 'lottery_tax_prize', 'lottery_gendan_brokerage', 'lottery_gendan_brokerage_all'])
->where('pid', $order->id)
->where('pay_state', PayState::SUCCESS)
->where('created_date', '>', Config::faDanVisibleDate())
->whereIn('lottery_state', [LottState::WAIT, LottState::WIN, LottState::LOSS, LottState::DRAFT, LottState::SEND]);
if ($customerId == $order->customer_id) {
$result = $query->orderBy('money', 'desc')
->paging($size);
} else {
$page = $request->input('page');
$gendanList = $query->orderBy('money', 'desc')
->limit(10)
->get();
$gt1Page = $page > 1;
$result['data'] = $gt1Page ? [] : $gendanList;
$result['per_page'] = $gt1Page ? 0 : 10;
$result['current_page'] = $gt1Page ? 0 : $page;
$result['total'] = $gt1Page ? 0 : 10;
}
return $this->jsonSuccess($result);
}
/**
* @api {POST} /api/customer/order/unions 合买-列表
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {Int} [lottery_type_id] 彩种类型id
* @apiParam {String} sort_filter schedule进度,new最新,seven七日命中,follow我的关注
*
* @apiSuccessExample {json} 返回结果
* {
* "code":200,
* "message":"",
* "data":{
* "current_page":1,
* "data":[
* {
* "customer":{
* "id":1,
* "nickname":"大象", // 合买发起人
* "avatar":"/avatar/TM1f852RwuK2Dh6b5xcfT8q3xPOdY2I3nKMl1G5P.png", // 头像
* "name":"发达",
* "level_score":0,
* "level_name":"储备组长",
* "level_score_group":"5000",
* "lottery_state_name":"",
* "client_type_name":"未知"
* },
* "lottery":{
* "id":1,
* "name":"竞彩足球", // 彩种
* },
* "id":2,
* "order_sn":"P2023041800000002",
* "money":24, // 方案金额
* "odds_early_close_time":"2023-04-05 15:38:38", // 截止时间
* "odds_late_close_time":"2023-04-06 15:38:38",
* "type_mode":3,
* "type_desc":"这是个合买", // 合买宣言
* "union_pay_success":0,
* "union_brokerage":1, // 佣金比例
* "union_keep":1, // 全额保底1是0否
* "union_piece_keep":6, // 保底份数
* "union_piece_self":0, // 发起人购买份数
* "union_piece_buy":6, // 已购买份数
* "union_piece_total":12, // 总份数
*
* }
* ],
* "from":1,
* "per_page":20,
* "to":4,
* "total":4 // 总条数
* }
* }
*
*/
public function unions(Request $request)
{
$size = $request->input('size');
$lottery_type_id = $request->input('lottery_type_id');
// schedule进度,new最新,seven七日命中,follow我的关注
$sort_filter = $request->input('sort_filter');
$query = Order::with([
'customer:id,nickname,avatar,name,level_score',
'lottery:id,name'
])
->where('shop_id', $this->customerShopId())
->whereRaw('id=pid')
->where('type', OrderType::UNION)
->where('pay_state', PayState::SUCCESS)
->where('odds_early_close_time', '>', date('Y-m-d H:i:s'))
->where('lottery_state', LottState::PENDING);
if ($lottery_type_id) {
$query->where('lottery_type_id', $lottery_type_id);
}
if ($sort_filter == 'schedule') {
$query->orderBy('union_schedule', 'desc');
}
if ($sort_filter == 'new') {
$query->orderBy('id', 'desc');
}
$orders = $query->paging($size);
return $this->jsonSuccess($orders);
}
/**
* @api {POST} /api/customer/order/union_show 合买-详情
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {String} order_sn 订单编号
*
* @apiSuccessExample {json} 返回结果
* {
* "code":200,
* "message":"",
* "data":{
* "customer":{
* "id":1,
* "name":"发达", // 真是姓名
* "nickname":"大象", // 用户昵称
* "email":"fdsa",
* "email_verified_at":null,
* "remark":"",
* "win_alway_num":0,
* "win_lead_num":0,
* "star":0,
* "agent":1,
* "avatar":"/avatar/TM1f852RwuK2Dh6b5xcfT8q3xPOdY2I3nKMl1G5P.png", // 头像
* "seven_hit":"",
* "profit_rate":"",
* "fans_num":0, // 粉丝数
* "follower_num":0,
* "pay_time":0,
* "pay_money":"0.00",
* "client_type":0,
* "level_name":"储备组长",
* },
* "order":{
* "id":6,
* "pid":0,
* "order_sn":"P2023041900000001", // 订单编号
* "customer_id":1,
* "lottery_id":1,
* "shop_id":1,
* "lottery_type_id":1,
* "type":3,
* "copy_num":0,
* "bets_num":2,
* "piao_num":2, // 票数
* "money":24,
* "prize_min":"11.04",
* "prize_max":"142.56",
* "pass_mode":[
* "2.1"
* ],
* "play_type":"mixed",
* "pay_type":"",
* "pay_state":"success",
* "pay_at":"2023-04-19 12:53:15",
* "odds_early_close_time":"2023-04-05 15:38:38", // 截止时间
* "created_at":"2023-04-06 15:38:38", // 发单时间
* "lottery_qupiao":0,
* "lottery_state":5,
* "lottery_prize":"0.00",
* "lottery_send_prize":0,
* "lottery_tax_prize":"0.00",
* "type_mode":3, // 1截止后公开2公开;3认购可见
* "type_desc":"这是个合买", // 合买宣言
* "union_pay_success":0,
* "union_brokerage":1, //
* "union_keep":1, // 是否全额保底
* "union_piece_keep":2, // 保底份数
* "union_piece_self":6, // 自购份数
* "union_piece_buy":6, // 已购份数
* "union_piece_total":12, // 总份数
* "union_schedule":50, // 合买进度1-100
*
* "sellings":[ // 参考订单详情
*
* ],
* "chang_num":2,
* "pass_mode_names":[
* "2串1"
* ],
* "play_name":"竞彩足球",
* "play_type_name":"混合投注",
* "buy_type_name":"自购",
* "pass_mode_name":[
* "2串1"
* ],
* },
* "users":[
* {
* "id":1,
* "order_id":6,
* "customer_id":1,
* "union_piece_total":12, // 总份额
* "union_piece_buy":6, // 购买份额
* "money":"16.00", // 出资
* "created_at":"2023-04-19T12:53:15.000000Z",
* "updated_at":"2023-04-19T12:53:15.000000Z",
* "deleted_at":null,
* "customer":{ // 彩友信息
* "id":1,
* "avatar":"/avatar/TM1f852RwuK2Dh6b5xcfT8q3xPOdY2I3nKMl1G5P.png",
* "nickname":"大象",
* "name":"发达",
* "level_score":0,
* "level_name":"储备组长",
* "level_score_group":"5000",
* "lottery_state_name":"",
* "client_type_name":"未知"
* }
* }
* ]
* }
* }
*
*/
public function unionShow(Request $request)
{
$orderSn = $request->input('order_sn');
/** @var Order $order */
$order = Order::with('materials:id,path')->sn($orderSn)->first();
$jingcaiService = LotteryService::getJingcaiServiceByOrder($order);
/** @var Customer $unionUser */
$unionUser = Customer::find($order->customer_id);
$orderData = $jingcaiService->showOrder($this->customer(), $order);
$unionUses = Order::with('customer:id,avatar,nickname,name,level_score')
->select(['id', 'pid', 'customer_id', 'union_piece_total', 'union_piece_buy'])
->where('pid', $order->id)
->where('type', OrderType::UNION)
->where('pay_state', PayState::SUCCESS)
->orderBy('union_piece_buy', 'desc')
->get();
return $this->jsonSuccess([
'customer' => $unionUser,
'order' => $orderData,
'users' => $unionUses,
]);
}
/**
* @api {POST} /api/customer/order/union_show 合买-剩余份数
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {String} order_sn 订单编号
*
* @apiSuccessExample {json} 返回结果
* {
* "code":200,
* "message":"",
* "data":{
* "order_sn":"P2023041900000001", // 订单号
* "remain_piece":6, // 剩余份数
* "union_piece_money":2 // 每份金额
* }
* }
*/
public function unionPieces(Request $request)
{
$orderSn = $request->input('order_sn');
/** @var Order $order */
$order = Order::sn($orderSn)->where('type', OrderType::UNION)->first();
ThrowException::isTrue(!$order, '无效数据');
return $this->jsonSuccess([
'order_sn' => $order->order_sn,
'remain_piece' => $order->union_piece_total - $order->union_piece_buy,
'union_price_money' => $order->union_piece_money,
]);
}
/**
* @api {POST} /api/customer/order/create_union 合买-创建合买订单
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {String} order_sn 订单编号
* @apiParam {Int} buy_piece 购买份数
*
* @apiSuccessExample {json} 返回结果
* {
* "code":200,
* "message":"",
* "data":{
* "order_sn":"P2023041900000001",
* "balance":"9840.00", // 余额
* "money":10, // 投注金额
* "username":"发达", // 昵称
* "lottery":"jc足球混合投注",
* "pay_types":[
* {
* "pay_type":"alipay",
* "name":"支付宝"
* }
* ]
* }
* }
*/
public function createUnion(Request $request)
{
$orderSn = $request->input('order_sn');
$buyPiece = $request->input('buy_piece');
/** @var Order $unionOrder */
$unionOrder = Order::sn($orderSn)->where('type', OrderType::UNION)->first();
ThrowException::isTrue(!$unionOrder, '无效数据');
$earlyTime = date('Y-m-d H:i:s', strtotime($unionOrder->odds_early_close_time));
ThrowException::isTrue(date('Y-m-d H:i:s') >= $earlyTime, '投注已截止');
ThrowException::isTrue($unionOrder->pay_state != PayState::SUCCESS, '合买不存在');
ThrowException::isTrue($buyPiece < 1, '购买份数不小于1');
// ThrowException::isTrue($this->customerId() == $unionOrder->customer_id, '发起者不能参与合买');
/** @var Lottery $lott */
$lott = Lottery::find($unionOrder->lottery_id);
$lott->validEnableHemai();
$remain = $unionOrder->union_piece_total - $unionOrder->union_piece_buy;
ThrowException::isTrue($buyPiece > $remain, "剩余{$remain}份,份数不足,请重新下单");
$exist = Order::where('customer_id', $this->customerId())
->where('pay_state', PayState::UNPAID)
->where('pid', $unionOrder->id)->first();
if ($exist) {
$exist->where('id', $exist->id)->where('pay_state', PayState::SUCCESS)->delete();
}
/** @var Customer $customer */
$customer = Customer::find($this->customerId());
if (!$this->getPayTypes()) {
ThrowException::isTrue($customer->balanceActive() <= 0, '该店铺暂未开启支付');
}
DB::beginTransaction();
try {
$order = new Order();
$order->pid = $unionOrder->id;
$order->customer_id = $this->customerId();
$order->lottery_id = $unionOrder->lottery_id;
$order->shop_id = $this->customerShopId();
$order->lottery_type_id = $unionOrder->lottery_type_id;
$order->order_sn = Order::makeOrderSn();
$order->play_type = '';
$order->bets_num = 0;
$order->piao_num = 0;
$order->money = $unionOrder->money;
$order->prize_min = 0;
$order->prize_max = 0;
$order->pay_state = PayState::UNPAID;
$order->pass_mode = $unionOrder->pass_mode;
$order->odds_close_time = $unionOrder->odds_close_time;
$order->odds_early_close_time = $unionOrder->odds_early_close_time;
$order->odds_late_close_time = $unionOrder->odds_late_close_time;
$order->type = $unionOrder->type;
$order->type_mode = $unionOrder->type_mode;
$order->type_desc = '';
$order->odds = [];
$order->union_piece_total = $unionOrder->union_piece_total;
$order->union_piece_money = $unionOrder->union_piece_money;
$order->union_money = $unionOrder->union_piece_money * $buyPiece;
$order->union_piece_buy = $buyPiece;
$order->union_piece_self = $buyPiece;
$order->union_piece_keep = 0;
$order->union_keep = 0;
$order->union_brokerage = 0;
$order->created_date = date('Ymd');
$order->save();
DB::commit();
$balance = $customer->balanceActive();
$payMoney = $order->getPayMoney();
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()
]);
} catch (JingCaiException $exception) {
DB::rollBack();
throw $exception;
} catch (\Exception $exception) {
DB::rollBack();
throw $exception;
}
}
}