Skip to content

Commit

Permalink
增加数据模型删除同时删除模型绑定;增加业务单元绑定了其他数据模型,进行提示;增加设备运行状态属性过滤空值;增加设备属性列表接口过滤空值。
Browse files Browse the repository at this point in the history
  • Loading branch information
microrain authored and microrain committed Jan 16, 2023
1 parent a77b15d commit c43938f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 31 deletions.
2 changes: 2 additions & 0 deletions internal/logic/datahub/data_source_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ func (s *sDataSource) GetDbData(ctx context.Context, sourceId uint64) (string, e
return rs[0].Json(), nil
}

// 获取数据源配置的数据库数据
// 数据源配置时,注意控制数据获取的数量,数量过大可能造成内存溢出
func (s *sDataSource) getDbData(ctx context.Context, sourceId uint64, limit int) (rs gdb.Result, ds *model.DataSourceOutput, err error) {
p, _ := s.Detail(ctx, sourceId)
if p == nil || p.DbConfig == nil {
Expand Down
7 changes: 7 additions & 0 deletions internal/logic/datahub/data_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ func (s *sDataTemplate) Del(ctx context.Context, ids []uint64) (err error) {
}
}

// 绑定业务
for _, id := range delIds {
err = service.DataTemplateBusi().Del(ctx, id)
if err != nil {
return err
}
}
return nil
})

Expand Down
40 changes: 36 additions & 4 deletions internal/logic/datahub/data_template_busi.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"github.com/sagoo-cloud/sagooiot/internal/model/do"
"github.com/sagoo-cloud/sagooiot/internal/model/entity"
"github.com/sagoo-cloud/sagooiot/internal/service"
"strconv"

"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
)

