37 Star 391 Fork 118

联犀/物联网iot模块

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
sceneIfTrigger.go 6.20 KB
一键复制 编辑 原始数据 按行查看 历史
杨磊 提交于 2024-12-06 16:51 . fix: 场景bug
package relationDB
import (
"context"
"gitee.com/unitedrhino/share/devices"
"gitee.com/unitedrhino/share/stores"
"gitee.com/unitedrhino/things/service/udsvr/internal/domain/scene"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
/*
这个是参考样例
使用教程:
1. 将example全局替换为模型的表名
2. 完善todo
*/
type SceneIfTriggerRepo struct {
db *gorm.DB
}
func NewSceneIfTriggerRepo(in any) *SceneIfTriggerRepo {
return &SceneIfTriggerRepo{db: stores.GetCommonConn(in)}
}
type SceneIfTriggerFilter struct {
ID int64
SceneID int64
Status scene.Status
Statuses []scene.Status
ExecAt *stores.Cmp
LastRunTime *stores.Cmp
ExecRepeat *stores.Cmp
RepeatType *stores.Cmp
ExecLoopStart *stores.Cmp
ExecLoopEnd *stores.Cmp
ExecType *stores.Cmp
ExecRepeatStartDate *stores.Cmp
ExecRepeatEndDate *stores.Cmp
Type string
ProjectID *stores.Cmp
AreaID *stores.Cmp
Device *devices.Core
DataID string
TriggerDeviceType scene.TriggerDeviceType
FirstTriggerTime *stores.Cmp
StateKeepType scene.StateKeepType
StateKeepValue *stores.Cmp
}
func (p SceneIfTriggerRepo) fmtFilter(ctx context.Context, f SceneIfTriggerFilter) *gorm.DB {
db := p.db.WithContext(ctx)
db = f.ProjectID.Where(db, "project_id")
db = f.AreaID.Where(db, "area_id")
db = f.ExecAt.Where(db, "timer_exec_at")
db = f.LastRunTime.Where(db, "last_run_time")
db = f.ExecRepeat.Where(db, "timer_exec_repeat")
db = f.ExecLoopStart.Where(db, "timer_exec_loop_start")
db = f.ExecLoopEnd.Where(db, "timer_exec_loop_end")
db = f.ExecRepeatStartDate.Where(db, "timer_exec_repeat_start_date")
db = f.ExecRepeatEndDate.Where(db, "timer_exec_repeat_end_date")
db = f.FirstTriggerTime.Where(db, "device_first_trigger_time")
db = f.StateKeepValue.Where(db, "device_state_keep_value")
db = f.RepeatType.Where(db, "timer_repeat_type")
if f.Status != 0 {
db = db.Where("status = ?", f.Status)
}
if len(f.Statuses) != 0 {
db = db.Where("status in ?", f.Statuses)
}
if f.ID != 0 {
db = db.Where("id = ?", f.ID)
}
db = f.ExecType.Where(db, "timer_exec_type")
if f.Type != "" {
db = db.Where("type = ?", f.Type)
}
if f.StateKeepType != "" {
db = db.Where("device_state_keep_type = ?", f.StateKeepType)
}
if f.SceneID != 0 {
db = db.Where("scene_id = ?", f.SceneID)
}
if f.Device != nil {
if f.DataID != "" {
db = db.Where("device_select_type=? and device_product_id=? and device_device_name=? and device_data_id=?",
scene.SelectDeviceFixed, f.Device.ProductID, f.Device.DeviceName, f.DataID).
Or("device_select_type=? and device_product_id=? and device_data_id=?", scene.SelectorDeviceAll,
f.Device.ProductID, f.DataID)
} else if f.TriggerDeviceType != "" {
db = db.Where("device_select_type=? and device_type=? and device_product_id=? and device_device_name=? ",
scene.SelectDeviceFixed, f.TriggerDeviceType, f.Device.ProductID, f.Device.DeviceName).
Or("device_select_type=? and device_type=? and device_product_id=? ", scene.SelectorDeviceAll, f.TriggerDeviceType,
f.Device.ProductID)
}
}
return db
}
func (p SceneIfTriggerRepo) Insert(ctx context.Context, data *UdSceneIfTrigger) error {
result := p.db.WithContext(ctx).Create(data)
return stores.ErrFmt(result.Error)
}
func (p SceneIfTriggerRepo) FindOneByFilter(ctx context.Context, f SceneIfTriggerFilter) (*UdSceneIfTrigger, error) {
var result UdSceneIfTrigger
db := p.fmtFilter(ctx, f)
err := db.Preload("SceneInfo").Preload("SceneInfo.Actions").First(&result).Error
if err != nil {
return nil, stores.ErrFmt(err)
}
return &result, nil
}
func (p SceneIfTriggerRepo) FindByFilter(ctx context.Context, f SceneIfTriggerFilter, page *stores.PageInfo) ([]*UdSceneIfTrigger, error) {
var results []*UdSceneIfTrigger
db := p.fmtFilter(ctx, f).Model(&UdSceneIfTrigger{})
db = page.ToGorm(db)
err := db.Preload("SceneInfo").Preload("SceneInfo.Actions").Find(&results).Error
if err != nil {
return nil, stores.ErrFmt(err)
}
return results, nil
}
func (p SceneIfTriggerRepo) FindByFilters(ctx context.Context, f []SceneIfTriggerFilter, page *stores.PageInfo) ([]*UdSceneIfTrigger, error) {
var results []*UdSceneIfTrigger
if len(f) == 0 {
return results, nil
}
db := p.fmtFilter(ctx, f[0]).Model(&UdSceneIfTrigger{})
for _, ff := range f[1:] {
db = db.Or(p.fmtFilter(ctx, ff))
}
db = page.ToGorm(db)
err := db.Preload("SceneInfo").Preload("SceneInfo.Actions").Find(&results).Error
if err != nil {
return nil, stores.ErrFmt(err)
}
return results, nil
}
func (p SceneIfTriggerRepo) CountByFilter(ctx context.Context, f SceneIfTriggerFilter) (size int64, err error) {
db := p.fmtFilter(ctx, f).Model(&UdSceneIfTrigger{})
err = db.Count(&size).Error
return size, stores.ErrFmt(err)
}
func (p SceneIfTriggerRepo) Update(ctx context.Context, data *UdSceneIfTrigger) error {
err := p.db.WithContext(ctx).Where("id = ?", data.ID).Save(data).Error
return stores.ErrFmt(err)
}
func (d SceneIfTriggerRepo) UpdateWithField(ctx context.Context, f SceneIfTriggerFilter, updates map[string]any) error {
db := d.fmtFilter(ctx, f)
err := db.Model(&UdSceneIfTrigger{}).Updates(updates).Error
return stores.ErrFmt(err)
}
func (p SceneIfTriggerRepo) DeleteByFilter(ctx context.Context, f SceneIfTriggerFilter) error {
db := p.fmtFilter(ctx, f)
err := db.Delete(&UdSceneIfTrigger{}).Error
return stores.ErrFmt(err)
}
func (p SceneIfTriggerRepo) Delete(ctx context.Context, id int64) error {
err := p.db.WithContext(ctx).Where("id = ?", id).Delete(&UdSceneIfTrigger{}).Error
return stores.ErrFmt(err)
}
func (p SceneIfTriggerRepo) FindOne(ctx context.Context, id int64) (*UdSceneIfTrigger, error) {
var result UdSceneIfTrigger
err := p.db.WithContext(ctx).Preload("SceneInfo").Preload("SceneInfo.Actions").Where("id = ?", id).First(&result).Error
if err != nil {
return nil, stores.ErrFmt(err)
}
return &result, nil
}
// 批量插入 LightStrategyDevice 记录
func (p SceneIfTriggerRepo) MultiInsert(ctx context.Context, data []*UdSceneIfTrigger) error {
err := p.db.WithContext(ctx).Clauses(clause.OnConflict{UpdateAll: true}).Model(&UdSceneIfTrigger{}).Create(data).Error
return stores.ErrFmt(err)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/unitedrhino/things.git
git@gitee.com:unitedrhino/things.git
unitedrhino
things
物联网iot模块
v1.1.1

搜索帮助