WatchedController.class.php 4.3 KB
<?php
/**
 * 【后台】直播活动观看情况接口
 * Created by PhpStorm.
 * User: yingcai
 * Date: 2018/1/10
 * Time: 下午2:18
 */

namespace Apicp\Controller\Room;

use Com\PackageValidate;
use Common\Common\Constant;
use Common\Service\MainService;
use Common\Service\ParticipateService;
use Common\Service\RangeService;

class WatchedController extends AbstractController
{
    /**
     * Watched
     * @author houyingcai
     * @desc 直播活动观看情况接口
     * @param Int page:false:1 页码
     * @param Int limit:false:20 每页记录数
     * @param Int lm_id:true:1 直播ID
     * @param Int watch_type:true:1 观看列表类型(1=已观看;2=未观看)
     * @return array|bool 直播列表
                array(
                    'page' => 1, // 页码
                    'limit' => 5, // 每页记录数
                    'total' => 20, // 记录总数
                    'watched_total' => 1, // 已观看人数
                    'unwatched_total' => 7, // 未观看人数
                    'list' => array(
                        array(
                            'memUid' => 'B9EB18FE7F0000016EA88956AAE8B326', // 人员UID
                            'memUsername' => '江潮', // 人员姓名
                            'dpName' => '技术中心', // 组织名称
                            'roleName' => '前端工程师', // 角色名称
                            'tagName' => '外贸', // 标签名称
                            'jobName' => '技术员', // 岗位名称
                            'telephone' => '18721492903', // 手机号
                            'last_watched_time' => 1515407868910, // 最后观看时间(只有已观看列表才存在)
                        ),
                    ),
                )
     */
    public function Index_post()
    {
        // 验证数据
        $validate = new PackageValidate(
            [
                'page' => 'integer',
                'limit' => 'integer',
                'lm_id' => 'require|integer',
                'watch_type' => 'require|integer|in:1,2',
            ], [
                'lm_id.require' => L('_EMPTY_LIVE_ID'),
                'watch_type' => L('_ERR_WATCH_TYPE_INVALID'),
            ], [
                'page',
                'limit',
                'lm_id',
                'watch_type',
            ]
        );
        $postData = $validate->postData;

        // 获取直播信息
        $mainService = new MainService();
        $mainDetail = $mainService->get($postData['lm_id']);

        // 直播信息不存在
        if (empty($mainDetail)) {

            E('_ERR_LIVE_IS_NOT_EXIST');
        }

        // 直播已观看、未观看、可观看人员UID
        $rangeServ = new RangeService();
        list($uids_watched, $uids_unwatch, $uids_all) = $rangeServ->getWatchDataUids($postData['lm_id']);

        // 可观看人员数与主表缓存的可观看人员数不相符
        $range_total = count($uids_all);
        if ($range_total != $mainDetail['range_total']) {
            // 更新主表中的可观看人员数
            $mainService->update($postData['lm_id'], ['range_total' => $range_total]);
        }

        // 初始化数据
        $list = [];
        $total = 0;
        // 已观看人员总数
        $watched_total = count($uids_watched);
        // 未观看人员总数
        $unwatch_total = count($uids_unwatch);

        $participateService = new ParticipateService();

        // 已观看人员列表
        if (Constant::LIVE_WATCH_TYPE_TRUE == $postData['watch_type']) {

            $total = $watched_total;
            $list = $participateService->getWatchedData($postData);
        } elseif(Constant::LIVE_WATCH_TYPE_FALSE == $postData['watch_type']) {

            // 未观看人员列表
            $total = $unwatch_total;
            $list = $participateService->getUnwatchData($postData, $uids_unwatch);
        }

        $this->_result = [
            'watched_total' => $watched_total,
            'unwatched_total' => $unwatch_total,
            'total' => $total,
            'limit' => !empty($postData['page']) ? intval($postData['page']) : Constant::PAGING_DEFAULT_PAGE,
            'page' => !empty($postData['limit']) ? intval($postData['limit']) : Constant::PAGING_DEFAULT_LIMIT,
            'list' => $list
        ];

        return true;
    }

}