EmailContact.php 2.9 KB
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * 邮箱联系人
 * @time 2022/7/29 15:09
 * Class EmailList
 * @package App\Mail\Models
 */
class EmailContact extends Model{

    /**
     * 当前邮箱下的所有联系人
     * @param $mail_id
     * @return mixed
     * @time 2022/8/3 16:45
     */
    public static function _all($mail_id,$is_group=false){
        return static::with($is_group ? 'group' : [])->where(['email_id'   =>  $mail_id, 'is_del'  =>  0])
            ->orderBy('id','desc')
            ->get(['id','email','email_name','group_id'])
            ->toArray();
    }

//    public static function _paginate($mail_id)
//    {
//        return static::where(['email_id'   =>  $mail_id, 'is_del'  =>  0])
//            ->get(['id','email','email_name'])
//            ->toArray();
//
//    }

    /**
     * 添加联系人
     * @param $mail_id
     * @param $email
     * @param $name
     * @return static
     * @time 2022/8/3 16:35
     */
    public static function _save($mail_id,$data){
        if (!empty($data['id'])){
            $model = static::where(['id'=>$data['id'],'email_id'=>$mail_id])->first();
            if(!$model){
                goto EmailContactNewSave;
            }
        }else{
            EmailContactNewSave:
            $model = new static();
            $model->email_id =   $mail_id;
        }

        $model->group_id   =   $data['group_id'];
        $model->email   =   $data['email'];
        $model->email_name   =   $data['email_name'];
        $model->remark   =   $data['remark']??'';
        $model->is_del   =   0;
        $model->save();
        return $model;
    }


    /**
     * 删除
     * @param int $email_id
     * @param int $id
     * @return mixed
     * @time 2022/8/4 9:57
     */
    public static function _del(int $email_id, int $id){
        return static::where(['id'=>$id,'email_id'=>$email_id])->update([
            'is_del'    =>  1,
            'updated_at'    =>  date('Y-m-d H:i:s')
        ]);
    }


    /**
     * 读取一条信息
     * @param $email_id
     * @param $id
     * @param false $is_del
     * @return array
     * @author:dc
     * @time 2022/11/4 16:39
     */
    public static function _first($email_id,$id, $is_del = false){
        $data   =   static::where([
            'email_id'  =>  $email_id,
            'id'    =>  $id,
            'is_del'    =>  $is_del ? 1 : 0
        ])->first();

        return $data ? $data->toArray() : [];
    }





    public function group(){
        return $this->hasOne(EmailContactGroup::class,'id','group_id')->where('is_del',0)->select(['id','group_name']);
    }


    /**
     * @param $group_id
     * @param $email_id
     * @return mixed
     * @author:dc
     * @time 2022/11/4 14:30
     */
    public static function _count($group_id,$email_id){
        return static::where(['email_id'=>$email_id,'group_id'=>$group_id,'is_del'=>0])->count();
    }










}