Commit 5ba89a1c authored by 杨培's avatar 杨培

我的会员列表

parent 644ae338
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace common\models; namespace common\models;
use Yii; use Yii;
use yii\data\Pagination;
/** /**
* This is the model class for table "member". * This is the model class for table "member".
...@@ -16,6 +17,10 @@ use Yii; ...@@ -16,6 +17,10 @@ use Yii;
*/ */
class Member extends \yii\db\ActiveRecord class Member extends \yii\db\ActiveRecord
{ {
const TYPE_VIP = 1; //付费会员
const TYPE_NORMAL = 0; //普通会员
/** /**
* @inheritdoc * @inheritdoc
*/ */
...@@ -49,4 +54,102 @@ class Member extends \yii\db\ActiveRecord ...@@ -49,4 +54,102 @@ class Member extends \yii\db\ActiveRecord
'updated_at' => 'Updated At', 'updated_at' => 'Updated At',
]; ];
} }
/**
* 获取指定用户所有的会员列表
*
* @param int $userId
* @param int $page 页数,从0开始,传入 null 则使用内置分页,默认为0
* @param int $num 每页获取数量,默认10
* @param null $type 默认为获取全部会员,传入状态获取付费会员或非付费会员
* @return array
*/
public static function getMembersByUserId($userId, $page=0, $num=10, $type=null)
{
$where = ['user_id'=>$userId];
$type !== null && $where['type'] = $type;
$count = self::find()->where($where)->count();
$return = ['count'=>$count, 'data'=>[]];
if ($page === NULL) {
$pagination = new Pagination(['totalCount' => $count, 'pageSize' => $num, 'forcePageParam' => FALSE]);
$offset = $pagination->offset;
$return['pagination'] = $pagination;
} else {
$offset = $page * $num;
}
if ($count > 0) {
$return['data'] = self::find()->select('m.id,m.member_id,m.type')->alias('m')
->where($where)->with('user')->offset($offset)->limit($num)
->orderBy(['id'=>SORT_DESC])->asArray()->all();
}
return $return;
}
/**
* 关联用户信息
* @return $this
*/
public function getUser()
{
return $this->hasOne(User::className(), ['id'=>'member_id'])->select('id,username,telephone,role,avatar');
}
/**
* 向成员列表中注入购买和课程相关信息
*
* 向传入的二维数组中注入 credit_total 消费总额和 first_order 第一个订单的数据(其中包括课程信息)
*
* @param int $userId 所属课堂用户ID
* @param array $list 包含 member_id 的二维数组
*/
public static function injectOrderInfo($userId, &$list)
{
$memberIds = array_column($list, 'member_id');
if (!$memberIds) return;
//先取出最小订单ID和消费总额
$group = Income::find()->alias('i')
->select('MIN(i.orders_id) AS min_id, o.user_id, SUM(o.credit) AS credit_total')
->join('LEFT JOIN', Orders::tableName().' o', 'o.id=i.orders_id')
->where(['i.user_id'=>$userId])->andWhere(['in', 'o.user_id', $memberIds])
->groupBy('o.user_id')->asArray()->indexBy('user_id')->all();
//再取出订单关联的课程信息
if ($group) {
$orders = Orders::find()->select('id,course_id,credit,updated_at')
->where(['in', 'id', array_column($group, 'min_id')])->with('course')
->asArray()->indexBy('id')->all();
foreach ($group as &$row) {
$row['first_order'] = isset($orders[$row['min_id']]) ? $orders[$row['min_id']] : null;
}
unset($row);
}
foreach ($list as &$row) {
$g = isset($group[$row['member_id']]) ? $group[$row['member_id']] : null;
$row['credit_total'] = $g ? $g['credit_total'] : 0;
$row['first_order'] = $g ? $g['first_order'] : null;
}
}
/**
* 关联成员首个订单
*
* 返回最小order_id,消费总额,以及课程信息
*
* @return $this
*/
public function getFirstOrder()
{
return $this->hasOne(Orders::className(), ['id'=>'orders_id'])->viaTable(Income::tableName(), ['user_id'=>'user_id'])
->with('course')->select('MIN(id) AS id,credit,course_id');
}
} }
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
namespace frontend\controllers; namespace frontend\controllers;
use common\models\Member;
use Yii;
class MyMemberController extends \yii\web\Controller class MyMemberController extends \yii\web\Controller
{ {
...@@ -9,7 +12,19 @@ class MyMemberController extends \yii\web\Controller ...@@ -9,7 +12,19 @@ class MyMemberController extends \yii\web\Controller
public function actionIndex() public function actionIndex()
{ {
return $this->render('index'); $type = Yii::$app->request->get('type');
in_array($type, [0, 1]) || $type = null;
$uid = Yii::$app->user->getId();
$data = Member::getMembersByUserId($uid, null, 20);
//注入订单等信息
if ($data['count'] > 0) {
Member::injectOrderInfo($uid, $data['data']);
}
return $this->render('index', $data);
} }
} }
...@@ -40,13 +40,12 @@ ...@@ -40,13 +40,12 @@
<td>2016-12-11</td> <td>2016-12-11</td>
<td><?=$row['user']['telephone']?> / <?=$row['user']['username']?></td> <td><?=$row['user']['telephone']?> / <?=$row['user']['username']?></td>
<td>推广</td> <td>推广</td>
<?php if (!empty($row['firstOrder'])) { ?> <?php if (!empty($row['first_order'])) { ?>
<td>《<?=$row['firstOrder']['course']['name']?>》</td> <td>《<?=$row['first_order']['course']['name']?>》</td>
<td>¥<?=$row['firstOrder']['credit_total']?></td>
<?php } else { ?> <?php } else { ?>
<td>无</td> <td>无</td>
<td>¥0</td>
<?php } ?> <?php } ?>
<td>¥<?=$row['credit_total']?></td>
</tr> </tr>
<?php } ?> <?php } ?>
</tbody> </tbody>
......
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