From 0635e37d73ad3f8ef9b1aa7a6024253978205c88 Mon Sep 17 00:00:00 2001 From: PandaX <18610165312@163.com> Date: Tue, 21 Nov 2023 10:27:07 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=E8=A7=84=E5=88=99=E9=93=BE=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=88=96=E4=BF=AE=E6=94=B9=E5=AD=98=E5=9C=A8=E5=A4=9A?= =?UTF-8?q?=E6=9D=A1=E4=B8=BB=E9=93=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/rule/api/rulechain.go | 3 ++- apps/rule/services/rulechain.go | 30 ++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) 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 }