EmailFolder.php 5.6 KB
<?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 EmailFolder 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)
//                ->where('user_id',$user_id)
            ->whereIn('email_id',$email_id);
        }else{
            $folders   =   static::where([
                'email_id'=>$email_id,
//                'user_id'=>$user_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] = MailFun::list2Tree($f);
            }

            return $fs;
        }else{
            if($folders && $is_tree){
                $folders    =   MailFun::list2Tree($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 $user_id
     * @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 $user_id, 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();

        $data['user_id']    =   $user_id;
        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;
    }


}