<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Jobs\ProcessPodcast;
use App\Models\Admin\Admission;
use App\Models\Admin\Order;
use App\Models\Admin\Userdo;
use App\Models\Admin\Vocalconcert;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class ConcertController extends Controller
{
public function concertList()
{
$data = Vocalconcert::orderBy('v_people','desc')->paginate(3);
return ['code'=>'200','msg'=>'成功','data'=>$data];
}
public function concertFind(Request $request)
{
$request->except('token');
$id = $request['v_id'];
$data = Vocalconcert::where('v_id',$id)->first();
return ['code'=>'200','msg'=>'成功','data'=>$data];
}
public function concertLike(Request $request)
{
$v_name = $request['text'];
$data = Vocalconcert::where('v_name','like','%'.$v_name.'%')
->orwhere('v_time','like','%'.$v_name.'%')
->get()->toArray();
return ['code'=>'200','msg'=>'成功','data'=>$data];
}
public function orderAdd(Request $request)
{
$data = Admission::where('status','0')->orwhere('status','3')->get()->toArray();
$a_id = array_push($data);
$uid = $request['uid'];
$v_id = $request['v_id'];
$vocal = Vocalconcert::where('v_id',$v_id)->get()->toArray();
$v_price = $vocal[0]['v_price'];
$o_card = md5(time()) . $v_id . $a_id;
$userdo = Userdo::where('uid',$uid)->first();
$balance = $userdo->balance;
if($balance < $v_price){
return ['code'=>4001,'msg'=>'余额不足','data'=>''];
}
//开启事务
DB::beginTransaction();
try {
$res = Order::create([
'a_id'=>$a_id,
'uid'=>$uid,
'v_id'=>$v_id,
'o_card'=>$o_card,
'price'=>$v_price
]);
Admission::where('a_id',$a_id)->update(['status'=>'2']);
$qian = $balance - $v_price;
Userdo::where('uid',$uid)->update(['balance'=>$qian]);
$phone = $userdo['phone'];
DB::commit();
$ak['phone'] = $phone;
$data = json_decode(json_encode($ak));
ProcessPodcast::dispatch($data);
return ['code'=>200,'msg'=>'购票成功','data'=>''];
} catch (\Exception $e) {
DB::rollBack();
return ['code' => 4000, 'msg' => '支付失败', 'data' => ''];
}
}
public function orderList()
{
$data = Order::join('vocalconcert', function ($join) {
$join->on('vocalconcert.v_id', '=','order.v_id');
})
->join('admission', function ($join) {
$join->on('admission.a_id', '=','order.a_id');
})
->get();
return ['code'=>'200','msg'=>'成功','data'=>$data];
}
public function orderRet(Request $request)
{
$id = $request['add']['id'];
$a_id = $request['add']['a_id'];
DB::beginTransaction();
try {
Admission::where('a_id',$a_id)->update(['status'=>'3']);
Order::where('id',$id)->update(['status'=>'3']);
$qian1 = Order::where('id',$id)->first();
$qian2 = $qian1['price'];
$uid = $request['uid'];
$data = Userdo::where('uid',$uid)->first();
$balance = $data['balance'] + $qian2;
Userdo::where('uid',$uid)->update(['balance'=>$balance]);
DB::commit();
return ['code'=>'200','msg'=>'退票成功','data'=>''];
} catch (\Exception $e) {
DB::rollBack();
return ['code' => 4000, 'msg' => '退票失败', 'data' => ''];
}
}
}
Route::group(['namespace'=>'Api','middleware'=>['JwtToken','throttle:60,1']],function (){
Route::post('concertLike',[ConcertController::class,'concertLike']);
Route::post('orderAdd',[ConcertController::class,'orderAdd']);
Route::get('orderList',[ConcertController::class,'orderList']);
Route::post('orderRet',[ConcertController::class,'orderRet']);
});