207 lines
7.0 KiB
PHP
207 lines
7.0 KiB
PHP
<?php
|
|
|
|
namespace app\api\model\course;
|
|
|
|
use think\Model;
|
|
use app\api\model\course\Course;
|
|
class GroupBind extends Model
|
|
{
|
|
|
|
|
|
// 表名
|
|
protected $name = 'course_bind_group';
|
|
|
|
// 自动写入时间戳字段
|
|
protected $autoWriteTimestamp = 'integer';
|
|
|
|
// 定义时间戳字段名
|
|
protected $createTime = 'createtime';
|
|
protected $updateTime = false;
|
|
protected $deleteTime = false;
|
|
|
|
|
|
public function course()
|
|
{
|
|
return $this->belongsTo('Course', 'course_id', 'id', [], 'right')->setEagerlyType(0);
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取课程列表
|
|
* @param $groupId 分组ID
|
|
* @param $params 分页信息 page页码 limit获取数量
|
|
* @return array
|
|
*/
|
|
public function getCourseList($groupId = 0,$params = []){
|
|
|
|
|
|
$where = [
|
|
'course.status'=>1,
|
|
'sales_type'=>['like',"%alone%"],
|
|
'hide'=>0
|
|
];
|
|
|
|
|
|
if(isset($params['sort']) && !empty($params['sort'])){
|
|
if($params['sort']=='time'){
|
|
$orderField = 'course.createtime';
|
|
}else{
|
|
$orderField = 'course.views';
|
|
}
|
|
}
|
|
|
|
if(isset($params['searchKey']) && !empty($params['searchKey'])){
|
|
$where['course.name'] = ['like',"%{$params['searchKey']}%"];
|
|
}
|
|
|
|
if(isset($params['type']) && !empty($params['type'])){
|
|
$where['course.type'] = $params['type'];
|
|
}
|
|
|
|
if($groupId){
|
|
$where['group_bind.group_id'] = $groupId;
|
|
$rows = self::with(['course'])->order('group_bind.sort','desc');
|
|
}else{
|
|
$rows = Course::alias('course')->order('createtime','desc');
|
|
}
|
|
|
|
|
|
|
|
$list = $rows
|
|
->where($where)
|
|
->limit($params['limit'])->page($params['page'])
|
|
->order($orderField,'desc')
|
|
->group('course.id')
|
|
->select();
|
|
|
|
$data = [];
|
|
|
|
if(!empty($list)){
|
|
|
|
$vipAppConfig = \app\common\model\app\Config::getConfig('vip');
|
|
|
|
foreach ($list as $item){
|
|
$item['is_vip_goods'] = false;
|
|
if($vipAppConfig['status'] == 1){
|
|
//判断是否为会员权益课程
|
|
$item['is_vip_goods'] = \app\api\model\app\vip\CardPrivilege::checkVipGoods($item['id']);
|
|
}
|
|
if($groupId){
|
|
$item->getRelation('course')->visible(['name','cover','price','price_marking','pay_type','type','id','briefing','is_vip_goods']);
|
|
$courseData = $item['course']->toArray();
|
|
$buyData = \app\common\model\course\Course::getPrice($courseData['id']);
|
|
$buyData['type_text'] = __('type.'.$courseData['type']);
|
|
$courseData = array_merge($courseData,$buyData);
|
|
|
|
$goodsType = $courseData['type'] ?? '';
|
|
$isVirtualPay = \app\common\library\pay\VirtualPayService::isUseVirtualPay($goodsType);
|
|
$courseData['is_virtual_pay'] = $isVirtualPay ? 1 : 0;
|
|
if ($isVirtualPay) {
|
|
$courseData = \app\common\library\pay\VirtualPayService::convertPriceFields($courseData, ['price', 'price_marking']);
|
|
}
|
|
|
|
$data[] = $courseData;
|
|
}else{
|
|
$item->visible(['name','cover','price','price_marking','pay_type','type','id','briefing','is_vip_goods']);
|
|
$itemData = $item->toArray();
|
|
$buyData = \app\common\model\course\Course::getPrice($itemData['id']);
|
|
$itemData = array_merge($itemData,$buyData);
|
|
|
|
$goodsType = $itemData['type'] ?? '';
|
|
$isVirtualPay = \app\common\library\pay\VirtualPayService::isUseVirtualPay($goodsType);
|
|
$itemData['is_virtual_pay'] = $isVirtualPay ? 1 : 0;
|
|
if ($isVirtualPay) {
|
|
$itemData = \app\common\library\pay\VirtualPayService::convertPriceFields($itemData, ['price', 'price_marking']);
|
|
}
|
|
|
|
$data[] = $itemData;
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 通过多个分组ID获取课程列表
|
|
* @param array $groupIds 分组ID数组
|
|
* @param array $params 分页信息 page页码 limit获取数量
|
|
* @return array
|
|
*/
|
|
public function getCourseListByGroupIds($groupIds = [], $params = [])
|
|
{
|
|
if (empty($groupIds)) {
|
|
return [];
|
|
}
|
|
|
|
$where = [
|
|
'course.status' => 1,
|
|
'sales_type' => ['like', "%alone%"],
|
|
'hide' => 0
|
|
];
|
|
|
|
$orderField = 'course.createtime';
|
|
if (isset($params['sort']) && !empty($params['sort'])) {
|
|
if ($params['sort'] == 'time') {
|
|
$orderField = 'course.createtime';
|
|
} else {
|
|
$orderField = 'course.views';
|
|
}
|
|
}
|
|
|
|
if (isset($params['searchKey']) && !empty($params['searchKey'])) {
|
|
$where['course.name'] = ['like', "%{$params['searchKey']}%"];
|
|
}
|
|
|
|
if (isset($params['type']) && !empty($params['type'])) {
|
|
$where['course.type'] = $params['type'];
|
|
}
|
|
|
|
$query = self::with(['course'])
|
|
->where('group_bind.group_id', 'in', $groupIds)
|
|
->where($where);
|
|
|
|
if (isset($params['limit']) && isset($params['page'])) {
|
|
$query = $query->limit($params['limit'])->page($params['page']);
|
|
}
|
|
|
|
$list = $query
|
|
->order('group_bind.sort', 'desc')
|
|
->order($orderField, 'desc')
|
|
->group('course.id')
|
|
->select();
|
|
|
|
$data = [];
|
|
|
|
if (!empty($list)) {
|
|
$vipAppConfig = \app\common\model\app\Config::getConfig('vip');
|
|
|
|
foreach ($list as $item) {
|
|
$item['is_vip_goods'] = false;
|
|
if ($vipAppConfig['status'] == 1) {
|
|
$item['is_vip_goods'] = \app\api\model\app\vip\CardPrivilege::checkVipGoods($item['id']);
|
|
}
|
|
$item->getRelation('course')->visible(['name', 'cover', 'price', 'price_marking', 'pay_type', 'type', 'id', 'briefing', 'is_vip_goods']);
|
|
$courseData = $item['course']->toArray();
|
|
$buyData = \app\common\model\course\Course::getPrice($courseData['id']);
|
|
$buyData['type_text'] = __('type.' . $courseData['type']);
|
|
$courseData = array_merge($courseData, $buyData);
|
|
|
|
$goodsType = $courseData['type'] ?? '';
|
|
$isVirtualPay = \app\common\library\pay\VirtualPayService::isUseVirtualPay($goodsType);
|
|
$courseData['is_virtual_pay'] = $isVirtualPay ? 1 : 0;
|
|
if ($isVirtualPay) {
|
|
$courseData = \app\common\library\pay\VirtualPayService::convertPriceFields($courseData, ['price', 'price_marking']);
|
|
}
|
|
|
|
$data[] = $courseData;
|
|
}
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
}
|