auto_reply_mail.php 3.5 KB
<?php

use Model\listsSql;


/**
 * 自动回复的邮件
 * @author:dc
 * @time 2024/9/6 17:09
 * Class HotMail
 */
class AutoMail {

    public function __construct(){
        $this->start();
    }

    /**
     * @var Lib\Es\Es
     */
    public $es;

    /**
     * @author:dc
     * @time 2024/7/18 14:04
     */
    private function start(){
        echo "start ";
        echo date('Y-m-d H:i:s');
        echo "\n";
        $at = 0;
        $this->es = es();
        $h = (int) date('H');
        while (true){
            $postid = redis()->lPop('resync_to_es_inbox_list');
            // 如果有增删 每天7点就开始自动刷
            if($postid == '0a'){
                if($h>18){
                    $postid = 0;
                }else{
                    redis()->rPush('resync_to_es_inbox_list','0a');
                    continue;
                }
            }

            if (!is_numeric($postid)){
                break;
            }
            if(redis()->add('resync_to_es_inbox:'.$postid,1,$postid?600:7200)){
                if (!$postid){
                    $post_ids =  fob_mysql()->all("select `post_id` from `e_mail_binds` group by `post_id`");
                    if($post_ids){
                        $post_ids = array_column($post_ids,'post_id','post_id');
                    }
                }else{
                    $post_ids = [$postid];
                }

                foreach ($post_ids as $post_id){
                    $total = $this->essss(0,$post_id,0);
                    $autoTotal = $this->essss(0,$post_id,1);
                    echo "postid {$post_id} inbox {$total} auto {$autoTotal}\n";
                    $at += $total + $autoTotal;
                }

            }

        }

        echo "总数量 {$at} ";
        echo "end ";
        echo date('Y-m-d H:i:s');
        echo "\n";



    }

    public function essss($id,$post_id,$is_auto){
        $total = 0;
        while ( true){
            // 查询自动回的邮件
            $this->es->setIndex('email_lists_branch_'.$post_id);

            $must = [
                ["term"=>["folder_as_int" =>  1]], // 收件箱
                ["term"=>["is_auto" =>  $is_auto]], // 非auto
                ["term"=>["is_hots" =>  0]], // 非预热
                ["term"=>["deleted" =>  0]], // 非删除
                ["term"=>["source" =>  2]], // 非删除
                ["range"=>[
                    'uuid'=>[
                        "gt"=>$id
                    ]
                ]]
            ];

            if($is_auto){
                $must[] = [
                    "match_phrase" =>[
                        "subject"=>"Re:"
                    ]
                ];
            }

            $lists = $this->es->search([
                "_source"  => ["uuid"],
                "query"=>[
                    "constant_score"=>[
                        "filter"=>[
                            "bool"=>[
                                "must"=> $must
                            ]
                        ]
                    ]
                ]
            ],0,1000,['uuid'=>'asc'],1000);

            if(empty($lists['hits']['hits'])){
                return $total;
            }
            foreach ($lists['hits']['hits']??[] as $list){
                $total++;
                $id = $list['_source']['uuid'];
                redis()->rPush('sync_to_es',$list['_source']['uuid'].'.1');
            }
        }
    }

}


include_once "../vendor/autoload.php";


new AutoMail();