EducationModel.class.php
9.31 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
/**
* 培训基础表Model
* Created by PhpStorm.
*/
namespace Common\Model;
use Common\Service\EducationService;
class EducationModel extends AbstractModel
{
// 库培训发布状态:草稿
const EDUCATION_PUBLISH_STATUS_DRAFT = 1;
// 库培训发布状态:已发布
const EDUCATION_PUBLISH_STATUS_PUBLISHED = 2;
// 库培训发布状态:已终止
const EDUCATION_PUBLISH_STATUS_STOP = 3;
// 培训进行状态:未开始
const EDUCATION_PROCESS_STATUS_UN_BEGIN = 1;
// 培训进行状态:进行中
const EDUCATION_PROCESS_ON_GOING = 2;
// 培训进行状态:已结束
const EDUCATION_PROCESS_END = 3;
// 培训进行状态:已终止
const EDUCATION_PROCESS_STOP = 4;
// 培训进行状态:草稿
const EDUCATION_PROCESS_DRAFT = 5;
// 搜索发布状态
const SEARCH_PUBLISH = 1;
// 搜索草稿
const SEARCH_DRAFT = 2;
public function __construct()
{
parent::__construct();
}
/**
* 更新已参加人数
* @author wanghuan
*
* @param int $ed_id 培训id
* @param int $type 更新类型:0=加1,1=减1
*
* @return bool
*/
public function update_joined($ed_id, $type)
{
// 减1
if ($type) {
return $this->_m->where('ed_id=' . $ed_id)->setDec('ed_joined_count', 1);
} else { // 加1
return $this->_m->where('ed_id=' . $ed_id)->setInc('ed_joined_count', 1);
}
}
/**
* 根据条件获取用户权限范围的培训总数
* @author wanghuan
*
* @param array $cond 查询条件
*
* @return array|bool
*/
public function user_education_count($cond = [])
{
// 条件
$where = [];
// 参数
$params = [];
if (!$this->parse_where_user_education($where, $params, $cond)) {
return false;
}
$sql = 'SELECT COUNT(e.ed_id) FROM __TABLE__ e LEFT JOIN oa_education_right_users u ON e.ed_id=u.ed_id WHERE ' . implode(' AND ',
$where);
return $this->_m->result($sql, $params);
}
/**
* 根据条件获取用户权限范围的培训列表
* @author wanghuan
*
* @param array $cond 查询条件
* @param null $page_option 分页
* @param array $order_option 排序
*
* @return array|bool
*/
public function user_education_list($cond = [], $page_option = null, $order_option = [])
{
// 条件
$where = [];
// 参数
$params = [];
// 参数解析
$this->parse_where_user_education($where, $params, $cond);
// 排序
$order_by = '';
if (!$this->_order_by($order_by, $order_option)) {
return false;
}
// 分页参数
$limit = '';
if (!$this->_limit($limit, $page_option)) {
return false;
}
$sql = 'SELECT e.* FROM __TABLE__ e LEFT JOIN oa_education_right_users u ON e.ed_id=u.ed_id WHERE ' . implode(' AND ',
$where) . $order_by . $limit;
return $this->_m->fetch_array($sql, $params);
}
/**
* 查询用户权限范围的培训列表sql参数解析
* @author wanghuan
*
* @param array $where sql查询条件
* @param array $params sql参数
* @param array $condition 传入参数
*
* @return bool
*/
protected function parse_where_user_education(&$where, &$params, $condition = [])
{
// 用户id
if ($condition['uid']) {
$where[] = 'u.ru_uid=?';
$params[] = $condition['uid'];
}
// 分类状态
if ($condition['ca_status']) {
$where[] = 'e.ca_status=?';
$params[] = $condition['ca_status'];
}
// 报名状态
if ($condition['ru_sign_status']) {
$where[] = 'u.ru_sign_status=?';
$params[] = $condition['ru_sign_status'];
}
// 报名状态
if ($condition['status']) {
// 未开始
if (EducationService::EDUCATION_PROCESS_STATUS_UN_BEGIN == $condition['status']) {
// 开始时间大于当前时间
$where[] = 'e.ed_begin_time >?';
$params[] = MILLI_TIME;
}
// 进行中
if (EducationService::EDUCATION_PROCESS_ON_GOING == $condition['status']) {
// 开始时间小于等于当前时间
$where[] = 'e.ed_begin_time <= ?';
$params[] = MILLI_TIME;
// 结束时间大于等于当前时间
$where[] = 'e.ed_end_time >= ?';
$params[] = MILLI_TIME;
}
// 已结束
if (EducationService::EDUCATION_PROCESS_END == $condition['status']) {
$where[] = 'e.ed_end_time < ?';
$params[] = MILLI_TIME;
}
}
// 企业标识
$where[] = 'e.domain=?';
$params[] = QY_DOMAIN;
// 状态
$where[] = 'e.status<?';
$params[] = self::ST_DELETE;
// 企业标识
$where[] = 'u.domain=?';
$params[] = QY_DOMAIN;
// 状态
$where[] = 'u.status<?';
$params[] = self::ST_DELETE;
return true;
}
/**
* 拼装 培训列表条件组装 where语句
*
* @author: 蔡建华
*
* @param array $params 查询条件参数
*
* @return string
*/
public function get_search_where($params = [])
{
// 组装查询语句
$where = ' status<' . self::ST_DELETE . " AND domain= '" . QY_DOMAIN . "'";
$search_key = trim($params['search_key']);
// 如果标题不为空
if (!empty($search_key)) {
$search_key = str_replace("%", '\%', $search_key);
$where .= " AND ed_name like '%" . $search_key . "%' ";
}
// 培训时间范围
if (!empty($params['begin_time']) && $params['begin_time'] != 'NaN' && $params['end_time'] != 'NaN') {
$where .= ' AND ((ed_end_time > ' . $params['begin_time'] . ' AND ed_begin_time < ' . $params['end_time'] . ') OR (ed_begin_time<' . $params['end_time'] . ' AND ed_end_time = 0 ))';
}
// 分类不为空
if (!empty($params['ca_id'])) {
$where .= ' AND ca_id=' . $params['ca_id'];
}
// 培训状态不为空
if (self::SEARCH_DRAFT == $params['type']) {
// 如果是草稿的时候执行
$where .= ' AND ed_status =' . self::EDUCATION_PUBLISH_STATUS_DRAFT;
} else {
// 不是草稿的时候执行
if (!empty($params['ed_status'])) {
switch ($params['ed_status']) {
// 草稿5
case self::EDUCATION_PROCESS_DRAFT:
$where .= ' AND ed_status =' . self::EDUCATION_PUBLISH_STATUS_DRAFT;
break;
// 未开始1
case self::EDUCATION_PROCESS_STATUS_UN_BEGIN:
$where .= ' AND ed_begin_time >' . MILLI_TIME . ' AND ed_status =' . self::EDUCATION_PUBLISH_STATUS_PUBLISHED;
break;
// 已开始2
case self::EDUCATION_PROCESS_ON_GOING:
$where .= ' AND ed_status =' . self::EDUCATION_PUBLISH_STATUS_PUBLISHED . ' AND ed_begin_time<' . MILLI_TIME . ' AND ed_end_time>=' . MILLI_TIME;
break;
// 已结束3
case self::EDUCATION_PROCESS_END:
$where .= ' AND ((ed_status =' . self::EDUCATION_PUBLISH_STATUS_PUBLISHED . ' AND ed_end_time> 0 AND ed_end_time<' . MILLI_TIME . ') OR ed_status =' . self::EDUCATION_PUBLISH_STATUS_STOP . ')';
break;
default:
}
}
if (self::SEARCH_PUBLISH == $params['type']) {
$where .= ' AND ed_status !=' . self::EDUCATION_PUBLISH_STATUS_DRAFT;
}
}
return $where;
}
/**
* 根据条件查询培训总数
*
* @author: 蔡建华
*
* @param $data array 查询条件
*
* @return int|mixed
*/
public function count_by_education($data = [])
{
$where = $this->get_search_where($data);
$sql = 'SELECT COUNT(*) FROM __TABLE__ WHERE ' . $where;
return $this->_m->result($sql, []);
}
/**
* 根据条件查询培训列表
*
* @author: 蔡建华
*
* @param $data array 查询条件
* @param null $page_option 分页参数
* @param array $order_option 排序参数
* @param string $fields 查询的字段
*
* @return array|bool
*/
public function list_by_education($data, $page_option = null, $order_option = [], $fields = ' * ')
{
$where = $this->get_search_where($data);
// 排序
$orderby = '';
if (!$this->_order_by($orderby, $order_option)) {
return false;
}
// 分页参数
$limit = '';
if (!$this->_limit($limit, $page_option)) {
return false;
}
$sql = "SELECT {$fields} FROM __TABLE__ WHERE " . $where . " {$orderby}{$limit}";
// 读取记录
return $this->_m->fetch_array($sql, []);
}
}