<?php /** * Created by PhpStorm. * User: pw * Date: 18/5/29 * Time: 16:27 */ namespace Rpc\Controller\OtherClass; use Com\PackageValidate; use Common\Common\Constant; use Common\Service\CategoryService; use Common\Service\PaperService; class ListController extends AbstractController { /** * List * @author pw * @desc 获取考试列表接口 * @param Int page:1 页码 * @param Int limit:20 每页记录数 * @param Int ec_id 分类ID * @param String keyword 标题关键字 * @return array array( 'page' => 1, // 页码 'limit' => 20, // 每页记录数 'total' => 200, // 记录总数 'list' => array( array( 'app_data_id' => '8852ce5784c43e6dddd942ae12d78fad', // 数据ID 'title' => '科目一', // 标题 'class_name' => '驾考', // 分类名称 'author' => '张三', // 发布人 'status' => '已发布', // 状态 ), ), ) */ public function Index() { $postData = $this->_params; // 验证规则 $rules = [ 'page' => 'integer', 'limit' => 'integer', 'class_id' => 'integer', 'keyword' => 'max:64', ]; // 验证请求数据 $validate = new PackageValidate(); $validate->postData = $postData; $validate->validateParams($rules); // 分页 $postData['page'] = isset($postData['page']) ? $postData['page'] : Constant::PAGING_DEFAULT_PAGE; $postData['limit'] = isset($postData['limit']) ? $postData['limit'] : Constant::PAGING_DEFAULT_LIMIT; list($start, $perpage) = page_limit($postData['page'], $postData['limit']); // 条件 $conds = [ 'exam_type' => Constant::EXAM_TYPE_OTHER, 'exam_status' => Constant::EXAM_STATUS_SEND ]; if (strlen($postData['keyword']) > 0) { $conds['ep_name like ?'] = '%' . $postData['keyword'] . '%'; } $classServ = new CategoryService(); if ($postData['class_id'] != 0) { // 无限级取出当前分类及所有已启用的子分类ID $classIds = $classServ->getChildClassIds($postData['class_id']); $conds['ec_id'] = $classIds; } else { // 最新考试列表,排除被禁用的分类 $isOpenClassIds = $classServ->getOpenClassIds(); $conds['ec_id'] = $isOpenClassIds; } // 排序条件 $order_option = ['updated' => 'desc']; // 过滤已被使用的考试 $be_used_ep_ids = $postData['app_data_ids']; if ($be_used_ep_ids && !empty($be_used_ep_ids)) { $conds['ep_id NOT IN (?)'] = $be_used_ep_ids; } // 考试列表 $list = []; $examServ = new PaperService(); $examList = $examServ->list_by_conds($conds, [$start, $perpage], $order_option); if (!empty($examList)) { // 分类数据 $classIds = array_column($examList, 'ec_id'); $classList = $classServ->list_by_conds(['ec_id' => $classIds]); if (!empty($classList)) { $classList = array_combine_by_key($classList, 'ec_id'); } foreach ($examList as $exam) { $list[] = [ 'app_data_id' => $exam['ep_id'], 'title' => $exam['ep_name'] ? $exam['ep_name'] : '', 'class_name' => isset($classList[$exam['ec_id']]) ? $classList[$exam['ec_id']]['ec_name'] : '', 'author' => $exam['launch_man'] ? $exam['launch_man'] : '', 'status' => '已发布', ]; } // 总数 $total = $examServ->count_by_conds($conds); } return [ 'page' => $postData['page'], 'limit' => $postData['limit'], 'total' => isset($total) ? $total : 0, 'list' => $list, ]; } }