DepInterTempSeeder.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Database\Seeders\Developer;
  3. use Database\Seeders\BaseSeeder;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\File;
  6. use Txj\Elastic\Facades\Eav;
  7. use Txj\Elastic\Facades\ES;
  8. /**
  9. * 接口模板
  10. */
  11. class DepInterTempSeeder extends BaseSeeder
  12. {
  13. public function getColumnData()
  14. {
  15. return [
  16. 'category_id' => ['type' => 'uBigInt', 'title' => '分类id', 'default' => 0],
  17. 'desc' => ['type' => 'text', 'title' => '接口描述'],
  18. 'title' => ['type' => 'string_60', 'title' => '接口标题'],
  19. 'en_alias' => ['type' => 'string_60', 'title' => '接口的英文别名'],
  20. 'category_type' => ['type' => 'uTinyint', 'title' => '接口分类', 'default' => 0, 'remark' => "1 普通接口 2 组合接口"],
  21. 'request_method' => ['type' => 'varchar', 'title' => '请求方式', 'default' => 'POST', 'remark' => 'POST GET PUT'],
  22. 'assemble_id' => ['type' => 'uBigInt', 'title' => '集合ID', 'default' => 0,],
  23. 'connect_db' => ['type' => 'varchar', 'title' => '数据库链接mysql elasticsearch redis mogodb', 'default' => 'mysql'],
  24. 'operate_type' => ['type' => 'uTinyint', 'title' => '操作类型', 'default' => 0, 'remark' => '1 查询 2 添加 3修改 4删除 5上传'],
  25. 'search_type' => ['type' => 'uTinyint', 'title' => '查询参数', 'default' => 0, 'remark' => '1 单条信息查询 2 分页列表查询 3 列表查询 4 树形列表查询 (对象) 5 条件总数 6 字段总和 7字段平均值 8 字段最大值 9 字段最小值',],
  26. 'search_column_id' => ['type' => 'uBigInt', 'title' => '查询需要的字段id', 'default' => 0, 'remark' => "为了防止名称修改,特存储id", 'is_null' => true],
  27. 'add_type' => ['type' => 'uTinyint', 'title' => '添加类型', 'default' => 0, 'remark' => '1添加单条数据 2批量添加 3添加树型数据'],
  28. 'update_type' => ['type' => 'uTinyint', 'title' => '编辑类型', 'default' => 0, 'remark' => '1更新单条数据 2批量更新 3更新单个树型数据 4累计加 5累计减'],
  29. 'delete_type' => ['type' => 'uTinyint', 'title' => '删除类型', 'default' => 0, 'remark' => '1伪删除单条数据 2真实删除单条数据 3批量伪删除'],
  30. 'upload_type' => ['type' => 'uTinyint', 'title' => '上传类型', 'default' => 0, 'remark' => '1图片 2音频 3视频 4其他'],
  31. 'request_settings' => ['type' => 'json', 'title' => '请求参数设置'],
  32. 'condition_settings' => ['type' => 'json', 'title' => '请求条件设置'],
  33. 'sort_settings' => ['type' => 'json', 'title' => '查询排序设置'],
  34. 'result_settings' => ['type' => 'json', 'title' => '请求结果设置'],
  35. 'conditions' => ['type' => 'json', 'title' => '组合接口,条件参数'],
  36. 'is_use' => ['type' => 'uTinyint', 'title' => '是否开启使用', 'default' => 0, 'remark' => '1 开启'],
  37. 'is_login' => ['type' => 'uTinyint', 'title' => '是否需要登录', 'default' => 0, 'remark' => '1 开启'],
  38. ];
  39. }
  40. public function init()
  41. {
  42. // 根据目录,获取接口模板
  43. $files = File::allFiles(__DIR__ . '/interfaceTemplate');
  44. foreach ($files as $file) {
  45. $content = file_get_contents($file);
  46. if ($content) {
  47. $content = trim($content);
  48. $content = preg_replace("/^return/is", '', $content, 1);
  49. $arr = json_decode($content, true);
  50. if (empty($arr)) {
  51. dd("文件json解析错误" . $file);
  52. }
  53. unset($arr['category_id']);
  54. unset($arr['desc']);
  55. $arr['website_id'] = 1;
  56. // 获取表id
  57. $arr['assemble_id'] = $this->getCustomAssembleId($arr['assemble_id']);
  58. // 将字段转为id
  59. $arr['condition_settings'] = $this->getConditionColumnId($arr['assemble_id'], $arr['condition_settings'] ?? []);
  60. if ($arr) {
  61. Eav::table($this->schema)->insert($arr);
  62. } else {
  63. dd("接口模板数据格式错误,请检查后,再试! 文件: {$file->getRelativePathname()}");
  64. }
  65. }
  66. }
  67. }
  68. public function run()
  69. {
  70. $this->schema = 'dep_inter_template';
  71. $this->title = '接口模板表';
  72. $this->start();
  73. }
  74. // 获取集合id
  75. private function getCustomAssembleId(string $table): int
  76. {
  77. $info = DB::table('sys_assemble')->where('schema', $table)->first();
  78. if ($info) {
  79. return $info->id;
  80. } else {
  81. return 0;
  82. }
  83. }
  84. // 根据字段名称设置字段的id
  85. private function getConditionColumnId(int $assemble_id, $conditionSettings): array
  86. {
  87. if ($assemble_id && $conditionSettings) {
  88. foreach ($conditionSettings as $key => $row) {
  89. if (isset($row['column_id'])) {
  90. $info = DB::table('sys_assemble_column')->where('assemble_id', $assemble_id)
  91. ->where('code', $row['column_id'])->first();
  92. if ($info) {
  93. $conditionSettings[$key]['column_id'] = $info->id;
  94. }
  95. }
  96. }
  97. }
  98. return $conditionSettings;
  99. }
  100. }