rkljw 2 dni temu
rodzic
commit
57ea4988bc

+ 35 - 10
app/JsonRpc/FormService.php

@@ -7,6 +7,7 @@ use App\Tools\Result;
 use App\Model\GlobalTable;
 use Hyperf\DbConnection\Db;
 use App\Model\GlobalTableField;
+use App\Model\GlobalTableFieldType;
 
 #[RpcService(name: "FormService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
 class FormService implements FormServiceInterface
@@ -61,13 +62,20 @@ class FormService implements FormServiceInterface
         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'] . '%'));
+                ->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)
@@ -78,7 +86,7 @@ class FormService implements FormServiceInterface
             if ($list->isEmpty()) {
                 return Result::success([
                     'list' => [],
-                    'total' => 0,
+                    'total' => $total,
                     'page' => $page,
                     'pageSize' => $pageSize
                 ]);
@@ -96,7 +104,7 @@ class FormService implements FormServiceInterface
                     $item->website_name = $websites[$item->website_id] ?? '';
                     return $item;
                 }),
-                'total' => $query->count(),
+                'total' => $total,
                 'page' => $page,
                 'pageSize' => $pageSize
             ]);
@@ -122,7 +130,6 @@ class FormService implements FormServiceInterface
      */
     public function upGlobalTable(array $data): array   
     {
-        var_dump($data);
         $globalTable = GlobalTable::where('id',$data['id'])->first();
         if(empty($globalTable)){
             return Result::error('表单不存在');
@@ -158,7 +165,7 @@ class FormService implements FormServiceInterface
      */
     public function getGlobalTableFieldList(array $data): array
     {
-        $globalTableField = GlobalTableField::where('table_id',$data['table_id'])->orderBy("sort","asc")->get();
+        $globalTableField = GlobalTableField::where('table_id',$data['id'])->orderBy("sort","asc")->get();
         return Result::success($globalTableField);
     }
     /**
@@ -188,8 +195,21 @@ class FormService implements FormServiceInterface
         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']}` {$data['field_type']}({$data['length']})"); 
+        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('添加成功');
@@ -211,8 +231,9 @@ class FormService implements FormServiceInterface
         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_type']}({$data['length']})");  
+        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('修改成功');
@@ -225,12 +246,16 @@ class FormService implements FormServiceInterface
      */
     public function delGlobalTableField(array $data): array
     {
-        $globalTable = GlobalTable::where('id',$data['table_id'])->first(); 
+        $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 `{$data['field_name']}`"); 
+        Db::connection('global')->statement("ALTER TABLE `{$globalTable->table}` DROP COLUMN `{$tableFieldInfo['field_name']}`"); 
         //给GlobalTableField表删除数据
         GlobalTableField::where('id',$data['id'])->delete();
         return Result::success('删除成功');

+ 27 - 0
app/JsonRpc/FormServiceInterface.php

@@ -29,6 +29,33 @@ interface FormServiceInterface
     */
     public function delGlobalTable(array $data):array;  
     
+    /**
+     * @param array $data
+     *  @return array
+    */
+    public function addGlobalTableField(array $data):array;
+    /**
+     * @param array $data
+     *  @return array
+    */
+    public function getGlobalTableFieldList(array $data):array;
+    /**
+     * @param array $data
+     *  @return array
+     */
+    public function getGlobalTableField(array $data):array;
+    /**
+     * @param array $data
+     *  @return array
+    */
+    public function upGlobalTableField(array $data):array;
+    /**
+     * @param array $data
+     *  @return array                   
+     * */
+    public function delGlobalTableField(array $data):array;
+    
+
 
 }
 

+ 13 - 3
app/Model/GlobalTableField.php

@@ -22,9 +22,19 @@ class GlobalTableField extends Model
     /**
      * The attributes that are mass assignable.
      */
-    // protected array $fillable = [
-    //     'website_id'
-    // ];
+    protected array $fillable = [
+        'table_id',
+        'field_name',
+        'title',
+        'field_type',   
+        'option',
+        'length',
+        'sort',
+        'is_check',
+        'admin_display',
+        'home_display',
+        'disable_del',
+    ];
 
     /**
      * The attributes that should be cast to native types.

+ 34 - 0
app/Model/GlobalTableFieldType.php

@@ -0,0 +1,34 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Model;
+
+use Hyperf\DbConnection\Model\Model;
+
+/**
+ */
+class GlobalTableFieldType extends Model
+{
+    /**
+     * The table associated with the model.
+     */
+    protected ?string $table = 'global_table_field_type';
+    /**
+     * The connection name for the model.
+     */
+    protected ?string $connection = 'global';
+
+    /**
+     * The attributes that are mass assignable.
+     */
+    // protected array $fillable = [
+    //     'website_id'
+    // ];
+
+    /**
+     * The attributes that should be cast to native types.
+     */
+    protected array $casts = [];
+
+}