DBTable.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. class DBTable {
  3. public string $name;
  4. public string $tableName;
  5. public array $columnArray;
  6. public string $line = '<br/>';
  7. const array FORM_EXCLUDE_COLUMNS = ['create_date', 'update_date', 'is_del', 'id', 'city', 'area', 'province', 'last_user_id', 'last_date'];
  8. const array INFO_EXCLUDE_COLUMNS = ['create_date', 'update_date', 'is_del'];
  9. const array TABLE_EXCLUDE_COLUMNS = ['create_date', 'update_date', 'is_del'];
  10. const array DETAIL_EXCLUDE_COLUMNS = ['is_del'];
  11. public function __construct(string $name) {
  12. $this->name = strtolower($name);
  13. $this->tableName = DB::formTableName($name);
  14. $sql = "show full columns from {$this->tableName }";
  15. $this->columnArray = \Yii::app()->db->createCommand($sql)->queryAll();
  16. if (!$this->columnArray) {
  17. Helper::error('该表不存在');
  18. }
  19. }
  20. public function echoEditPhp(): void
  21. {
  22. $notNullField = [];
  23. $allowEmptyField = [];
  24. echo '$data = [' . $this->line;
  25. foreach ($this->columnArray as $column) {
  26. if (in_array($column['Field'], self::FORM_EXCLUDE_COLUMNS)) {
  27. continue;
  28. }
  29. $name = $column['Field'];
  30. $tmpl = "'{$name}' => Helper::{{method}}('{$name}')," . $this->line;
  31. if (Helper::hasAnyString($column['Type'], ['decimal', 'double', 'float'])) {
  32. $method = 'getPostFloat';
  33. } elseif (Helper::hasAnyString($column['Type'], ['int'])) {
  34. $method = 'getPostInt';
  35. } elseif (Helper::hasAnyString($column['Type'], ['datetime'])) {
  36. $method = 'getPostDatetime';
  37. } elseif (Helper::hasAnyString($column['Type'], ['date'])) {
  38. $method = 'getPostDate';
  39. } else {
  40. $method = 'getPostString';
  41. }
  42. echo str_replace('{{method}}', $method, $tmpl);
  43. if ($column['Null'] == 'NO') {
  44. $notNullField[] = $name;
  45. }
  46. if ($column['Default'] != null || $method == 'getPostInt') {
  47. $allowEmptyField[] = $name;
  48. }
  49. }
  50. echo '];' . $this->line . $this->line;
  51. echo '$notNullField = ' . json_encode($notNullField) . ';' . $this->line;
  52. echo '$allowEmptyField = ' . json_encode($allowEmptyField) . ';' . $this->line;
  53. }
  54. public function editVue()
  55. {
  56. $formDefault = $this->getFormDefault(false);
  57. list($template, $rule) = $this->getFormAndRule();
  58. $editTmpl = str_replace(
  59. ['{{template}}', '{{table}}', '{{formDefault}}', '{{formRule}}', '{{ucTable}}'],
  60. [$template, $this->name, $formDefault, $rule, ucfirst($this->name)],
  61. file_get_contents(RUNTIME_PATH . '/edit.tmpl')
  62. );
  63. $filePath = RUNTIME_PATH . "/edit.vue";
  64. file_put_contents($filePath, $editTmpl);
  65. // 格式化代码
  66. echo "<h4>命令后执行下面代码</h4>";
  67. echo "<p style='color: red;'>npx prettier --write $filePath</p>";
  68. }
  69. public function echoDetail()
  70. {
  71. echo $this->getFormDefault();
  72. list($template, $rule) = $this->getFormAndRule();
  73. }
  74. /**
  75. * 生成ts接口
  76. * @return string
  77. */
  78. public function getTsInterFace(): string
  79. {
  80. $content = '';
  81. foreach ($this->columnArray as $column) {
  82. if (in_array($column['Field'], self::INFO_EXCLUDE_COLUMNS)) {
  83. continue;
  84. }
  85. $content.= (new DBColumn($column))->getTsDefine() . $this->line;
  86. }
  87. $template = <<<typescript
  88. interface {$this->name} { {$this->line}
  89. $content} {$this->line} {$this->line}
  90. typescript;
  91. return $template;
  92. }
  93. /**
  94. * 生成form默认值
  95. * @return string
  96. */
  97. private function getFormDefault($withLine = true): string
  98. {
  99. $content = '';
  100. $line = $withLine ? $this->line : '';
  101. foreach ($this->columnArray as $column) {
  102. if (in_array($column['Field'], self::FORM_EXCLUDE_COLUMNS)) {
  103. continue;
  104. }
  105. $content.= (new DBColumn($column))->getDefaultValue() . $line;
  106. }
  107. return $content;
  108. }
  109. /**
  110. * 生成form验证规则 前后端需要保持一致
  111. * @return array
  112. */
  113. private function getFormAndRule(): array
  114. {
  115. $html = '';
  116. $ruleHtml = '';
  117. foreach ($this->columnArray as $column) {
  118. if (in_array($column['Field'], self::FORM_EXCLUDE_COLUMNS)) {
  119. continue;
  120. }
  121. list($template, $rule) = (new DBColumn($column))->getFormHtmlAndRule();
  122. $html.= $template;
  123. $ruleHtml.= $rule;
  124. }
  125. $content = <<<typescript
  126. <template>
  127. <ElForm ref="formRef" :model="formData" :rules="rules" label-width="auto">
  128. <el-row :gutter="20">
  129. {$html}
  130. <el-col :span="24">
  131. <ElFormItem label=" " prop="">
  132. <ElButton type="primary" @click="handleSubmit">提交</ElButton>
  133. </ElFormItem>
  134. </el-col>
  135. </el-row>
  136. </ElForm>
  137. </template>
  138. typescript;
  139. return [$content, $ruleHtml];
  140. }
  141. /**
  142. * 生成详情
  143. * @return array
  144. */
  145. public function getDetailHtml(): string
  146. {
  147. echo $this->getFormDefault();
  148. $html = '';
  149. foreach ($this->columnArray as $column) {
  150. if (in_array($column['Field'], self::DETAIL_EXCLUDE_COLUMNS)) {
  151. continue;
  152. }
  153. $html.= (new DBColumn($column))->getDetailHtml();
  154. }
  155. $content = <<<typescript
  156. <template>
  157. <div>
  158. <el-row :gutter="20" class="detail">
  159. {$html}
  160. </el-row>
  161. </div>
  162. </template>
  163. typescript;
  164. file_put_contents(PROJECT_PATH . "/protected/runtime/detail.vue", $content);
  165. return $content;
  166. }
  167. /**
  168. * 生成table字段配置
  169. * @return string
  170. */
  171. public function getTableHtml(): string
  172. {
  173. $tableContent = '';
  174. foreach ($this->columnArray as $column) {
  175. if (in_array($column['Field'], self::TABLE_EXCLUDE_COLUMNS)) {
  176. continue;
  177. }
  178. $arr = (new DBColumn($column))->getTableAttr();
  179. $tableContent.= $this->getHtmlWithJsTableArr($arr);
  180. }
  181. $template = <<<typescript
  182. $tableContent
  183. typescript;
  184. return $template;
  185. }
  186. private function getHtmlWithJsTableArr(array $arr): string
  187. {
  188. if (!$arr) {
  189. return '';
  190. }
  191. $str = '{';
  192. foreach ($arr as $k => $v) {
  193. $str.= " {$k}:'{$v}',";
  194. }
  195. return trim($str, ',') . ' },' . $this->line;
  196. }
  197. }