EmailContactGroup.php
2.6 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
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
/**
* 邮箱联系分组
* @time 2022/7/29 15:09
* Class EmailContactGroup
* @package App\Mail\Models
*/
class EmailContactGroup extends Model{
/**
* 当前邮箱下的所有联系人
* @param $mail_id
* @return mixed
* @time 2022/8/3 16:45
*/
public static function _all(int $mail_id,$is_contact=false){
return static::with($is_contact?'contacts':[])
->where(['email_id' => $mail_id, 'is_del' => 0])
->get(['id','group_name'])
->toArray();
}
/**
* 关联联系人
* @return \Illuminate\Database\Eloquent\Relations\HasMany
* @author:dc
* @time 2022/11/5 9:43
*/
public function contacts(){
return $this->hasMany(EmailContact::class,'group_id','id')
->where(['is_del'=>0])->select(['id','email','email_name','group_id']);
}
/**
* 添加联系人
* @param int $mail_id
* @param string $name
* @param int $id
* @return static
* @author:dc
* @time 2022/11/4 11:21
*/
public static function _save(int $mail_id, string $name,$id = 0){
if($id) {
$model = static::where(['id' => $id, 'email_id' => $mail_id])->first();
if (!$model){
return new static;
}
} else {
$model = new static();
$model->email_id = $mail_id;
}
$model->group_name = $name;
$model->save();
return $model;
}
/**
* 查询一条
* @param int $mail_id
* @param string $name
* @author:dc
* @time 2022/11/3 10:42
*/
public static function _firstByName(int $mail_id, string $name){
return static::where(['email_id'=>$mail_id,'group_name'=>$name,'is_del'=>0])->first();
}
/**
* 删除
* @param int $id
* @param int $email_id
* @return mixed
* @time 2022/8/4 10:49
*/
public static function _del(int $id, int $email_id){
return static::where(['id'=>$id,'email_id'=>$email_id])->update([
'is_del' => 1,
'updated_at' => date('Y-m-d H:i:s')
]);
}
/**
* 读取一条信息
* @param $id
* @param false $is_del
* @return mixed
* @time 2022/8/3 16:52
*/
public static function _first(int $id, bool $is_del = false){
$data = static::where([
'id' => $id,
'is_del' => $is_del ? 1 : 0
])->first();
return $data ? $data->toArray() : [];
}
}