ListController.class.php
2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* 获取试卷列表
* ListController.class.php
* User: daijun
* Date: 2017年09月05日
*/
namespace Rpc\Controller\TaskCenter;
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::TASK_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['app_data_ids'])) {
// 如果已使用过IDS存在
$conds['ep_id NOT IN(?)'] = $params['app_data_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_all();
// 将分类列表转换成以分类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;
}
}