From d0c8f4ba020a05539569f690b9076f60fbda179f Mon Sep 17 00:00:00 2001 From: songlai <959009@qq.com> Date: Wed, 21 Jun 2023 20:03:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=A1=B9=E7=9B=AE=E3=80=81?= =?UTF-8?q?=E5=88=86=E7=BB=84=E3=80=81=E8=AE=BE=E5=A4=87=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TP.sql | 3 +++ TP_update.sql | 11 +++++++++++ controllers/asset.go | 1 + controllers/business.go | 7 +++++-- controllers/device.go | 2 ++ models/asset.go | 3 ++- models/business.go | 1 + models/device.go | 1 + services/asset_service.go | 8 ++++---- services/business_service.go | 13 ++++++++----- services/device_service.go | 11 ++++++----- validate/asset_validate.go | 1 + validate/business_validate.go | 2 ++ validate/device_validate.go | 5 +++++ 14 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 TP_update.sql diff --git a/TP.sql b/TP.sql index cf1f14413..d7ccda309 100644 --- a/TP.sql +++ b/TP.sql @@ -7,6 +7,7 @@ CREATE TABLE business ( id varchar(36) NOT NULL, "name" varchar(255) NULL, + "sort" int NOT NULL, created_at int8 NULL, app_type varchar(255) NOT NULL DEFAULT ''::character varying, -- 应用类型 app_id varchar(255) NOT NULL DEFAULT ''::character varying, -- app id @@ -340,6 +341,7 @@ CREATE TABLE asset ( customer_id varchar(36) NULL, -- 客户ID "name" varchar(255) NULL, -- 名称 "label" varchar(255) NULL, -- 标签 + "sort" int NOT NULL, search_text varchar(255) NULL, "type" varchar(255) NULL, -- 类型 parent_id varchar(36) NULL, -- 父级ID @@ -377,6 +379,7 @@ CREATE TABLE device ( "type" varchar(255) NULL, -- 插件类型 "name" varchar(255) NULL, -- 插件名 "label" varchar(255) NULL, + "sort" int NOT NULL, search_text varchar(255) NULL, "extension" varchar(50) NULL, -- 插件( 目录名) protocol varchar(50) NULL, diff --git a/TP_update.sql b/TP_update.sql new file mode 100644 index 000000000..8722c342c --- /dev/null +++ b/TP_update.sql @@ -0,0 +1,11 @@ +alter table business + add sort int not null;; +comment on column business.sort is '排序'; + +alter table device + add sort int not null;; +comment on column device.sort is '排序'; + +alter table asset + add sort int not null;; +comment on column asset.sort is '排序'; diff --git a/controllers/asset.go b/controllers/asset.go index 1e2e86c4d..826e07565 100644 --- a/controllers/asset.go +++ b/controllers/asset.go @@ -191,6 +191,7 @@ func (c *AssetController) UpdateOnly() { } f.BusinessID = reqData.BusinessID f.Name = reqData.Name + f.Sort = reqData.Sort f.ParentID = reqData.ParentID f.Tier = reqData.Tier result := psql.Mydb.Save(&f) diff --git a/controllers/business.go b/controllers/business.go index 637779cbd..8bb0a8910 100644 --- a/controllers/business.go +++ b/controllers/business.go @@ -29,6 +29,7 @@ type PaginateBusiness struct { type AddBusiness struct { ID string `json:"id"` Name string `json:"name"` + Sort int64 `json:"sort"` CreatedAt int64 `json:"created_at"` } @@ -92,6 +93,7 @@ func (c *BusinessController) Index() { item := services.PaginateBusiness{ ID: bv.ID, Name: bv.Name, + Sort: bv.Sort, CreatedAt: bv.CreatedAt, IsDevice: is_device, } @@ -136,7 +138,7 @@ func (this *BusinessController) Add() { return } var BusinessService services.BusinessService - f, id := BusinessService.Add(addBusinessValidate.Name, tenantId) + f, id := BusinessService.Add(addBusinessValidate.Name, addBusinessValidate.Sort, tenantId) if f { b, i, err := BusinessService.GetBusinessById(id) if err != nil && i == 0 { @@ -146,6 +148,7 @@ func (this *BusinessController) Add() { u := AddBusiness{ ID: b.ID, Name: b.Name, + Sort: b.Sort, CreatedAt: b.CreatedAt, } response.SuccessWithDetailed(200, "新增成功", u, map[string]string{}, (*context2.Context)(this.Ctx)) @@ -180,7 +183,7 @@ func (this *BusinessController) Edit() { return } var BusinessService services.BusinessService - f := BusinessService.Edit(editBusinessValidate.ID, editBusinessValidate.Name, tenantId) + f := BusinessService.Edit(editBusinessValidate, tenantId) if f { response.SuccessWithMessage(200, "编辑成功", (*context2.Context)(this.Ctx)) return diff --git a/controllers/device.go b/controllers/device.go index 40b5df257..ecb0388cd 100644 --- a/controllers/device.go +++ b/controllers/device.go @@ -134,6 +134,7 @@ type DeviceDash struct { CustomerID string `json:"customer_id" gorm:"size:36"` Type string `json:"type"` // 插件类型 Name string `json:"name"` // 插件名 + Sort int64 `json:"sort"` Label string `json:"label"` SearchText string `json:"search_text"` ChartOption string `json:"chart_option"` @@ -177,6 +178,7 @@ func (c *DeviceController) AddOnly() { AdditionalInfo: reqData.AdditionalInfo, Type: reqData.Type, Name: reqData.Name, + Sort: reqData.Sort, Label: reqData.Label, SearchText: reqData.SearchText, Protocol: reqData.Protocol, diff --git a/models/asset.go b/models/asset.go index c307f5d6f..27a3e45b4 100644 --- a/models/asset.go +++ b/models/asset.go @@ -7,7 +7,8 @@ type Asset struct { Name string `json:"name"` // 名称 Label string `json:"label"` // 标签 SearchText string `json:"search_text"` - Type string `json:"type"` // 类型 + Type string `json:"type"` // 类型 + Sort int64 `json:"sort"` ParentID string `json:"parent_id" gorm:"size:36"` // 父级ID Tier int64 `json:"tier"` // 层级 BusinessID string `json:"business_id" gorm:"size:36"` // 业务ID diff --git a/models/business.go b/models/business.go index 0893d35f7..f71a6e3e9 100644 --- a/models/business.go +++ b/models/business.go @@ -3,6 +3,7 @@ package models type Business struct { ID string `json:"id" gorm:"primaryKey,size:36"` Name string `json:"name" gorm:"size:255"` + Sort int64 `json:"sort"` CreatedAt int64 `json:"created_at"` AppType string `json:"app_type"` // 应用类型 AppID string `json:"app_id"` // application id diff --git a/models/device.go b/models/device.go index a2c937486..701fc92cc 100644 --- a/models/device.go +++ b/models/device.go @@ -8,6 +8,7 @@ type Device struct { CustomerID string `json:"customer_id,omitempty" gorm:"size:36"` Type string `json:"type,omitempty"` // 插件类型 Name string `json:"name,omitempty"` // 插件名 + Sort int64 `json:"sort,omitempty"` Label string `json:"label,omitempty"` SearchText string `json:"search_text,omitempty"` ChartOption string `json:"chart_option,omitempty" gorm:"type:longtext"` // 插件( 目录名) diff --git a/services/asset_service.go b/services/asset_service.go index b4d84fc9f..6ec8fb411 100644 --- a/services/asset_service.go +++ b/services/asset_service.go @@ -964,12 +964,12 @@ func (*AssetService) Simple() (assets []models.Simple, err error) { // 通过业务id获取设备分组 func (*AssetService) PageGetDeviceGroupByBussinessID(business_id, tenant_id string, current int, pageSize int) ([]map[string]interface{}, int64) { - sqlWhere := `select a.id,a."name" , (with RECURSIVE ast as + sqlWhere := `select a.id, a."name", a."sort", (with RECURSIVE ast as ( - (select aa.id,cast('/'as varchar(255))as name,aa.parent_id from asset aa where id=a.id) + (select aa.id,cast('/'as varchar(255))as name, aa.parent_id from asset aa where id=a.id) union - (select tt.id,cast (CONCAT('/',kk.name,tt.name ) as varchar(255))as name ,kk.parent_id from ast tt inner join asset kk on kk.id = tt.parent_id ) - )select name from ast where parent_id='0' limit 1) as parent_group ,a.parent_id from asset a where business_id = ? and tenant_id = ? order by parent_group asc` + (select tt.id,cast (CONCAT('/',kk.name,tt.name ) as varchar(255))as name, kk.parent_id from ast tt inner join asset kk on kk.id = tt.parent_id ) + )select name from ast where parent_id='0' limit 1) as parent_group ,a.parent_id from asset a where business_id = ? and tenant_id = ? order by a.sort desc` var values []interface{} values = append(values, business_id, tenant_id) var count int64 diff --git a/services/business_service.go b/services/business_service.go index 3b53dcd32..b4c9e5edd 100644 --- a/services/business_service.go +++ b/services/business_service.go @@ -4,6 +4,7 @@ import ( "ThingsPanel-Go/initialize/psql" "ThingsPanel-Go/models" uuid "ThingsPanel-Go/utils" + valid "ThingsPanel-Go/validate" "errors" "time" @@ -23,6 +24,7 @@ type BusinessService struct { type PaginateBusiness struct { ID string `json:"id"` Name string `json:"name"` + Sort int64 `json:"sort"` CreatedAt int64 `json:"created_at"` IsDevice int `json:"is_device"` } @@ -48,7 +50,7 @@ func (*BusinessService) Paginate(name string, offset int, pageSize int, tenantId logs.Error(err.Error()) return businesses, count } - err = tx.Order("created_at desc").Limit(pageSize).Offset(offset).Find(&businesses).Error + err = tx.Order("sort desc").Order("created_at desc").Limit(pageSize).Offset(offset).Find(&businesses).Error if err != nil { logs.Error(err.Error()) @@ -72,9 +74,9 @@ func (*BusinessService) GetBusinessById(id string) (*models.Business, int64, err } // Add新增一条business数据 -func (*BusinessService) Add(name, tenantId string) (bool, string) { +func (*BusinessService) Add(name string, sort int64, tenantId string) (bool, string) { bussiness_id := uuid.GetUuid() - business := models.Business{ID: bussiness_id, Name: name, TenantId: tenantId, CreatedAt: time.Now().Unix()} + business := models.Business{ID: bussiness_id, Name: name, Sort: sort, TenantId: tenantId, CreatedAt: time.Now().Unix()} result := psql.Mydb.Create(&business) if result.Error != nil { logs.Error(result.Error.Error()) @@ -85,6 +87,7 @@ func (*BusinessService) Add(name, tenantId string) (bool, string) { asset := models.Asset{ ID: asset_id, Name: name, + Sort: 10, Tier: 1, ParentID: "0", BusinessID: bussiness_id, @@ -95,8 +98,8 @@ func (*BusinessService) Add(name, tenantId string) (bool, string) { } // 根据ID编辑一条business数据 -func (*BusinessService) Edit(id string, name string, tenantId string) bool { - result := psql.Mydb.Model(&models.Business{}).Where("id = ? and tenant_id = ?", id, tenantId).Update("name", name) +func (*BusinessService) Edit(model valid.EditBusiness, tenantId string) bool { + result := psql.Mydb.Model(&models.Business{}).Where("id = ? and tenant_id = ?", model.ID, tenantId).Updates(&model) if result.Error != nil { logs.Error(result.Error.Error()) return false diff --git a/services/device_service.go b/services/device_service.go index a31d70870..5bdc3c349 100644 --- a/services/device_service.go +++ b/services/device_service.go @@ -75,7 +75,7 @@ func (*DeviceService) PageGetDevicesByAssetID(business_id string, asset_id strin union (select tt.id,cast (kk.name||'/'||tt.name as varchar(255))as name ,kk.parent_id from ast tt inner join asset kk on kk.id = tt.parent_id ) )select name from ast where parent_id='0' limit 1) - as asset_name,b.id as business_id ,b."name" as business_name,d.d_id,d.location,a.id as asset_id ,d.id as device ,d."name" as device_name, + as asset_name,b.id as business_id ,b."name" as business_name,d.d_id,d.location,a.id as asset_id ,d.id as device , d."name" as device_name, d."sort" as device_sort, d."token" as device_token,d."type" as device_type,d.protocol as protocol ,(select ts from ts_kv_latest tkl where tkl.entity_id = d.id order by ts desc limit 1) as latest_ts from device d left join asset a on d.asset_id = a.id left join business b on b.id = a.business_id where 1=1 ` sqlWhereCount := `select count(1) from device d left join asset a on d.asset_id = a.id left join business b on b.id = a.business_id where 1=1` @@ -134,7 +134,7 @@ func (*DeviceService) PageGetDevicesByAssetIDTree(req valid.DevicePageListValida union (select tt.id,cast (kk.name||'/'||tt.name as varchar(255))as name ,kk.parent_id from ast tt inner join asset kk on kk.id = tt.parent_id ) )select name from ast where parent_id='0' limit 1) - as asset_name,b.id as business_id ,b."name" as business_name,d.d_id,d.location,a.id as asset_id ,d.id as device ,d."name" as device_name,d.device_type as device_type,d.parent_id as parent_id,d.protocol_config as protocol_config, + as asset_name,b.id as business_id ,b."name" as business_name,d.d_id,d.location,a.id as asset_id ,d.id as device ,d."name" as device_name, d."sort" as device_sort,d.device_type as device_type,d.parent_id as parent_id,d.protocol_config as protocol_config, d.additional_info as additional_info,d.sub_device_addr as sub_device_addr,d."token" as device_token,d."type" as "type",d.protocol as protocol ,dm.model_name as plugin_name,(select ts from ts_kv_latest tkl where tkl.entity_id = d.id order by ts desc limit 1) as latest_ts from device d left join asset a on d.asset_id = a.id left join business b on b.id = a.business_id LEFT JOIN device_model dm ON d.type = dm.id where 1=1 and d.device_type != '3'` sqlWhereCount := `select count(1) from device d left join asset a on d.asset_id = a.id left join business b on b.id = a.business_id where d.device_type != '3'` @@ -178,7 +178,7 @@ func (*DeviceService) PageGetDevicesByAssetIDTree(req valid.DevicePageListValida } var offset int = (req.CurrentPage - 1) * req.PerPage var limit int = req.PerPage - sqlWhere += "order by d.created_at desc offset ? limit ?" + sqlWhere += "order by d.sort desc, d.created_at desc offset ? limit ?" values = append(values, offset, limit) var deviceList []map[string]interface{} dataResult := psql.Mydb.Raw(sqlWhere, values...).Scan(&deviceList) @@ -224,9 +224,9 @@ func (*DeviceService) PageGetDevicesByAssetIDTree(req valid.DevicePageListValida union (select tt.id,cast (kk.name||'/'||tt.name as varchar(255))as name ,kk.parent_id from ast tt inner join asset kk on kk.id = tt.parent_id ) )select name from ast where parent_id='0' limit 1) - as asset_name,b.id as business_id ,b."name" as business_name,d.d_id,d.location,a.id as asset_id ,d.id as device ,d."name" as device_name,d.device_type as device_type,d.parent_id as parent_id,d.protocol_config as protocol_config,d.sub_device_addr as sub_device_addr, + as asset_name,b.id as business_id ,b."name" as business_name,d.d_id,d.location,a.id as asset_id ,d.id as device ,d."name" as device_name ,d."sort" as device_sort,d.device_type as device_type,d.parent_id as parent_id,d.protocol_config as protocol_config,d.sub_device_addr as sub_device_addr, d.additional_info as additional_info,d."token" as device_token,d."type" as "type",d.protocol as protocol ,dm.model_name as plugin_name,(select ts from ts_kv_latest tkl where tkl.entity_id = d.id order by ts desc limit 1) as latest_ts - from device d left join asset a on d.asset_id = a.id left join business b on b.id = a.business_id LEFT JOIN device_model dm ON d.type = dm.id where 1=1 and d.device_type = '3' and d.parent_id = '` + device["device"].(string) + "' order by d.created_at desc" + from device d left join asset a on d.asset_id = a.id left join business b on b.id = a.business_id LEFT JOIN device_model dm ON d.type = dm.id where 1=1 and d.device_type = '3' and d.parent_id = '` + device["device"].(string) + "' order by d.sort desc, d.created_at desc" result := psql.Mydb.Raw(sql).Scan(&subDeviceList) if result.Error != nil { //errors.Is(result.Error, gorm.ErrRecordNotFound) @@ -625,6 +625,7 @@ func (*DeviceService) Edit(deviceModel valid.EditDevice) error { AssetID: deviceModel.AssetID, Type: deviceModel.Type, Name: deviceModel.Name, + Sort: deviceModel.Sort, DeviceType: deviceModel.DeviceType, ParentId: deviceModel.ParentId, ProtocolConfig: deviceModel.ProtocolConfig, diff --git a/validate/asset_validate.go b/validate/asset_validate.go index aa92229fa..db2cbf401 100644 --- a/validate/asset_validate.go +++ b/validate/asset_validate.go @@ -6,6 +6,7 @@ type Asset struct { CustomerID string `json:"customer_id" gorm:"size:36"` // 客户ID Name string `json:"name"` // 名称 Label string `json:"label"` // 标签 + Sort int64 `json:"sort"` SearchText string `json:"search_text"` Type string `json:"type"` // 类型 ParentID string `json:"parent_id" gorm:"size:36"` // 父级ID diff --git a/validate/business_validate.go b/validate/business_validate.go index 7bcd207b3..f06264829 100644 --- a/validate/business_validate.go +++ b/validate/business_validate.go @@ -10,12 +10,14 @@ type PaginateBusiness struct { // AddBusiness 校验 type AddBusiness struct { Name string `json:"name" alias:"名称" valid:"Required; MaxSize(255)"` + Sort int64 `json:"sort" alias:"排序" valid:"Required;"` } // EditBusiness 校验 type EditBusiness struct { ID string `json:"id" alias:"ID" valid:"Required; MaxSize(36)"` Name string `json:"name" alias:"名称" valid:"Required; MaxSize(255)"` + Sort int64 `json:"sort" alias:"排序" valid:"Required;"` } // DeleteBusiness 校验 diff --git a/validate/device_validate.go b/validate/device_validate.go index 2bcf5bddd..a7560cc14 100644 --- a/validate/device_validate.go +++ b/validate/device_validate.go @@ -15,6 +15,7 @@ type EditDevice struct { Type string `json:"type"` // 插件类型 Name string `json:"name"` // 插件名 Label string `json:"label"` + Sort int64 `json:"sort"` SearchText string `json:"search_text"` ChartOption string `json:"chart_option"` Protocol string `json:"protocol" gorm:"size:50"` @@ -36,6 +37,7 @@ type EditDevice struct { type AddDevice struct { Token string `json:"token"` Name string `json:"name"` + Sort int64 `json:"sort"` AssetId string `json:"asset_id"` DeviceType string `json:"device_type" gorm:"size:2"` ParentId string `json:"parent_id" gorm:"size:36"` @@ -66,6 +68,7 @@ type Device struct { CustomerID string `json:"customer_id" gorm:"size:36"` Type string `json:"type"` // 插件类型 Name string `json:"name"` // 插件名 + Sort int64 `json:"sort"` Label string `json:"label"` SearchText string `json:"search_text"` ChartOption string `json:"chart_option"` @@ -91,6 +94,7 @@ type UpdateDevice struct { AdditionalInfo string `json:"additional_info,omitempty" gorm:"type:longtext"` // 存储基本配置 Type string `json:"type,omitempty"` // 插件类型 Name string `json:"name,omitempty"` // 插件名 + Sort int64 `json:"sort"` Label string `json:"label,omitempty"` SearchText string `json:"search_text,omitempty"` ChartOption string `json:"chart_option"` @@ -124,6 +128,7 @@ type DevicePageListValidate struct { DeviceType string `json:"device_type" alias:"设备id" valid:"MaxSize(36)"` Token string `json:"token" alias:"设备id" valid:"MaxSize(36)"` Name string `json:"name" alias:"设备名称" valid:"MaxSize(99)"` + Sort int64 `json:"sort" alias:"排序"` ParentId string `json:"parent_id" gorm:"size:36"` NotGateway int `json:"not_gateway" valid:"Max(2)"` }