@@ -152,3 +152,166 @@ func (dictionaryService *DictionaryService) checkCircularReference(currentID uin
152152
153153 return nil
154154}
155+
156+ //@author: [yourname]
157+ //@function: ExportSysDictionary
158+ //@description: 导出字典JSON(包含字典详情)
159+ //@param: id uint
160+ //@return: exportData map[string]interface{}, err error
161+
162+ func (dictionaryService * DictionaryService ) ExportSysDictionary (id uint ) (exportData map [string ]interface {}, err error ) {
163+ var dictionary system.SysDictionary
164+ // 查询字典及其所有详情
165+ err = global .GVA_DB .Where ("id = ?" , id ).Preload ("SysDictionaryDetails" , func (db * gorm.DB ) * gorm.DB {
166+ return db .Order ("sort" )
167+ }).First (& dictionary ).Error
168+ if err != nil {
169+ return nil , err
170+ }
171+
172+ // 构造导出数据
173+ exportData = map [string ]interface {}{
174+ "name" : dictionary .Name ,
175+ "type" : dictionary .Type ,
176+ "status" : dictionary .Status ,
177+ "desc" : dictionary .Desc ,
178+ "details" : dictionary .SysDictionaryDetails ,
179+ }
180+
181+ return exportData , nil
182+ }
183+
184+ //@author: [yourname]
185+ //@function: ImportSysDictionary
186+ //@description: 导入字典JSON(包含字典详情)
187+ //@param: importData map[string]interface{}
188+ //@return: err error
189+
190+ func (dictionaryService * DictionaryService ) ImportSysDictionary (importData map [string ]interface {}) error {
191+ // 解析基本字典信息
192+ name , ok := importData ["name" ].(string )
193+ if ! ok || name == "" {
194+ return errors .New ("字典名称不能为空" )
195+ }
196+
197+ dictType , ok := importData ["type" ].(string )
198+ if ! ok || dictType == "" {
199+ return errors .New ("字典类型不能为空" )
200+ }
201+
202+ // 检查字典类型是否已存在
203+ if ! errors .Is (global .GVA_DB .First (& system.SysDictionary {}, "type = ?" , dictType ).Error , gorm .ErrRecordNotFound ) {
204+ return errors .New ("存在相同的type,不允许导入" )
205+ }
206+
207+ // 创建字典
208+ dictionary := system.SysDictionary {
209+ Name : name ,
210+ Type : dictType ,
211+ }
212+
213+ // 处理status字段
214+ if status , ok := importData ["status" ].(bool ); ok {
215+ dictionary .Status = & status
216+ }
217+
218+ // 处理desc字段
219+ if desc , ok := importData ["desc" ].(string ); ok {
220+ dictionary .Desc = desc
221+ }
222+
223+ // 开启事务
224+ return global .GVA_DB .Transaction (func (tx * gorm.DB ) error {
225+ // 创建字典
226+ if err := tx .Create (& dictionary ).Error ; err != nil {
227+ return err
228+ }
229+
230+ // 处理字典详情
231+ if details , ok := importData ["details" ].([]interface {}); ok && len (details ) > 0 {
232+ // 创建一个映射来跟踪旧ID到新ID的对应关系
233+ idMap := make (map [uint ]uint )
234+
235+ // 第一遍:创建所有详情记录(不设置parent_id)
236+ for _ , detail := range details {
237+ detailMap , ok := detail .(map [string ]interface {})
238+ if ! ok {
239+ continue
240+ }
241+
242+ label , _ := detailMap ["label" ].(string )
243+ value , _ := detailMap ["value" ].(string )
244+
245+ if label == "" || value == "" {
246+ continue
247+ }
248+
249+ detailRecord := system.SysDictionaryDetail {
250+ Label : label ,
251+ Value : value ,
252+ SysDictionaryID : int (dictionary .ID ),
253+ }
254+
255+ // 处理extend字段
256+ if extend , ok := detailMap ["extend" ].(string ); ok {
257+ detailRecord .Extend = extend
258+ }
259+
260+ // 处理status字段
261+ if status , ok := detailMap ["status" ].(bool ); ok {
262+ detailRecord .Status = & status
263+ }
264+
265+ // 处理sort字段
266+ if sort , ok := detailMap ["sort" ].(float64 ); ok {
267+ detailRecord .Sort = int (sort )
268+ }
269+
270+ // 创建详情记录
271+ if err := tx .Create (& detailRecord ).Error ; err != nil {
272+ return err
273+ }
274+
275+ // 记录ID映射(如果有原始ID)
276+ if oldID , ok := detailMap ["ID" ].(float64 ); ok {
277+ idMap [uint (oldID )] = detailRecord .ID
278+ }
279+ }
280+
281+ // 第二遍:更新parent_id关系
282+ for i , detail := range details {
283+ detailMap , ok := detail .(map [string ]interface {})
284+ if ! ok {
285+ continue
286+ }
287+
288+ // 如果有parentID,更新它
289+ if oldParentID , ok := detailMap ["parentID" ].(float64 ); ok && oldParentID > 0 {
290+ if newParentID , exists := idMap [uint (oldParentID )]; exists {
291+ // 获取新创建的记录ID(按顺序)
292+ if oldID , ok := detailMap ["ID" ].(float64 ); ok {
293+ if newID , exists := idMap [uint (oldID )]; exists {
294+ if err := tx .Model (& system.SysDictionaryDetail {}).Where ("id = ?" , newID ).Update ("parent_id" , newParentID ).Error ; err != nil {
295+ return err
296+ }
297+ }
298+ } else {
299+ // 如果没有ID,使用索引来查找
300+ var allDetails []system.SysDictionaryDetail
301+ if err := tx .Where ("sys_dictionary_id = ?" , dictionary .ID ).Order ("id" ).Find (& allDetails ).Error ; err != nil {
302+ return err
303+ }
304+ if i < len (allDetails ) {
305+ if err := tx .Model (& system.SysDictionaryDetail {}).Where ("id = ?" , allDetails [i ].ID ).Update ("parent_id" , newParentID ).Error ; err != nil {
306+ return err
307+ }
308+ }
309+ }
310+ }
311+ }
312+ }
313+ }
314+
315+ return nil
316+ })
317+ }
0 commit comments