Skip to content

Commit 0569e6a

Browse files
committed
修改一些问题
1 parent e19cbf2 commit 0569e6a

12 files changed

Lines changed: 218 additions & 13 deletions

File tree

iot/device-mqtt.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,11 @@ func mqttSubscribeDevice() {
338338
})
339339

340340
mqtt.SubscribeStruct[SyncResponse]("device/+/sync/response", func(topic string, resp *SyncResponse) {
341-
ss := strings.Split(topic, "/")
342-
id := ss[1]
341+
id := resp.DeviceId
342+
if resp.DeviceId == "" {
343+
ss := strings.Split(topic, "/")
344+
id = ss[1]
345+
}
343346
dev := devices.Load(id)
344347
if dev == nil {
345348
return
@@ -348,8 +351,11 @@ func mqttSubscribeDevice() {
348351
})
349352

350353
mqtt.SubscribeStruct[ReadResponse]("device/+/read/response", func(topic string, resp *ReadResponse) {
351-
ss := strings.Split(topic, "/")
352-
id := ss[1]
354+
id := resp.DeviceId
355+
if resp.DeviceId == "" {
356+
ss := strings.Split(topic, "/")
357+
id = ss[1]
358+
}
353359
dev := devices.Load(id)
354360
if dev == nil {
355361
return
@@ -358,8 +364,11 @@ func mqttSubscribeDevice() {
358364
})
359365

360366
mqtt.SubscribeStruct[WriteResponse]("device/+/write/response", func(topic string, resp *WriteResponse) {
361-
ss := strings.Split(topic, "/")
362-
id := ss[1]
367+
id := resp.DeviceId
368+
if resp.DeviceId == "" {
369+
ss := strings.Split(topic, "/")
370+
id = ss[1]
371+
}
363372
dev := devices.Load(id)
364373
if dev == nil {
365374
return
@@ -368,8 +377,11 @@ func mqttSubscribeDevice() {
368377
})
369378

370379
mqtt.SubscribeStruct[ActionResponse]("device/+/action/response", func(topic string, resp *ActionResponse) {
371-
ss := strings.Split(topic, "/")
372-
id := ss[1]
380+
id := resp.DeviceId
381+
if resp.DeviceId == "" {
382+
ss := strings.Split(topic, "/")
383+
id = ss[1]
384+
}
373385
dev := devices.Load(id)
374386
if dev == nil {
375387
return

pages/device-detail.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@
8585
"title": "告警",
8686
"page":"alarm",
8787
"params_func": "return {device_id: params.id}"
88+
},
89+
{
90+
"title": "数据修改(旧)",
91+
"page":"device-values-setting",
92+
"params_func": "return {id: params.id}"
8893
}
8994
]
9095
}

pages/device-settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"mount": "this.load_device()",
55
"methods": {
66
"load_device": "this.request.get('table/device/detail/'+this.params.id).subscribe(res=>{if(res.error)return; this.load_model(res.data.product_id)})",
7-
"load_model": ["pid","this.request.get('product/'+pid+'/setting/model').subscribe(res=>{if(res.error)return; res.data.content && this.render_settings(res.data.content); })"],
7+
"load_model": ["pid","this.request.get('product/'+pid+'/setting/setting').subscribe(res=>{if(res.error)return; res.data.content && this.render_settings(res.data.content); })"],
88
"render_settings": ["settings", "settings && settings.map(p=>{ this.content.children.push({span: 24, params:{id:this.params.id, name:p.name}, content:Object.assign({title:p.label, fields: p.fields}, this.content.setting_content) }) })"]
99
},
1010
"children": [],

pages/gateway-detail.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@
8686
"page":"device-settings",
8787
"params_func": "return {id: params.id}"
8888
},
89+
{
90+
"title": "设备操作",
91+
"page":"device-actions",
92+
"params_func": "return {id: params.id}"
93+
},
8994
{
9095
"title": "子设备",
9196
"page":"gateway-device",
@@ -115,6 +120,11 @@
115120
"title": "数据绑定",
116121
"page":"binding",
117122
"params_func": "return {gateway_id: params.id}"
123+
},
124+
{
125+
"title": "数据修改(旧)",
126+
"page":"device-values-setting",
127+
"params_func": "return {id: params.id}"
118128
}
119129
]
120130
}

pages/product-edit.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@
4040
"submit_success": "this.navigate('/page/product-detail?id='+this.params.id)",
4141
"mount": "this.load_protocols()",
4242
"methods": {
43-
"load_protocols": "this.request.get('protocol/list').subscribe(res=>{this.content.fields[5].options=res.data.map(d=>{return {value:d.name,label:d.description}}); })"
43+
"load_protocols": "this.request.get('protocol/list').subscribe(res=>{this.content.fields[4].options=res.data.map(d=>{return {value:d.name,label:d.description}}); })"
4444
}
4545
}

pkg/table/delete.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"strings"
55

66
"github.com/gin-gonic/gin"
7+
"github.com/spf13/viper"
78
)
89

910
func ApiDelete(ctx *gin.Context) {
@@ -14,6 +15,24 @@ func ApiDelete(ctx *gin.Context) {
1415
}
1516

1617
id := strings.TrimLeft(ctx.Param("id"), "/")
18+
19+
//多租户过滤
20+
if viper.GetBool("tenant") {
21+
tid := ctx.GetString("tenant")
22+
if tid != "" {
23+
column := table.Column("tenant_id")
24+
if column != nil {
25+
cnt, err := table.DeleteByIdEx(id, map[string]any{"tenant_id": tid})
26+
if err != nil {
27+
Error(ctx, err)
28+
return
29+
}
30+
OK(ctx, cnt)
31+
return
32+
}
33+
}
34+
}
35+
1736
cnt, err := table.DeleteById(id)
1837
if err != nil {
1938
Error(ctx, err)

pkg/table/export.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/gin-gonic/gin"
10+
"github.com/spf13/viper"
1011
)
1112

1213
func ApiExport(ctx *gin.Context) {
@@ -23,6 +24,23 @@ func ApiExport(ctx *gin.Context) {
2324
return
2425
}
2526

27+
//多租户过滤
28+
if viper.GetBool("tenant") {
29+
tid := ctx.GetString("tenant")
30+
if tid != "" {
31+
column := table.Column("tenant_id")
32+
if column != nil {
33+
if body.Filter == nil {
34+
body.Filter = make(map[string]any)
35+
}
36+
//只有未传值tenant_id时,才会赋值用户所在的tenant_id
37+
if _, ok := body.Filter["tenant_id"]; !ok {
38+
body.Filter["tenant_id"] = tid
39+
}
40+
}
41+
}
42+
}
43+
2644
//查询
2745
results, err := table.Find(&body)
2846
if err != nil {

pkg/table/import.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package table
22

33
import (
44
"encoding/json"
5+
"errors"
56
"io"
67
"mime/multipart"
78

@@ -77,16 +78,25 @@ func ApiImport(ctx *gin.Context) {
7778
}
7879
}
7980

81+
var errs []error
82+
8083
//依次写入
8184
var ids []any
8285
for _, doc := range docs {
8386
id, err := table.Insert(doc)
8487
if err != nil {
85-
Error(ctx, err)
86-
return
88+
errs = append(errs, err)
89+
//不阻碍其他导入
90+
//Error(ctx, err)
91+
//return
8792
}
8893
ids = append(ids, id)
8994
}
9095

96+
if len(errs) > 0 {
97+
Error(ctx, errors.Join(errs...))
98+
return
99+
}
100+
91101
OK(ctx, ids)
92102
}

pkg/table/table.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,63 @@ func (t *Table) UpdateById(id any, values map[string]any) (rows int64, err error
452452
return res.RowsAffected()
453453
}
454454

455+
func (t *Table) UpdateByIdEx(id any, filter map[string]any, values map[string]any) (rows int64, err error) {
456+
for _, column := range t.Columns {
457+
if column.Updated {
458+
values[column.Name] = time.Now().Format(time.DateTime) //直接格式化
459+
}
460+
461+
if column.Json {
462+
if val, ok := values[column.Name]; ok {
463+
values[column.Name], _ = json.Marshal(val)
464+
}
465+
}
466+
}
467+
468+
if t.BeforeUpdate != nil {
469+
err = t.BeforeUpdate(id, values)
470+
if err != nil {
471+
return
472+
}
473+
}
474+
475+
var updates []builder.Cond
476+
for k, v := range values {
477+
updates = append(updates, builder.Eq{db.Engine().Quote(k): v})
478+
}
479+
bdr := builder.Dialect(db.Engine().DriverName()).Update(updates...).From(db.Engine().Quote(t.Name))
480+
481+
//bdr.Where(builder.Eq{"id": id})
482+
cs, err := t.condId(id)
483+
if err != nil {
484+
return 0, err
485+
}
486+
for _, c := range cs {
487+
bdr.Where(c)
488+
}
489+
490+
if len(filter) > 0 {
491+
cs, err = t.condWhere(filter, false)
492+
if err != nil {
493+
return 0, err
494+
}
495+
for _, c := range cs {
496+
bdr.Where(c)
497+
}
498+
}
499+
500+
res, err := db.Engine().ID(id).Exec(bdr)
501+
if err != nil {
502+
return 0, err
503+
}
504+
505+
if t.AfterUpdate != nil {
506+
err = t.AfterUpdate(id, values, values)
507+
}
508+
509+
return res.RowsAffected()
510+
}
511+
455512
func (t *Table) Delete(filter map[string]any) (rows int64, err error) {
456513
cs, err := t.condWhere(filter, false)
457514
if err != nil {
@@ -501,6 +558,49 @@ func (t *Table) DeleteById(id any) (rows int64, err error) {
501558
return res.RowsAffected()
502559
}
503560

561+
func (t *Table) DeleteByIdEx(id any, filter map[string]any) (rows int64, err error) {
562+
bdr := builder.Dialect(db.Engine().DriverName()).Delete().From(db.Engine().Quote(t.Name))
563+
564+
if t.BeforeDelete != nil {
565+
err = t.BeforeDelete(id)
566+
if err != nil {
567+
return
568+
}
569+
}
570+
571+
cs, err := t.condId(id)
572+
if err != nil {
573+
return 0, err
574+
}
575+
for _, c := range cs {
576+
bdr.Where(c)
577+
}
578+
579+
if len(filter) > 0 {
580+
cs, err = t.condWhere(filter, false)
581+
if err != nil {
582+
return 0, err
583+
}
584+
for _, c := range cs {
585+
bdr.Where(c)
586+
}
587+
}
588+
589+
res, err := db.Engine().Exec(bdr)
590+
if err != nil {
591+
return 0, err
592+
}
593+
594+
if t.AfterDelete != nil {
595+
err = t.AfterDelete(id, nil)
596+
if err != nil {
597+
return 0, err
598+
}
599+
}
600+
601+
return res.RowsAffected()
602+
}
603+
504604
func (t *Table) Find(body *ParamSearch) (rows []map[string]any, err error) {
505605
columns := body.Fields
506606
if len(columns) == 0 {

pkg/table/update.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"strings"
55

66
"github.com/gin-gonic/gin"
7+
"github.com/spf13/viper"
78
)
89

910
func ApiUpdate(ctx *gin.Context) {
@@ -21,6 +22,24 @@ func ApiUpdate(ctx *gin.Context) {
2122
}
2223

2324
id := strings.TrimLeft(ctx.Param("id"), "/")
25+
26+
//多租户过滤
27+
if viper.GetBool("tenant") {
28+
tid := ctx.GetString("tenant")
29+
if tid != "" {
30+
column := table.Column("tenant_id")
31+
if column != nil {
32+
cnt, err := table.UpdateByIdEx(id, map[string]any{"tenant_id": tid}, update)
33+
if err != nil {
34+
Error(ctx, err)
35+
return
36+
}
37+
OK(ctx, cnt)
38+
return
39+
}
40+
}
41+
}
42+
2443
cnt, err := table.UpdateById(id, update)
2544
if err != nil {
2645
Error(ctx, err)

0 commit comments

Comments
 (0)