作者 邓超

重新同步

@@ -15,80 +15,90 @@ class AutoMail { @@ -15,80 +15,90 @@ class AutoMail {
15 $this->start(); 15 $this->start();
16 } 16 }
17 17
  18 + /**
  19 + * @var Lib\Es\Es
  20 + */
  21 + public $es;
18 22
19 /** 23 /**
20 * @author:dc 24 * @author:dc
21 * @time 2024/7/18 14:04 25 * @time 2024/7/18 14:04
22 */ 26 */
23 private function start(){ 27 private function start(){
  28 + echo "start ";
  29 + echo date('Y-m-d H:i:s');
  30 + echo "\n";
  31 + if(redis()->get('resync_to_es_inbox') == 1){
  32 +
  33 + $post_ids = fob_mysql()->all("select `post_id` from `e_mail_binds` group by `post_id`");
  34 +
  35 + $this->es = es();
  36 +
  37 + foreach ($post_ids as $post_id){
  38 + $total = $this->essss(0,$post_id,0);
  39 + $autoTotal = $this->essss(0,$post_id,1);
  40 + echo "postid {$post_id} inbox {$total} auto {$autoTotal}\n";
  41 + }
24 42
25 - $old = redis()->get('prev_auto_mail_keyword',[]);  
26 -  
27 - $filter = isAiAutoMail(true,true);  
28 - if(!$filter) return 0;  
29 - redis()->set('prev_auto_mail_keyword',$filter,86400);  
30 -  
31 - $t = function ($vs){  
32 - return array_map(function ($v){  
33 - return $v[0].":".$v[1];  
34 - },$vs);  
35 - };  
36 - // 找到新增的  
37 - $q = array_diff($t($filter),$t($old));  
38 - if(!$q){  
39 - return 0;  
40 } 43 }
41 44
42 - $es = es('email_lists_copy'); 45 + echo "end ";
  46 + echo date('Y-m-d H:i:s');
  47 + echo "\n";
43 48
44 - $not_must = [];  
45 - foreach ($q as $f){  
46 - list($t,$str) = explode(':',$f,2);  
47 - if($t==2){  
48 - $not_must[] = ["match_phrase"=>["subject"=>$str]];  
49 49
50 - }elseif ($t==1){  
51 - $not_must[] = ["match_phrase"=>["from.email"=>$str]];  
52 50
  51 + }
  52 +
  53 + public function essss($id,$post_id,$is_auto){
  54 + $total = 0;
  55 + while ( true){
  56 + // 查询自动回的邮件
  57 + $this->es->setIndex('email_lists_branch_'.$post_id);
  58 +
  59 + $must = [
  60 + ["term"=>["folder_as_int" => 1]], // 收件箱
  61 + ["term"=>["is_auto" => $is_auto]], // 非auto
  62 + ["term"=>["is_hots" => 0]], // 非预热
  63 + ["term"=>["deleted" => 0]], // 非删除
  64 + ["term"=>["source" => 2]], // 非删除
  65 + ["range"=>[
  66 + 'uuid'=>[
  67 + "egt"=>$id
  68 + ]
  69 + ]]
  70 + ];
  71 +
  72 + if($is_auto){
  73 + $must[] = [
  74 + "match_phrase" =>[
  75 + "subject"=>"Re:"
  76 + ]
  77 + ];
53 } 78 }
54 - }  
55 79
56 - // 查询自动回的邮件  
57 - $lists = $es->search([  
58 - "_source" => ["uuid","subject","from"],  
59 - "query"=>[  
60 - "constant_score"=>[  
61 - "filter"=>[  
62 - "bool"=>[  
63 - "must"=>[  
64 - ["term"=>["folder_as_int" => 1]], // 收件箱  
65 - ["term"=>["is_auto" => 0]], // 非auto  
66 - ["term"=>["is_hots" => 0]], // 非预热  
67 - ["term"=>["deleted" => 0]], // 非删除  
68 - ["term"=>["source" => 2]], // 非删除  
69 - // 自动关键字  
70 - [  
71 - "bool" => [  
72 - "should"=> $not_must,  
73 - "minimum_should_match" => 1  
74 - ]  
75 - ] 80 + $lists = $this->es->search([
  81 + "_source" => ["uuid"],
  82 + "query"=>[
  83 + "constant_score"=>[
  84 + "filter"=>[
  85 + "bool"=>[
  86 + "must"=> $must
76 ] 87 ]
77 ] 88 ]
78 ] 89 ]
79 ] 90 ]
80 - ]  
81 - ],0,1000,['udate'=>'desc'],1000);  
82 -  
83 - foreach ($lists['hits']['hits']??[] as $list){  
84 - $list = $list['_source'];  
85 - $id = intval($list['uuid']??0);  
86 - if($id && isAiAutoMail($list['from']['email'],$list['subject'])){  
87 - _echo("id = ".$id);  
88 - redis()->rPush('sync_to_es',$id); 91 + ],0,1000,['uuid'=>'asc'],1000);
  92 +
  93 + if(empty($lists['hits']['hits'])){
  94 + return $total;
  95 + }
  96 + foreach ($lists['hits']['hits']??[] as $list){
  97 + $total++;
  98 + $id = $list['_source']['uuid'];
  99 + redis()->rPush('sync_to_es',$list['_source']['uuid'].'.1');
89 } 100 }
90 } 101 }
91 -  
92 } 102 }
93 103
94 } 104 }
@@ -1126,6 +1126,25 @@ class Home extends Base { @@ -1126,6 +1126,25 @@ class Home extends Base {
1126 1126
1127 } 1127 }
1128 1128
  1129 + /**
  1130 + * 标记一下 把邮件重新同步
  1131 + * @throws \Lib\Err
  1132 + * @author:dc
  1133 + * @time 2025/9/18 16:50
  1134 + */
  1135 + public function resync_to_es(){
  1136 + if(redis()->add('resync_to_es_inbox',1,600)){
  1137 + app()->e('请求成功,请等待5-15分钟');
  1138 + }else{
  1139 + app()->e('失败,请等待10分钟后在重试');
  1140 + }
  1141 + }
  1142 +
  1143 +
  1144 +
  1145 +
  1146 +
  1147 +
1129 } 1148 }
1130 1149
1131 1150
@@ -53,6 +53,8 @@ return [ @@ -53,6 +53,8 @@ return [
53 // v2 版本的 53 // v2 版本的
54 'v2/sync' => [\Controller\v2\Home::class, 'sync'], 54 'v2/sync' => [\Controller\v2\Home::class, 'sync'],
55 'v3/sync' => [\Controller\v3\Home::class, 'sync'], 55 'v3/sync' => [\Controller\v3\Home::class, 'sync'],
  56 + // 重新同步收件箱到es库
  57 + 'resync_to_es' => [\Controller\Home::class, 'resync_to_es'],
56 // 标记为已读 58 // 标记为已读
57 'seen_2_unseen' => [\Controller\Home::class, 'seen_2_unseen'], 59 'seen_2_unseen' => [\Controller\Home::class, 'seen_2_unseen'],
58 // 标记为已回复/未回复 60 // 标记为已回复/未回复