DBTable.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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'];
  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 echoEditHtml(): 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();
  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(): string
  98. {
  99. $content = '';
  100. foreach ($this->columnArray as $column) {
  101. if (in_array($column['Field'], self::FORM_EXCLUDE_COLUMNS)) {
  102. continue;
  103. }
  104. $content.= (new DBColumn($column))->getDefaultValue() . $this->line;
  105. }
  106. return $content;
  107. }
  108. /**
  109. * 生成form验证规则 前后端需要保持一致
  110. * @return array
  111. */
  112. private function getFormAndRule(): array
  113. {
  114. $html = '';
  115. $ruleHtml = '';
  116. foreach ($this->columnArray as $column) {
  117. if (in_array($column['Field'], self::FORM_EXCLUDE_COLUMNS)) {
  118. continue;
  119. }
  120. list($template, $rule) = (new DBColumn($column))->getFormHtmlAndRule();
  121. $html.= $template;
  122. $ruleHtml.= $rule;
  123. }
  124. $content = <<<typescript
  125. <template>
  126. <ElForm ref="formRef" :model="formData" :rules="rules" label-width="auto">
  127. <el-row :gutter="20">
  128. {$html}
  129. <el-col :span="24">
  130. <ElFormItem label=" " prop="">
  131. <ElButton type="primary" @click="handleSubmit">提交</ElButton>
  132. </ElFormItem>
  133. </el-col>
  134. </el-row>
  135. </ElForm>
  136. </template>
  137. typescript;
  138. return [$content, $ruleHtml];
  139. }
  140. /**
  141. * 生成详情
  142. * @return array
  143. */
  144. public function getDetailHtml(): string
  145. {
  146. echo $this->getFormDefault();
  147. $html = '';
  148. foreach ($this->columnArray as $column) {
  149. if (in_array($column['Field'], self::DETAIL_EXCLUDE_COLUMNS)) {
  150. continue;
  151. }
  152. $html.= (new DBColumn($column))->getDetailHtml();
  153. }
  154. $content = <<<typescript
  155. <template>
  156. <div>
  157. <el-row :gutter="20" class="detail">
  158. {$html}
  159. </el-row>
  160. </div>
  161. </template>
  162. typescript;
  163. file_put_contents(PROJECT_PATH . "/protected/runtime/detail.vue", $content);
  164. return $content;
  165. }
  166. /**
  167. * 生成table字段配置
  168. * @return string
  169. */
  170. public function getTableHtml(): string
  171. {
  172. $tableContent = '';
  173. foreach ($this->columnArray as $column) {
  174. if (in_array($column['Field'], self::TABLE_EXCLUDE_COLUMNS)) {
  175. continue;
  176. }
  177. $arr = (new DBColumn($column))->getTableAttr();
  178. $tableContent.= $this->getHtmlWithJsTableArr($arr);
  179. }
  180. $template = <<<typescript
  181. $tableContent
  182. typescript;
  183. return $template;
  184. }
  185. private function getHtmlWithJsTableArr(array $arr): string
  186. {
  187. if (!$arr) {
  188. return '';
  189. }
  190. $str = '{';
  191. foreach ($arr as $k => $v) {
  192. $str.= " {$k}:'{$v}',";
  193. }
  194. return trim($str, ',') . ' },' . $this->line;
  195. }
  196. }