ListController.class.php
4.22 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
120
121
122
123
<?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,
];
}
}