@@ -166,7 +166,7 @@ func (dictionaryDetailService *DictionaryDetailService) updateChildrenLevelAndPa
166166//@return: sysDictionaryDetail system.SysDictionaryDetail, err error
167167
168168func (dictionaryDetailService * DictionaryDetailService ) GetSysDictionaryDetail (id uint ) (sysDictionaryDetail system.SysDictionaryDetail , err error ) {
169- err = global .GVA_DB .Preload ( "Children" ). Where ("id = ?" , id ).First (& sysDictionaryDetail ).Error
169+ err = global .GVA_DB .Where ("id = ?" , id ).First (& sysDictionaryDetail ).Error
170170 return
171171}
172172
@@ -205,14 +205,14 @@ func (dictionaryDetailService *DictionaryDetailService) GetSysDictionaryDetailIn
205205 if err != nil {
206206 return
207207 }
208- err = db .Preload ( "Children" ). Limit (limit ).Offset (offset ).Order ("sort" ).Find (& sysDictionaryDetails ).Error
208+ err = db .Limit (limit ).Offset (offset ).Order ("sort" ).Find (& sysDictionaryDetails ).Error
209209 return sysDictionaryDetails , total , err
210210}
211211
212212// 按照字典id获取字典全部内容的方法
213213func (dictionaryDetailService * DictionaryDetailService ) GetDictionaryList (dictionaryID uint ) (list []system.SysDictionaryDetail , err error ) {
214214 var sysDictionaryDetails []system.SysDictionaryDetail
215- err = global .GVA_DB .Preload ( "Children" ). Find (& sysDictionaryDetails , "sys_dictionary_id = ?" , dictionaryID ).Error
215+ err = global .GVA_DB .Find (& sysDictionaryDetails , "sys_dictionary_id = ?" , dictionaryID ).Error
216216 return sysDictionaryDetails , err
217217}
218218
@@ -265,19 +265,29 @@ func (dictionaryDetailService *DictionaryDetailService) GetDictionaryDetailsByPa
265265 db = db .Where ("parent_id IS NULL" )
266266 }
267267
268+ err = db .Order ("sort" ).Find (& list ).Error
269+ if err != nil {
270+ return list , err
271+ }
272+
273+ // 如果需要包含子级数据,使用递归方式加载所有层级的子项
268274 if req .IncludeChildren {
269- db = db .Preload ("Children" )
275+ for i := range list {
276+ err = dictionaryDetailService .loadChildren (& list [i ])
277+ if err != nil {
278+ return list , err
279+ }
280+ }
270281 }
271282
272- err = db .Order ("sort" ).Find (& list ).Error
273283 return list , err
274284}
275285
276286// 按照字典type获取字典全部内容的方法
277287func (dictionaryDetailService * DictionaryDetailService ) GetDictionaryListByType (t string ) (list []system.SysDictionaryDetail , err error ) {
278288 var sysDictionaryDetails []system.SysDictionaryDetail
279289 db := global .GVA_DB .Model (& system.SysDictionaryDetail {}).Joins ("JOIN sys_dictionaries ON sys_dictionaries.id = sys_dictionary_details.sys_dictionary_id" )
280- err = db .Debug (). Preload ( "Children" ). Find (& sysDictionaryDetails , "type = ?" , t ).Error
290+ err = db .Find (& sysDictionaryDetails , "type = ?" , t ).Error
281291 return sysDictionaryDetails , err
282292}
283293
@@ -308,15 +318,15 @@ func (dictionaryDetailService *DictionaryDetailService) GetDictionaryTreeListByT
308318// 按照字典id+字典内容value获取单条字典内容
309319func (dictionaryDetailService * DictionaryDetailService ) GetDictionaryInfoByValue (dictionaryID uint , value string ) (detail system.SysDictionaryDetail , err error ) {
310320 var sysDictionaryDetail system.SysDictionaryDetail
311- err = global .GVA_DB .Preload ( "Children" ). First (& sysDictionaryDetail , "sys_dictionary_id = ? and value = ?" , dictionaryID , value ).Error
321+ err = global .GVA_DB .First (& sysDictionaryDetail , "sys_dictionary_id = ? and value = ?" , dictionaryID , value ).Error
312322 return sysDictionaryDetail , err
313323}
314324
315325// 按照字典type+字典内容value获取单条字典内容
316326func (dictionaryDetailService * DictionaryDetailService ) GetDictionaryInfoByTypeValue (t string , value string ) (detail system.SysDictionaryDetail , err error ) {
317327 var sysDictionaryDetails system.SysDictionaryDetail
318328 db := global .GVA_DB .Model (& system.SysDictionaryDetail {}).Joins ("JOIN sys_dictionaries ON sys_dictionaries.id = sys_dictionary_details.sys_dictionary_id" )
319- err = db .Preload ( "Children" ). First (& sysDictionaryDetails , "sys_dictionaries.type = ? and sys_dictionary_details.value = ?" , t , value ).Error
329+ err = db .First (& sysDictionaryDetails , "sys_dictionaries.type = ? and sys_dictionary_details.value = ?" , t , value ).Error
320330 return sysDictionaryDetails , err
321331}
322332
0 commit comments