123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- <?php
- namespace App\JsonRpc;
- use Hyperf\RpcServer\Annotation\RpcService;
- use App\Tools\Result;
- use App\Model\GlobalTable;
- use Hyperf\DbConnection\Db;
- use App\Model\GlobalTableField;
- use App\Model\GlobalTableFieldType;
- use App\Model\GlobalTableFieldValue;
- #[RpcService(name: "FormService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
- class FormService implements FormServiceInterface
- {
- /**
- * 添加全局表单
- * @param array $data
- * @return array|mixed
- */
- public function addGlobalTable(array $data): array
- {
- // 过滤掉空值
- $data = array_filter($data, function($value) {
- return !empty($value);
- });
- // 检查是否已存在相同名称的记录
- $globalTable = GlobalTable::on('global')->where(['table'=> $data['table'],'website_id'=>$data['website_id']])->first();
- if (empty($globalTable)) {
- $id = GlobalTable::on('global')->insertGetId($data);
- //给global库创建表 表名为$data['table'] 的值,并初始化一个字段id,类型为int,自增,主键,再初始化一个字段title,类型为varchar,长度为255
- Db::connection('global')->statement("CREATE TABLE IF NOT EXISTS `{$data['table']}` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `title` varchar(255) DEFAULT NULL,
- PRIMARY KEY (`id`)
- )");
- //给GlobalTableField表初始化数据
- GlobalTableField::on('global')->insert([
- [
- 'table_id' => $id,
- 'field_name' => 'id',
- 'title' => '编号',
- 'field_type' => '1',
- 'length' => 255,
- 'sort' => 0,
- 'is_check' => 1,
- 'admin_display' => 1,
- 'home_display' => 1,
- 'disable_del' => 1,
- ],
- [
- 'table_id' => $id,
- 'field_name' => 'title',
- 'title' => '标题',
- 'field_type' => '1',
- 'length' => 255,
- 'sort' => 0,
- 'is_check' => 1,
- 'admin_display' => 1,
- 'home_display' => 1,
- 'disable_del' => 1,
- ]
-
- ]);
-
- return Result::success('添加成功');
- }
- return Result::error('表单已存在');
- }
- /**
- * 获取全局表单列表
- * @param array $data
- * @return array
- */
- public function getGlobalTableList(array $data): array
- {
- try {
- // 构建查询
- $query = GlobalTable::query()
- ->when(!empty($data['website_id']), function($q) use ($data) {
- return $q->where('website_id', $data['website_id']);
- })
- ->when(!empty($data['name']), function($q) use ($data) {
- return $q->where('name', 'like', '%' . $data['name'] . '%');
- });
-
- // 分页参数
- $page = (int)($data['page'] ?? 1);
- $pageSize = (int)($data['pageSize'] ?? 10);
-
- // 先获取总数
- $total = $query->count();
-
- // 获取数据
- $list = $query->orderBy('updated_at', 'desc')
- ->offset(($page - 1) * $pageSize)
- ->limit($pageSize)
- ->get();
-
- // 如果没有数据,直接返回空结果
- if ($list->isEmpty()) {
- return Result::success([
- 'list' => [],
- 'total' => $total,
- 'page' => $page,
- 'pageSize' => $pageSize
- ]);
- }
-
- // 获取并合并 website 数据
- $websites = Db::connection('secondary')
- ->table('website')
- ->whereIn('id', $list->pluck('website_id'))
- ->pluck('website_name', 'id');
-
- // 合并数据并返回
- return Result::success([
- 'list' => $list->map(function($item) use ($websites) {
- $item->website_name = $websites[$item->website_id] ?? '';
- return $item;
- }),
- 'total' => $total,
- 'page' => $page,
- 'pageSize' => $pageSize
- ]);
-
- } catch (\Throwable $e) {
- return Result::error('查询失败:' . $e->getMessage());
- }
- }
- /**
- * 获取全局表单
- * @param array $data
- * @return array
- */
- public function getGlobalTable(array $data): array
- {
- $globalTable = GlobalTable::where('id',$data['id'])->first();
- return Result::success($globalTable);
- }
- /**
- * 修改全局表单
- * @param array $data
- * @return array
- */
- public function upGlobalTable(array $data): array
- {
- $globalTable = GlobalTable::where('id',$data['id'])->first();
- if(empty($globalTable)){
- return Result::error('表单不存在');
- }
- $data = array_filter($data,function($value){
- return !empty($value);
- });
- GlobalTable::where(['id'=>$data['id']])->update($data);
- return Result::success('修改成功');
- }
- /**
- * 删除全局表单
- * @param array $data
- * @return array
- */
- public function delGlobalTable(array $data): array
- {
- $globalTable = GlobalTable::where('id', $data['id'])->first();
- if(empty($globalTable)){
- return Result::error('表单不存在');
- }
- //删除global库的表
- Db::connection('global')->statement("DROP TABLE IF EXISTS `{$globalTable->table}`");
- //删除GlobalTableField表中table_id为$data['id']的数据
- GlobalTableField::where('table_id', $data['id'])->delete();
- GlobalTable::where('id', $data['id'])->delete();
- return Result::success('删除成功');
- }
- /**
- * 获取表单字段列表
- * @param array $data
- * @return array
- */
- public function getGlobalTableFieldList(array $data): array
- {
- $globalTableField = GlobalTableField::where('table_id',$data['id'])->orderBy("sort","asc")->get();
- return Result::success($globalTableField);
- }
- /**
- * 获取表单字段
- * @param array $data
- * @return array
- */
- public function getGlobalTableField(array $data): array
- {
- $globalTableField = GlobalTableField::where('id',$data['id'])->first();
- return Result::success($globalTableField);
- }
- /**
- * 添加表单字段
- * @param array $data
- * @return array
- * @throws \Throwable
- */
- public function addGlobalTableField(array $data): array
- {
- $globalTable = GlobalTable::where('id',$data['table_id'])->first();
- if(empty($globalTable)){
- return Result::error('表单不存在');
- }
- //检查是否已存在相同名称的记录
- $globalTableField = GlobalTableField::where(['table_id'=>$data['table_id'],'field_name'=>$data['field_name']])->first();
- if(!empty($globalTableField)){
- return Result::error('字段已存在');
- }
- // 检查表中是否已存在该列
- try {
- $columns = Db::connection('global')->select("SHOW COLUMNS FROM `{$globalTable->table}`");
- $columnNames = array_column($columns, 'Field');
- if (in_array($data['field_name'], $columnNames)) {
- return Result::error('字段名已存在于数据表中');
- }
- } catch (\Throwable $e) {
- return Result::error('检查字段是否存在时出错:' . $e->getMessage());
- }
- $globalTableFieldTypeInfo = GlobalTableFieldType::where('id',$data['field_type'])->first();
- //给global库的表添加字段
- Db::connection('global')->statement("ALTER TABLE `{$globalTable->table}` ADD COLUMN `{$data['field_name']}` {$globalTableFieldTypeInfo['field_type']}({$data['length']}) COMMENT '{$data['title']}'");
- //给GlobalTableField表添加数据
- GlobalTableField::create($data);
- return Result::success('添加成功');
- }
- /**
- * 修改表单字段
- * @param array $data
- * @return array
- * @throws \Throwable
- */
- public function upGlobalTableField(array $data): array
- {
- $globalTable = GlobalTable::where('id',$data['table_id'])->first();
- if(empty($globalTable)){
- return Result::error('表单不存在');
- }
- //检查是否已存在相同名称的记录
- $globalTableField = GlobalTableField::where(['table_id'=>$data['table_id'],'field_name'=>$data['field_name']])->first();
- if(!empty($globalTableField) && $globalTableField->id != $data['id']){
- return Result::error('字段已存在');
- }
- $globalTableFieldTypeInfo = GlobalTableFieldType::where('id',$data['field_type'])->first();
- //给global库的表修改字段
- Db::connection('global')->statement("ALTER TABLE `{$globalTable->table}` CHANGE COLUMN `{$data['field_name']}` `{$data['field_name']}` {$globalTableFieldTypeInfo['field_type']}({$data['length']}) COMMENT '{$data['title']}'");
- //给GlobalTableField表修改数据
- GlobalTableField::where('id',$data['id'])->update($data);
- return Result::success('修改成功');
- }
- /**
- * 删除表单字段
- * @param array $data
- * @return array
- * @throws \Throwable
- */
- public function delGlobalTableField(array $data): array
- {
- $tableFieldInfo = GlobalTableField::where('id',$data['id'])->first();
- if(empty($tableFieldInfo)){
- return Result::error('字段不存在');
- }
- $globalTable = GlobalTable::where('id',$tableFieldInfo['table_id'])->first();
- if(empty($globalTable)){
- return Result::error('表单不存在');
- }
- //给global库的表删除字段
- Db::connection('global')->statement("ALTER TABLE `{$globalTable->table}` DROP COLUMN `{$tableFieldInfo['field_name']}`");
- //给GlobalTableField表删除数据
- GlobalTableField::where('id',$data['id'])->delete();
- return Result::success('删除成功');
- }
- /**
- * 获取自定义生成表里面的数据
- */
- public function getGlobalTableData(array $data): array
- {
- try {
- //查询global库的表GlobalTableField模型中table_id为$data['id']的数据条件为admin_display=1的数据
- $globalTableFields = GlobalTableField::where(['table_id'=>$data['id'],'admin_display'=>1])->get();
-
- //查询global库的表GlobalTable模型中id为$data['id']的数据
- $globalTable = GlobalTable::where('id',$data['id'])->first();
- if(empty($globalTable)){
- return Result::error('表单不存在');
- }
- // 构建查询
- $query = Db::connection('global')->table($globalTable->table);
-
- // 添加title模糊搜索
- if (!empty($data['title'])) {
- $query->where('title', 'like', '%' . $data['title'] . '%');
- }
- // 分页参数
- $page = (int)($data['page'] ?? 1);
- $pageSize = (int)($data['pageSize'] ?? 10);
-
- // 先获取总数
- $total = $query->count();
-
- // 获取分页数据
- $list = $query->select($globalTableFields->pluck('field_name')->toArray())
- ->orderBy('id', 'desc')
- ->offset(($page - 1) * $pageSize)
- ->limit($pageSize)
- ->get();
- // 处理返回数据,将field_name替换为title
- $list = $list->map(function($item) use ($globalTableFields) {
- $newItem = new \stdClass();
- foreach ($globalTableFields as $field) {
- $fieldName = $field->field_name;
- if (isset($item->$fieldName)) {
- $newItem->{$field->title} = $item->$fieldName;
- }
- }
- return $newItem;
- });
- return Result::success([
- 'list' => $list,
- 'total' => $total,
- 'page' => $page,
- 'pageSize' => $pageSize
- ]);
- } catch (\Throwable $e) {
- return Result::error('查询失败:' . $e->getMessage());
- }
- }
- }
|