Commit f16822b1 authored by 杨培's avatar 杨培

课堂概况显示各种信息

parent f5dc8573
......@@ -189,4 +189,33 @@ class Course extends \yii\db\ActiveRecord
{
return $this->hasOne(User::className(), ['id' => 'user_id'])->select('id,username,role,avatar');;
}
/**
* 统计指定用户发布的课程数量
*
* @param int $userId
* @param int $status 默认只统计审核通过的,设为 null 则统计全部
* @return int
*/
public static function countCourseByUserId($userId, $status=self::STATUS_OK)
{
$where = ['user_id'=>$userId];
$status !== null && $where['status'] = $status;
return self::find()->where($where)->count();
}
/**
* 统计指定用户发布课程的评论总数
*
* @param int $userId
* @param int $status 默认只统计审核通过的课程评论,设为 null 则统计全部
* @return int
*/
public static function countCommentsByUserId($userId, $status=self::STATUS_OK)
{
$where = ['user_id'=>$userId];
$status !== null && $where['status'] = $status;
return self::find()->where($where)->sum('comment_count');
}
}
......@@ -75,6 +75,7 @@ class Income extends \yii\db\ActiveRecord
}
/**
* 关联订单详细数据
* @return $this
*/
public function getOrder()
......@@ -82,12 +83,6 @@ class Income extends \yii\db\ActiveRecord
return $this->hasOne(Orders::className(), ['id'=>'orders_id'])->with('user')->with('course');
}
//public function getUser()
//{
// return $this->hasOne(User::className(), ['id'=>'user_id'])->viaTable(Orders::tableName(), ['id'=>'orders_id'])
// ->select('id,username,role,avatar');
//}
/**
* 获取指定用户已出售课程的订单列表
*
......@@ -132,9 +127,25 @@ class Income extends \yii\db\ActiveRecord
$where = ['i.user_id'=>$userId];
$status !== null && $where['i.status'] = $status;
return (float)self::find()->alias('i')->select('SUM(o.credit)')->where($where)
return (float)self::find()->alias('i')->where($where)
->join('LEFT JOIN', Orders::tableName().' o', 'o.id=i.orders_id')
->sum('o.credit');
}
/**
* 统计指定用户时间范围内的收入
*
* @param int $userId
* @param int $timeBetween 起始时间戳
* @param int $timeAnd 结束时间戳
* @return float
*/
public static function getMoneyTotalByTime($userId, $timeBetween, $timeAnd)
{
return (float)self::find()->alias('i')
->join('LEFT JOIN', Orders::tableName().' o', 'o.id=i.orders_id')
->scalar();
->where(['i.user_id'=>$userId])->andWhere(['between', 'i.updated_at', $timeBetween, $timeAnd])
->sum('o.credit');
}
}
......@@ -3,6 +3,8 @@
namespace frontend\controllers;
use common\models\Classes;
use common\models\Course;
use common\models\Income;
use frontend\components\ClassesFilter;
use Yii;
use yii\filters\AccessControl;
......@@ -36,10 +38,29 @@ class ClassesInfoController extends \yii\web\Controller
];
}
/**
* 课堂概况
* @return string
*/
public function actionIndex()
{
$uid = Yii::$app->user->getId();
//课堂信息
$model = Classes::findOne($this->classStatus['id']);
return $this->render('index', compact('model'));
//统计课程数量
$courseCount = Course::countCourseByUserId($uid);
//统计今日收
$timeBetween = strtotime('Today');
$timeAnd = strtotime('Tomorrow') - 1;
$todayIncome = Income::getMoneyTotalByTime($uid, $timeBetween, $timeAnd);
//统计评论数量
$commentCount = Course::countCommentsByUserId($uid);
return $this->render('index', compact('model', 'courseCount', 'todayIncome', 'commentCount'));
}
/**
......@@ -60,9 +81,7 @@ class ClassesInfoController extends \yii\web\Controller
} else {
$agreement = 1;
}
echo $model->status;
if (Yii::$app->request->getIsPost()) {
$model->load(Yii::$app->request->post());
$model->user_id = Yii::$app->user->getId();
......
......@@ -2,26 +2,31 @@
/**
* @var $this yii\web\View
* @var \common\models\Classes $model
* @var $courseCount int
* @var $todayIncome float
* @var $commentCount int
*/
use yii\helpers\Url;
$this->title = '我的课堂';
?>
<div class="class-name"><?=$model->name?></div>
<div class="class-intro">
<div class="col-md-4">
<h3>课程总数</h3>
<p>10<small>讲</small></p>
<a href="#">发布课程</a>
<p><?=$courseCount?><small>讲</small></p>
<a href="<?=Url::to(['my-course/publish'])?>">发布课程</a>
<i class="icon-play"></i>
</div>
<div class="col-md-4">
<h3>今日收益</h3>
<p>192.00<small>元</small></p>
<a href="#">查看账单</a>
<p><?=$todayIncome?><small>元</small></p>
<a href="<?=Url::to(['my-income/index'])?>">查看账单</a>
<i class="icon-clipboard"></i>
</div>
<div class="col-md-4">
<h3>今日发言</h3>
<p>891<small>条</small></p>
<p><?=$commentCount?><small>条</small></p>
<i class="icon-speech-bubble"></i>
</div>
</div>
\ No newline at end of file
<?php
/* @var $this yii\web\View */
?>
<div class="vip-top">
<div class="vip-top clearfix">
<div class="col-md-6">
<div class="reg-vip-box">
<b>47</b>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment