优化
This commit is contained in:
+11
-12
@@ -111,19 +111,18 @@ func (r *dictionaryRepo) UpdateDictionary(ctx context.Context, v *biz.Dictionary
|
||||
return r.data.gormDB.WithContext(ctx).Model(&dictionaryPO{}).Where("id = ?", v.ID).Updates(map[string]any{"name": v.Name, "type": v.Type, "status": v.Status, "desc": v.Desc, "parent_id": v.ParentID}).Error
|
||||
}
|
||||
func (r *dictionaryRepo) DeleteDictionary(ctx context.Context, id uint) error {
|
||||
return r.data.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var count int64
|
||||
if err := tx.Model(&dictionaryPO{}).Where("parent_id = ?", id).Count(&count).Error; err != nil {
|
||||
return err
|
||||
db := r.data.gormDB.WithContext(ctx)
|
||||
var dictionary dictionaryPO
|
||||
if err := db.First(&dictionary, id).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.New("请不要搞事")
|
||||
}
|
||||
if count > 0 {
|
||||
return errors.New("存在子字典不可删除")
|
||||
}
|
||||
if err := tx.Where("sys_dictionary_id = ?", id).Delete(&dictionaryDetailPO{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Delete(&dictionaryPO{}, id).Error
|
||||
})
|
||||
return err
|
||||
}
|
||||
if err := db.Delete(&dictionary).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return db.Where("sys_dictionary_id = ?", id).Delete(&dictionaryDetailPO{}).Error
|
||||
}
|
||||
func (r *dictionaryRepo) FindDictionary(ctx context.Context, id uint, typ string, status *bool, details bool) (*biz.Dictionary, error) {
|
||||
var po dictionaryPO
|
||||
|
||||
Reference in New Issue
Block a user