FormService.php 12 KB

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