<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Course;
use Illuminate\Http\Request;
use App\Models\Share;
use App\Models\Read;
use Illuminate\Support\Facades\DB;

class DataController extends Controller
{
    //获取统计数据
    public function index(Request $request){
        $data['course_count']=Course::where('state',1)->count();//全部转发量
        $data['share_count']=Course::sum('share');//全部转发量
        $data['read_count']=Course::sum('read');//全部阅读量
//        $data['read_list']=Read::with('article:id,title,stage')->select(DB::raw('count(*) as count'),'article_id')->groupBy('article_id')->get();
        return $this->success($data);
    }

    //获取课程阅读量
    public function getCourseRead(Request  $request)
    {
        $page_index=$request->input('page_index');
        $page_size=$request->input('page_size');
        $type=$request->input('type');
        $sort=$request->input('sort');
        $num=$page_size*($page_index-1);
        $count=Course::query()->where('state',1)->count();
        $list=Course::setField()->where('state',1)
            ->orderBy($type,$sort)->skip($num)->take($page_size)->get();
        return $this->success_list($list,'',$count);
    }
}
