TopicModel.class.php
6.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
<?php
/**
* 考试-题目表
* @author: houyingcai
* @email: 594609175@qq.com
* @date : 2017-05-19 18:07:28
*/
namespace Common\Model;
class TopicModel extends AbstractModel
{
/**
* 构造方法
*/
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__ AS t " . $where;
return $this->_m->result($sql, []);
}
/**
* 根据条件查询试题列表
*
* @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 = '*')
{
// 排序
$orderby = '';
if (!$this->_order_by($orderby, $order_option)) {
return false;
}
// 分页参数
$limit = '';
if (!$this->_limit($limit, $page_option)) {
return false;
}
$where = $this->get_where($params);
$sql = " SELECT {$fields} FROM __TABLE__ AS t " . $where . " {$orderby}{$limit}";
return $this->_m->fetch_array($sql, []);
}
/**
* 组装sql语句
*
* @param array $params 参数列表
*
* @return string
*/
protected function get_where($params = [])
{
$where = " WHERE t.status<" . self::ST_DELETE . " AND t.domain= '" . QY_DOMAIN . "' ";
// 如果题库ID不为空
if ($params['eb_id']) {
$where .= ' AND t.eb_id =' . $params['eb_id'];
}
// 题目属性不为空
if (!empty($params['attr_id'])) {
$where .= ' AND t.et_id in (SELECT a.et_id FROM `oa_exam_topic_attr` a WHERE a.attr_id=' . $params['attr_id'] . ' AND a.status<' . self::ST_DELETE . ' AND a.domain=\'' . QY_DOMAIN . '\')';
}
// 题目类型不为空
if (!empty($params['et_type'])) {
if (is_array($params['et_type'])) {
$where .= " AND t.et_type IN (" . implode(',', $params['et_type']) . ")";
} else {
$where .= ' AND t.et_type = ' . $params['et_type'];
}
}
// 题目标题不为空
if (!empty($params['title'])) {
$where .= " AND t.title like '%" . $params['title'] . "%'";
}
return $where;
}
/**
* 给指定字段加1
*
* @param string $field
* @param array $condition
* @param int $step
*
* @return boolean
*/
public function setIncNum($field, $condition = [], $step = 1)
{
$this->where($condition);
return $this->setInc($field, $step);
}
/**
* 给字段添加自动添加一个数
*
* @autor 蔡建华
* @param array $conds
*
* @return boolean
*/
public function AddIncNum($conds)
{
// 更新条件
$params = [];
// 更新条件
$wheres = [];
if (!$this->_parse_where($wheres, $params, $conds)) {
return false;
}
// 企业标记
$wheres[] = "`{$this->prefield}domain`=?";
$params[] = QY_DOMAIN;
// 状态条件
$wheres[] = "`{$this->prefield}status`<?";
$params[] = $this->get_st_delete();
return $this->_m->execsql('UPDATE __TABLE__ SET `use_num`=`use_num`+1 WHERE ' . implode(' AND ', $wheres),
$params);
}
/**
* 给字段添加自动添就减一个数
*
* @autor 蔡建华
* @param array $conds
*
* @return boolean
*/
public function MinusIncNum($conds)
{
// 更新条件
$params = [];
// 更新条件
$wheres = [];
if (!$this->_parse_where($wheres, $params, $conds)) {
return false;
}
// 企业标记
$wheres[] = "`{$this->prefield}domain`=?";
$params[] = QY_DOMAIN;
// 状态条件
$wheres[] = "`{$this->prefield}status`<?";
$params[] = $this->get_st_delete();
return $this->_m->execsql('UPDATE __TABLE__ SET `use_num`=`use_num`-1 WHERE ' . implode(' AND ', $wheres),
$params);
}
/**
* 根据条件,查询包含已删除的数据
*
* @param array $conds 条件数组
* @param int|array $page_option 分页参数
* @param array $order_option 排序
* @param string $fields 读取字段
*
* @return array|bool
*/
public function list_topic_contain_del($conds, $page_option = null, $order_option = [], $fields = '*')
{
$params = [];
// 条件
$wheres = [];
if (!$this->_parse_where($wheres, $params, $conds)) {
return false;
}
// 企业标记
$wheres[] = "`{$this->prefield}domain`=?";
$params[] = QY_DOMAIN;
// 排序
$orderby = '';
if (!$this->_order_by($orderby, $order_option)) {
return false;
}
// 分页参数
$limit = '';
if (!$this->_limit($limit, $page_option)) {
return false;
}
// 读取记录
return $this->_m->fetch_array("SELECT {$fields} FROM __TABLE__ WHERE " . implode(' AND ',
$wheres) . "{$orderby}{$limit}", $params);
}
/**
* 根据条件计算数量(包含已删除)
*
* @param array $conds
* @param string $fields
*
* @throws service_exception
* @return number
*/
public function count_topic_contain_del($conds, $fields = '*')
{
$params = [];
// 更新条件
$wheres = [];
if (!$this->_parse_where($wheres, $params, $conds)) {
return false;
}
// 企业标记
$wheres[] = "`{$this->prefield}domain`=?";
$params[] = QY_DOMAIN;
return $this->_m->result('SELECT COUNT(' . $fields . ') FROM __TABLE__ WHERE ' . implode(' AND ', $wheres),
$params);
}
}