PaperListController.class.php 2.47 KB
<?php
/**
 * 【考试中心-后台】获取激励试卷列表
 * @author: houyingcai
 * @email:  594609175@qq.com
 * @date :  2017-06-01 17:47:52
 * @version $Id$
 */

namespace Apicp\Controller\Encourage;

use Common\Service\PaperService;
use Common\Service\CategoryService;

class PaperListController extends AbstractController
{

    /** @var  PaperService  实例化试卷表对象 */
    protected $paper_service;

    /** @var  CategoryService 实例化试卷分类表对象 */
    protected $cate_service;

    public function before_action($action = '')
    {
        if (!parent::before_action($action)) {
            return false;
        }

        $this->paper_service = new PaperService();
        $this->cate_service = new CategoryService();

        return true;
    }

    public function Index_post()
    {
        $params = I('post.');

        // 每页条数
        $limit = empty($params['limit']) ? 10 : intval($params['limit']);
        $page = empty($params['page']) ? 1 : $params['page'];

        list($start, $limit, $page) = page_limit($page, $limit);
        // 分页参数
        $page_option = [$start, $limit];

        $conds = [
            'cate_status' => PaperService::EC_OPEN_STATES,
            'exam_status' => PaperService::PAPER_PUBLISH,
            'end_time > ?' => MILLI_TIME
        ];

        if (!empty($params['ec_id'])) {
            // 分类id不为空
            $conds['ec_id'] = intval($params['ec_id']);
        }

        if (!empty($params['title'])) {
            // 标题关键字不为空
            $conds['ep_name like ?'] = '%' . $params['title'] . '%';
        }

        // 查询总数
        $count = $this->paper_service->count_by_conds($conds);

        $paper_list = [];
        if ($count) {
            // 排序参数,最后更新时间倒序
            $order_option = ['last_time' => 'DESC'];
            // 查询列表
            $paper_list = $this->paper_service->list_by_conds($conds, $page_option, $order_option, 'ep_id,ep_name,ec_id');
        }

        $cate_list = $this->cate_service->list_all();

        $cate_list = array_combine_by_key($cate_list, 'ec_id');

        foreach ($paper_list as &$v) {

            $v['ep_id'] = intval($v['ep_id']);
            $v['ec_name'] = $cate_list[$v['ec_id']]['ec_name'];
        }

        $this->_result = [
            'page' => intval($page),
            'limit' => intval($limit),
            'total' => $count,
            'list' => $paper_list
        ];

        return true;
    }
}