ActivityModel.class.php
4.03 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
<?php
namespace Common\Model;
use Common\Common\Constant;
class ActivityModel extends AbstractModel
{
/**
* 构造方法
*/
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, []);
}
public function get_where($params = [])
{
// 组装查询语句
$where = ' status<' . self::ST_DELETE . " AND domain= '" . QY_DOMAIN . "'";
if (!empty($params['title'])) {
$params['title'] = str_replace("%", '\%', $params['title']);
// 如果标题不为空
$where .= " AND title like '%" . trim($params['title']) . "%' ";
}
// 活动时间范围
if (!empty($params['start_time']) && $params['start_time'] != 'NaN' && $params['end_time'] != 'NaN') {
$start_time = rstrtotime(rgmdate(strval($params['start_time']), 'Y-m-d') . ' 00:00:00');
$end_time = rstrtotime(rgmdate(strval($params['end_time']), 'Y-m-d') . ' 23:59:59');
$where .= ' AND ( end_time >' . $start_time . ' AND start_time <' . $end_time . ' )';
}
if (!empty($params['activity_status'])) {
switch ($params['activity_status']) {
// 草稿
case Constant::STATUS_DRAFT:
$where .= ' AND activity_status = ' . Constant::ACTIVITY_DRAFT;
break;
// 未开始
case Constant::STATUS_NOT_START:
$where .= ' AND activity_status = ' . Constant::ACTIVITY_PUBLISH . ' AND start_time > ' . MILLI_TIME;
break;
// 进行中
case Constant::STATUS_ING:
$where .= ' AND activity_status = ' . Constant::ACTIVITY_PUBLISH . ' AND start_time < ' . MILLI_TIME . ' AND end_time >= ' . MILLI_TIME;
break;
// 已结束
case Constant::STATUS_END:
$where .= ' AND activity_status = ' . Constant::ACTIVITY_PUBLISH . ' AND end_time < ' . MILLI_TIME;
break;
// 已终止
case Constant::STATUS_STOP:
$where .= ' AND activity_status = ' . Constant::ACTIVITY_STOP;
break;
default:
}
}
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;
}
}