<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Article;
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['share_count']=Share::count();//全部转发量
        $data['read_count']=Read::count();//全部播放量
//        $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 getArticleRead(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=Article::query()->where('shelves',1)->count();
        $list=Article::query()->where('shelves',1)
            ->select('title','stage','audio','perface','created_at','id','duration','size','shelves','read','like','share','timing_shelves')
            ->orderBy($type,$sort)->skip($num)->take($page_size)->get();
        return $this->success_list($list,'',$count);
    }
}
