FormService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. use App\Model\GlobalTableFieldValue;
  10. #[RpcService(name: "FormService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  11. class FormService implements FormServiceInterface
  12. {
  13. /**
  14. * 添加全局表单
  15. * @param array $data
  16. * @return array|mixed
  17. */
  18. public function addGlobalTable(array $data): array
  19. {
  20. // 过滤掉空值
  21. $data = array_filter($data, function($value) {
  22. return !empty($value);
  23. });
  24. // 检查是否已存在相同名称的记录
  25. $globalTable = GlobalTable::on('global')->where(['table'=> $data['table'],'website_id'=>$data['website_id']])->first();
  26. if (empty($globalTable)) {
  27. $id = GlobalTable::on('global')->insertGetId($data);
  28. //给global库创建表 表名为$data['table'] 的值,并初始化一个字段id,类型为int,自增,主键,再初始化一个字段title,类型为varchar,长度为255
  29. Db::connection('global')->statement("CREATE TABLE IF NOT EXISTS `{$data['table']}` (
  30. `id` int(11) NOT NULL AUTO_INCREMENT,
  31. `title` varchar(255) DEFAULT NULL,
  32. PRIMARY KEY (`id`)
  33. )");
  34. //给GlobalTableField表初始化数据
  35. GlobalTableField::on('global')->insert([
  36. [
  37. 'table_id' => $id,
  38. 'field_name' => 'id',
  39. 'title' => '编号',
  40. 'field_type' => '1',
  41. 'length' => 255,
  42. 'sort' => 0,
  43. 'is_check' => 1,
  44. 'admin_display' => 1,
  45. 'home_display' => 1,
  46. 'disable_del' => 1,
  47. ],
  48. [
  49. 'table_id' => $id,
  50. 'field_name' => 'title',
  51. 'title' => '标题',
  52. 'field_type' => '1',
  53. 'length' => 255,
  54. 'sort' => 0,
  55. 'is_check' => 1,
  56. 'admin_display' => 1,
  57. 'home_display' => 1,
  58. 'disable_del' => 1,
  59. ]
  60. ]);
  61. return Result::success('添加成功');
  62. }
  63. return Result::error('表单已存在');
  64. }
  65. /**
  66. * 获取全局表单列表
  67. * @param array $data
  68. * @return array
  69. */
  70. public function getGlobalTableList(array $data): array
  71. {
  72. try {
  73. // 构建查询
  74. $query = GlobalTable::query()
  75. ->when(!empty($data['website_id']), function($q) use ($data) {
  76. return $q->where('website_id', $data['website_id']);
  77. })
  78. ->when(!empty($data['name']), function($q) use ($data) {
  79. return $q->where('name', 'like', '%' . $data['name'] . '%');
  80. });
  81. // 分页参数
  82. $page = (int)($data['page'] ?? 1);
  83. $pageSize = (int)($data['pageSize'] ?? 10);
  84. // 先获取总数
  85. $total = $query->count();
  86. // 获取数据
  87. $list = $query->orderBy('updated_at', 'desc')
  88. ->offset(($page - 1) * $pageSize)
  89. ->limit($pageSize)
  90. ->get();
  91. // 如果没有数据,直接返回空结果
  92. if ($list->isEmpty()) {
  93. return Result::success([
  94. 'list' => [],
  95. 'total' => $total,
  96. 'page' => $page,
  97. 'pageSize' => $pageSize
  98. ]);
  99. }
  100. // 获取并合并 website 数据
  101. $websites = Db::connection('secondary')
  102. ->table('website')
  103. ->whereIn('id', $list->pluck('website_id'))
  104. ->pluck('website_name', 'id');
  105. // 合并数据并返回
  106. return Result::success([
  107. 'list' => $list->map(function($item) use ($websites) {
  108. $item->website_name = $websites[$item->website_id] ?? '';
  109. return $item;
  110. }),
  111. 'total' => $total,
  112. 'page' => $page,
  113. 'pageSize' => $pageSize
  114. ]);
  115. } catch (\Throwable $e) {
  116. return Result::error('查询失败:' . $e->getMessage());
  117. }
  118. }
  119. /**
  120. * 获取全局表单
  121. * @param array $data
  122. * @return array
  123. */
  124. public function getGlobalTable(array $data): array
  125. {
  126. $globalTable = GlobalTable::where('id',$data['id'])->first();
  127. return Result::success($globalTable);
  128. }
  129. /**
  130. * 修改全局表单
  131. * @param array $data
  132. * @return array
  133. */
  134. public function upGlobalTable(array $data): array
  135. {
  136. $globalTable = GlobalTable::where('id',$data['id'])->first();
  137. if(empty($globalTable)){
  138. return Result::error('表单不存在');
  139. }
  140. $data = array_filter($data,function($value){
  141. return !empty($value);
  142. });
  143. GlobalTable::where(['id'=>$data['id']])->update($data);
  144. return Result::success('修改成功');
  145. }
  146. /**
  147. * 删除全局表单
  148. * @param array $data
  149. * @return array
  150. */
  151. public function delGlobalTable(array $data): array
  152. {
  153. $globalTable = GlobalTable::where('id', $data['id'])->first();
  154. if(empty($globalTable)){
  155. return Result::error('表单不存在');
  156. }
  157. //删除global库的表
  158. Db::connection('global')->statement("DROP TABLE IF EXISTS `{$globalTable->table}`");
  159. //删除GlobalTableField表中table_id为$data['id']的数据
  160. GlobalTableField::where('table_id', $data['id'])->delete();
  161. GlobalTable::where('id', $data['id'])->delete();
  162. return Result::success('删除成功');
  163. }
  164. /**
  165. * 获取表单字段列表
  166. * @param array $data
  167. * @return array
  168. */
  169. public function getGlobalTableFieldList(array $data): array
  170. {
  171. $fields = GlobalTableField::from('global_table_field as a')
  172. ->join('global_table_field_type as b', 'a.field_type', '=', 'b.id')
  173. ->where('a.table_id', $data['id'])
  174. ->orderBy('a.sort', 'asc')
  175. ->select(
  176. // global_table_field 所有字段
  177. 'a.*',
  178. // global_table_field_type 字段
  179. 'b.type_name',
  180. 'b.type_name_alias',
  181. 'b.field_type as type_definition'
  182. )
  183. ->get();
  184. return Result::success($fields);
  185. }
  186. /**
  187. * 获取表单字段
  188. * @param array $data
  189. * @return array
  190. */
  191. public function getGlobalTableField(array $data): array
  192. {
  193. $globalTableField = GlobalTableField::where('id',$data['id'])->first();
  194. return Result::success($globalTableField);
  195. }
  196. /**
  197. * 添加表单字段
  198. * @param array $data
  199. * @return array
  200. * @throws \Throwable
  201. */
  202. public function addGlobalTableField(array $data): array
  203. {
  204. $globalTable = GlobalTable::where('id',$data['table_id'])->first();
  205. if(empty($globalTable)){
  206. return Result::error('表单不存在');
  207. }
  208. //检查是否已存在相同名称的记录
  209. $globalTableField = GlobalTableField::where(['table_id'=>$data['table_id'],'field_name'=>$data['field_name']])->first();
  210. if(!empty($globalTableField)){
  211. return Result::error('字段已存在');
  212. }
  213. // 检查表中是否已存在该列
  214. try {
  215. $columns = Db::connection('global')->select("SHOW COLUMNS FROM `{$globalTable->table}`");
  216. $columnNames = array_column($columns, 'Field');
  217. if (in_array($data['field_name'], $columnNames)) {
  218. return Result::error('字段名已存在于数据表中');
  219. }
  220. } catch (\Throwable $e) {
  221. return Result::error('检查字段是否存在时出错:' . $e->getMessage());
  222. }
  223. $globalTableFieldTypeInfo = GlobalTableFieldType::where('id',$data['field_type'])->first();
  224. //给global库的表添加字段
  225. Db::connection('global')->statement("ALTER TABLE `{$globalTable->table}` ADD COLUMN `{$data['field_name']}` {$globalTableFieldTypeInfo['field_type']}({$data['length']}) COMMENT '{$data['title']}'");
  226. //给GlobalTableField表添加数据
  227. GlobalTableField::create($data);
  228. return Result::success('添加成功');
  229. }
  230. /**
  231. * 修改表单字段
  232. * @param array $data
  233. * @return array
  234. * @throws \Throwable
  235. */
  236. public function upGlobalTableField(array $data): array
  237. {
  238. $globalTable = GlobalTable::where('id',$data['table_id'])->first();
  239. if(empty($globalTable)){
  240. return Result::error('表单不存在');
  241. }
  242. //检查是否已存在相同名称的记录
  243. $globalTableField = GlobalTableField::where(['table_id'=>$data['table_id'],'field_name'=>$data['field_name']])->first();
  244. if(!empty($globalTableField) && $globalTableField->id != $data['id']){
  245. return Result::error('字段已存在');
  246. }
  247. $globalTableFieldTypeInfo = GlobalTableFieldType::where('id',$data['field_type'])->first();
  248. //给global库的表修改字段
  249. Db::connection('global')->statement("ALTER TABLE `{$globalTable->table}` CHANGE COLUMN `{$data['field_name']}` `{$data['field_name']}` {$globalTableFieldTypeInfo['field_type']}({$data['length']}) COMMENT '{$data['title']}'");
  250. //给GlobalTableField表修改数据
  251. GlobalTableField::where('id',$data['id'])->update($data);
  252. return Result::success('修改成功');
  253. }
  254. /**
  255. * 删除表单字段
  256. * @param array $data
  257. * @return array
  258. * @throws \Throwable
  259. */
  260. public function delGlobalTableField(array $data): array
  261. {
  262. $tableFieldInfo = GlobalTableField::where('id',$data['id'])->first();
  263. if(empty($tableFieldInfo)){
  264. return Result::error('字段不存在');
  265. }
  266. $globalTable = GlobalTable::where('id',$tableFieldInfo['table_id'])->first();
  267. if(empty($globalTable)){
  268. return Result::error('表单不存在');
  269. }
  270. //给global库的表删除字段
  271. Db::connection('global')->statement("ALTER TABLE `{$globalTable->table}` DROP COLUMN `{$tableFieldInfo['field_name']}`");
  272. //给GlobalTableField表删除数据
  273. GlobalTableField::where('id',$data['id'])->delete();
  274. return Result::success('删除成功');
  275. }
  276. /**
  277. * 获取自定义生成表里面的数据
  278. */
  279. public function getGlobalTableData(array $data): array
  280. {
  281. try {
  282. //查询global库的表GlobalTableField模型中table_id为$data['id']的数据条件为admin_display=1的数据
  283. $globalTableFields = GlobalTableField::where(['table_id'=>$data['id'],'admin_display'=>1])->get();
  284. //查询global库的表GlobalTable模型中id为$data['id']的数据
  285. $globalTable = GlobalTable::where('id',$data['id'])->first();
  286. if(empty($globalTable)){
  287. return Result::error('表单不存在');
  288. }
  289. // 构建查询
  290. $query = Db::connection('global')->table($globalTable->table);
  291. // 添加title模糊搜索
  292. if (!empty($data['title'])) {
  293. $query->where('title', 'like', '%' . $data['title'] . '%');
  294. }
  295. // 分页参数
  296. $page = (int)($data['page'] ?? 1);
  297. $pageSize = (int)($data['pageSize'] ?? 10);
  298. // 先获取总数
  299. $total = $query->count();
  300. // 获取分页数据
  301. $list = $query->select($globalTableFields->pluck('field_name')->toArray())
  302. ->orderBy('id', 'desc')
  303. ->offset(($page - 1) * $pageSize)
  304. ->limit($pageSize)
  305. ->get();
  306. // 处理返回数据,将field_name替换为title
  307. // $list = $list->map(function($item) use ($globalTableFields) {
  308. // $newItem = new \stdClass();
  309. // foreach ($globalTableFields as $field) {
  310. // $fieldName = $field->field_name;
  311. // if (isset($item->$fieldName)) {
  312. // $newItem->{$field->title} = $item->$fieldName;
  313. // }
  314. // }
  315. // return $newItem;
  316. // });
  317. return Result::success([
  318. 'tableFields' => $globalTableFields,
  319. 'list' => $list,
  320. 'total' => $total,
  321. 'page' => $page,
  322. 'pageSize' => $pageSize
  323. ]);
  324. } catch (\Throwable $e) {
  325. return Result::error('查询失败:' . $e->getMessage());
  326. }
  327. }
  328. /**
  329. * 获取字段类型列表
  330. */
  331. public function getGlobalTableFieldTypeList(array $data): array
  332. {
  333. try {
  334. $list = Db::connection('global')->table("global_table_field_type")->get();
  335. return Result::success($list);
  336. }catch (\Throwable $e){
  337. return Result::error('查询失败:' . $e->getMessage());
  338. }
  339. }
  340. /**
  341. * 删除自定义表里面的某一条数据
  342. */
  343. public function delGlobalTableData(array $data): array
  344. {
  345. try {
  346. //查询global库的表GlobalTable模型中id为$data['id']的数据
  347. $globalTable = GlobalTable::where('id',$data['table_id'])->first();
  348. if(empty($globalTable)){
  349. return Result::error('表单不存在');
  350. }
  351. // 构建查询
  352. $query = Db::connection('global')->table($globalTable->table);
  353. $query->where('id',$data['id']);
  354. $query->delete();
  355. return Result::success('删除成功');
  356. }catch (\Throwable $e){
  357. return Result::error('删除失败:' . $e->getMessage());
  358. }
  359. }
  360. /**
  361. * 修改自定义表里面的某一条数据
  362. */
  363. public function updateGlobalTableData(array $data): array
  364. {
  365. try {
  366. //查询global库的表GlobalTable模型中id为$data['id']的数据
  367. $globalTable = GlobalTable::where('id',$data['table_id'])->first();
  368. if(empty($globalTable)){
  369. return Result::error('表单不存在');
  370. }
  371. unset($data['table_id']);
  372. // 构建查询
  373. $query = Db::connection('global')->table($globalTable->table);
  374. $query->where('id',$data['id']);
  375. $query->update($data);
  376. return Result::success('修改成功');
  377. }catch (\Throwable $e){
  378. return Result::error('修改失败:' . $e->getMessage());
  379. }
  380. }
  381. /**
  382. * 查看自定义表里面的某条数据
  383. */
  384. public function getGlobalTableDataById(array $data): array
  385. {
  386. try {
  387. //查询
  388. $globalTable = GlobalTable::where('id',$data['table_id'])->first();
  389. if(empty($globalTable)){
  390. return Result::error('表单不存在');
  391. }
  392. $query = Db::connection('global')->table($globalTable->table);
  393. $query->where('id',$data['id']);
  394. $dataInfo = $query->first();
  395. $globalTableField = GlobalTableField::where(['table_id'=>$data['table_id']])->get();
  396. $return = [
  397. 'data'=>$dataInfo,
  398. 'tableFields'=>$globalTableField->toArray(),
  399. ];
  400. return Result::success($return);
  401. }catch (\Throwable $e){
  402. return Result::error('查询失败:' . $e->getMessage());
  403. }
  404. }
  405. }