FormService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\JsonRpc;
  3. use Hyperf\RpcServer\Annotation\RpcService;
  4. use App\Tools\Result;
  5. use App\Model\GlobalTable;
  6. use Hyperf\DbConnection\Db;
  7. use App\Model\GlobalTableField;
  8. #[RpcService(name: "FormService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  9. class FormService implements FormServiceInterface
  10. {
  11. /**
  12. * 添加全局表单
  13. * @param array $data
  14. * @return array|mixed
  15. */
  16. public function addGlobalTable(array $data): array
  17. {
  18. // 过滤掉空值
  19. $data = array_filter($data, function($value) {
  20. return !empty($value);
  21. });
  22. // 检查是否已存在相同名称的记录
  23. $globalTable = GlobalTable::on('global')->where(['table'=> $data['table'],'website_id'=>$data['website_id']])->first();
  24. if (empty($globalTable)) {
  25. $id = GlobalTable::on('global')->insertGetId($data);
  26. //给global库创建表 表名为$data['table'] 的值,并初始化一个字段id,类型为int,自增,主键,再初始化一个字段title,类型为varchar,长度为255
  27. Db::connection('global')->statement("CREATE TABLE IF NOT EXISTS `{$data['table']}` (
  28. `id` int(11) NOT NULL AUTO_INCREMENT,
  29. `title` varchar(255) DEFAULT NULL,
  30. PRIMARY KEY (`id`)
  31. )");
  32. //给GlobalTableField表初始化一条数据,表table_id为$id,field_name为title字符串,title为标题,field_type为varchar,length为255,is_required为1,is_unique为0,is_primary为0,is_index为0,is_foreign为0,is_auto_increment为1,is_default为0,default_value为'',description为'',website_id为$data['website_id']
  33. GlobalTableField::on('global')->insert([
  34. 'table_id' => $id,
  35. 'field_name' => 'title',
  36. 'title' => '标题',
  37. 'field_type' => '1',
  38. 'length' => 255,
  39. 'sort' => 0,
  40. 'is_check' => 1,
  41. 'admin_display' => 1,
  42. 'home_display' => 1,
  43. ]);
  44. return Result::success('添加成功');
  45. }
  46. return Result::error('表单已存在');
  47. }
  48. /**
  49. * 获取全局表单列表
  50. * @param array $data
  51. * @return array
  52. */
  53. public function getGlobalTableList(array $data): array
  54. {
  55. try {
  56. // 构建查询
  57. $query = GlobalTable::query()
  58. ->when(!empty($data['website_id']), fn($q) => $q->where('website_id', $data['website_id']))
  59. ->when(!empty($data['name']), fn($q) => $q->where('name', 'like', '%' . $data['name'] . '%'));
  60. // 分页参数
  61. $page = (int)($data['page'] ?? 1);
  62. $pageSize = (int)($data['pageSize'] ?? 10);
  63. // 获取数据
  64. $list = $query->orderBy('updated_at', 'desc')
  65. ->offset(($page - 1) * $pageSize)
  66. ->limit($pageSize)
  67. ->get();
  68. // 如果没有数据,直接返回空结果
  69. if ($list->isEmpty()) {
  70. return Result::success([
  71. 'list' => [],
  72. 'total' => 0,
  73. 'page' => $page,
  74. 'pageSize' => $pageSize
  75. ]);
  76. }
  77. // 获取并合并 website 数据
  78. $websites = Db::connection('secondary')
  79. ->table('website')
  80. ->whereIn('id', $list->pluck('website_id'))
  81. ->pluck('website_name', 'id');
  82. // 合并数据并返回
  83. return Result::success([
  84. 'list' => $list->map(function($item) use ($websites) {
  85. $item->website_name = $websites[$item->website_id] ?? '';
  86. return $item;
  87. }),
  88. 'total' => $query->count(),
  89. 'page' => $page,
  90. 'pageSize' => $pageSize
  91. ]);
  92. } catch (\Throwable $e) {
  93. return Result::error('查询失败:' . $e->getMessage());
  94. }
  95. }
  96. /**
  97. * 获取全局表单
  98. * @param array $data
  99. * @return array
  100. */
  101. public function getGlobalTable(array $data): array
  102. {
  103. $globalTable = GlobalTable::where('id',$data['id'])->first();
  104. return Result::success($globalTable);
  105. }
  106. /**
  107. * 修改全局表单
  108. * @param array $data
  109. * @return array
  110. */
  111. public function upGlobalTable(array $data): array
  112. {
  113. var_dump($data);
  114. $globalTable = GlobalTable::where('id',$data['id'])->first();
  115. if(empty($globalTable)){
  116. return Result::error('表单不存在');
  117. }
  118. $data = array_filter($data,function($value){
  119. return !empty($value);
  120. });
  121. GlobalTable::where(['id'=>$data['id']])->update($data);
  122. return Result::success('修改成功');
  123. }
  124. /**
  125. * 删除全局表单
  126. * @param array $data
  127. * @return array
  128. */
  129. public function delGlobalTable(array $data): array
  130. {
  131. $globalTable = GlobalTable::where('id', $data['id'])->first();
  132. if(empty($globalTable)){
  133. return Result::error('表单不存在');
  134. }
  135. //删除global库的表
  136. Db::connection('global')->statement("DROP TABLE IF EXISTS `{$globalTable->table}`");
  137. //删除GlobalTableField表中table_id为$data['id']的数据
  138. GlobalTableField::where('table_id', $data['id'])->delete();
  139. GlobalTable::where('id', $data['id'])->delete();
  140. return Result::success('删除成功');
  141. }
  142. }