Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions TP.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions TP_update.sql
Original file line number Diff line number Diff line change
@@ -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 '排序';
1 change: 1 addition & 0 deletions controllers/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions controllers/business.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions controllers/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion models/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions models/business.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions models/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"` // 插件( 目录名)
Expand Down
8 changes: 4 additions & 4 deletions services/asset_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions services/business_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"ThingsPanel-Go/initialize/psql"
"ThingsPanel-Go/models"
uuid "ThingsPanel-Go/utils"
valid "ThingsPanel-Go/validate"
"errors"
"time"

Expand All @@ -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"`
}
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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,
Expand All @@ -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
Expand Down
11 changes: 6 additions & 5 deletions services/device_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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'`
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions validate/asset_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions validate/business_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 校验
Expand Down
5 changes: 5 additions & 0 deletions validate/device_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
Expand Down Expand Up @@ -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"`
Expand All @@ -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"`
Expand Down Expand Up @@ -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)"`
}
Expand Down