123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?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;
- #[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表初始化一条数据,表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']
- GlobalTableField::on('global')->insert([
- 'table_id' => $id,
- 'field_name' => 'title',
- 'title' => '标题',
- 'field_type' => '1',
- 'length' => 255,
- 'sort' => 0,
- 'is_check' => 1,
- 'admin_display' => 1,
- 'home_display' => 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']), fn($q) => $q->where('website_id', $data['website_id']))
- ->when(!empty($data['name']), fn($q) => $q->where('name', 'like', '%' . $data['name'] . '%'));
-
- // 分页参数
- $page = (int)($data['page'] ?? 1);
- $pageSize = (int)($data['pageSize'] ?? 10);
-
- // 获取数据
- $list = $query->orderBy('updated_at', 'desc')
- ->offset(($page - 1) * $pageSize)
- ->limit($pageSize)
- ->get();
-
- // 如果没有数据,直接返回空结果
- if ($list->isEmpty()) {
- return Result::success([
- 'list' => [],
- 'total' => 0,
- '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' => $query->count(),
- '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
- {
- var_dump($data);
- $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('删除成功');
- }
- }
|