diff --git a/apps/rule/api/rulechain.go b/apps/rule/api/rulechain.go index 51e95a2..9e0dbc7 100644 --- a/apps/rule/api/rulechain.go +++ b/apps/rule/api/rulechain.go @@ -114,7 +114,8 @@ func (p *RuleChainApi) UpdateRuleRoot(rc *restfulx.ReqCtx) { var rule entity.RuleChain restfulx.BindJsonAndValid(rc, &rule) // 修改主链为普通链 - p.RuleChainApp.UpdateByRoot() + err := p.RuleChainApp.UpdateByRoot() + biz.ErrIsNil(err, "修改主链错误") // 修改当前链为主链 p.RuleChainApp.Update(rule) } diff --git a/apps/rule/services/rulechain.go b/apps/rule/services/rulechain.go index 2a6404b..ff7106b 100644 --- a/apps/rule/services/rulechain.go +++ b/apps/rule/services/rulechain.go @@ -19,7 +19,7 @@ type ( Insert(data entity.RuleChain) *entity.RuleChain FindOne(id string) *entity.RuleChain FindOneByRoot() *entity.RuleChain - UpdateByRoot() + UpdateByRoot() error FindListPage(page, pageSize int, data entity.RuleChain) (*[]entity.RuleChainBase, int64) FindList(data entity.RuleChain) *[]entity.RuleChain FindListBaseLabel(data entity.RuleChain) *[]entity.RuleChainBaseLabel @@ -37,8 +37,18 @@ var RuleChainModelDao RuleChainModel = &ruleChainModelImpl{ } func (m *ruleChainModelImpl) Insert(data entity.RuleChain) *entity.RuleChain { + tx := global.Db.Begin() + // 如果新增的链为主链,那么将原来的设置为普通连 + if data.Root == "1" { + err := m.UpdateByRoot() + biz.ErrIsNil(err, "修改主链错误") + } err := global.Db.Table(m.table).Create(&data).Error + if err != nil { + tx.Rollback() + } biz.ErrIsNil(err, "添加规则链失败") + tx.Commit() return &data } @@ -60,8 +70,9 @@ func (m *ruleChainModelImpl) FindOneByRoot() *entity.RuleChain { } // UpdateByRoot 修改主链为普通链 -func (m *ruleChainModelImpl) UpdateByRoot() { - biz.ErrIsNil(global.Db.Table(m.table).Where("root = ?", "1").Update("root", "0").Error, "修改规则链失败") +func (m *ruleChainModelImpl) UpdateByRoot() error { + tx := global.Db.Table(m.table).Where("root = ?", "1").Update("root", "0") + return tx.Error } func (m *ruleChainModelImpl) FindListPage(page, pageSize int, data entity.RuleChain) (*[]entity.RuleChainBase, int64) { @@ -114,11 +125,22 @@ func (m *ruleChainModelImpl) FindListBaseLabel(data entity.RuleChain) *[]entity. } func (m *ruleChainModelImpl) Update(data entity.RuleChain) *entity.RuleChain { + tx := global.Db.Begin() + // 如果新增的链为主链,那么将原来的设置为普通连 + if data.Root == "1" { + err := m.UpdateByRoot() + biz.ErrIsNil(err, "修改主链错误") + } + err := global.Db.Table(m.table).Updates(&data).Error + if err != nil { + tx.Rollback() + } + biz.ErrIsNil(err, "修改规则链失败") + tx.Commit() //更改本地规则链缓存 if data.RuleDataJson != "" { go global.EventEmitter.Emit(events.ProductChainRuleEvent, data.Id, data.RuleDataJson) } - biz.ErrIsNil(global.Db.Table(m.table).Updates(&data).Error, "修改规则链失败") return &data }