ModelManage.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace app\common\model\base\models;
  3. /**
  4. * @title : 模型管理
  5. * @desc :
  6. * @Author : Rock
  7. * @Date : 2023-04-11 20:02:14
  8. */
  9. use app\common\model\Common;
  10. use think\facade\Db;
  11. use think\model\concern\SoftDelete;
  12. class ModelManage extends Common
  13. {
  14. use SoftDelete;
  15. protected $name = 'system_auto_model';
  16. protected $pk = 'id';
  17. protected $append = [
  18. 'status_txt',
  19. 'classname'
  20. ];
  21. public function getDirnameAttr($value,$data)
  22. {
  23. $value = $value??$data['dirname'];
  24. return str_replace(['/','\\'],'/',$value);
  25. }
  26. public function getClassnameAttr($value,$data)
  27. {
  28. return $data['name']?self::getCamel($data['name']):'';
  29. }
  30. static public function statusList()
  31. {
  32. return [1=>'启用',2=>'停用'];
  33. }
  34. public function getStatusTxtAttr($value,$row)
  35. {
  36. $statusList = self::statusList();
  37. return $statusList[$value ?? $row['status']] ?? '';
  38. }
  39. static public function engineList()
  40. {
  41. return ['MyISAM','InnoDB'];
  42. }
  43. public function fields()
  44. {
  45. return $this->hasMany(ModelFields::class,'name','model_name');
  46. }
  47. /**
  48. * @title: 获取所有数据表
  49. * @desc: 描述
  50. * @return {*}
  51. * @author: Rock
  52. * @method: POST
  53. * @Date: 2023-07-26 10:20:58
  54. */
  55. static public function getTableList():array
  56. {
  57. $sql = "SHOW TABLE STATUS";
  58. return Db::query($sql);
  59. }
  60. /**
  61. * @title: 检查表是否存在
  62. * @desc: 描述
  63. * @param {string} {name} {} {待检查的表名}
  64. * @return {boolean} {} {} {true存在,false不存在}
  65. * @author: Rock
  66. * @method: POST
  67. * @Date: 2023-04-17 08:16:41
  68. */
  69. static public function checkTable(string $name):bool
  70. {
  71. $database = config('database.connections.mysql.database');
  72. $sql = "SELECT * FROM information_schema.TABLES WHERE TABLE_NAME = '$name' AND TABLE_SCHEMA='$database'";
  73. $res = Db::query($sql);
  74. return $res && count($res) > 0;
  75. }
  76. /**
  77. * @title: 获取数据表主键
  78. * @desc: 描述
  79. * @param {string} {table} {} {表名}
  80. * @return {*}
  81. * @author: Rock
  82. * @method: POST
  83. * @Date: 2023-07-26 10:08:30
  84. */
  85. static public function pk(string $table):string
  86. {
  87. $fieldList = self::getFieldList($table);
  88. foreach($fieldList as $item){
  89. if('PRI'==$item['Key']){
  90. return $item['Field'];
  91. }
  92. }
  93. return '';
  94. }
  95. /**
  96. * @title: 获取数据表注释
  97. * @desc: 描述
  98. * @param {string} {table} {} {表名}
  99. * @return {*}
  100. * @author: Rock
  101. * @method: POST
  102. * @Date: 2023-07-26 10:14:13
  103. */
  104. static public function getComment(string $table):string
  105. {
  106. $database = config('database.connections.mysql.database');
  107. $sql = "SELECT TABLE_COMMENT FROM information_schema.TABLES WHERE table_name = '$table' AND TABLE_SCHEMA='$database'";
  108. $res = Db::query($sql);
  109. return $res?$res[0]['TABLE_COMMENT']:'';
  110. }
  111. /**
  112. * @title: 获取数据表字段
  113. * @desc: 描述
  114. * @param {string} {table} {} {表名]}
  115. * @return {*}
  116. * @author: Rock
  117. * @method: POST
  118. * @Date: 2023-07-26 10:15:35
  119. */
  120. static public function getFieldList(string $table):array
  121. {
  122. $sql = "SHOW FULL COLUMNS FROM `$table`";
  123. return Db::query($sql);
  124. }
  125. /**
  126. * @title: 创建表
  127. * @desc: 描述
  128. * @return {*}
  129. * @author: Rock
  130. * @method: POST
  131. * @Date: 2023-04-14 09:08:20
  132. */
  133. static public function createTable(int $id)
  134. {
  135. $info = self::find($id);
  136. $tableName = $info->getAttr('name');
  137. $title = $info->title;
  138. $engine = $info->engine;
  139. $sql = <<<SQL
  140. CREATE TABLE IF NOT EXISTS `$tableName` (
  141. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  142. `create_at` datetime DEFAULT NULL COMMENT '创建时间',
  143. `update_at` datetime DEFAULT NULL COMMENT '更新时间',
  144. `delete_at` datetime DEFAULT NULL COMMENT '删除时间',
  145. PRIMARY KEY (`id`)
  146. ) ENGINE=$engine AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='$title'
  147. SQL;
  148. $res = Db::execute($sql);
  149. return $res;
  150. }
  151. /**
  152. * @title: 修改表
  153. * @desc: 描述
  154. * @param {int} {id} {} {模型ID}
  155. * @param {string} {oldname} {} {原表名}
  156. * @return {*}
  157. * @author: Rock
  158. * @method: POST
  159. * @Date: 2023-04-17 09:08:41
  160. */
  161. static public function updateTable(int $id,string $oldname)
  162. {
  163. $info = self::find($id);
  164. $name = $info->getAttr('name');
  165. $engine = $info->getAttr('engine');
  166. $title = $info->getAttr('title');
  167. $sql = "ALTER TABLE $oldname RENAME $name" ;
  168. $res = Db::execute($sql);
  169. $sql = "ALTER TABLE $name ENGINE = $engine COMMENT = '$title'";
  170. $res = Db::execute($sql);
  171. return $res;
  172. }
  173. /**
  174. * @title: 删除表
  175. * @desc: 描述
  176. * @param {int} {id} {} {模型ID}
  177. * @return {*}
  178. * @author: Rock
  179. * @method: POST
  180. * @Date: 2023-04-14 09:08:47
  181. */
  182. static public function deleteTable(int $id)
  183. {
  184. $info = self::find($id);
  185. $name = $info->getAttr('name');
  186. $sql = "DROP TABLE IF EXISTS $name";
  187. $res = Db::execute($sql);
  188. return $res;
  189. }
  190. /**
  191. * @title: 复制表
  192. * @desc: 描述
  193. * @param {int} $id
  194. * @return {*}
  195. * @author: Rock
  196. * @method: POST
  197. * @Date: 2023-04-27 14:37:08
  198. */
  199. static public function copyTable(int $id)
  200. {
  201. $info = self::find($id);
  202. $name = $info->getAttr('name');
  203. $title = $info->getAttr('title');
  204. $sql = "SHOW CREATE TABLE $name";
  205. $tableSql = Db::query($sql);
  206. if($tableSql){
  207. $sql = $tableSql[0]['Create Table'];
  208. $sql = str_replace("TABLE `$name`","TABLE `{$name}_copy`",$sql);
  209. $sql = str_replace("COMMENT='$title'","COMMENT='{$title}副本'",$sql);
  210. Db::execute($sql);
  211. return true;
  212. }
  213. return false;
  214. }
  215. /**
  216. * @title: 获取英文的驼峰写法
  217. * @desc: 描述
  218. * @param {string} $name
  219. * @return {*}
  220. * @author: Rock
  221. * @method: POST
  222. * @Date: 2023-04-25 11:46:37
  223. */
  224. static public function getCamel(string $name)
  225. {
  226. $arr = explode('_',$name);
  227. $arr = array_filter($arr);
  228. $classname = "";
  229. foreach($arr as $item){
  230. $classname .= ucfirst($item);
  231. }
  232. return $classname;
  233. }
  234. }