@@ -3,6 +3,12 @@ package system
33import (
44 "context"
55 "fmt"
6+ "go/token"
7+ "os"
8+ "path/filepath"
9+ "strings"
10+ "text/template"
11+
612 "github.com/flipped-aurora/gin-vue-admin/server/global"
713 common "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
814 model "github.com/flipped-aurora/gin-vue-admin/server/model/system"
@@ -11,12 +17,7 @@ import (
1117 "github.com/flipped-aurora/gin-vue-admin/server/utils/ast"
1218 "github.com/flipped-aurora/gin-vue-admin/server/utils/autocode"
1319 "github.com/pkg/errors"
14- "go/token"
1520 "gorm.io/gorm"
16- "os"
17- "path/filepath"
18- "strings"
19- "text/template"
2021)
2122
2223var AutoCodePackage = new (autoCodePackage )
@@ -162,7 +163,6 @@ func (s *autoCodePackage) All(ctx context.Context) (entities []model.SysAutoCode
162163 "api" : true ,
163164 "config" : true ,
164165 "initialize" : true ,
165- "model" : true ,
166166 "plugin" : true ,
167167 "router" : true ,
168168 "service" : true ,
@@ -179,14 +179,25 @@ func (s *autoCodePackage) All(ctx context.Context) (entities []model.SysAutoCode
179179 }
180180 }
181181 }
182- if len (dirNameMap ) != 0 {
183- continue
182+
183+ var desc string
184+ if len (dirNameMap ) == 0 {
185+ // 完全符合标准结构
186+ desc = "系统自动读取" + pluginDir [i ].Name () + "插件,使用前请确认是否为v2版本插件"
187+ } else {
188+ // 缺少某些结构,生成警告描述
189+ var missingDirs []string
190+ for dirName := range dirNameMap {
191+ missingDirs = append (missingDirs , dirName )
192+ }
193+ desc = fmt .Sprintf ("系统自动读取,但是缺少 %s 结构,不建议自动化和mcp使用" , strings .Join (missingDirs , "、" ))
184194 }
195+
185196 pluginPackage := model.SysAutoCodePackage {
186197 PackageName : pluginDir [i ].Name (),
187198 Template : "plugin" ,
188199 Label : pluginDir [i ].Name () + "插件" ,
189- Desc : "系统自动读取" + pluginDir [ i ]. Name () + "插件,使用前请确认是否为v2版本插件" ,
200+ Desc : desc ,
190201 Module : global .GVA_CONFIG .AutoCode .Module ,
191202 }
192203 plugin = append (plugin , pluginPackage )
@@ -225,6 +236,40 @@ func (s *autoCodePackage) All(ctx context.Context) (entities []model.SysAutoCode
225236 entities = append (entities , createEntity ... )
226237 }
227238
239+ // 处理数据库存在但实体文件不存在的情况 - 删除数据库中对应的数据
240+ existingPackageNames := make (map [string ]bool )
241+ // 收集所有存在的包名
242+ for i := 0 ; i < len (server ); i ++ {
243+ existingPackageNames [server [i ].PackageName ] = true
244+ }
245+ for i := 0 ; i < len (plugin ); i ++ {
246+ existingPackageNames [plugin [i ].PackageName ] = true
247+ }
248+
249+ // 找出需要删除的数据库记录
250+ deleteEntityIDs := []uint {}
251+ for i := 0 ; i < len (entities ); i ++ {
252+ if ! existingPackageNames [entities [i ].PackageName ] {
253+ deleteEntityIDs = append (deleteEntityIDs , entities [i ].ID )
254+ }
255+ }
256+
257+ // 删除数据库中不存在文件的记录
258+ if len (deleteEntityIDs ) > 0 {
259+ err = global .GVA_DB .WithContext (ctx ).Delete (& model.SysAutoCodePackage {}, deleteEntityIDs ).Error
260+ if err != nil {
261+ return nil , errors .Wrap (err , "删除不存在的包记录失败!" )
262+ }
263+ // 从返回结果中移除已删除的记录
264+ filteredEntities := []model.SysAutoCodePackage {}
265+ for i := 0 ; i < len (entities ); i ++ {
266+ if existingPackageNames [entities [i ].PackageName ] {
267+ filteredEntities = append (filteredEntities , entities [i ])
268+ }
269+ }
270+ entities = filteredEntities
271+ }
272+
228273 return entities , nil
229274}
230275
0 commit comments