GenerateColumnsLogic.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\app\logic\tool;
  8. use plugin\saiadmin\app\model\tool\GenerateColumns;
  9. use plugin\saiadmin\basic\BaseLogic;
  10. use plugin\saiadmin\utils\Helper;
  11. /**
  12. * 代码生成业务字段逻辑层
  13. */
  14. class GenerateColumnsLogic extends BaseLogic
  15. {
  16. /**
  17. * 构造函数
  18. */
  19. public function __construct()
  20. {
  21. $this->model = new GenerateColumns();
  22. }
  23. public function saveExtra($data)
  24. {
  25. $default_column = ['create_time', 'update_time', 'created_by', 'updated_by', 'delete_time', 'remark'];
  26. // 组装数据
  27. foreach ($data as $k => $item) {
  28. if ($item['column_name'] == 'delete_time') {
  29. continue;
  30. }
  31. $column = [
  32. 'table_id' => $item['table_id'],
  33. 'column_name' => $item['column_name'],
  34. 'column_comment' => $item['column_comment'],
  35. 'column_type' => $item['column_type'],
  36. 'default_value' => $item['default_value'],
  37. 'is_pk' => ($item['column_key'] == 'PRI') ? 2 : 1 ,
  38. 'is_required' => $item['is_nullable'] == 'NO' ? 2 : 1,
  39. 'query_type' => 'eq',
  40. 'view_type' => 'input',
  41. 'sort' => count($data) - $k,
  42. 'options' => $item['options'] ?? null
  43. ];
  44. // 设置默认选项
  45. if (!in_array($item['column_name'], $default_column) && empty($item['column_key'])) {
  46. $column = array_merge(
  47. $column,
  48. [
  49. 'is_insert' => 2,
  50. 'is_edit' => 2,
  51. 'is_list' => 2,
  52. 'is_query' => 1,
  53. 'is_sort' => 1,
  54. ]
  55. );
  56. }
  57. $keyList = [
  58. 'column_comment', 'column_type', 'default_value', 'is_pk', 'is_required', 'is_insert', 'is_edit', 'is_list',
  59. 'is_query', 'is_sort', 'query_type', 'view_type', 'dict_type', 'options', 'sort', 'is_cover'
  60. ];
  61. foreach ($keyList as $key) {
  62. if (isset($item[$key])) $column[$key] = $item[$key];
  63. }
  64. GenerateColumns::create($this->fieldDispose($column));
  65. }
  66. }
  67. public function update($data, $where)
  68. {
  69. $data['is_insert'] = $data['is_insert'] ? 2 : 1;
  70. $data['is_edit'] = $data['is_edit'] ? 2 : 1;
  71. $data['is_list'] = $data['is_list'] ? 2 : 1;
  72. $data['is_query'] = $data['is_query'] ? 2 : 1;
  73. $data['is_sort'] = $data['is_sort'] ? 2 : 1;
  74. $data['is_required'] = $data['is_required'] ? 2 : 1;
  75. $this->model->update($data, $where);
  76. }
  77. private function fieldDispose(array $column): array
  78. {
  79. $object = new class {
  80. public function viewTypeDispose(&$column): void
  81. {
  82. switch ($column['column_type']) {
  83. case 'varchar':
  84. $column['view_type'] = 'input';
  85. break;
  86. // 富文本
  87. case 'text':
  88. case 'longtext':
  89. $column['is_list'] = 1;
  90. $column['is_query'] = 1;
  91. $column['view_type'] = 'wangEditor';
  92. $options = [
  93. 'height' => 400,
  94. ];
  95. $column['options'] = $options;
  96. break;
  97. // 日期字段
  98. case 'datetime':
  99. $column['view_type'] = 'date';
  100. $options = [
  101. 'mode' => 'date',
  102. 'showTime' => true,
  103. ];
  104. $column['options'] = $options;
  105. $column['query_type'] = 'between';
  106. break;
  107. case 'date':
  108. $column['view_type'] = 'date';
  109. $options = [
  110. 'mode' => 'date',
  111. 'showTime' => false,
  112. ];
  113. $column['options'] = $options;
  114. $column['query_type'] = 'between';
  115. break;
  116. }
  117. }
  118. public function columnName(&$column): void
  119. {
  120. if (stristr($column['column_name'], 'name')) {
  121. $column['is_query'] = 2;
  122. $column['is_required'] = 2;
  123. $column['query_type'] = 'like';
  124. }
  125. if (stristr($column['column_name'], 'title')) {
  126. $column['is_query'] = 2;
  127. $column['is_required'] = 2;
  128. $column['query_type'] = 'like';
  129. }
  130. if (stristr($column['column_name'], 'type')) {
  131. $column['is_query'] = 2;
  132. $column['is_required'] = 2;
  133. $column['query_type'] = 'eq';
  134. }
  135. if (stristr($column['column_name'], 'image')) {
  136. $column['is_query'] = 1;
  137. $column['view_type'] = 'uploadImage';
  138. $options = [
  139. 'multiple' => false,
  140. 'limit' => 3,
  141. ];
  142. $column['options'] = $options;
  143. }
  144. if (stristr($column['column_name'], 'file')) {
  145. $column['is_query'] = 1;
  146. $column['view_type'] = 'uploadFile';
  147. $options = [
  148. 'multiple' => false,
  149. 'limit' => 3,
  150. ];
  151. $column['options'] = $options;
  152. }
  153. if (stristr($column['column_name'], 'attach')) {
  154. $column['is_query'] = 1;
  155. $column['view_type'] = 'uploadFile';
  156. $options = [
  157. 'multiple' => false,
  158. 'limit' => 3,
  159. ];
  160. $column['options'] = $options;
  161. }
  162. }
  163. };
  164. if(!$column['is_cover']) {
  165. $object->viewTypeDispose($column);
  166. $object->columnName($column);
  167. }
  168. $column['options'] = json_encode($column['options'], JSON_UNESCAPED_UNICODE);
  169. return $column;
  170. }
  171. }