<?php

namespace frontend\jobs;

use Yii;
use yii\base\BaseObject;
use yii\httpclient\Client;
use backend\models\Galleriesbe;
use backend\models\Sysmodels;
use backend\models\Categoriesbe;
use backend\models\Productsbe;
use backend\models\Lookuptablebe;
use yii\helpers\Json;

class Checkids extends BaseObject implements \common\components\Queue\JobInterface
{
    public $filepath;
    public $model;
        /**
     * Runs Imports from Various Sources.
     *
     * @param [type] $queue
     * @return void
     */
    public function execute($queue)
    {
        $model = $this->model;
        echo $model. PHP_EOL;
        $data = json_decode(file_get_contents($this->filepath), 1);
        $lmodel = $model::instance();
        $pk = $lmodel->tableSchema->primaryKey;
        $pkex = $pk[0];
        echo $pkex . PHP_EOL;
        echo 'Get S: ' . date('H:i:s') . PHP_EOL;
        $results = $model::find()->select($pkex)->column();
        echo 'GET F: ' . date('H:i:s'). PHP_EOL;
        
        $local = [];
        $deleted = 0;
        echo 'AD S: ' . date('H:i:s') . PHP_EOL;
        echo '---------------------------'. PHP_EOL;
        //$diff = array_diff($data, $results);
        //echo count($diff) .PHP_EOL;
        foreach($results as $id){
            !in_array($id, $data) ? $diff[] = $id : false;
        }
        echo '---------------------------'. PHP_EOL;
        echo 'AD F: ' . date('H:i:s') . PHP_EOL;
        if(!empty($diff)){
            $model::deleteAll([$pkex => array_values($diff)]);
        }
        unlink($this->filepath);
        
    }

    public function getTtr(){
        return 3600;
    }
}
