EmailList.php
6.3 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
<?php
namespace App\Models;
use App\Repositories\LengthAwarePaginator;
use Illuminate\Container\Container;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\Paginator;
/**
* 邮件列表
* @time 2022/7/29 15:09
* Class EmailList
* @package App\Mail\Models
*/
class EmailList extends Model{
protected $appends = ['to_time'];
// 收到的时间
public function getToTimeAttribute()
{
if($this->udate){
$date = date('Y-m-d',$this->udate);
if($date==date('Y-m-d')){
return date('今天 H:i',$this->udate);
}
return $date;
}
return '';
}
/**
* 批量插入
* @param array $datas
* @return array
* @time 2022/8/1 18:14
*/
public static function _insertAll(array $datas){
$ids = [];
foreach ($datas as $data){
try {
if($data['id']){
$id = $data['id'];
}else{
$id = static::where('uuid',$data['uuid'])->value('id');
}
unset($data['id']);
if($id){
static::where('id',$id)->update($data);
}else{
$id = static::insertGetId($data);
}
if($id){
$ids[] = $id;
}
}catch (\Throwable $e){
EmailLog::error([$data,$e->getMessage(),$e->getTraceAsString()]);
}
}
return $ids;
}
/**
* 来自某某标题
* @param $val
* @return string
* @author:dc
* @time 2022/10/24 17:04
*/
public function getFromAttribute($val)
{
if($val){
return htmlspecialchars($val);
}
return '';
}
/**
* 最后一次获取到邮件的时间,以收到时间为准,发送时间有时没有
* @param int $email_id
* @param int $folder_id
* @return mixed
* @time 2022/8/1 17:32
*/
public static function _lastDate(int $email_id, int $folder_id = 0 ){
return static::where(['email_id'=>$email_id,'folder_id'=>$folder_id])
->orderBy('udate','desc')
->value('udate');
}
/**
* 获取邮件消息序号最大的一个,邮件序号是可变的
* @param int $email_id
* @param int $folder_id
* @return mixed
* @author:dc
* @time 2022/10/27 13:48
*/
public static function _lastMsgno(int $email_id, int $folder_id = 0 ){
return static::where(['email_id'=>$email_id,'folder_id'=>$folder_id])
->max('msgno');
}
/**
* 查询某天已存在的uid
* @param string $email_id
* @param int $date
* @param int $folder_id
* @return array
* @time 2022/8/4 15:27
*/
public static function _lastExistUid(string $email_id, int $date, int $folder_id = 0 ):array {
$uids = static::where(['email_id'=>$email_id,'folder_id'=>$folder_id])
->where('udate','>',$date)
->get(['uid'])
->pluck('uid','uid')
->toArray();
return $uids ? $uids : [];
}
/**
* 查询详情
* @param int $id
* @return \Illuminate\Database\Eloquent\Builder|Model|object|null
* @time 2022/8/2 14:25
*/
public static function _firstWithBody(int $id){
return static::with('body')->where('id',$id)->first();
}
/**
* 查询列表
* @param $where
* @param int $perPage
* @param int $total
* @return mixed|object
* @throws \Illuminate\Contracts\Container\BindingResolutionException
* @time 2022/8/16 17:34
*/
public static function _paginate($where, int $perPage = 20, $total = 0){
$currentPage = Paginator::resolveCurrentPage();
if(!$total){
$total = static::where($where)->count();
}
$items = static::where($where)->orderBy('udate','desc')->orderBy('id','desc')->forPage($currentPage, $perPage)->get();
$options = [
'path' => Paginator::resolveCurrentPath(),
'pageName' => 'page',
];
return Container::getInstance()->makeWith(LengthAwarePaginator::class, compact(
'items', 'total', 'perPage', 'currentPage', 'options'
));
}
/**
* 未读邮件,只有inbox才有
* @param $email_ids
* @return mixed
* @author:dc
* @time 2022/10/31 17:21
*/
public static function _getUnseenNum($email_ids){
return static::whereIn('email_id',$email_ids)->where('seen',0)->count();
}
/**
* 一条数据
* @param $id
* @return array
* @author:dc
* @time 2022/11/8 22:38
*/
public static function _first($id){
$data = static::where('id',$id)->first();
return $data ? $data->toArray() : [];
}
/**
* 关于body
* @return \Illuminate\Database\Eloquent\Relations\HasOne
* @time 2022/8/2 14:25
*/
public function body(){
return $this->hasOne(EmailBody::class,'email_lists_id','id');
}
/**
* 更加id获取邮件的uid
* @param array $ids
* @param int $email_id
* @return array
* @author:dc
* @time 2022/10/26 17:28
*/
public static function _getUidsByIds(array $ids, int $email_id):array {
$uids = static::whereIn('id',$ids)->where('email_id',$email_id)->get(['id','uid'])->pluck('uid','id')->toArray();
return is_array($uids) ? $uids : [];
}
/**
* 设置标记
* @param array $ids
* @param int $email_id
* @param string $key
* @param int $value
* @return bool
* @author:dc
* @time 2022/10/26 17:43
*/
public static function _setFlags(array $ids, int $email_id, string $key, int $value):bool{
return static::whereIn('id',$ids)->where('email_id',$email_id)->update([$key=>$value]);
}
/**
* 根据编号查询id
* @param $email_id
* @param $folder
* @param $msgno
* @return mixed
* @author:dc
* @time 2022/11/8 17:38
*/
public static function _getIdsByMsgno($email_id,$folder,$msgno){
return static::where([
'email_id'=>$email_id,
'folder_id' =>$folder
])
->whereIn('msgno',$msgno)
->get(['id','msgno'])->pluck('id','msgno')->toArray();
}
}