type sDataTemplateBusi struct{}
Expand All @@ -31,13 +33,28 @@ func (s *sDataTemplateBusi) Add(ctx context.Context, in *model.DataTemplateBusiA
//获取当前登录用户ID
loginUserId := service.Context().GetUserId(ctx)

// 获取业务单元类型
var dtList []model.SysDictDataOut
dao.SysDictData.Ctx(ctx).Where(dao.SysDictType.Columns().DictType, "busi_types").Scan(&dtList)

c := dao.DataTemplateBusi.Columns()
for _, v := range in.BusiTypes {
id, _ := dao.DataTemplateBusi.Ctx(ctx).
dtb, _ := dao.DataTemplateBusi.Ctx(ctx).
Where(c.BusiTypes, v).
Value("id")
if id.Int() > 0 {
continue
One()
if len(dtb) > 0 {
if dtb["data_template_id"].Uint64() == in.DataTemplateId {
continue
}

var name string
for _, d := range dtList {
if d.DictValue == strconv.Itoa(v) {
name = d.DictLabel
}
}
err = gerror.Newf("%s, 该业务已绑定其他模型", name)
return
}

param := do.DataTemplateBusi{
Expand Down Expand Up @@ -79,3 +96,18 @@ func (s *sDataTemplateBusi) GetTable(ctx context.Context, busiTypes int) (table
table = fmt.Sprintf("data_template_%d", busi.DataTemplateId)
return
}

func (s *sDataTemplateBusi) Del(ctx context.Context, tid uint64) error {
//获取当前登录用户ID
loginUserId := service.Context().GetUserId(ctx)

_, err := dao.DataTemplateBusi.Ctx(ctx).
Data(do.DataTemplateBusi{
DeletedBy: uint(loginUserId),
DeletedAt: gtime.Now(),
}).
Where(dao.DataTemplateBusi.Columns().DataTemplateId, tid).
Unscoped().
Update()
return err
}
32 changes: 21 additions & 11 deletions internal/logic/product/dev_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ func (s *sDevDevice) Deploy(ctx context.Context, id uint) (err error) {
if pd == nil {
return gerror.New("产品不存在")
}
if pd.Status != model.ProductStatusOn {
return gerror.New("产品未发布,请先发布产品")
}

err = dao.DevDevice.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
_, err = dao.DevDevice.Ctx(ctx).
Expand Down Expand Up @@ -543,20 +546,25 @@ func (s *sDevDevice) RunStatus(ctx context.Context, id uint) (out *model.DeviceR
// 获取当天属性值列表
var ls gdb.Result
if _, ok := rs[v.Key]; ok {
sql := "select ? from ? where ts >= '?' order by ts desc"
ls, _ = service.TdEngine().GetAll(ctx, sql, v.Key, p.Key, gtime.Now().Format("Y-m-d"))
sql := "select ? from ? where ts >= '?' and ? is not null order by ts desc"
ls, _ = service.TdEngine().GetAll(ctx, sql, v.Key, p.Key, gtime.Now().Format("Y-m-d"), v.Key)
}

unit := ""
if v.ValueType.Unit != nil {
unit = *v.ValueType.Unit
}

value := rs[v.Key]
if value.IsEmpty() && ls.Len() > 0 {
value = ls.Array(v.Key)[ls.Len()-1]
}
pro := model.DevicePropertiy{
Key: v.Key,
Name: v.Name,
Type: v.ValueType.Type,
Unit: unit,
Value: rs[v.Key],
Value: value,
List: ls.Array(v.Key),
}
properties = append(properties, pro)
Expand All @@ -578,8 +586,8 @@ func (s *sDevDevice) GetProperty(ctx context.Context, in *model.DeviceGetPropert
}

// 属性值获取
sql := "select ? from ? order by ts desc limit 1"
rs, err := service.TdEngine().GetOne(ctx, sql, in.PropertyKey, p.Key)
sql := "select ? from ? where ? is not null order by ts desc limit 1"
rs, err := service.TdEngine().GetOne(ctx, sql, in.PropertyKey, p.Key, in.PropertyKey)
if err != nil {
return
}
Expand All @@ -601,8 +609,8 @@ func (s *sDevDevice) GetProperty(ctx context.Context, in *model.DeviceGetPropert
out.Value = rs[in.PropertyKey]

// 获取当天属性值列表
sql = "select ? from ? where ts >= '?' order by ts desc"
ls, _ := service.TdEngine().GetAll(ctx, sql, in.PropertyKey, p.Key, gtime.Now().Format("Y-m-d"))
sql = "select ? from ? where ts >= '?' and ? is not null order by ts desc"
ls, _ := service.TdEngine().GetAll(ctx, sql, in.PropertyKey, p.Key, gtime.Now().Format("Y-m-d"), in.PropertyKey)
out.List = ls.Array(in.PropertyKey)

return
Expand All @@ -622,20 +630,22 @@ func (s *sDevDevice) GetPropertyList(ctx context.Context, in *model.DeviceGetPro
out = new(model.DeviceGetPropertyListOutput)

// TDengine
sql := "select count(*) as num from ? where ts >= '?'"
rs, err := service.TdEngine().GetOne(ctx, sql, p.Key, gtime.Now().Format("Y-m-d"))
sql := "select count(*) as num from ? where ts >= '?' and ? is not null"
rs, err := service.TdEngine().GetOne(ctx, sql, p.Key, gtime.Now().Format("Y-m-d"), in.PropertyKey)
if err != nil {
return
}
out.Total = rs["num"].Int()
out.CurrentPage = in.PageNum

sql = "select ts, ? from ? where ts >= '?' order by ts desc limit ?, ?"
sql = "select ts, ? from ? where ts >= '?' and ? is not null order by ts desc limit ?, ?"
ls, _ := service.TdEngine().GetAll(
ctx, sql,
ctx,
sql,
in.PropertyKey,
p.Key,
gtime.Now().Format("Y-m-d"),
in.PropertyKey,
(in.PageNum-1)*in.PageSize,
in.PageSize,
)
Expand Down
33 changes: 17 additions & 16 deletions internal/service/datahub.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c43938f

Please sign in to comment.