ActivityModel.class.php
5.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
<?php
namespace Common\Model;
use Common\Common\Helper;
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 TIME_EMPTY = 0;
/**
* 构造方法
*/
public function __construct()
{
parent::__construct();
}
/**
* 查询总数
*
* @param array $params 条件sql,不包含where关键字
*
* @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, []);
}
/**
* 获取全公司可见的活动列表
* @return array
*/
public function get_is_all_look_activity_list()
{
// 组装查询语句
$where = ' status<' . self::ST_DELETE . " AND domain= '" . QY_DOMAIN . "'";
$sql = "SELECT ac_id FROM __TABLE__ WHERE (is_all = 1 OR is_all_look = 1 ) AND " . $where;
// 读取记录
return $this->_m->fetch_array($sql, []);
}
/**
* 解析查询条件
*
* @param array $params
*
* @return string
*/
public function get_where($params = [])
{
// 组装查询语句
$where = ' status<' . self::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 Helper::STATUS_DRAFT:
$where .= ' AND activity_status =' . ActivityModel::ACTIVITY_DRAFT;
break;
// 未开始
case Helper::STATUS_NOT_START:
$where .= ' AND activity_status =' . ActivityModel::ACTIVITY_PUBLISH . ' AND begin_time>' . MILLI_TIME;
break;
// 进行中
case Helper::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 Helper::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;
default:
}
}
// 如果搜索类型
if (!empty($params['activity_type'])) {
$where .= ' AND activity_type =' . $params['activity_type'];
}
return $where;
}
/**
* 查询列表
*
* @param array $params 条件sql,不包含where关键字
* @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, []);
}
/**
* 执行自定义查询SQL语句(安装应用回调时用)
*
* @author 侯英才
*
* @param string $sql 执行的SQL语句
*
* @return mixed
*/
public function query($sql)
{
$res = $this->_m->query($sql);
return $res;
}
/**
* 执行自定义非查询SQL语句(安装应用回调时用)
*
* @author 侯英才
*
* @param string $sql 执行的SQL语句
*
* @return mixed
*/
public function execute($sql)
{
$res = $this->_m->execute($sql);
return $res;
}
}