0
0
Files
Babyclass/app/Http/Controllers/LessonsController.php
2020-08-04 10:09:42 +08:00

149 lines
5.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Cart;
use App\Models\Category;
use App\Models\SellerLesson;
use App\Models\SellerLessonLog;
use Auth;
use DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
class LessonsController extends Controller
{
public function __construct(Request $request)
{
parent::__construct($request);
$this->middleware('auth')->except(['show', 'category']);
View::share('nav', 3);
}
public function index(Request $request)
{
$type = $request->type ?? 'unoverdue';
$lists = SellerLessonLog::with('lesson')->whereHas('lesson', function ($query) use ($type) {
if ($type == 'unoverdue') {
$query->where('end_at', '>=', date('Y-m-d H:i:s', strtotime("+1 days", time())));
} else {
$query->where('end_at', '<=', date('Y-m-d H:i:s', strtotime("+1 days", time())));
}
})->where('user_id', Auth::id())->where('status', 1)->get();
view()->share('nav', 3);
return view('lesson.index', compact('lists', 'type'));
}
public function show(SellerLesson $lesson)
{
if (Auth::guest()) {
$favorite = collect();
} else {
$favorite = Auth::user()->lessonFavorite()->where('item_id', $lesson->id)->first();
}
$cart = Cart::where('user_id', Auth::id())->where('lesson_id', $lesson->id)->first();
return view('lesson.show', compact('lesson', 'favorite', 'cart'));
}
//分类
public function category(Request $request)
{
$category_id = $request->category_id;
$location = Parent::getDefaultLocation();
$lat = $location['lat'];
$lng = $location['lng'];
$area = $location['area'];
$category = Category::find($category_id);
$user = Auth::user();
$organCateIds = [];
if ($user && $user->organ) {
$organCateIds = $user->organ->top_cate_id;
}
$lists = SellerLesson::with(['organ' => function ($query) use ($lat, $lng) {
$query->select('id', 'name', 'storage_id', 'user_id', 'category_id', DB::raw('round(ACOS(SIN((' . $lat . ' * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS((' . $lat . ' * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS((' . $lng . ' * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380,2) as distance'));
}])->when(is_numeric($category_id), function ($query) use ($category_id) {
$query->whereHas('category', function ($q) use ($category_id) {
$q->where('id', $category_id)->Orwhere('parent_id', $category_id);
});
})->when($area, function ($query) use ($area) {
$query->whereHas('organ', function ($query) use ($area) {
if ($area->depth == 2) {
$query->where('city_sn', $area->sn);
} elseif ($area->depth == 3) {
$query->where('area_sn', $area->sn);
} else {
$query->where('province_sn', $area->sn);
}
});
})->when(in_array($category_id, $organCateIds), function ($q) use ($user) {
$q->where('seller_id', $user->organ->id);
})->where('status', 1)->get();
$search_id = ($category->parent_id == 1) ? $category->id : $category->parent_id;
$categorys = Category::with('storage')->where('parent_id', $search_id)
->orderBy('sort', 'asc')
->orderBy('created_at', 'desc')->get();
return view('lesson.category', compact('lists', 'categorys', 'search_id'));
}
public function report(Request $request)
{
return view('lesson.report');
}
//所有课程
public function all(Request $request)
{
$user = Auth::user();
$category_id = $request->category_id ?? '';
$location = Parent::getDefaultLocation();
$lat = $location['lat'];
$lng = $location['lng'];
$area = $location['area'];
$organCateIds = [];
if ($user && $user->organ) {
$organCateIds = $user->organ->top_cate_id;
}
$lists = SellerLesson::with(['organ' => function ($query) use ($lat, $lng) {
$query->select('id', 'name', 'storage_id', 'user_id', 'category_id', DB::raw('round(ACOS(SIN((' . $lat . ' * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS((' . $lat . ' * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS((' . $lng . ' * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380,2) as distance'));
}])->when(is_numeric($category_id), function ($query) use ($category_id) {
$query->whereHas('category', function ($q) use ($category_id) {
$q->where('id', $category_id)->Orwhere('parent_id', $category_id);
});
})->when($area, function ($query) use ($area) {
$query->whereHas('organ', function ($query) use ($area) {
if ($area->depth == 2) {
$query->where('city_sn', $area->sn);
} elseif ($area->depth == 3) {
$query->where('area_sn', $area->sn);
} else {
$query->where('province_sn', $area->sn);
}
});
})->when(in_array($category_id, $organCateIds), function ($q) use ($user) {
$q->where('seller_id', $user->organ->id);
})->where('status', 1)->get();
$lists = $lists->sortBy('button_value');
$categorys = Category::with('storage')->where('parent_id', 1)
->orderBy('sort', 'asc')
->get();
return view('lesson.all', compact('lists', 'categorys'));
}
}