更改规则链缓存

This commit is contained in:
XM-GO
2023-09-13 17:45:08 +08:00
parent ed8613b6fd
commit 70fb34b90f
11 changed files with 302 additions and 29 deletions

View File

@@ -5,10 +5,7 @@ import (
"github.com/PandaXGO/PandaKit/biz"
"log"
"pandax/apps/device/entity"
ruleEntity "pandax/apps/rule/entity"
ruleService "pandax/apps/rule/services"
"pandax/pkg/global"
"time"
)
type (
@@ -41,23 +38,10 @@ func (m *productModelImpl) Insert(data entity.Product) *entity.Product {
tx.Rollback()
biz.ErrIsNil(err, "添加设备失败,超级表创建失败")
}
setProductRule(&data)
tx.Commit()
return &data
}
// 向redis中添加产品对应的规则链
func setProductRule(data *entity.Product) {
var rule *ruleEntity.RuleChain
if data.RuleChainId == "" {
rule = ruleService.RuleChainModelDao.FindOneByRoot()
} else {
rule = ruleService.RuleChainModelDao.FindOne(data.RuleChainId)
}
data.RuleChainId = rule.Id
biz.ErrIsNil(global.RedisDb.Set(data.Id, rule.RuleDataJson, time.Hour*24*365), "Redis 存储失败")
}
func (m *productModelImpl) FindOne(id string) *entity.ProductRes {
resData := new(entity.ProductRes)
db := global.Db.Table(m.table).Where("id = ?", id)
@@ -141,7 +125,6 @@ func (m *productModelImpl) FindList(data entity.Product) *[]entity.ProductRes {
}
func (m *productModelImpl) Update(data entity.Product) *entity.Product {
setProductRule(&data)
// go的一些默认值 int 0 bool false 保存失败需要先转成map
err := global.Db.Table(m.table).Where("id = ?", data.Id).Updates(data).Error
log.Println("update", err)

View File

@@ -8,7 +8,6 @@ package services
import (
"github.com/PandaXGO/PandaKit/biz"
"log"
"pandax/apps/rule/entity"
"pandax/pkg/events"
"pandax/pkg/global"
@@ -38,7 +37,6 @@ var RuleChainModelDao RuleChainModel = &ruleChainModelImpl{
func (m *ruleChainModelImpl) Insert(data entity.RuleChain) *entity.RuleChain {
err := global.Db.Table(m.table).Create(&data).Error
log.Println(err)
biz.ErrIsNil(err, "添加规则链失败")
return &data
}