FormService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. use App\Model\GlobalTableFieldType;
  9. #[RpcService(name: "FormService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  10. class FormService implements FormServiceInterface
  11. {
  12. /**
  13. * 添加全局表单
  14. * @param array $data
  15. * @return array|mixed
  16. */
  17. public function addGlobalTable(array $data): array
  18. {
  19. // 过滤掉空值
  20. $data = array_filter($data, function($value) {
  21. return !empty($value);
  22. });
  23. // 检查是否已存在相同名称的记录
  24. $globalTable = GlobalTable::on('global')->where(['table'=> $data['table'],'website_id'=>$data['website_id']])->first();
  25. if (empty($globalTable)) {
  26. $id = GlobalTable::on('global')->insertGetId($data);
  27. //给global库创建表 表名为$data['table'] 的值,并初始化一个字段id,类型为int,自增,主键,再初始化一个字段title,类型为varchar,长度为255
  28. Db::connection('global')->statement("CREATE TABLE IF NOT EXISTS `{$data['table']}` (
  29. `id` int(11) NOT NULL AUTO_INCREMENT,
  30. `title` varchar(255) DEFAULT NULL,
  31. PRIMARY KEY (`id`)
  32. )");
  33. //给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']
  34. GlobalTableField::on('global')->insert([
  35. 'table_id' => $id,
  36. 'field_name' => 'title',
  37. 'title' => '标题',
  38. 'field_type' => '1',
  39. 'length' => 255,
  40. 'sort' => 0,
  41. 'is_check' => 1,
  42. 'admin_display' => 1,
  43. 'home_display' => 1,
  44. 'disable_del' => 1,
  45. ]);
  46. return Result::success('添加成功');
  47. }
  48. return Result::error('表单已存在');
  49. }
  50. /**
  51. * 获取全局表单列表
  52. * @param array $data
  53. * @return array
  54. */
  55. public function getGlobalTableList(array $data): array
  56. {
  57. try {
  58. // 构建查询
  59. $query = GlobalTable::query()
  60. ->when(!empty($data['website_id']), function($q) use ($data) {
  61. return $q->where('website_id', $data['website_id']);
  62. })
  63. ->when(!empty($data['name']), function($q) use ($data) {
  64. return $q->where('name', 'like', '%' . $data['name'] . '%');
  65. });
  66. // 分页参数
  67. $page = (int)($data['page'] ?? 1);
  68. $pageSize = (int)($data['pageSize'] ?? 10);
  69. // 先获取总数
  70. $total = $query->count();
  71. // 获取数据
  72. $list = $query->orderBy('updated_at', 'desc')
  73. ->offset(($page - 1) * $pageSize)
  74. ->limit($pageSize)
  75. ->get();
  76. // 如果没有数据,直接返回空结果
  77. if ($list->isEmpty()) {
  78. return Result::success([
  79. 'list' => [],
  80. 'total' => $total,
  81. 'page' => $page,
  82. 'pageSize' => $pageSize
  83. ]);
  84. }
  85. // 获取并合并 website 数据
  86. $websites = Db::connection('secondary')
  87. ->table('website')
  88. ->whereIn('id', $list->pluck('website_id'))
  89. ->pluck('website_name', 'id');
  90. // 合并数据并返回
  91. return Result::success([
  92. 'list' => $list->map(function($item) use ($websites) {
  93. $item->website_name = $websites[$item->website_id] ?? '';
  94. return $item;
  95. }),
  96. 'total' => $total,
  97. 'page' => $page,
  98. 'pageSize' => $pageSize
  99. ]);
  100. } catch (\Throwable $e) {
  101. return Result::error('查询失败:' . $e->getMessage());
  102. }
  103. }
  104. /**
  105. * 获取全局表单
  106. * @param array $data
  107. * @return array
  108. */
  109. public function getGlobalTable(array $data): array
  110. {
  111. $globalTable = GlobalTable::where('id',$data['id'])->first();
  112. return Result::success($globalTable);
  113. }
  114. /**
  115. * 修改全局表单
  116. * @param array $data
  117. * @return array
  118. */
  119. public function upGlobalTable(array $data): array
  120. {
  121. $globalTable = GlobalTable::where('id',$data['id'])->first();
  122. if(empty($globalTable)){
  123. return Result::error('表单不存在');
  124. }
  125. $data = array_filter($data,function($value){
  126. return !empty($value);
  127. });
  128. GlobalTable::where(['id'=>$data['id']])->update($data);
  129. return Result::success('修改成功');
  130. }
  131. /**
  132. * 删除全局表单
  133. * @param array $data
  134. * @return array
  135. */
  136. public function delGlobalTable(array $data): array
  137. {
  138. $globalTable = GlobalTable::where('id', $data['id'])->first();
  139. if(empty($globalTable)){
  140. return Result::error('表单不存在');
  141. }
  142. //删除global库的表
  143. Db::connection('global')->statement("DROP TABLE IF EXISTS `{$globalTable->table}`");
  144. //删除GlobalTableField表中table_id为$data['id']的数据
  145. GlobalTableField::where('table_id', $data['id'])->delete();
  146. GlobalTable::where('id', $data['id'])->delete();
  147. return Result::success('删除成功');
  148. }
  149. /**
  150. * 获取表单字段列表
  151. * @param array $data
  152. * @return array
  153. */
  154. public function getGlobalTableFieldList(array $data): array
  155. {
  156. $globalTableField = GlobalTableField::where('table_id',$data['id'])->orderBy("sort","asc")->get();
  157. return Result::success($globalTableField);
  158. }
  159. /**
  160. * 获取表单字段
  161. * @param array $data
  162. * @return array
  163. */
  164. public function getGlobalTableField(array $data): array
  165. {
  166. $globalTableField = GlobalTableField::where('id',$data['id'])->first();
  167. return Result::success($globalTableField);
  168. }
  169. /**
  170. * 添加表单字段
  171. * @param array $data
  172. * @return array
  173. * @throws \Throwable
  174. */
  175. public function addGlobalTableField(array $data): array
  176. {
  177. $globalTable = GlobalTable::where('id',$data['table_id'])->first();
  178. if(empty($globalTable)){
  179. return Result::error('表单不存在');
  180. }
  181. //检查是否已存在相同名称的记录
  182. $globalTableField = GlobalTableField::where(['table_id'=>$data['table_id'],'field_name'=>$data['field_name']])->first();
  183. if(!empty($globalTableField)){
  184. return Result::error('字段已存在');
  185. }
  186. // 检查表中是否已存在该列
  187. try {
  188. $columns = Db::connection('global')->select("SHOW COLUMNS FROM `{$globalTable->table}`");
  189. $columnNames = array_column($columns, 'Field');
  190. if (in_array($data['field_name'], $columnNames)) {
  191. return Result::error('字段名已存在于数据表中');
  192. }
  193. } catch (\Throwable $e) {
  194. return Result::error('检查字段是否存在时出错:' . $e->getMessage());
  195. }
  196. $globalTableFieldTypeInfo = GlobalTableFieldType::where('id',$data['field_type'])->first();
  197. //给global库的表添加字段
  198. Db::connection('global')->statement("ALTER TABLE `{$globalTable->table}` ADD COLUMN `{$data['field_name']}` {$globalTableFieldTypeInfo['field_type']}({$data['length']}) COMMENT '{$data['title']}'");
  199. //给GlobalTableField表添加数据
  200. GlobalTableField::create($data);
  201. return Result::success('添加成功');
  202. }
  203. /**
  204. * 修改表单字段
  205. * @param array $data
  206. * @return array
  207. * @throws \Throwable
  208. */
  209. public function upGlobalTableField(array $data): array
  210. {
  211. $globalTable = GlobalTable::where('id',$data['table_id'])->first();
  212. if(empty($globalTable)){
  213. return Result::error('表单不存在');
  214. }
  215. //检查是否已存在相同名称的记录
  216. $globalTableField = GlobalTableField::where(['table_id'=>$data['table_id'],'field_name'=>$data['field_name']])->first();
  217. if(!empty($globalTableField) && $globalTableField->id != $data['id']){
  218. return Result::error('字段已存在');
  219. }
  220. $globalTableFieldTypeInfo = GlobalTableFieldType::where('id',$data['field_type'])->first();
  221. //给global库的表修改字段
  222. Db::connection('global')->statement("ALTER TABLE `{$globalTable->table}` CHANGE COLUMN `{$data['field_name']}` `{$data['field_name']}` {$globalTableFieldTypeInfo['field_type']}({$data['length']}) COMMENT '{$data['title']}'");
  223. //给GlobalTableField表修改数据
  224. GlobalTableField::where('id',$data['id'])->update($data);
  225. return Result::success('修改成功');
  226. }
  227. /**
  228. * 删除表单字段
  229. * @param array $data
  230. * @return array
  231. * @throws \Throwable
  232. */
  233. public function delGlobalTableField(array $data): array
  234. {
  235. $tableFieldInfo = GlobalTableField::where('id',$data['id'])->first();
  236. if(empty($tableFieldInfo)){
  237. return Result::error('字段不存在');
  238. }
  239. $globalTable = GlobalTable::where('id',$tableFieldInfo['table_id'])->first();
  240. if(empty($globalTable)){
  241. return Result::error('表单不存在');
  242. }
  243. //给global库的表删除字段
  244. Db::connection('global')->statement("ALTER TABLE `{$globalTable->table}` DROP COLUMN `{$tableFieldInfo['field_name']}`");
  245. //给GlobalTableField表删除数据
  246. GlobalTableField::where('id',$data['id'])->delete();
  247. return Result::success('删除成功');
  248. }
  249. /**
  250. * 获取自定义生成表里面的数据
  251. */
  252. public function getCustomTableData(array $data): array
  253. {
  254. $globalTable = GlobalTable::where('id',$data['table_id'])->first();
  255. if(empty($globalTable)){
  256. return Result::error('表单不存在');
  257. }
  258. //根据返回的table名称,查询这个表的数据
  259. $data = Db::connection('global')->table($globalTable->table)->get();
  260. return Result::success($data);
  261. }
  262. }