BaseSeeder.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Database\Seeders;
  3. use App\Http\Admin\AssembleController;
  4. use App\Http\Admin\ColumnController;
  5. use App\Http\Admin\Requests\AssembleForm;
  6. use App\Http\Admin\Requests\ColumnForm;
  7. use Txj\Elastic\Services\ColumnsService;
  8. use Illuminate\Database\Seeder;
  9. use Illuminate\Support\Facades\Cache;
  10. use Illuminate\Support\Facades\DB;
  11. use Txj\Elastic\Table\Assemble;
  12. class BaseSeeder extends Seeder
  13. {
  14. public int $category_type = 2; // // 字段类型 2 表字段 3 扩展字段 4 辅助字段
  15. public string $schema = '';
  16. public string $title = '';
  17. private array $assembleIds = [];
  18. /**
  19. * @param $schema
  20. */
  21. public function setColumn($schema)
  22. {
  23. // 获取项目的id
  24. $assembleInfo = DB::table('sys_assemble')->where('schema', $schema)->where('is_delete', 0)->first();
  25. $data = $this->getColumnData();
  26. $columnService = new ColumnsService($assembleInfo->id, $this->category_type);
  27. $columnData = $columnService->getColumnsAll($data);
  28. foreach ($columnData as $rowData) {
  29. $column = new ColumnController();
  30. $form = new ColumnForm();
  31. foreach ($rowData as $key => $item) {
  32. $form->request->set($key, $item);
  33. }
  34. $result = $column->save($form);
  35. $result = json_decode($result, true);
  36. if ($result && !$result['error']) {
  37. } else {
  38. }
  39. }
  40. }
  41. public function start()
  42. {
  43. $assembleData = $this->getAssembleData();
  44. $form = new AssembleForm();
  45. foreach ($assembleData as $column => $value) {
  46. $form->request->set($column, $value);
  47. }
  48. $assemble = new AssembleController();
  49. $response = $assemble->save($form);
  50. if ($response) {
  51. $result = $response->getContent();
  52. $result = json_decode($result, true);
  53. if (!$result['error']) {
  54. // 设置字段
  55. $this->setColumn($this->schema);
  56. // 清除字段缓存
  57. $cache_key = 'SYS:schema:' . $this->schema;
  58. Cache::forget($cache_key);
  59. // 初始化内容
  60. $this->init();
  61. } else {
  62. echo $result['msg'];
  63. }
  64. }
  65. }
  66. /**
  67. * 必须填写 否则组件无法使用
  68. * 该数据再用户执行添加的时候,会自动把该表信息保存到数据库
  69. *
  70. * @return array
  71. */
  72. public function getAssembleData(): array
  73. {
  74. return [
  75. 'title' => $this->title,
  76. 'schema' => $this->schema,
  77. 'connect_db_id' => 1,
  78. 'remark' => '',
  79. ];
  80. }
  81. /**
  82. * 获取集合id
  83. *
  84. * @param $name
  85. * @return mixed
  86. */
  87. public function getAssembleId($name): int
  88. {
  89. if (isset($this->assembleIds[$name])) {
  90. return $this->assembleIds[$name];
  91. } else {
  92. $assemble = new Assemble($name);
  93. return $this->assembleIds[$name] = $assemble->getAssembleId();
  94. }
  95. }
  96. }