InsertCrmArchive.php 1.22 KB
<?php namespace App\Console\Commands;

use Illuminate\Console\Command;
//use Mail;
use DB;
use Config;

use App\Models\User;
use App\Models\Accesslog;

use App\Models\CRMCall;
use Schema;
use PDO;
use App\Models\Notification;
use App\Jobs\KHRMSLib;

use Input;
use App\Models\Sipid;
use App\Models\Kqueue;
use App\Models\Dialline;
use App\Models\Session;

use Illuminate\Database\Schema\Blueprint;

class InsertCrmArchive extends Command {

	/**
	 * The console command name.
	 *
	 * @var string
	 */
	protected $signature = 'InsertCrmArchive';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'Insert updated data into crmcalls_archive from crmcalls';

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function handle()
	{
		echo 'Start';

		$minidObj = DB::selectOne(DB::raw("SELECT MIN(id) AS min_id FROM crmcalls_archive WHERE created_at >= DATE_SUB((SELECT MAX(created_at) from crmcalls_archive), INTERVAL 1 HOUR)"));
		
		$min_id = $minidObj->min_id;

		DB::delete(DB::raw("DELETE FROM crmcalls_archive WHERE id >= '$min_id'"));

		DB::insert(DB::raw("insert into crmcalls_archive select * from crmcalls where id>(select max(id) from crmcalls_archive)"));

		echo 'End';
	}
}