ActivityModel.class.php
8.2 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
<?php
namespace Common\Model;
class ActivityModel extends AbstractModel
{
// 活动数据状态:草稿
const ACTIVITY_DRAFT = 0;
// 活动数据状态:已发布
const ACTIVITY_PUBLISH = 1;
// 活动数据状态:提前终止
const ACTIVITY_STOP = 2;
// 推送消息
const NOTICE_ON = 1;
// 全公司
const ACTIVITY_COMPANY_ALL = 1;
// 活动综合状态 :草稿
const STATUS_DRAFT = 1;
// 活动综合状态 :未开始
const STATUS_NOT_START = 2;
// 活动综合状态 :进行中
const STATUS_ING = 3;
// 活动综合状态 :已结束
const STATUS_END = 4;
// 活动综合状态 :已终止
const STATUS_STOP = 5;
//时间为空
const TIME_EMPTY = 0;
/**
* 构造方法
*/
public function __construct()
{
parent::__construct();
}
/**
* 查询总数
* @param array $params 条件参数
* @return int|mixed
*/
public function count_by_where($params = [])
{
$where = $this->get_where($params);
$sql = 'SELECT COUNT(*) FROM __TABLE__ WHERE ' . $where;
return $this->_m->result($sql, []);
}
public function get_where($params = [])
{
// 组装查询语句
$where = ' status<' . ActivityModel::ST_DELETE . " AND domain= '" . QY_DOMAIN . "'";
if (!empty($params['subject'])) {
$params['subject'] = str_replace("%", '\%', $params['subject']);
// 如果标题不为空
$where .= " AND subject like '%" . trim($params['subject']) . "%' ";
}
// 活动时间范围
if (!empty($params['begin_time']) && $params['begin_time'] != 'NaN' && $params['end_time'] != 'NaN') {
$where .= ' AND ((end_time > ' . $params['begin_time'] . '&& begin_time < ' . $params['end_time'] . ')
OR (begin_time<' . $params['end_time'] . '&& end_time = 0 ))';
}
if (!empty($params['last_begin_time'])) {
$where .= ' AND last_time >' . $params['last_begin_time'];
}
if (!empty($params['last_end_time'])) {
$where .= ' AND last_time <' . $params['last_end_time'];
}
if (!empty($params['activity_status'])) {
switch ($params['activity_status']) {
// 草稿
case self::STATUS_DRAFT:
$where .= ' AND activity_status =' . ActivityModel::ACTIVITY_DRAFT;
break;
// 未开始
case self::STATUS_NOT_START:
$where .= ' AND activity_status =' . ActivityModel::ACTIVITY_PUBLISH . ' AND begin_time>' . MILLI_TIME;
break;
// 进行中
case self::STATUS_ING:
$where .= ' AND activity_status =' . ActivityModel::ACTIVITY_PUBLISH . ' AND begin_time<' . MILLI_TIME . ' AND (end_time>=' . MILLI_TIME . ' OR end_time=0)';
break;
// 已结束
case self::STATUS_END:
$where .= ' AND ((activity_status=' . ActivityModel::ACTIVITY_PUBLISH . ' AND end_time>' . self::TIME_EMPTY . ' AND end_time<' . MILLI_TIME . ') OR (activity_status=' . ActivityModel::ACTIVITY_STOP . '))';
break;
// 已终止
// case self::STATUS_STOP:
// $where .= ' AND activity_status=' . ActivityModel::ACTIVITY_STOP;
//
// break;
default:
}
}
// 如果搜索类型
if (!empty($params['activity_type'])) {
$where .= ' AND activity_type =' . $params['activity_type'];
}
return $where;
}
/**
* 查询列表
* @param array $params 条件参数
* @param null $page_option 分页参数
* @param array $order_option 排序参数
* @param string $fields 查询的字段
* @return array|bool
*/
public function list_by_where($params = [], $page_option = null, $order_option = [], $fields = '*')
{
$where = $this->get_where($params);
// 排序
$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, []);
}
/**
* 查询总数
* @param $data array 查询条件
* @return int|mixed
*/
public function count_by_active($data = [])
{
list($where, $params) = $this->get_where_active($data);
$sql = 'SELECT COUNT(*) FROM __TABLE__ WHERE ' . $where;
return $this->_m->result($sql, $params);
}
/**
* 查询列表
* @param $data array 查询条件
* @param null $page_option 分页参数
* @param array $order_option 排序参数
* @param string $fields 查询的字段
* @return array|bool
*/
public function list_by_active($data, $page_option = null, $order_option = [], $fields = '*')
{
list($where, $params) = $this->get_where_active($data);
// 排序
$orderby = '';
if (!$this->_order_by($orderby, $order_option)) {
return false;
}
$orderby .= ' ,ABS(cast(UNIX_TIMESTAMP()*1000 as signed) - cast(begin_time as signed)) asc ';
// 分页参数
$limit = '';
if (!$this->_limit($limit, $page_option)) {
return false;
}
$sql = "SELECT {$fields} FROM __TABLE__ WHERE " . $where . " {$orderby}{$limit} ";
return $this->_m->fetch_array($sql, $params);
}
/**
* 拼接Sql语句
* @param array $data 查询条件
* @return array
*/
public function get_where_active($data = [])
{
// 组装查询语句
$where = " activity_type=? AND status <? AND domain=?";
// 查询活动类型为常规类型
$params[] = ActivityModel::ST_CREATE;
// 操作状态和域名
$params[] = ActivityModel::ST_DELETE;
$params[] = QY_DOMAIN;
// 活动状态
$where .= " and activity_status > ? ";
$params[] = ActivityModel::ACTIVITY_DRAFT;
$params[] = ActivityModel::ACTIVITY_COMPANY_ALL;
// 权限判断
$rightModel = new RightModel();
$table = $rightModel->get_tname();
$right = $data['right'];
if (!empty($right)) {
$where_right = " 0 ";
$params[] = ActivityModel::ST_DELETE;
$params[] = QY_DOMAIN;
if (!empty($right['memID'])) {
$where_right .= " OR uid =? ";
$params[] = $right['memID'];
}
//部门
if (!empty($right['dpIds'])) {
$where_right .= " OR `dp_id` IN (?) ";
$params[] = $right['dpIds'];
}
// 岗位
if (!empty($right['jobIds'])) {
$where_right .= "OR `job_id` IN (?) ";
$params[] = $right['jobIds'];
}
// 角色
if (!empty($right['roleIds'])) {
$where_right .= "OR `role_id` IN (?) ";
$params[] = $right['roleIds'];
}
$right_sql = " ac_id in( select distinct ac_id from " . $table . " where status <? AND domain= ? AND (" . $where_right . "))";
}
if ($right_sql) {
$where .= " and (is_all =? OR $right_sql)";
} else {
$where .= " and (is_all =?)";
}
return array($where, $params);
}
/**
* 执行自定义查询SQL语句(安装应用回调时用)
*
* @author 侯英才
* @param string $sql 执行的SQL语句
*
* @return mixed
*/
public function query($sql)
{
return $this->_m->query($sql);
}
/**
* 执行自定义非查询SQL语句(安装应用回调时用)
*
* @author 侯英才
* @param string $sql 执行的SQL语句
*
* @return mixed
*/
public function execute($sql)
{
return $this->_m->execute($sql);
}
}