<?php /** * 获取试卷列表 * ListController.class.php * User: daijun * Date: 2017年09月05日 */ namespace Rpc\Controller\Train; use Common\Service\CategoryService; use Common\Service\PaperService; class ListController extends AbstractController { public function Index() { // 获取参数 $params = $this->_params; // 默认值 $page = !empty($params['page']) ? intval($params['page']) : 1; $limit = !empty($params['limit']) ? intval($params['limit']) : 10; list($start, $limit) = page_limit($page, $limit); // 分页参数 $page_option = array($start, $limit); // 排序参数 $order_option = array('ep_id' => 'DESC'); // 考试类型为线下培训 $conds['exam_type'] = PaperService::TRAIN_TYPE; // 试卷状态为已发布 $conds['exam_status'] = PaperService::PAPER_PUBLISH; if (!empty($params['class_id'])) { // 如果分类ID不为空,则查询分类下的试卷 $conds['ec_id'] = $params['class_id']; } if (!empty($params['keyword'])) { // 如果试卷名称关键字不为空,则模糊查询试卷名称 $conds['ep_name like ?'] = '%' . trim($params['keyword']) . '%'; } if (!empty($params['ids'])) { // 如果已使用过IDS存在 $conds['ep_id NOT IN(?)'] = $params['ids']; } // 实例化service $service = new PaperService(); $field = 'ep_id,ec_id,exam_type,ep_name'; $list = []; // 获取总记录数 $total = $service->count_by_conds($conds); if ($total > 0) { // 查询列表 $list = $service->list_by_conds($conds, $page_option, $order_option, $field); } $result = [ 'page' => $page, 'limit' => $limit, 'total' => intval($total), 'list' => $this->format_list($list) ]; return $result; } /** * 格式化列表数据 * @param array $list * @return array */ private function format_list($list = []) { $res_list = []; if (empty($list)) { return $res_list; } // 实例化分类service $cate_serv = new CategoryService(); // 获取分类列表 $cate_list = $cate_serv->list_by_conds([]); // 将分类列表转换成以分类ID为key的二维数组 $cate_list = array_combine_by_key($cate_list, 'ec_id'); // 循环格式化数据 foreach ($list as $v) { $cate_data = $cate_list[$v['ec_id']]; $arr = array( 'app_data_id' => intval($v['ep_id']), 'title' => $v['ep_name'], 'class_name' => $cate_data['ec_name'], 'author' => '', 'status' => '进行中' ); $res_list[] = $arr; } return $res_list; } }