EmailContactGroup.php 2.6 KB
<?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() : [];
    }


















}