<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\crm\lib;
use think\Controller;
use think\Exception;

class RunStatistics extends Controller {

	protected $start;
	protected $end;
	protected $type;

	public function __construct(){
		$allMethod = $this->allMethod;
		foreach ($allMethod as $key => $value) {
			$table = $value['table'];
			$column= explode(',',$value['column']);
			// var_dump($column);
            $sql = '';
			if($this->isTableExist($table) === false){
				$sql .= 'CREATE TABLE `'.trim($table).'`(';
				$sql .= $this->createColumn($column);
				$sql .= ') COLLATE=\'utf8_unicode_ci\' ';
				$sql .= ' ENGINE=InnoDB COMMENT="'.$value['name'].'"';
            }
			echo $sql;
		}
	}
	// 查看表是否存在
	function isTableExist($table){
        $sql = "show tables;";
        $res = db()->master()->query($sql);
        foreach($res as $value){
            if(array_search($table, $value)){
                return true;
            }
        }
        return false;
    }
    /**
     * 创建表时组成字段语句
     * @param  type $config
     * @return type
     */
    function createColumn($column){
        $ret = '';
        $primary = '';
        $keys = '';
        $ret .= '`id` int(11) unsigned NOT NULL AUTO_INCREMENT,';
        foreach ($column as $value){
            $ret .= "`".$value."` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL ,";
        }        
        $ret .= 'PRIMARY KEY (`id`)';
        return $ret;
    }
}