<?php

use Illuminate\Support\Facades\DB;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateVLeftmenuView extends Migration
{
    public function up()
    {
        DB::statement("CREATE VIEW `v_leftmenu` AS
            SELECT 
                `funmenu`.`icon` AS `icon`,
                `funmenu`.`MenuName` AS `MenuName`,
                `funmenu`.`Corder` AS `FunMenuCorder`,
                `funmenudetail`.`FunMenuId` AS `FunMenuId`,
                `funmenudetail`.`FunId` AS `FunId`,
                `functions`.`FunName` AS `FunName`,
                `functions`.`FunLink` AS `FunLink`,
                `functions`.`FunDesc` AS `FunDesc`,
                `functions`.`Valid` AS `Valid`,
                `functions`.`created_at` AS `created_at`,
                `functions`.`updated_at` AS `updated_at`,
                `functions`.`Oid` AS `Oid`,
                `funmenudetail`.`Corder` AS FunMenuDetailCOrder
            FROM
                ((`funmenu`
                JOIN funmenudetail ON ((`funmenu`.`id` = `funmenudetail`.`FunMenuId`)))
                JOIN functions ON ((`funmenudetail`.`FunId` = `functions`.`id`)))
            WHERE
                `functions`.`id` IN (SELECT 
                        `Functions_1`.`id`
                    FROM
                        functions Functions_1
                    WHERE
                        (LOCATE((CONVERT( CONCAT(',', `Functions_1`.`id`, ',') USING UTF8MB3) COLLATE utf8_unicode_ci),
                                CONVERT( CONCAT(',',
                                        (SELECT 
                                                `funusergroups`.`FunList`
                                            FROM
                                                funusergroups
                                            WHERE
                                                (`funusergroups`.`id` = 1)),
                                        ',') USING UTF8MB3)) > 0))
            ORDER BY `funmenu`.`Corder` , `funmenudetail`.`Corder`
            LIMIT 0 , 1000
        ");
    }

    public function down()
    {
        DB::statement("DROP VIEW v_leftmenu");
    }
}
