FormService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. $globalTableField = GlobalTableField::where('table_id',$data['id'])->orderBy("sort","asc")->get();
  172. return Result::success($globalTableField);
  173. }
  174. /**
  175. * 获取表单字段
  176. * @param array $data
  177. * @return array
  178. */
  179. public function getGlobalTableField(array $data): array
  180. {
  181. $globalTableField = GlobalTableField::where('id',$data['id'])->first();
  182. return Result::success($globalTableField);
  183. }
  184. /**
  185. * 添加表单字段
  186. * @param array $data
  187. * @return array
  188. * @throws \Throwable
  189. */
  190. public function addGlobalTableField(array $data): array
  191. {
  192. $globalTable = GlobalTable::where('id',$data['table_id'])->first();
  193. if(empty($globalTable)){
  194. return Result::error('表单不存在');
  195. }
  196. //检查是否已存在相同名称的记录
  197. $globalTableField = GlobalTableField::where(['table_id'=>$data['table_id'],'field_name'=>$data['field_name']])->first();
  198. if(!empty($globalTableField)){
  199. return Result::error('字段已存在');
  200. }
  201. // 检查表中是否已存在该列
  202. try {
  203. $columns = Db::connection('global')->select("SHOW COLUMNS FROM `{$globalTable->table}`");
  204. $columnNames = array_column($columns, 'Field');
  205. if (in_array($data['field_name'], $columnNames)) {
  206. return Result::error('字段名已存在于数据表中');
  207. }
  208. } catch (\Throwable $e) {
  209. return Result::error('检查字段是否存在时出错:' . $e->getMessage());
  210. }
  211. $globalTableFieldTypeInfo = GlobalTableFieldType::where('id',$data['field_type'])->first();
  212. //给global库的表添加字段
  213. Db::connection('global')->statement("ALTER TABLE `{$globalTable->table}` ADD COLUMN `{$data['field_name']}` {$globalTableFieldTypeInfo['field_type']}({$data['length']}) COMMENT '{$data['title']}'");
  214. //给GlobalTableField表添加数据
  215. GlobalTableField::create($data);
  216. return Result::success('添加成功');
  217. }
  218. /**
  219. * 修改表单字段
  220. * @param array $data
  221. * @return array
  222. * @throws \Throwable
  223. */
  224. public function upGlobalTableField(array $data): array
  225. {
  226. $globalTable = GlobalTable::where('id',$data['table_id'])->first();
  227. if(empty($globalTable)){
  228. return Result::error('表单不存在');
  229. }
  230. //检查是否已存在相同名称的记录
  231. $globalTableField = GlobalTableField::where(['table_id'=>$data['table_id'],'field_name'=>$data['field_name']])->first();
  232. if(!empty($globalTableField) && $globalTableField->id != $data['id']){
  233. return Result::error('字段已存在');
  234. }
  235. $globalTableFieldTypeInfo = GlobalTableFieldType::where('id',$data['field_type'])->first();
  236. //给global库的表修改字段
  237. Db::connection('global')->statement("ALTER TABLE `{$globalTable->table}` CHANGE COLUMN `{$data['field_name']}` `{$data['field_name']}` {$globalTableFieldTypeInfo['field_type']}({$data['length']}) COMMENT '{$data['title']}'");
  238. //给GlobalTableField表修改数据
  239. GlobalTableField::where('id',$data['id'])->update($data);
  240. return Result::success('修改成功');
  241. }
  242. /**
  243. * 删除表单字段
  244. * @param array $data
  245. * @return array
  246. * @throws \Throwable
  247. */
  248. public function delGlobalTableField(array $data): array
  249. {
  250. $tableFieldInfo = GlobalTableField::where('id',$data['id'])->first();
  251. if(empty($tableFieldInfo)){
  252. return Result::error('字段不存在');
  253. }
  254. $globalTable = GlobalTable::where('id',$tableFieldInfo['table_id'])->first();
  255. if(empty($globalTable)){
  256. return Result::error('表单不存在');
  257. }
  258. //给global库的表删除字段
  259. Db::connection('global')->statement("ALTER TABLE `{$globalTable->table}` DROP COLUMN `{$tableFieldInfo['field_name']}`");
  260. //给GlobalTableField表删除数据
  261. GlobalTableField::where('id',$data['id'])->delete();
  262. return Result::success('删除成功');
  263. }
  264. /**
  265. * 获取自定义生成表里面的数据
  266. */
  267. public function getGlobalTableData(array $data): array
  268. {
  269. try {
  270. //查询global库的表GlobalTableField模型中table_id为$data['id']的数据条件为admin_display=1的数据
  271. $globalTableFields = GlobalTableField::where(['table_id'=>$data['id'],'admin_display'=>1])->get();
  272. //查询global库的表GlobalTable模型中id为$data['id']的数据
  273. $globalTable = GlobalTable::where('id',$data['id'])->first();
  274. if(empty($globalTable)){
  275. return Result::error('表单不存在');
  276. }
  277. // 构建查询
  278. $query = Db::connection('global')->table($globalTable->table);
  279. // 添加title模糊搜索
  280. if (!empty($data['title'])) {
  281. $query->where('title', 'like', '%' . $data['title'] . '%');
  282. }
  283. // 分页参数
  284. $page = (int)($data['page'] ?? 1);
  285. $pageSize = (int)($data['pageSize'] ?? 10);
  286. // 先获取总数
  287. $total = $query->count();
  288. // 获取分页数据
  289. $list = $query->select($globalTableFields->pluck('field_name')->toArray())
  290. ->orderBy('id', 'desc')
  291. ->offset(($page - 1) * $pageSize)
  292. ->limit($pageSize)
  293. ->get();
  294. // 处理返回数据,将field_name替换为title
  295. $list = $list->map(function($item) use ($globalTableFields) {
  296. $newItem = new \stdClass();
  297. foreach ($globalTableFields as $field) {
  298. $fieldName = $field->field_name;
  299. if (isset($item->$fieldName)) {
  300. $newItem->{$field->title} = $item->$fieldName;
  301. }
  302. }
  303. return $newItem;
  304. });
  305. return Result::success([
  306. 'list' => $list,
  307. 'total' => $total,
  308. 'page' => $page,
  309. 'pageSize' => $pageSize
  310. ]);
  311. } catch (\Throwable $e) {
  312. return Result::error('查询失败:' . $e->getMessage());
  313. }
  314. }
  315. }