123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- namespace app\common\model\base\models;
- /**
- * @title : 模型管理
- * @desc :
- * @Author : Rock
- * @Date : 2023-04-11 20:02:14
- */
- use app\common\model\Common;
- use think\facade\Db;
- use think\model\concern\SoftDelete;
- class ModelManage extends Common
- {
- use SoftDelete;
- protected $name = 'system_auto_model';
- protected $pk = 'id';
- protected $append = [
- 'status_txt',
- 'classname'
- ];
- public function getDirnameAttr($value,$data)
- {
- $value = $value??$data['dirname'];
- return str_replace(['/','\\'],'/',$value);
- }
- public function getClassnameAttr($value,$data)
- {
- return $data['name']?self::getCamel($data['name']):'';
- }
- static public function statusList()
- {
- return [1=>'启用',2=>'停用'];
- }
- public function getStatusTxtAttr($value,$row)
- {
- $statusList = self::statusList();
- return $statusList[$value ?? $row['status']] ?? '';
- }
- static public function engineList()
- {
- return ['MyISAM','InnoDB'];
- }
- public function fields()
- {
- return $this->hasMany(ModelFields::class,'name','model_name');
- }
- /**
- * @title: 获取所有数据表
- * @desc: 描述
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-07-26 10:20:58
- */
- static public function getTableList():array
- {
- $sql = "SHOW TABLE STATUS";
- return Db::query($sql);
- }
- /**
- * @title: 检查表是否存在
- * @desc: 描述
- * @param {string} {name} {} {待检查的表名}
- * @return {boolean} {} {} {true存在,false不存在}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-17 08:16:41
- */
- static public function checkTable(string $name):bool
- {
- $database = config('database.connections.mysql.database');
- $sql = "SELECT * FROM information_schema.TABLES WHERE TABLE_NAME = '$name' AND TABLE_SCHEMA='$database'";
- $res = Db::query($sql);
- return $res && count($res) > 0;
- }
- /**
- * @title: 获取数据表主键
- * @desc: 描述
- * @param {string} {table} {} {表名}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-07-26 10:08:30
- */
- static public function pk(string $table):string
- {
- $fieldList = self::getFieldList($table);
- foreach($fieldList as $item){
- if('PRI'==$item['Key']){
- return $item['Field'];
- }
- }
- return '';
- }
- /**
- * @title: 获取数据表注释
- * @desc: 描述
- * @param {string} {table} {} {表名}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-07-26 10:14:13
- */
- static public function getComment(string $table):string
- {
- $database = config('database.connections.mysql.database');
- $sql = "SELECT TABLE_COMMENT FROM information_schema.TABLES WHERE table_name = '$table' AND TABLE_SCHEMA='$database'";
- $res = Db::query($sql);
- return $res?$res[0]['TABLE_COMMENT']:'';
- }
- /**
- * @title: 获取数据表字段
- * @desc: 描述
- * @param {string} {table} {} {表名]}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-07-26 10:15:35
- */
- static public function getFieldList(string $table):array
- {
- $sql = "SHOW FULL COLUMNS FROM `$table`";
- return Db::query($sql);
- }
- /**
- * @title: 创建表
- * @desc: 描述
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-14 09:08:20
- */
- static public function createTable(int $id)
- {
- $info = self::find($id);
- $tableName = $info->getAttr('name');
- $title = $info->title;
- $engine = $info->engine;
- $sql = <<<SQL
- CREATE TABLE IF NOT EXISTS `$tableName` (
- `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
- `create_at` datetime DEFAULT NULL COMMENT '创建时间',
- `update_at` datetime DEFAULT NULL COMMENT '更新时间',
- `delete_at` datetime DEFAULT NULL COMMENT '删除时间',
- PRIMARY KEY (`id`)
- ) ENGINE=$engine AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='$title'
- SQL;
- $res = Db::execute($sql);
- return $res;
- }
- /**
- * @title: 修改表
- * @desc: 描述
- * @param {int} {id} {} {模型ID}
- * @param {string} {oldname} {} {原表名}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-17 09:08:41
- */
- static public function updateTable(int $id,string $oldname)
- {
- $info = self::find($id);
- $name = $info->getAttr('name');
- $engine = $info->getAttr('engine');
- $title = $info->getAttr('title');
- $sql = "ALTER TABLE $oldname RENAME $name" ;
- $res = Db::execute($sql);
- $sql = "ALTER TABLE $name ENGINE = $engine COMMENT = '$title'";
- $res = Db::execute($sql);
- return $res;
- }
- /**
- * @title: 删除表
- * @desc: 描述
- * @param {int} {id} {} {模型ID}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-14 09:08:47
- */
- static public function deleteTable(int $id)
- {
- $info = self::find($id);
- $name = $info->getAttr('name');
- $sql = "DROP TABLE IF EXISTS $name";
- $res = Db::execute($sql);
- return $res;
- }
- /**
- * @title: 复制表
- * @desc: 描述
- * @param {int} $id
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-27 14:37:08
- */
- static public function copyTable(int $id)
- {
- $info = self::find($id);
- $name = $info->getAttr('name');
- $title = $info->getAttr('title');
- $sql = "SHOW CREATE TABLE $name";
- $tableSql = Db::query($sql);
- if($tableSql){
- $sql = $tableSql[0]['Create Table'];
- $sql = str_replace("TABLE `$name`","TABLE `{$name}_copy`",$sql);
- $sql = str_replace("COMMENT='$title'","COMMENT='{$title}副本'",$sql);
- Db::execute($sql);
- return true;
- }
- return false;
- }
- /**
- * @title: 获取英文的驼峰写法
- * @desc: 描述
- * @param {string} $name
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-25 11:46:37
- */
- static public function getCamel(string $name)
- {
- $arr = explode('_',$name);
- $arr = array_filter($arr);
- $classname = "";
- foreach($arr as $item){
- $classname .= ucfirst($item);
- }
- return $classname;
- }
- }
|