Files
amb_wechatapp/application/common/model/user/Collect.php
T

126 lines
2.7 KiB
PHP

<?php
namespace app\common\model\user;
use think\Model;
/**
* 用户收藏
*/
class Collect extends Model
{
// 表名
protected $name = 'collect';
public function course()
{
return $this->belongsTo('\app\common\model\course\Course', 'item_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
/**
* 获取课程列表
* @param $userId 分组ID
* @param $params 分页信息 page页码 limit获取数量
* @return array
*/
public function getMyCollect($userId = 0,$params = []){
$rows = self::with(['course']);
$where = [
'user_id'=>$userId,
'course.status'=>1
];
if($params['type'] != 'all'){
$where['course.type'] = $params['type'];
}
$list = $rows
->where($where)
->limit($params['limit'])->page($params['page'])
->order('createtime','desc')
->select();
$data = [];
if(!empty($list)){
foreach ($list as $item){
$item->getRelation('course')->visible(['name','cover','type','id']);
$data[] = $item;
}
}
return $data;
}
/**
* 获取课程列表
* @param $userId 分组ID
* @param $params 分页信息 page页码 limit获取数量
* @return array
*/
public function getStudyLog($userId = 0,$params = []){
$rows = self::with(['course']);
$where = [
'user_id'=>$userId
];
$list = $rows
->where($where)
->limit($params['limit'])->page($params['page'])
->order('study.end_time','desc')
->group('study.course_id')
->field('sum(study.total_time) as total_time')
->select();
$data = [];
if(!empty($list)){
foreach ($list as $item){
$item->getRelation('course')->visible(['name','cover','type','id']);
$data[] = $item;
}
}
return $data;
}
/**
* 收藏
* @param $userId 用户ID
* @param $itemId 收藏对象
* @param $type 收藏对象类型
* @return void
*/
public function setCollect($userId,$itemId,$type='course'){
//判断最近指定范围内是否有记录
$params = [
'user_id'=>$userId,
'item_id'=>$itemId,
'uniacid'=>UNIACID,
'type'=>$type
];
$log = self::where($params)->find();
if(!$log){
$params['createtime'] = time();
self::insert($params);
}else{
self::where([
'id'=>$log['id']
])->delete();
}
return true;
}
}