diff --git a/internal/models/fault_center.go b/internal/models/fault_center.go new file mode 100644 index 0000000..2640e7f --- /dev/null +++ b/internal/models/fault_center.go @@ -0,0 +1 @@ +package models diff --git a/internal/repo/gorm.go b/internal/repo/gorm.go index e06184d..7ff4109 100644 --- a/internal/repo/gorm.go +++ b/internal/repo/gorm.go @@ -18,109 +18,86 @@ type InterGormDBCli interface { func NewInterGormDBCli(db *gorm.DB) InterGormDBCli { return &GormDBCli{ - db, + db: db, } } +// Create 插入数据 func (g GormDBCli) Create(table, value interface{}) error { + return g.executeTransaction(func(tx *gorm.DB) error { + return tx.Model(table).Create(value).Error + }, "数据写入失败") +} - tx := g.db.Begin() - err := tx.Model(table).Create(value).Error - if err != nil { - tx.Rollback() - return fmt.Errorf("数据写入失败 -> %s", err) - } - err = tx.Commit().Error - if err != nil { - tx.Rollback() - return fmt.Errorf("事务提交失败 -> %s", err) - } - - return nil - +// Update 更新单条数据 +func (g GormDBCli) Update(value Update) error { + return g.executeTransaction(func(tx *gorm.DB) error { + tx = tx.Model(value.Table) + for column, val := range value.Where { + tx = tx.Where(column, val) + } + return tx.Update(value.Update[0], value.Update[1:]).Error + }, "数据更新失败") } -type Update struct { - Table interface{} - Where []interface{} - Update []string +// Updates 更新多条数据 +func (g GormDBCli) Updates(value Updates) error { + return g.executeTransaction(func(tx *gorm.DB) error { + tx = tx.Model(value.Table) + for column, val := range value.Where { + tx = tx.Where(column, val) + } + return tx.Updates(value.Updates).Error + }, "数据更新失败") } -func (g GormDBCli) Update(value Update) error { +// Delete 删除数据 +func (g GormDBCli) Delete(value Delete) error { + return g.executeTransaction(func(tx *gorm.DB) error { + tx = tx.Model(value.Table) + for column, val := range value.Where { + tx = tx.Where(column, val) + } + return tx.Delete(value.Table).Error + }, "数据删除失败") +} +// executeTransaction 执行事务并处理错误 +func (g GormDBCli) executeTransaction(operation func(tx *gorm.DB) error, errorMessage string) error { tx := g.db.Begin() - tx = tx.Model(value.Table) - for column, val := range value.Where { - tx = tx.Where(column, val) + if tx.Error != nil { + return fmt.Errorf("事务启动失败, err: %s", tx.Error) } - err := tx.Update(value.Update[0], value.Update[1:]).Error - if err != nil { + + if err := operation(tx); err != nil { tx.Rollback() - return fmt.Errorf("数据更新失败 -> %s", err) + return fmt.Errorf("%s -> %s", errorMessage, err) } - err = tx.Commit().Error - if err != nil { + + if err := tx.Commit().Error; err != nil { tx.Rollback() - return fmt.Errorf("事务提交失败 -> %s", err) + return fmt.Errorf("事务提交失败, err: %s", err) } return nil +} +// Update 定义更新单条数据的结构 +type Update struct { + Table interface{} + Where map[string]interface{} + Update []string } +// Updates 定义更新多条数据的结构 type Updates struct { Table interface{} Where map[string]interface{} Updates interface{} } -func (g GormDBCli) Updates(value Updates) error { - - tx := g.db.Begin() - tx = tx.Model(value.Table) - for column, val := range value.Where { - tx = tx.Where(column, val) - } - err := tx.Updates(value.Updates).Error - if err != nil { - tx.Rollback() - return fmt.Errorf("数据更新失败 -> %s", err) - } - - err = tx.Commit().Error - if err != nil { - tx.Rollback() - return fmt.Errorf("事务提交失败 -> %s", err) - } - - return nil - -} - +// Delete 定义删除数据的结构 type Delete struct { Table interface{} Where map[string]interface{} } - -func (g GormDBCli) Delete(value Delete) error { - - tx := g.db.Begin() - - tx = tx.Model(value.Table) - for column, val := range value.Where { - tx = tx.Where(column, val) - } - err := tx.Delete(value.Table).Error - if err != nil { - tx.Rollback() - return fmt.Errorf("数据删除失败 -> %s", err) - } - err = tx.Commit().Error - if err != nil { - tx.Rollback() - return fmt.Errorf("事务提交失败 -> %s", err) - } - - return nil - -}