UsageController.class.php 4.13 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhoutao
 * Date: 2018/2/5
 * Time: 下午5:02
 */

namespace Apicp\Controller\Room;

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

class UsageController extends AbstractController
{
    /**
     * Usage
     * @author zhoutao
     * @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 终止开始时间
     * @return array|bool 使用情况
            array(
                'page' => 1, // 页码
                'limit' => 5, // 每页记录数
                'total' => 20, // 列表记录总数
                'list' => array(
                    array(
                        'lm_id' => 1, // 直播ID
                        'name' => '产品培训讲解', // 直播名称
                        'watched_total' => 150, // 已观看直播人数
                        'range_total' => 200, // 可观看直播总人数
                        'start_time' => 1515207667910, // 开始时间
                        'end_time' => 1515207667910, // 结束时间
                        'online_peak' => 21, // 消耗并发
                        'end_concurrent_member_number' => 122 // 结束时企业还剩余并发人数 (-1 为还未统计)
                    ),
                ),
            )
     */
    public function Index_post()
    {
        // 验证规则
        $rules = [
            'page' => 'integer',
            'limit' => 'integer',
            'name' => 'max:30',
            'start_time_begin' => 'integer',
            'start_time_end' => 'integer',
        ];

        // 验证数据
        $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, '*', false, true);
        $list = [];
        // 记录总数不为空:有数据
        if ($total) {

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

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

        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'];
        }

        return $conds;
    }
}