Files
amb_wechatapp/application/admin/controller/data/Live.php
T

217 lines
6.9 KiB
PHP

<?php
namespace app\admin\controller\data;
use app\common\controller\Backend;
use app\common\model\user\Study as StudyModel;
/**
* 直播数据
*
* @icon fa fa-circle-o
*/
class Live extends Backend
{
/**
* Course模型对象
* @var \app\admin\model\order\Course
*/
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = new StudyModel();
$this->timeModel = new \app\admin\model\data\Time();
}
/**
* 直播统计
* @return void
*/
public function total(){
$time = $this->request->post('time/a',0);
if(!$time){
$this->success('获取成功', []);
}
if(count($time) == 1){
$time = $time[0];
}
$unit = $this->request->post('unit',0);
$this->timeModel = new \app\admin\model\data\Time();
$time = $this->timeModel->getFilterStartEndTime($time,$unit);
$where = [
'start_time'=>['between',[$time['start'],$time['end']]]
];
$data = $this->model
->where($where)
->field([
"COUNT(DISTINCT user_id) AS study_user_count",
"SUM(total_time) AS study_time_total",
"COUNT(*) AS study_count",
"COUNT(DISTINCT course_id) AS study_courses"
])
->find();
if($data['study_time_total'] && $data['study_user_count']){
$data['user_study_time_average'] = $data['study_time_total'] / $data['study_user_count'];
$data['user_study_time_average'] = number_format($data['user_study_time_average']/ 60 / 60 ,2);
}else{
$data['user_study_time_average'] = 0;
}
if($data['study_time_total']){
$data['study_time_total'] = number_format($data['study_time_total']/ 60 / 60,2 );
}
$data['study_time_total'] = floatval($data['study_time_total']);
$data['user_study_time_average'] = floatval($data['user_study_time_average']);
$this->success('获取成功', $data);
}
/**
* 直播趋势
* @return void
*/
public function trend(){
$time = $this->request->post('time/a',0);
$courseId = input('course_id');
if(!$time){
$this->success('获取成功', []);
}
if(count($time) == 1){
$time = $time[0];
}
$unit = $this->request->post('unit',0);
$this->timeModel = new \app\admin\model\data\Time();
$emptyData = [
'study_user_count'=>0,
'study_time_total'=>0,
'study_count'=>0,
'study_courses'=>0,
'user_study_time_average'=>0
];
$time = $this->timeModel->getFilterStartEndTime($time,$unit);
if($unit == 'date'){
$interval = 3600;
$timeFormat = 'H:i';
$timeFormatSql = '%H';
}else{
$interval = 86400;
$timeFormat = 'Y-m-d';
$timeFormatSql = '%Y-%m-%d';
}
$where = [
'start_time'=>['between',[$time['start'],$time['end']]]
];
if($courseId){
$where['course_id']=$courseId;
}
$result = $this->model
->where($where)
->field([
"FROM_UNIXTIME(start_time, '{$timeFormatSql}') as date", // 转换创建时间为日期
"COUNT(DISTINCT user_id) AS study_user_count",
"SUM(total_time) AS study_time_total",
"COUNT(*) AS study_count",
"COUNT(DISTINCT course_id) AS study_courses"
])
->group("FROM_UNIXTIME(start_time, '{$timeFormatSql}')")
->order("start_time", "desc")
->select();
$list = [];
if($result){
foreach ($result as $item){
$timeUnit = $item['date'];
if($unit == 'date'){
$timeUnit = $timeUnit.":00";
}
$list[$timeUnit] = $item;
}
}
$data = [];
for($i=$time['start'];$i<$time['end'];$i+=$interval){
$timeUnit = date($timeFormat,$i);
if(!isset($data[$timeUnit])){
$data[$timeUnit] = $emptyData;
}
if(isset($list[$timeUnit])){
$data[$timeUnit] = $list[$timeUnit];
}
if($data[$timeUnit]['study_time_total'] && $data[$timeUnit]['study_user_count']){
$data[$timeUnit]['user_study_time_average'] = $data[$timeUnit]['study_time_total'] / $data[$timeUnit]['study_user_count'];
$data[$timeUnit]['user_study_time_average'] = number_format($data[$timeUnit]['user_study_time_average']/ 60 / 60 ,2);
}else{
$data[$timeUnit]['user_study_time_average'] = 0;
}
if($data[$timeUnit]['study_time_total']){
$data[$timeUnit]['study_time_total'] = number_format($data[$timeUnit]['study_time_total']/ 60 / 60,2 );
}
}
$this->success('获取成功', $data);
}
/**
* 排名
* @return void
*/
public function rank(){
//当前是否为关联查询
$this->relationSearch = false;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
//如果发送的来源是Selectpage,则转发到Selectpage
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$liveIds = (new \app\admin\model\course\Course)->getLiveCourseIds();
$list = $this->model
->setUniacid(false)
->with(['course'])
->field([
'study.course_id', // 按课程分组
'COUNT(*) as total_views', // 累计观看人次
'COUNT(DISTINCT user_id) as total_users', // 累计观看人数
// 'AVG(total_time) as avg_watch_time', // 人均观看时长
// '(SELECT COUNT(*) FROM '.$this->model->getTable().' AS t WHERE t.course_id = study.course_id) as course_views', // 课程浏览量
// '(SELECT MAX(concurrent_users)
// FROM (
// SELECT COUNT(DISTINCT a.user_id) as concurrent_users
// FROM '.$this->model->getTable().' a
// JOIN '.$this->model->getTable().' b
// ON a.start_time BETWEEN b.start_time AND b.end_time
// OR b.start_time BETWEEN a.start_time AND a.end_time
// WHERE a.course_id = b.course_id
// GROUP BY a.start_time
// ) as concurrent_table) as max_concurrent_users' // 最高同时学习人数
])
->group('study.course_id') // 按课程分组
->order($sort, $order)
->paginate($limit);
$result = array("total" => $list->total(), "rows" => $list->items(),'attr'=>[]);
$this->success('获取成功', $result);
}
}