EList.php 6.3 KB
<?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 EList 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();
    }

}