<?php
/**
 * Created by PhpStorm.
 * User: 陈武杰
 * Date: 2021/1/24
 * Time: 14:31
 */

namespace App\Http\Controllers\Admin;


class CommonController
{
    public function doc()
    {
        $database = env('DB_DATABASE');
        $prefix = '';
        $exclude_tables = "'admin_access','admin_menus','admin_operate_logs','admin_roles','admin_user_resets','admin_users'
                          ,'base_area','base_attachments','base_dictionary'
                          ,'oauth_access_tokens','oauth_auth_codes','oauth_clients','oauth_personal_access_clients','oauth_refresh_tokens'
                          ,'migrations','cache'
                          ";

        $sql = "select TABLE_NAME name,TABLE_COMMENT comment from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='".$database."' and TABLE_NAME not in (".$exclude_tables.")";
        $tables = \DB::select($sql);
        $map1 = $map2 = [];
        $i = round(count($tables)/2);
        foreach ($tables as $k => $v) {
            $name = str_replace($prefix, '', $v->name);
            if ($k >= $i) {
                $map1[$v->name] = $name.'('.$v->comment.')';
            }
            else {
                $map2[$v->name] = $name.'('.$v->comment.')';
            }
        }

        $data1 = [];
        foreach ($map1 as $k => $v){
            $sql = "select COLUMN_NAME name, DATA_TYPE type, COLUMN_COMMENT comment from INFORMATION_SCHEMA.COLUMNS where table_schema = '".$database."' AND table_name = '".$k."'";
            $comment = \DB::select($sql);
            $data1[$v] = $comment;
        }

        $data2 = [];
        foreach ($map2 as $k => $v){
            $sql = "select COLUMN_NAME name, DATA_TYPE type, COLUMN_COMMENT comment from INFORMATION_SCHEMA.COLUMNS where table_schema = '".$database."' AND table_name = '".$k."'";
            $comment = \DB::select($sql);
            $data2[$v] = $comment;
        }

        return view('doc', ['data1' => $data1, 'data2' => $data2]);
    }
}