Folder.php
6.1 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
<?php
namespace App\Models;
use App\Mail\lib\MailFun;
use Illuminate\Database\Eloquent\Model;
/**
* 邮件文件夹
* @time 2022/8/1 11:26
* Class EmailFolder
* @package App\Mail\Models
*/
class Folder extends Model {
const UPDATED_AT = null;
const CREATED_AT = null;
static $allData;
/**
*
* @param $email_id
* @param bool $is_tree
* @return array
* @author:dc
* @time 2022/11/8 14:32
*/
public static function _all($email_id, $is_tree = true):array {
if(is_array($email_id)){
$folders = static::where('is_del',0)
->whereIn('email_id',$email_id);
}else{
$folders = static::where([
'email_id'=>$email_id,
'is_del' => 0
]);
}
$folders = $folders
->get(['id','folder','pid','exsts','unseen','origin_folder'])
->toArray();
if(is_array($email_id)){
$fs = [];
foreach ($email_id as $item){
foreach ($folders as $folder){
if($item == $folder['email_id']){
$fs[$item][] = $folder;
}
}
}
foreach ($fs as $fk=>$f){
$fs[$fk] = list_to_tree($f);
}
return $fs;
}else{
if($folders && $is_tree){
$folders = list_to_tree($folders);
}
return $folders ? $folders : [];
}
}
/**
* 查询一条
* @param $id
* @param false $is_del
* @return mixed
* @time 2022/8/17 9:14
*/
public static function _first($id){
return static::where('id',$id)->get()->toArray();
}
/**
* @param $id
* @param $email_id
* @return mixed
* @author:dc
* @time 2022/10/26 10:10
*/
public static function _firstAndEmailId($id,$email_id){
return static::where(['id'=>$id,'email_id'=>$email_id])->get()->toArray();
}
/**
* 获取文件夹id
* @param $email_id
* @param string $folder
* @return mixed
* @author:dc
* @time 2022/11/8 14:44
*/
public static function _user_folders($email_id,$folder='INBOX'){
$where = [
// 'user_id'=>$user_id,
'is_del' => 0,
'folder' => $folder
];
// if($email_id) $where['email_id'] = $email_id;
return static::where($where)
->where(function ($query)use($email_id){
if(is_array($email_id)){
$query->whereIn('email_id',$email_id);
}else{
$query->where('email_id',$email_id);
}
})
->get(['id','exsts','unseen'])
->toArray();
}
/**
* 删除
* @param int $id
* @param string $folder
* @return bool
* @time 2022/8/4 15:23
*/
public static function _del(int $id, string $folder = ''):bool {
$where = [];
if($folder){
$where['email_id'] = $id;
$where['folder'] = $folder;
}else{
$where['id'] = $id;
}
$where['is_del'] = 0;
// 已删除
if(static::where($where)->value('id')){
return true;
}
unset($where['is_del']);
return static::where($where)
->update(['is_del'=>1]);
}
/**
* 插入
* @param int $email_id
* @param string $name
* @param string $origin_name
* @param int $pid
* @return mixed
* @time 2022/8/5 17:36
*/
public static function _insert(int $email_id, string $name, string $origin_name, int $pid=0){
$data = [];
$data['email_id'] = $email_id;
$data['folder'] = $name;
$data['origin_folder'] = $origin_name;
$data['pid'] = $pid;
$model = static::where($data)->first();
if($model){
if($model->is_del){
$model->is_del = 0;
$model->save();
}
return $model->id;
}
return static::insertGetId($data);
}
/**
* 返回 dir/dir/dir
* @param array $lists
* @param int $id
* @param string $folder
* @param string $field 获取什么名字
* @author:dc
* @time 2022/10/31 16:30
*/
public static function _firstTree(array $lists, int $id,string &$folder,$field='folder'):void {
foreach ($lists as $list){
if($list['id'] == $id){
$folder = $list[$field].($folder ? '/'.$folder : '');
if ($list['pid']!=0){
self::_firstTree($list,$list['pid'],$folder,$field);
}
}
}
}
/**
* @param $id
* @param null $exsts
* @param null $unseen
* @return false
* @time 2022/8/8 9:21
*/
public static function _updateNum($id, $exsts = null, $unseen = null){
$model = static::where('id',$id)->first();
if($model){
$u = false;
if($exsts != null) {
$u = true;
$model->exsts = $exsts;
}
if($unseen != null) {
$u = true;
$model->unseen = $unseen;
}
if ($u){
return $model->save();
}
}
return false;
}
/**
* 读取源名称
* @param $id
* @return string
* @author:dc
* @time 2023/2/7 17:29
*/
public static function getOriginName($id){
$folder = static::where('id',$id)->select(['id','pid','origin_folder'])->first();
$folder_name = '';
if ($folder){
$folder_name = $folder->origin_folder;
if($folder->pid){
$pfolder = static::where('id',$folder->pid)->select(['id','pid','origin_folder'])->first();
if($pfolder){
$folder_name .= '/'.$pfolder->origin_folder;
}
}
}
return $folder_name;
}
}