SyncMailToEs.php 2.6 KB
<?php

namespace Event;

use Lib\Es\Es;
use Model\listsSql;


/**
 * 同步数据到es
 * @author:dc
 * @time 2025/3/1 17:57
 * Class SyncMailToEs
 * @package Event
 */
class SyncMailToEs {

    public $type = '';
    public $table = '';
    public $where = '';
    public $data = [];

    public function __construct($type,$table, $where=[],$data=[])
    {
        $this->type = $type;
        $this->table = $table;
        if ($type == 'create'){
            $this->data = $where;
        }else{
            $this->where = is_array($where) ? dbWhere($where) : $where;
            $this->data = $data;
        }

        try {
            $this->handler();
        }catch (\Throwable $e){
            logs('操作es失败 '.$e);
        }

    }

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

    public function handler(){
        if(in_array($this->table,['lists','lists_auto','lists_hot'])){

            if($this->type=='create'){
                $this->create();
            }
            else if($this->type=='update'){
                $this->update();
            }
            else{

            }

        }
    }

    /**
     * @author:dc
     * @time 2025/3/3 9:41
     */
    public function create(){
        if($this->table == 'lists_hot'){
            return ;
        }
        else if($this->table == 'lists'){
            redis()->rPush('sync_to_es',$this->data['id']);
//            $id = $this->data['email_id'].'_'.$this->data['folder_id'].'_'.$this->data['uid'];
//            if(empty($this->data['is_auto'])){
//                $this->data['is_auto'] = 0;
//            }
//            $this->es->save($id,$this->getData($this->data));
        }
        else if($this->table == 'lists_auto'){
            redis()->rPush('sync_to_es',$this->data['list_id']);
//            $data = db()->first(listsSql::first('`id` = '.$this->data['list_id']));
//            $id = $data['email_id'].'_'.$data['folder_id'].'_'.$data['uid'];
//            $this->es->save($id,$this->getData($data));
        }

    }

    /**
     * 更新数据
     * @author:dc
     * @time 2025/3/3 10:53
     */
    public function update(){
//        if($this->table == 'lists_hot'){
//
//        }
        if($this->table == 'lists'){
            // 更新es
            $lists = db()->all(listsSql::all($this->where,'`id`'));
            foreach ($lists as $list){
                redis()->rPush('sync_to_es',$list['id']);
//                $id = $list['email_id'].'_'.$list['folder_id'].'_'.$list['uid'];
//                $this->es->save($id,$this->getData($list));
            }
        }

    }












}