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

73 lines
2.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Category;
use App\Models\Seller;
use App\Models\SellerLesson;
use App\Models\SellerLessonLog;
use App\User;
use Auth;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
class AgencyController extends Controller
{
public function __construct(Request $request)
{
$this->middleware('auth');
View::share('nav', 2);
}
public function index(Request $request)
{
$user = Auth::user();
$user = User::find(1);
if (!$user->agency || $user->agency->status != 1) {
return redirect()->route('user.index');
}
$category = $request->category;
$sellers = Seller::with(['category'])
->withCount('lesson')
->where('agency_id', $user->agency->id)
->when(is_numeric($category), function ($q) use ($category) {
$q->where('category_id', $category);
})
->get();
$categoryids = Seller::where('agency_id', $user->agency->id)->groupBy('category_id')->pluck('category_id')->toArray();
$categorys = Category::whereIn('id', $categoryids)->get();
return view('agency.index', compact('sellers', 'categorys'));
}
public function lesson(Request $request, Seller $seller)
{
$lessons = SellerLesson::withCount('logs')->where('seller_id', $seller->id)->get();
return view('agency.lesson', compact('lessons', 'seller'));
}
public function data()
{
$user = Auth::user();
$user = User::find(1);
if (!$user->agency || $user->agency->status != 1) {
return redirect()->route('user.index');
}
$dayBetween = [Carbon::today()->toDateTimeString(), Carbon::today()->addSecond(86399)->toDateTimeString()];
$sellerids = Seller::where('agency_id', $user->agency->id)->pluck('id')->toArray();
$lessonids = SellerLesson::whereIn('seller_id', $sellerids)->pluck('id')->toArray();
$data = [
'seller_count' => count($sellerids), //机构总数
'lessons_count' => count($lessonids), //课程总数
'apply_lesson_all' => SellerLessonLog::whereIn('lesson_id', $lessonids)->count(), //报名总数
'apply_lesson_day' => SellerLessonLog::whereIn('lesson_id', $lessonids)->whereBetween('created_at', $dayBetween)->count(), //今日报名数
];
return view('agency.data', compact('data'));
}
}