ListController.class.php 6.81 KB
<?php
/**
 * 直播活动列表接口
 * Created by PhpStorm.
 * User: yingcai
 * Date: 2018/1/9
 * Time: 下午3:16
 */

namespace Apicp\Controller\Room;

use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\Order;
use Common\Service\MainService;
use Common\Service\StudioFileService;
use Common\Service\StudioPlaybackService;

class ListController extends AbstractController
{
    /**
     * List
     * @author houyingcai
     * @desc 直播活动列表接口
     * @param Int page:false:1 页码
     * @param Int limit:false:20 每页记录数
     * @param String name:false 名称关键词
     * @param Int start_time_begin:false 起始开始时间
     * @param Int start_time_end:false 终止开始时间
     * @param Int live_status:false 活动状态
     * @return array|bool 直播列表
                array(
                    'page' => 1, // 页码
                    'limit' => 5, // 每页记录数
                    'total' => 20, // 列表记录总数
                    'list' => array(
                        array(
                            'lm_id' => 1, // 直播ID
                            'name' => '产品培训讲解', // 直播名称
                            'pic' => 'b3ddbc502e307665f346cbd6e52cc10d', // 封面ID
                            'pic_url' => 'http://qy.vchangyi.org', // 封面图片URL
                            'live_status' => 1, // 直播状态(1=草稿;2=未开始;3=进行中;4=已结束)
                            'watched_total' => 150, // 已观看直播人数
                            'range_total' => 200, // 可观看直播总人数
                            'updated' => 1515407868910, // 最后更新时间
                            'start_time' => 1515207667910, // 开始时间
                            'video_url' => 'http://qy.vchangyi.org', // 视频回放地址
                            'has_playback' => true, // 是否有回放 true 有 false 无
                        ),
                    ),
                )
     */
    public function Index_post()
    {
        // 验证规则
        $rules = [
            'page' => 'integer',
            'limit' => 'integer',
            'name' => 'max:30',
            'start_time_begin' => 'integer',
            'start_time_end' => 'integer',
            'live_status' => 'integer|in:1,2,3,4',
        ];

        // 验证数据
        $validate = new PackageValidate($rules, [], array_keys($rules));
        $postData = $validate->postData;

        // 分页默认值
        $page = isset($postData['page']) ? $postData['page'] : Constant::PAGING_DEFAULT_PAGE;
        $limit = isset($postData['limit']) ? $postData['limit'] : Constant::PAGING_DEFAULT_LIMIT;
        list($start, $limit) = page_limit($page, $limit);

        // 组合搜索条件
        $conds = $this->assemble_where($postData);

        // 获取记录总数
        $mainService = new MainService();
        $total = $mainService->count_by_conds($conds);
        $list = [];
        // 记录总数不为空:有数据
        if ($total) {

            // 分页参数
            $pageOption = [$start, $limit];
            // 排序
            $orderOption = [
                'updated'=> 'DESC',
                'created' => 'DESC',
            ];
            // 查询字段
            $fields = 'lm_id,name,pic,live_status,start_time,watched_total,range_total,updated,created';
            // 获取列表数据
            $list = $mainService->list_by_conds($conds, $pageOption, $orderOption, $fields);

            // 获取对应的回放地址
            $studioPlaybackServ = new StudioPlaybackService();
            $playBackList = $studioPlaybackServ->list_by_conds(['lm_id' => array_column($list, 'lm_id')]);
            $lmIdWithPlayBack = [];
            foreach ($playBackList as $item) {
                // 根据 lm_id 归总 并把序号作为 key 后面再拿来排序用
                $lmIdWithPlayBack[$item['lm_id']][$item['number']] = $item['sd'];
            }

            // 格式化列表数据
            foreach ($list as &$v) {
                // 封面图URL
                $v['pic_url'] = $mainService->formatCover($v['pic']);
                // 转换状态
                $v['live_status'] = $mainService->live_status($v['live_status'], $v['start_time']);
                // 最后更新时间
                $v['updated'] = $v['updated'] ? $v['updated'] : $v['created'];
                // 视频回放地址
                ksort($lmIdWithPlayBack[$v['lm_id']]);
                $v['video_url'] = !empty($lmIdWithPlayBack[$v['lm_id']]) ?
                    array_values($lmIdWithPlayBack[$v['lm_id']]) : [];
                // 是否有视频回放
                $v['has_playback'] = !empty($lmIdWithPlayBack);
            }
        }

        // 判断是否还有一条直播活动
        $hasAnotherLive = $mainService->get_by_conds(['live_status' => Constant::LIVE_STATUS_PUBLISHED]);

        // 返回结果
        $this->_result = [
            'page' => intval($page),
            'limit' => intval($limit),
            'total' => intval($total),
            'list' => $list,
            'can_create' => empty($hasAnotherLive)
        ];

        return true;
    }

    /**
     * @desc 组合搜索条件
     * @param array $postData 搜索条件
     * @return array
     */
    private function assemble_where($postData)
    {
        $conds = [];
        if (isset($postData['name'])) {

            $postData['name'] = str_replace("%", '\%', $postData['name']);
            $conds['name like ?'] = '%' . $postData['name'] . '%';
        }

        if (isset($postData['start_time_begin']) && isset($postData['start_time_end'])) {

            $conds['start_time >= ?'] = $postData['start_time_begin'];
            $conds['start_time <= ?'] = $postData['start_time_end'];
        }

        // 直播状态不为空
        if (isset($postData['live_status'])) {

            switch ($postData['live_status']) {
                // 草稿
                case Constant::LIVE_WEB_STATUS_DRAFT:
                    $conds['live_status'] = Constant::LIVE_STATUS_DEAFT;
                    break;
                // 未开始
                case Constant::LIVE_WEB_STATUS_NOT_START:
                    $conds['start_time > ?'] = MILLI_TIME;
                    $conds['live_status'] = Constant::LIVE_STATUS_PUBLISHED;
                    break;
                // 进行中
                case Constant::LIVE_WEB_STATUS_ING:
                    $conds['live_status'] = Constant::LIVE_STATUS_PUBLISHED;
                    $conds['start_time < ?'] = MILLI_TIME;
                    break;
                //  已结束
                case Constant::LIVE_WEB_STATUS_END:
                    $conds['live_status'] = Constant::LIVE_STATUS_OVER;
                    break;
                default:
            }
        }

        return $conds;
    }
}