94 lines
2.9 KiB
PHP
94 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Cart;
|
|
use App\Models\GoodsParams;
|
|
use App\Models\SellerLesson;
|
|
use Auth;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\View;
|
|
use RuLong\Coupon\Models\CouponUserLog;
|
|
|
|
class CartController extends Controller
|
|
{
|
|
|
|
public function __construct(Request $request)
|
|
{
|
|
parent::__construct($request);
|
|
$this->middleware('auth')->except(['store']);
|
|
View::share('nav', 3);
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$lists = Cart::mine()->with('lesson')->orderBy('id', 'desc')->get();
|
|
|
|
$gifts = GoodsParams::whereHas('goods', function ($query) {
|
|
$query->where('status', 1)->where('is_seller_gift', 1);
|
|
})->where('stock', '>', 0)->get();
|
|
|
|
$coupon_list = CouponUserLog::with('info')->whereHas('info', function ($query) {
|
|
$query->where('type', 'lesson')
|
|
->where('start_at', '<=', date('Y-m-d H:i:s', time()))
|
|
->where('end_at', '>=', date('Y-m-d H:i:s', time()));
|
|
})->where('user_id', Auth::id())->where('status', 0)->get();
|
|
|
|
$coupon = $coupon_list->sortByDesc(function ($coupon, $key) {
|
|
return $coupon->info->bouns;
|
|
})->first();
|
|
|
|
$apply_lesson_price = \Params::get('apply_lesson_price');
|
|
return view('cart.index', compact('lists', 'gifts', 'coupon', 'apply_lesson_price'));
|
|
}
|
|
|
|
public function show(Request $request, $params_id)
|
|
{
|
|
$info = GoodsParams::with('goods.storage')->find($params_id);
|
|
return view('cart.show', compact('info'));
|
|
}
|
|
|
|
public function delete(Cart $cart)
|
|
{
|
|
if ($cart->delete()) {
|
|
return $this->success('取消成功');
|
|
} else {
|
|
return $this->error('取消失败');
|
|
}
|
|
}
|
|
|
|
public function store(SellerLesson $lesson)
|
|
{
|
|
if (Auth::guest()) {
|
|
return $this->error('请登录后在进行此操作', route('login'));
|
|
}
|
|
|
|
$user_id = Auth::id();
|
|
$max_num = \Params::get('lesson_num');
|
|
$count = Cart::where('user_id', $user_id)->count();
|
|
|
|
if ($count >= $max_num) {
|
|
return $this->error('您已经报满' . $max_num . '个机构课程请前去支付', route('cart.index'));
|
|
}
|
|
|
|
$info = Cart::where('lesson_id', $lesson->id)->where('user_id', $user_id)->first();
|
|
if ($info) {
|
|
return $this->error('您已经报名此课程');
|
|
}
|
|
|
|
$seller = Cart::where('seller_id', $lesson->organ->id)->where('user_id', $user_id)->first();
|
|
if ($seller) {
|
|
return $this->error('一个机构只能报名一门课程');
|
|
}
|
|
|
|
$result = Cart::create([
|
|
'user_id' => Auth::id(),
|
|
'lesson_id' => $lesson->id,
|
|
'seller_id' => $lesson->organ->id,
|
|
'number' => 1,
|
|
]);
|
|
return $this->success('报名成功', route('cart.index'));
|
|
|
|
}
|
|
}
|