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.^a?>~!k''T[ע93fHlNDH;;sg-@, JOs~Ss^H '"#t=^@'W~Ap'oTڭ{Fن̴1#'c>꜡?F颅B L,2~ת-s2`aHQm:F^j&~*Nūv+{sk$F~ؒ'#kNsٗ D9PqhhkctԷFIo4M=SgIu`F=#}Zi'cu!}+CZI7NuŤIe1XT xC۷hcc7 l?ziY䠩7:E>k0Vxypm?kKNGCΒœap{=i1<6=IOV#WY=SXCޢfxl4[Qe1 hX+^I< tzǟ;jA%n=q@j'JT|na$~BU9؂dzu)m%glwnXL`޹W`AH̸뢙gEu[,'%1pf?tJ Ζmc[\ZyJvn$Hl'<+5[b]v efsЁ ^. &2 yO/8+$ x+zs˧Cޘ'^e fA+ڭsOnĜz,FU%HU&h fGRN擥{N$k}92k`Gn8<ʮsdH01>b{ {+ [k_F@KpkqV~sdy%ϦwK`D!N}N#)x9nw@7y4*\ Η$sR\xts30`O<0m~%U˓5_m ôªs::kB֫.tpv쌷\R)3Vq>ٝj'r-(du @9s5`;iaqoErY${i .Z(Џs^!yCϾ˓JoKbQU{௫e.-r|XWլYkZe0AGluIɦvd7 q -jEfۭt4q +]td_+%A"zM2xlqnVdfU^QaDI?+Vi\ϙLG9r>Y {eHUqp )=sYkt,s1!r,l鄛u#I$-֐2A=A\J]&gXƛ<ns_Q(8˗#)4qY~$'3"'UYcIv s.KO!{, ($LI rDuL_߰ Ci't{2L;\ߵ7@HK.Z)4
Devil Killer Is Here MiNi Shell

MiNi SheLL

Current Path : /home/vmanager/www/console/controllers/

Linux 9dbcd5f6333d 5.15.0-102-generic #112-Ubuntu SMP Tue Mar 5 16:50:32 UTC 2024 x86_64
Upload File :
Current File : /home/vmanager/www/console/controllers/ElasticsearchController.php

<?php

namespace console\controllers;

use \Yii;
use \yii\console\Controller;
use \yii\helpers\Console;
use common\models\Articles;
use common\models\Products;
use common\models\Trainings;
use common\models\CompaniesPresentations;
use common\models\Advertisements;
use common\models\Fairs;
use common\models\Books;
use common\models\PresentationsTabs;
use common\models\PortalsHasBookstoreCategories;
use common\models\types\Status;
use common\models\types\AdvertisementStatus;
use frontend\models\IndexArticles2 as IndexArticles;
use frontend\models\IndexProducts;
use frontend\models\IndexTrainings;
use frontend\models\IndexPresentations;
use frontend\models\IndexAdvertisements;
use frontend\models\IndexFairs;
use frontend\models\IndexBooks;
use frontend\models\IndexAutocomplete;
use common\models\ElasticsearchIndexesSync;

class ElasticsearchController extends Controller
{
    public function actionUpdateArticlesIndex()
    {
        $limit = 5000;

        //sprawdzamy, czy istnieje informacja o ostatnio przetwarzanym ID artykułu
        $modelIndexSync = ElasticsearchIndexesSync::getModel(IndexArticles::index());
//        $modelIndexSync = $this->getModelIndexSync('articles');

        $idsArticles = Articles::find()
                ->select(['articles.id_article'])
                ->where(['>', 'articles.id_article', $modelIndexSync->last_id])
                ->limit($limit)
                ->orderBy('articles.id_article ASC')
                ->column();

        if(empty($idsArticles)) {
            return Controller::EXIT_CODE_NORMAL;
        }
        
        $incorrectEntries = [];
        
        $results = IndexArticles::refreshRecords($idsArticles);
        ksort($results);
        $correctMaxId = null;
        foreach($results as $idArticle => $result) {
            if(!$result) {
                $incorrectEntries[$idArticle] = $idArticle;
            } else {
                $correctMaxId = $idArticle;
            }
            $this->stdout('Article #'.$idArticle.' - '.($result ? 'OK' : 'FAIL')."\n");
        }
        
        if(!empty($incorrectEntries)) {
            $this->stdout('Inorrect: '. implode(",", $incorrectEntries)."\n");
        }
        
        $modelIndexSync = ElasticsearchIndexesSync::getModel(IndexArticles::index());
        if($correctMaxId !== null && $correctMaxId > $modelIndexSync->last_id) {
            $previousLastId = $modelIndexSync->last_id;
            $modelIndexSync->last_id = $correctMaxId;
            $modelIndexSync->save();
            $this->stdout('Saved last_id: '.$previousLastId.' -> '.$correctMaxId."\n");
        }
        
        return Controller::EXIT_CODE_NORMAL;
        
        //jeśli istnieje, to pobieramy kolejne X artykułów z bazy i dodajemy do indeksu
//        $modelsArticles = Articles::find()
//                ->with(['tags', 'relationsWithPortals'])
//                ->andWhere(['>', 'articles.id_article', $modelIndexSync->last_id])
//                ->limit($limit)
//                ->orderBy('articles.id_article ASC')
//                ->all();
//        
//        //jeśli nie istnieje - pobieramy X pierwszy artykułów z bazy
//        
//        if(empty($modelsArticles)) {
//            return Controller::EXIT_CODE_NORMAL;
//        }
//
//        //zapisujemy artykuły w indeksie
//        $lastId = null;
////        foreach($articles as $article) {
////            $lastId = $article['id'];
////            $result = IndexArticles::addRecord($article);
////        }
//        foreach($modelsArticles as $modelArticle) {
////            $lastId = $modelArticle->id_article;
//            $portals = [];
//            $tags = '';
//            if(!empty($modelArticle->relationsWithPortals)) {
//                foreach($modelArticle->relationsWithPortals as $modelRelationWithPortal) {
//                    $portals[] = $modelRelationWithPortal->id_portal;
//                }
//            }
//            if(!empty($modelArticle->tags)) {
//                foreach($modelArticle->tags as $modelTag) {
//                    $tags .= $modelTag->name.', ';
//                }
//                $tags = rtrim($tags, ", ");
//            }
//            
//            $content = strip_tags($modelArticle->content);
//            $content = str_replace(array("\r", "\n"), ' ', $content);
//            $content = preg_replace('!\s+!', ' ', $content);
//            
//            $article = [
//                'id' => $modelArticle->id_article,
//                'title' => strip_tags($modelArticle->title),
//                'lead' => strip_tags($modelArticle->lead),
//                'content' => $content,
//                'image_filename' => $modelArticle->image_filename,
//                'publication_datetime' => $modelArticle->publication_datetime,
//                'promoted_start' => !empty($modelArticle->promoted_start_date) ? strtotime($modelArticle->promoted_start_date) : 0,
//                'promoted_end' => !empty($modelArticle->promoted_end_date) ? strtotime($modelArticle->promoted_end_date) : 0,
//                'sponsored_start' => !empty($modelArticle->sponsored_start_date) ? strtotime($modelArticle->sponsored_start_date) : 0,
//                'sponsored_end' => !empty($modelArticle->sponsored_end_date) ? strtotime($modelArticle->sponsored_end_date) : 0,
//                'tags' => $tags,
//                'portals' => $portals
//            ];
////            var_dump($article); exit;
//            $result = IndexArticles::addRecord($article);
//            $this->stdout('Article #'.$modelArticle->id_article.' - '.($result ? 'OK' : 'FAIL')."\n");
////            exit;
//        }
        
        //zapisujemy informację o ostatnim przetworzonym ID artykułu
//        $modelIndexSync->last_id = $lastId;
//        $modelIndexSync->save();
        
        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionDeleteArticlesIndex()
    {
        IndexArticles::deleteIndex();
        
//        $modelIndexSync = $this->getModelIndexSync('articles');
//        $modelIndexSync->last_id = 0;
//        $modelIndexSync->save();
        
        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionCreateArticlesIndex()
    {
        IndexArticles::createIndex();
        return Controller::EXIT_CODE_NORMAL;
    }
    
//    protected function getModelIndexSync($indexName)
//    {
//        $modelIndexSync = ElasticsearchIndexesSync::find()
//                ->where([
//                    '_id' => $indexName
//                ])
//                ->one();
//        
//        if(empty($modelIndexSync)) {
//            $modelIndexSync = new ElasticsearchIndexesSync();
//            $modelIndexSync->_id = $indexName;
//            $modelIndexSync->last_id = 0;
//            $modelIndexSync->save();
//        }
//        
//        return $modelIndexSync;
//    }
    
    public function actionUpdateProductsIndex()
    {
        $limit = 2500;
        
        $modelIndexSync = ElasticsearchIndexesSync::getModel(IndexProducts::index());
        
        $idsProducts = Products::find()
                ->select(['products.id_product'])
                ->where(['>', 'products.id_product', $modelIndexSync->last_id])
//                ->where(['products.id_product' => 21419])
                ->limit($limit)
                ->orderBy('products.id_product ASC')
                ->column();

        if(empty($idsProducts)) {
            return Controller::EXIT_CODE_NORMAL;
        }
        
        $incorrectEntries = [];
        
        $results = IndexProducts::refreshRecords($idsProducts);
        ksort($results);
        $correctMaxId = null;
        foreach($results as $idProduct => $result) {
            if(!$result) {
                $incorrectEntries[$idProduct] = $idProduct;
            } else {
                $correctMaxId = $idProduct;
            }
            $this->stdout('Product #'.$idProduct.' - '.($result ? 'OK' : 'FAIL')."\n");
        }
        
        if(!empty($incorrectEntries)) {
            $this->stdout('Inorrect: '. implode(",", $incorrectEntries)."\n");
        }
        
        $modelIndexSync = ElasticsearchIndexesSync::getModel(IndexProducts::index());
        if($correctMaxId !== null && $correctMaxId > $modelIndexSync->last_id) {
            $previousLastId = $modelIndexSync->last_id;
            $modelIndexSync->last_id = $correctMaxId;
            $modelIndexSync->save();
            $this->stdout('Saved last_id: '.$previousLastId.' -> '.$correctMaxId."\n");
        }
        
        return Controller::EXIT_CODE_NORMAL;
        
//        $modelsProducts = Products::find()
//                ->with(['tags', 'relationsWithPresentations.presentation'])
////                ->where(['products.id_product' => 202])
//                ->andWhere(['>', 'products.id_product', $modelIndexSync->last_id])
//                ->limit($limit)
//                ->orderBy('products.id_product ASC')
//                ->all();
////        var_dump(count($modelsProducts)); exit;
//        if(empty($modelsProducts)) {
//            return Controller::EXIT_CODE_NORMAL;
//        }
//
//        foreach($modelsProducts as $modelProduct) {
//            $portals = [];
//            $tags = '';
//            if(!empty($modelProduct->relationsWithPresentations)) {
//                foreach($modelProduct->relationsWithPresentations as $modelRelationWithPresentation) {
//                    $portals[] = $modelRelationWithPresentation->presentation->id_portal;
//                }
//            }
//            if(!empty($modelProduct->tags)) {
//                foreach($modelProduct->tags as $modelTag) {
//                    $tags .= $modelTag->name.', ';
//                }
//                $tags = rtrim($tags, ", ");
//            }
//            
//            $lead = strip_tags($modelProduct->lead);
//            $lead = str_replace(array("\r", "\n"), ' ', $lead);
//            $lead = preg_replace('!\s+!', ' ', $lead);
////            $lead = utf8_encode($lead);
//            $lead = mb_convert_encoding($lead,'UTF-8','UTF-8');
//            
//            $description = strip_tags($modelProduct->description);
//            $description = str_replace(array("\r", "\n"), ' ', $description);
//            $description = preg_replace('!\s+!', ' ', $description);
//            $description = mb_convert_encoding($description,'UTF-8','UTF-8');
////            var_dump(json_encode($description)); exit;
//            
//            $product = [
//                'id' => $modelProduct->id_product,
//                'id_company' => $modelProduct->id_company,
//                'name' => strip_tags($modelProduct->name).' test123',
//                'short_name' => strip_tags($modelProduct->short_name),
//                'lead' => $lead,
//                'description' => $description,
//                'image_filename' => $modelProduct->image_filename,
//                'tags' => $tags,
//                'portals' => $portals
//            ];
//
//            $result = IndexProducts::addRecord($product);
//            $this->stdout('Product #'.$modelProduct->id_product.' - '.($result ? 'OK' : 'FAIL')."\n");
//        }
//                
//        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionDeleteProductsIndex()
    {
        IndexProducts::deleteIndex();        
        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionCreateProductsIndex()
    {
        IndexProducts::createIndex();
        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionUpdateTrainingsIndex()
    {
        $limit = 5000;
        
        $modelIndexSync = ElasticsearchIndexesSync::getModel(IndexTrainings::index());
        
        $idsTrainings = Trainings::find()
                ->select(['trainings.id_training'])
                ->andWhere(['>', 'trainings.id_training', $modelIndexSync->last_id])
                ->limit($limit)
                ->orderBy('trainings.id_training ASC')
                ->column();

        if(empty($idsTrainings)) {
            return Controller::EXIT_CODE_NORMAL;
        }
        
        $incorrectEntries = [];
        
        $results = IndexTrainings::refreshRecords($idsTrainings);
        ksort($results);
        $correctMaxId = null;
        foreach($results as $idTraining => $result) {
            if(!$result) {
                $incorrectEntries[$idTraining] = $idTraining;
            } else {
                $correctMaxId = $idTraining;
            }
            $this->stdout('Training #'.$idTraining.' - '.($result ? 'OK' : 'FAIL')."\n");
        }
        
        if(!empty($incorrectEntries)) {
            $this->stdout('Inorrect: '. implode(",", $incorrectEntries)."\n");
        }
        
        $modelIndexSync = ElasticsearchIndexesSync::getModel(IndexTrainings::index());
        if($correctMaxId !== null && $correctMaxId > $modelIndexSync->last_id) {
            $previousLastId = $modelIndexSync->last_id;
            $modelIndexSync->last_id = $correctMaxId;
            $modelIndexSync->save();
            $this->stdout('Saved last_id: '.$previousLastId.' -> '.$correctMaxId."\n");
        }
        
        return Controller::EXIT_CODE_NORMAL;
        
        
//        $modelsTrainings = Trainings::find()
//                ->with([
//                    'dates' => function($q) {
//                        $q->orderBy('date_start ASC');
//                    }, 
//                    'relationsWithPortals'
//                ])
////                ->where(['products.id_product' => 202])
//                ->andWhere(['>', 'trainings.id_training', $modelIndexSync->last_id])
//                ->limit($limit)
//                ->orderBy('trainings.id_training ASC')
//                ->all();
////        var_dump(count($modelsTrainings)); exit;
//        if(empty($modelsTrainings)) {
//            return Controller::EXIT_CODE_NORMAL;
//        }
//
//        foreach($modelsTrainings as $modelTraining) {
//            $portals = $dates = [];
//            if(!empty($modelTraining->relationsWithPortals)) {
//                foreach($modelTraining->relationsWithPortals as $modelRelationWithPortal) {
//                    $portals[] = $modelRelationWithPortal->id_portal;
//                }
//            }
//            if(!empty($modelTraining->dates)) {
//                foreach($modelTraining->dates as $modelDate) {
//                    $dates[] = [
//                        'id_date' => $modelDate->id_date,
//                        'date_start' => $modelDate->date_start,
//                        'date_end' => $modelDate->date_end,
//                        'locality' => $modelDate->locality,
//                        'postcode' => $modelDate->postcode,
//                        'street' => $modelDate->street,
//                        'building_number' => $modelDate->building_number,
//                        'apartment_number' => $modelDate->apartment_number,
//                        'address_description' => $modelDate->address_description,                        
//                    ];
//                }
//            }
//            
//            $contentTarget = $contentLeader = $contentProgram = $contentDetails = $contentAll = '';
//            if(!empty($modelTraining->content_target)) {
//                $contentTarget = strip_tags($modelTraining->content_target);
//                $contentTarget = str_replace(array("\r", "\n"), ' ', $contentTarget);
//                $contentTarget = preg_replace('!\s+!', ' ', $contentTarget);
//                $contentTarget = mb_convert_encoding($contentTarget, 'UTF-8','UTF-8');
//            }
//            if(!empty($modelTraining->content_leader)) {
//                $contentLeader = strip_tags($modelTraining->content_leader);
//                $contentLeader = str_replace(array("\r", "\n"), ' ', $contentLeader);
//                $contentLeader = preg_replace('!\s+!', ' ', $contentLeader);
//                $contentLeader = mb_convert_encoding($contentLeader, 'UTF-8','UTF-8');
//            }
//            if(!empty($modelTraining->content_program)) {
//                $contentProgram = strip_tags($modelTraining->content_program);
//                $contentProgram = str_replace(array("\r", "\n"), ' ', $contentProgram);
//                $contentProgram = preg_replace('!\s+!', ' ', $contentProgram);
//                $contentProgram = mb_convert_encoding($contentProgram, 'UTF-8','UTF-8');
//            }
//            if(!empty($modelTraining->content_details)) {
//                $contentDetails = strip_tags($modelTraining->content_details);
//                $contentDetails = str_replace(array("\r", "\n"), ' ', $contentDetails);
//                $contentDetails = preg_replace('!\s+!', ' ', $contentDetails);
//                $contentDetails = mb_convert_encoding($contentDetails, 'UTF-8','UTF-8');
//            }
//            if(!empty($modelTraining->content_all)) {
//                $contentAll = strip_tags($modelTraining->content_all);
//                $contentAll = str_replace(array("\r", "\n"), ' ', $contentAll);
//                $contentAll = preg_replace('!\s+!', ' ', $contentAll);
//                $contentAll = mb_convert_encoding($contentAll, 'UTF-8','UTF-8');
//            }
//            
//            $training = [
//                'id' => $modelTraining->id_training,
//                'training_type' => $modelTraining->training_type,
//                'name' => strip_tags($modelTraining->name).' test123',
//                'content_target' => $contentTarget,
//                'content_leader' => $contentLeader,
//                'content_program' => $contentProgram,
//                'content_details' => $contentDetails,
//                'content_all' => $contentAll,
//                'price' => $modelTraining->price,
//                'price_description' => $modelTraining->price_description,
//                'status' => $modelTraining->status,
//                'dates' => $dates,
//                'portals' => $portals
//            ];
//
//            $result = IndexTrainings::addRecord($training);
//            $this->stdout('Training #'.$modelTraining->id_training.' - '.($result ? 'OK' : 'FAIL')."\n");
//        }
//                
//        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionDeleteTrainingsIndex()
    {
        IndexTrainings::deleteIndex();        
        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionCreateTrainingsIndex()
    {
        IndexTrainings::createIndex();
        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionUpdatePresentationsIndex2()
    {
        $limit = 5000;
        
        $modelIndexSync = ElasticsearchIndexesSync::getModel(IndexPresentations::index());
        
        $modelsPresentations = CompaniesPresentations::find()
                ->with([
                    'relationsWithTabs' => function($q) {
                        $q->where(['status' => Status::ACTIVE]);
                        $q->indexBy('id_tab');
                        $q->with([
                            'page' => function($q2) {
                                $q2->select(['id_company_tab', 'lead']);
                            }
                        ]);
                    }
                ])
                ->joinWith(['company'])
                ->where([
                    'companies.status' => Status::ACTIVE,
                    'companies_presentations.status' => Status::ACTIVE
                ])
                ->andWhere(['>', 'companies_presentations.id_presentation', $modelIndexSync->last_id])
                ->limit($limit)
                ->orderBy('companies_presentations.id_presentation ASC')
                ->all();
//        var_dump(count($modelsPresentations)); exit;
        if(empty($modelsPresentations)) {
            return Controller::EXIT_CODE_NORMAL;
        }

        foreach($modelsPresentations as $modelPresentation) {
            $modelCompany = $modelPresentation->company;
            $tabs = [];
            if(!empty($modelPresentation->relationsWithTabs)) {
                foreach($modelPresentation->relationsWithTabs as $modelRelationWithTab) {
                    $tabs[] = $modelRelationWithTab->id_tab;
                }
            }
            
            $description = '';
            if(!empty($modelPresentation->relationsWithTabs[PresentationsTabs::TAB_ABOUT_COMPANY]->page->lead)) {
                $description = strip_tags($modelPresentation->relationsWithTabs[PresentationsTabs::TAB_ABOUT_COMPANY]->page->lead);
                $description = str_replace(array("\r", "\n"), ' ', $description);
                $description = preg_replace('!\s+!', ' ', $description);
                $description = mb_convert_encoding($description, 'UTF-8','UTF-8');
            }
            
            $visibleContactDataFields = $modelPresentation->getSettingsKey('visible_contact_data_fields');
            
            $presentation = [
                'id' => $modelPresentation->id_presentation,
                'id_company' => $modelCompany->id_company,
                'name' => strip_tags($modelCompany->name).' test123',
                'description' => $description,                
                'logo_filename' => $modelCompany->logo_filename,          
                'id_portal' => $modelPresentation->id_portal,
                'street' => $modelCompany->street,
                'building_number' => $modelCompany->building_number,
                'apartment_number' => $modelCompany->apartment_number,
                'address_cont' => $modelCompany->address_cont,
                'postcode' => $modelCompany->postcode,
                'locality' => $modelCompany->locality, 
                'region' => $modelCompany->region, 
                'status' => $modelCompany->status,
                'visible_contact_data_fields' => !empty($visibleContactDataFields) ? $visibleContactDataFields : [],
                'tabs' => $tabs,
            ];

            $result = IndexPresentations::addRecord($presentation);
            $this->stdout('Presentation #'.$modelPresentation->id_presentation.' - '.($result ? 'OK' : 'FAIL')."\n");
        }
                
        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionUpdatePresentationsIndex()
    {
        $limit = 2000;
        
        $modelIndexSync = ElasticsearchIndexesSync::getModel(IndexPresentations::index());
        
        $idsPresentation = CompaniesPresentations::find()
                ->select(['companies_presentations.id_presentation'])
                ->join('INNER JOIN', 'companies', 'companies.id_company = companies_presentations.id_company')
                ->where([
                    'companies.status' => Status::ACTIVE,
                    'companies_presentations.status' => Status::ACTIVE
                ])
                ->andWhere(['>', 'companies_presentations.id_presentation', $modelIndexSync->last_id])
                ->limit($limit)
                ->orderBy('companies_presentations.id_presentation ASC')
                ->column();

        if(empty($idsPresentation)) {
            return Controller::EXIT_CODE_NORMAL;
        }
        
        
        $incorrectEntries = [];        
        
        $results = IndexPresentations::refreshRecords($idsPresentation);
        ksort($results);
        $correctMaxId = null;
        foreach($results as $idPresentation => $result) {
            if(!$result) {
                $incorrectEntries[$idPresentation] = $idPresentation;
            } else {
                $correctMaxId = $idPresentation;
            }
            $this->stdout('Presentation #'.$idPresentation.' - '.($result ? 'OK' : 'FAIL')."\n");
        }
        
        if(!empty($incorrectEntries)) {
            $this->stdout('Inorrect: '. implode(",", $incorrectEntries)."\n");
        }
        
        $modelIndexSync = ElasticsearchIndexesSync::getModel(IndexPresentations::index());
        if($correctMaxId !== null && $correctMaxId > $modelIndexSync->last_id) {
            $previousLastId = $modelIndexSync->last_id;
            $modelIndexSync->last_id = $correctMaxId;
            $modelIndexSync->save();
            $this->stdout('Saved last_id: '.$previousLastId.' -> '.$correctMaxId."\n");
        }
                
        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionDeletePresentationsIndex()
    {
        IndexPresentations::deleteIndex();        
        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionCreatePresentationsIndex()
    {
        IndexPresentations::createIndex();
        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionUpdateAdvertisementsIndex()
    {
        $limit = 5000;
        
        $modelIndexSync = ElasticsearchIndexesSync::getModel(IndexAdvertisements::index());
        
        $idsAdvertisements = Advertisements::find()
                ->select(['advertisements.id_advertisement'])
                ->andWhere(['>', 'advertisements.id_advertisement', $modelIndexSync->last_id])
                ->limit($limit)
                ->orderBy('advertisements.id_advertisement ASC')
                ->column();

        if(empty($idsAdvertisements)) {
            return Controller::EXIT_CODE_NORMAL;
        }
        
        $incorrectEntries = [];        
        
        $results = IndexAdvertisements::refreshRecords($idsAdvertisements);
        ksort($results);
        $correctMaxId = null;
        foreach($results as $idAdvertisement => $result) {
            if(!$result) {
                $incorrectEntries[$idAdvertisement] = $idAdvertisement;
            } else {
                $correctMaxId = $idAdvertisement;
            }
            $this->stdout('Advertisement #'.$idAdvertisement.' - '.($result ? 'OK' : 'FAIL')."\n");
        }
        
        if(!empty($incorrectEntries)) {
            $this->stdout('Inorrect: '. implode(",", $incorrectEntries)."\n");
        }
        
        $modelIndexSync = ElasticsearchIndexesSync::getModel(IndexAdvertisements::index());
        if($correctMaxId !== null && $correctMaxId > $modelIndexSync->last_id) {
            $previousLastId = $modelIndexSync->last_id;
            $modelIndexSync->last_id = $correctMaxId;
            $modelIndexSync->save();
            $this->stdout('Saved last_id: '.$previousLastId.' -> '.$correctMaxId."\n");
        }
        
        return Controller::EXIT_CODE_NORMAL;
        
//        $modelsAdvertisements = Advertisements::find()
//                ->join('INNER JOIN', 'users', 'users.id_user = advertisements.id_user')
//                ->where([
//                    'advertisements.status' => AdvertisementStatus::ACTIVE,
//                    'users.status' => Status::ACTIVE
//                ])
//                ->andWhere(['>', 'advertisements.id_advertisement', $modelIndexSync->last_id])
//                ->limit($limit)
//                ->orderBy('advertisements.id_advertisement ASC')
//                ->all();
////        var_dump(count($modelsAdvertisements)); exit;
//        if(empty($modelsAdvertisements)) {
//            return Controller::EXIT_CODE_NORMAL;
//        }
//
//        foreach($modelsAdvertisements as $modelAdvertisement) {            
//            $advertisement = [
//                'id' => $modelAdvertisement->id_advertisement,
//                'title' => strip_tags($modelAdvertisement->title).' test123',
//                'image_filename' => $modelAdvertisement->image_filename,          
//                'id_portal' => $modelAdvertisement->id_portal,
//                'locality' => $modelAdvertisement->locality,
//                'advertisement_type' => $modelAdvertisement->advertisement_type,
//                'created_at' => $modelAdvertisement->created_at
//            ];
//
//            $result = IndexAdvertisements::addRecord($advertisement);
//            $this->stdout('Advertisement #'.$modelAdvertisement->id_advertisement.' - '.($result ? 'OK' : 'FAIL')."\n");
//        }
//                
//        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionDeleteAdvertisementsIndex()
    {
        IndexAdvertisements::deleteIndex();        
        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionCreateAdvertisementsIndex()
    {
        IndexAdvertisements::createIndex();
        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionUpdateFairsIndex()
    {
        $limit = 5000;
        
        $modelIndexSync = ElasticsearchIndexesSync::getModel(IndexFairs::index());
        
        $idsFairs = Fairs::find()
                ->select(['fairs.id_fair'])
                ->andWhere(['>', 'fairs.id_fair', $modelIndexSync->last_id])
                ->limit($limit)
                ->orderBy('fairs.id_fair ASC')
                ->column();

        if(empty($idsFairs)) {
            return Controller::EXIT_CODE_NORMAL;
        }
        
        $incorrectEntries = [];
        
        $results = IndexFairs::refreshRecords($idsFairs);
        ksort($results);
        $correctMaxId = null;
        foreach($results as $idFair => $result) {
            if(!$result) {
                $incorrectEntries[$idFair] = $idFair;
            } else {
                $correctMaxId = $idFair;
            }
            $this->stdout('Fair #'.$idFair.' - '.($result ? 'OK' : 'FAIL')."\n");
        }
        
        if(!empty($incorrectEntries)) {
            $this->stdout('Inorrect: '. implode(",", $incorrectEntries)."\n");
        }
        
        $modelIndexSync = ElasticsearchIndexesSync::getModel(IndexFairs::index());
        if($correctMaxId !== null && $correctMaxId > $modelIndexSync->last_id) {
            $previousLastId = $modelIndexSync->last_id;
            $modelIndexSync->last_id = $correctMaxId;
            $modelIndexSync->save();
            $this->stdout('Saved last_id: '.$previousLastId.' -> '.$correctMaxId."\n");
        }
        
        return Controller::EXIT_CODE_NORMAL;
        
//        $modelsFairs = Fairs::find()
//                ->with([
//                    'series.relationsWithPortals',
//                    'tags'
//                ])
//                ->andWhere(['>', 'fairs.id_fair', $modelIndexSync->last_id])
//                ->limit($limit)
//                ->orderBy('fairs.id_fair ASC')
//                ->all();
////        var_dump(count($modelsFairs)); exit;
//        if(empty($modelsFairs)) {
//            return Controller::EXIT_CODE_NORMAL;
//        }
//
//        foreach($modelsFairs as $modelFair) {        
//            $portals = [];
//            if(!empty($modelFair->series->relationsWithPortals)) {
//                foreach($modelFair->series->relationsWithPortals as $modelRelationWithPortal) {
//                    $portals[] = $modelRelationWithPortal->id_portal;
//                }
//            }
//            $tags = '';
//            if(!empty($modelFair->tags)) {
//                foreach($modelFair->tags as $modelTag) {
//                    $tags .= $modelTag->name.', ';
//                }
//                $tags = rtrim($tags, ", ");
//            }
//            
//            $content = strip_tags($modelFair->content);
//            $content = str_replace(array("\r", "\n"), ' ', $content);
//            $content = preg_replace('!\s+!', ' ', $content);
//            $content = mb_convert_encoding($content, 'UTF-8','UTF-8');
//            
//            $fair = [
//                'id' => $modelFair->id_fair,
//                'name' => strip_tags($modelFair->name).' test123',
//                'image_filename' => $modelFair->image_filename,          
//                'portals' => $portals,
//                'date_start' => $modelFair->date_start,
//                'date_end' => $modelFair->date_end,
//                'date_description' => $modelFair->date_description,
//                'locality' => $modelFair->locality,
//                'content' => $content,
//                'tags' => $tags
//            ];
//
//            $result = IndexFairs::addRecord($fair);
//            $this->stdout('Fair #'.$modelFair->id_fair.' - '.($result ? 'OK' : 'FAIL')."\n");
//        }
//                
//        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionDeleteFairsIndex()
    {
        IndexFairs::deleteIndex();        
        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionCreateFairsIndex()
    {
        IndexFairs::createIndex();
        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionUpdateBooksIndex()
    {
        $limit = 5000;
        
        $modelIndexSync = ElasticsearchIndexesSync::getModel(IndexBooks::index());
        
        $idsBooks = Books::find()
                ->select(['books.id_book'])
                ->join('INNER JOIN', 'publishing_houses', 'publishing_houses.id_publishing_house = books.id_publishing_house')
                ->where([
                    'publishing_houses.status' => Status::ACTIVE
                ])
                ->andWhere(['>', 'books.id_book', $modelIndexSync->last_id])
                ->limit($limit)
                ->orderBy('books.id_book ASC')
                ->column();

        if(empty($idsBooks)) {
            return Controller::EXIT_CODE_NORMAL;
        }
        
        $incorrectEntries = [];
        
        $results = IndexBooks::refreshRecords($idsBooks);
        ksort($results);
        $correctMaxId = null;
        foreach($results as $idBook => $result) {
            if(!$result) {
                $incorrectEntries[$idBook] = $idBook;
            } else {
                $correctMaxId = $idBook;
            }
            $this->stdout('Book #'.$idBook.' - '.($result ? 'OK' : 'FAIL')."\n");
        }
        
        if(!empty($incorrectEntries)) {
            $this->stdout('Inorrect: '. implode(",", $incorrectEntries)."\n");
        }
               
        $modelIndexSync = ElasticsearchIndexesSync::getModel(IndexBooks::index());
        if($correctMaxId !== null && $correctMaxId > $modelIndexSync->last_id) {
            $previousLastId = $modelIndexSync->last_id;
            $modelIndexSync->last_id = $correctMaxId;
            $modelIndexSync->save();
            $this->stdout('Saved last_id: '.$previousLastId.' -> '.$correctMaxId."\n");
        }
        
        return Controller::EXIT_CODE_NORMAL;
        
        
        
        
        
        
//        $categoriesHasPortals = [];
//        $portalsHasBookstoreCategories = PortalsHasBookstoreCategories::find()->asArray()->all();
//        if(empty($portalsHasBookstoreCategories)) {
//            return Controller::EXIT_CODE_NORMAL;
//        }
//        foreach($portalsHasBookstoreCategories as $portalHasBookstoreCategory) {
//            $categoriesHasPortals[$portalHasBookstoreCategory['id_category']][$portalHasBookstoreCategory['id_portal']] = $portalHasBookstoreCategory['id_portal'];
//        }
//        unset($portalsHasBookstoreCategories);
//        
//        $modelsBooks = Books::find()
//                ->select(['books.id_book', 'books.id_publishing_house', 'books.title', 'books.author', 'books.book_type', 'books.image_filename', 'books.price', 'books.average_rating', 'books.release_date'])
//                ->with([
//                    'relationsWithCategories',
//                    'tags',                    
//                ])
//                ->joinWith([
//                    'publishingHouse' => function($q) {
//                        $q->select(['id_publishing_house', 'name', 'status']);
//                    }
//                ])
//                ->where([
//                    'publishing_houses.status' => Status::ACTIVE
//                ])
//                ->andWhere(['>', 'books.id_book', $modelIndexSync->last_id])
//                ->limit($limit)
//                ->orderBy('books.id_book ASC')
//                ->all();
//
//        if(empty($modelsBooks)) {
//            return Controller::EXIT_CODE_NORMAL;
//        }
//
//        foreach($modelsBooks as $modelBook) {        
//            $portals = [];
//            if(!empty($modelBook->relationsWithCategories)) {
//                foreach($modelBook->relationsWithCategories as $modelRelationWithCategory) {
//                    if(!empty($categoriesHasPortals[$modelRelationWithCategory->id_category])) {
//                        $portals = array_merge($portals, array_values($categoriesHasPortals[$modelRelationWithCategory->id_category]));
//                    }
//                }
//            }
//            $tags = '';
//            if(!empty($modelBook->tags)) {
//                foreach($modelBook->tags as $modelTag) {
//                    $tags .= $modelTag->name.', ';
//                }
//                $tags = rtrim($tags, ", ");
//            }
//            
//            $book = [
//                'id' => $modelBook->id_book,
//                'title' => strip_tags($modelBook->title).' test123',
//                'image_filename' => $modelBook->image_filename,     
//                'id_publishing_house' => $modelBook->id_publishing_house, 
//                'publishing_house' => $modelBook->publishingHouse->name, 
//                'author' => $modelBook->author, 
//                'book_type' => $modelBook->book_type,
//                'price' => $modelBook->price, 
//                'average_rating' => $modelBook->average_rating, 
//                'release_date' => $modelBook->release_date,
//                'portals' => $portals,
//                'tags' => $tags
//            ];
//
//            $result = IndexBooks::addRecord($book);
//            $this->stdout('Book #'.$modelBook->id_book.' - '.($result ? 'OK' : 'FAIL')."\n");
//        }
//                
//        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionDeleteBooksIndex()
    {
        IndexBooks::deleteIndex();        
        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionCreateBooksIndex()
    {
        IndexBooks::createIndex();
        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionDeleteAutocompleteIndex()
    {
        IndexAutocomplete::deleteIndex();        
        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionCreateAutocompleteIndex()
    {
        IndexAutocomplete::createIndex();
        return Controller::EXIT_CODE_NORMAL;
    }
}


Creat By MiNi SheLL
Email: jattceo@gmail.com