MessageModel.class.php
6.91 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
<?php
namespace Common\Model;
class MessageModel extends AbstractModel
{
public function __construct()
{
parent::__construct();
}
/**
* 添加留言内容
* User: <362431947@qq.com>
* @param array $data
* @return bool|mixed
* Date: 2018-10-10 Time: 22:32
*/
public function addOne($data = [])
{
if(! is_array($data)|| ! $data){
return false;
}
$data['created'] = $this->microtime();
$result = $this->_m->insert($data);
return $result;
}
/**
* 留言信息列表
* @author <362431947@qq.com>
* @date 2018-10-10
* @param $page int 页码
* @return array
*/
public function message_list($conds = [],$page = 0)
{
if ($page < 1 || !$page) {
$page = self::PAGING_DEFAULT_PAGE;
}
$params = [];
$wheres = [];
$conds['a.status'] = $this->get_st_create();
if (!$this->_parse_where($wheres, $params, $conds)) {
return false;
}
$page_size = self::PAGING_DEFAULT_LIMIT;
$page_start = ($page - 1) * $page_size;
$option = [$page_start,$page_size];
$m_person = new PersonModel();
$table = $m_person->get_tname();
if (in_array('title',$conds)) {
$where = " title LIKE '?' ";
}
else{
$fields = array_keys($conds);
if ($fields != 'status') {
$field = $fields[0];
}
$where = " $field = ? ";
}
$sql = "SELECT a.message_id,a.title,a.message,IFNULL(b.name,'') AS `from`,IFNULL(c.name,'') AS `to`,a.checked
FROM __TABLE__ a
LEFT JOIN {$table} b ON a.uid = b.person_id
LEFT JOIN {$table} c ON a.receiver_uid = c.person_id
WHERE $where";
$list = $this->_m->fetch_array($sql, $params,$option);
$messageList['total'] = count($list);
$messageList['page'] = $page;
$messageList['limit'] = $page_size;
$messageList['list'] = $list;
return $messageList;
}
/**
* 后台留言删除
* User: <362431947@qq.com>
* @param int $message_id
* @return bool
* Date: 2018-10-10 Time: 22:24
*/
public function del($message_id = 0)
{
$this->_m->startTrans();
$attachment_id = $this->getAttachmentIdByMsgId($message_id);
// 删除相关附件图片
$attachment_del = true;
if ($attachment_id) {
$m_attachment = new AttachmentModel();
$attachment_del = $m_attachment->del($attachment_id);
}
// 删除留言
$message_del = $this->_m->delete($message_id);
// 删除留言评论
$m_comment = new CommentModel();
$exists = $m_comment->getCommentByMsgId($message_id = 0);
$comment_del = true;
if ($exists) {
$comment_del = $m_comment->del($message_id);
}
if ($message_del && $comment_del && $attachment_del) {
$this->_m->commit();
return true;
}
else{
$this->_m->rollback();
return false;
}
}
/**
* 根据搜索条件获取留言信息
* User: <362431947@qq.com>
* @param array $conds 搜索条件
* @return array|bool
* Date: 2018-10-10 Time: 22:24
*/
public function search($conds = [])
{
$params = [];
$wheres = [];
if (!$this->_parse_where($wheres, $params, $conds)) {
return false;
}
$m_person = new PersonModel();
$table = $m_person->get_tname();
if (in_array('title',$conds)) {
$where = " title LIKE ? ";
}
else{
$fields = array_keys($conds);
$field = $fields[0];
$where = " $field = ? ";
}
if ($where) {
$sql = "SELECT a.* FROM __TABLE__ a
JOIN {$table} b ON a.uid = b.person_id
WHERE $where";
}
else {
return [];
}
return $this->_m->fetch_array($sql,$params);
}
/**
* 留言详情
* User: <362431947@qq.com>
* @param int $message_id
* @return array|bool
* Date: 2018-10-11 Time: 11:47
*/
public function message_detail($message_id = 0)
{
$params = [];
$wheres = [];
$conds = ['message_id' =>$message_id];
if (!$this->_parse_where($wheres, $params, $conds)) {
return false;
}
$m_person = new PersonModel();
$table_person = $m_person->get_tname();
$sql = "SELECT a.message_id,a.title,a.message,b.name as `from`,IFNULL(c.name,'') as `to`
FROM __TABLE__ a
LEFT JOIN {$table_person} b ON a.uid = b.person_id
LEFT JOIN {$table_person} c ON a.receiver_uid = c.person_id
WHERE `message_id` = ? ";
$message = $this->_m->fetch_row($sql,$params);
$m_attachment = new AttachmentModel();
$table_attachment = $m_attachment->get_tname();
$attachment_id_str = $this->getAttachmentIdByMsgId($message_id);
$attachment = [];
if ($attachment_id_str) {
$sql = "SELECT `path` FROM {$table_attachment}
WHERE attachment_id IN ($attachment_id_str)";
$attachment = $this->_m->fetch_array($sql,$params);
}
$message['attachment'] = $attachment;
// 获取评论信息
$m_comment = new CommentModel();
$message['comment'] = $m_comment->getCommentByMsgId($message_id);
return $message;
}
/**
* getAttachmentIdByMsgId
* User: <362431947@qq.com>
* @param int $message_id
* @return bool
* Date: 2018-10-10 Time: 22:18
*/
public function getAttachmentIdByMsgId($message_id = 0)
{
$params = [];
$wheres = [];
$conds = [
'message_id' => $message_id,
];
if (!$this->_parse_where($wheres, $params, $conds)) {
return false;
}
$sql = "SELECT attachment
FROM __TABLE__ WHERE `message_id` = ? ";
$attachment = $this->_m->fetch_row($sql,$params);
return isset($attachment['attachment']) ? $attachment['attachment'] : '';
}
/**
* 留言审核
* User: <362431947@qq.com>
* @param int $message_id
* @return bool
* Date: 2018-10-11 Time: 21:32
*/
public function check($message_id = 0,$checked = 1)
{
// // 未审核状态
// $conds = [
// 'message_id' => $message_id,
// ];
$exists = $this->_m->field('message_id')->find($message_id);
if ($exists) {
$data = [
'checked' =>$checked,
];
return $this->update($message_id,$data,true);
}
else{
return false;
}
}
}