ListController.class.php
4.79 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
* 【手机端】01-培训列表接口
* Created by PhpStorm.
* @author:wanghuan
* @date:2017-08-29
*/
namespace Api\Controller\Education;
use Common\Service\EducationService;
use Common\Service\CategoryService;
use Common\Service\RightUsersService;
class ListController extends \Api\Controller\AbstractController
{
/** @var EducationService */
protected $edu_s;
/** @var CategoryService */
protected $cate_s;
/** @var RightUsersService */
protected $right_users_s;
public function before_action($action = '')
{
if (!parent::before_action($action)) {
return false;
}
// 实例化培训service
$this->edu_s = new EducationService();
// 实例化培训分类service
$this->cate_s = new CategoryService();
// 实例化员工名单service
$this->right_users_s = new RightUsersService();
return true;
}
/**
* 培训列表
*/
public function Index_post()
{
// 接收post参数
$params = I('post.');
// 判断分页参数是否为空,为空赋默认值
$page = !empty($params['page']) ? intval($params['page']) : self::DEFAULT_PAGE;
$limit = !empty($params['limit']) ? intval($params['limit']) : self::DEFAULT_LIMIT;
// 分页
list($start, $limit) = page_limit($page, $limit);
// 培训查询条件:培训分类开启、培训已发布或已终止
$cond = [
'uid' => $this->uid,
'ca_status' => CategoryService::CATEGORY_OPEN,
'ed_status>?' => EducationService::EDUCATION_PUBLISH_STATUS_DRAFT
];
// 数据类型参数不为空:我的培训(个人中心中的我参加的培训)
if ($params['type']) {
// 查询条件追加已报名状态
$cond['ru_sign_status'] = RightUsersService::SIGN_STATUS_SIGNED;
}
// 如果需要查询状态
if (!empty($params['status'])) {
$cond['status'] = $params['status'];
}
// 获取记录总数
$total = $this->edu_s->user_education_count($cond);
$list = [];
// 记录总数不为空,查询列表数据
if ($total) {
// 分页参数
$page_option = [$start, $limit];
// 排序:培训开始时间倒序
$order_option = [
'ed_begin_time' => 'DESC'
];
// 获取列表数据
$list = $this->edu_s->user_education_list($cond, $page_option, $order_option);
}
if (!empty($list)) {
// 数据格式化
$list = $this->format_data($list);
}
// 返回结果
$this->_result = [
'total' => intval($total),
'page' => intval($page),
'limit' => intval($limit),
'list' => $list
];
}
/**
* 培训数据格式化
*
* @param array $data 待格式化数据
*
* @return array 格式化后数据
*/
protected function format_data($data)
{
// 分类状态:启用
$cate_cond['ca_status'] = CategoryService::CATEGORY_OPEN;
// 获取分类数据
$category_list = $this->cate_s->list_by_conds($cate_cond);
$category_list = array_combine_by_key($category_list, 'ca_id');
// 员工名单查询条件
$right_cond['ru_uid'] = $this->uid;
// 查询用户报名数据
$right_users = $this->right_users_s->list_by_conds($right_cond);
$right_users = array_combine_by_key($right_users, 'ed_id');
$result = [];
foreach ($data as $key => $val) {
// 分类详情
$category = $category_list[$val['ca_id']];
// 分类名称
$ca_name = '';
if (!empty($category)) {
$ca_name = $category['ca_name'];
}
// 培训进行状态
$ed_show_status = $this->edu_s->get_ed_status($val);
// 查询用户报名数据
$right_users_info = $right_users[$val['ed_id']];
// 用户报名状态
$sign_up_status = $this->right_users_s->get_sign_up_status($val, $right_users_info);
$result[$key] = [
'ed_id' => intval($val['ed_id']),
'ed_name' => $val['ed_name'],
'ca_name' => $ca_name,
'ed_cover_url' => CategoryService::format_cover($val['ed_cover_id']),
'ed_begin_time' => $val['ed_begin_time'],
'ed_status' => intval($ed_show_status),
'ed_joined_count' => intval($val['ed_joined_count']),
'ed_address' => $val['ed_address'],
'sign_up_status' => $sign_up_status
];
}
return $result;
}
}