zhangzengfei
2023-11-28 3a706d3378aa3626501370352963883fd2783558
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package models
 
import "strconv"
 
type Device struct {
    Id                 string             `gorm:"primary_key;column:id" json:"id"`
    DevId             string             `gorm:"column:devId;unique;not null;" json:"devId"`         //设备id
    DevType         string             `gorm:"column:devType" json:"devType"`                         //设备类型,如:存储、分析(盒子或amd服务器)、分析存储一体
    DevMode         string             `gorm:"column:devMode" json:"devMode"`                         //设备型号,如:Bsk-JS1000X
    DevName         string             `gorm:"column:devName" json:"devName"`                        //设备名称
    MachineCode     string             `gorm:"column:machineCode" json:"machineCode"`                //机器码
    ActivateCode     string             `gorm:"column:activateCode" json:"activateCode"`            //激活码
    ProductId         string             `gorm:"column:productId" json:"productId"`                    //产品id
    UserId             string             `gorm:"column:userId" json:"userId"`                        //用户id
    Address         string             `gorm:"column:address" json:"address"`                        //地址
    DevIp              string             `gorm:"column:devIp" json:"devIp"`                            //ip
    DevCpu            string             `gorm:"column:devCpu" json:"devCpu"`                         //cpu
    DevGpu            string             `gorm:"column:devGpu" json:"devGpu"`                        //gpu
    Mem             int             `gorm:"column:mem" json:"mem"`                              //内存
    Disk             string             `gorm:"column:disk" json:"disk"`                              //硬盘
    ChannelCount     int             `gorm:"column:channelCount;default:16" json:"channelCount"` //算力数量
    MasterVersion     string             `gorm:"column:masterVersion" json:"masterVersion"`           //主控版本
    WebVersion         string             `gorm:"column:webVersion" json:"webVersion"`                   //web版本
    ServerPort         string             `gorm:"column:serverPort" json:"serverPort"`                   //端口:默认7003
    SubMask         string             `gorm:"column:subMask" json:"subMask"`                           //子网掩码
    Gateway         string             `gorm:"column:gateway" json:"gateway"`                            //网关
    Dns             string             `gorm:"column:dns" json:"dns"`                                    //dns
    Runtime         string             `gorm:"column:runtime" json:"runtime"`                         //运行时长
 
    InstallTime     string             `gorm:"column:installTime" json:"installTime"`               //安装时间
    FirstUseTime     string             `gorm:"column:firstUseTime" json:"firstUseTime"`             //首次使用时间
 
    //集群相关信息
    ClusterId         string             `gorm:"column:clusterId" json:"clusterId"`                   //集群id
    ClusterName     string             `gorm:"column:clusterName" json:"clusterName"`                //集群名称
    Status             int             `gorm:"column:status;default:0;" json:"status"`             //状态 -1:离线, 1:在线
 
    CreateTime         string             `gorm:"column:createTime" json:"createTime"`                //记录时间
    UpdateTime         string             `gorm:"column:updateTime" json:"updateTime"`                //更新时间
}
 
const (
    DevStatus_OnLine = 1   //在线
    DevStatus_OffLine = -1   //离线
)
 
func (Device) TableName() string {
    return "t_device"
}
 
func (d *Device) Insert() bool {
    result := db.Table(d.TableName()).Create(&d)
    if result.Error !=nil {
        return false
    }
    return result.RowsAffected>0
}
 
func (d *Device) Update() bool {
    result := db.Table(d.TableName()).Update(&d)
    if result.Error !=nil {
        return false
    }
    return result.RowsAffected>0
}
 
func (d *Device) SelectById(id string) (int64, error){
    result := db.Table(d.TableName()).Where("id=?",id).First(&d)
    if result.Error != nil || result.RowsAffected == 0 {
        return 0, err
    }
    return result.RowsAffected, nil
}
 
func (d *Device) FindByDevId(devId string) (int64,error) {
    result := db.Table(d.TableName()).Where("devId=?", devId).First(&d)
    if result.Error != nil || result.RowsAffected == 0 {
        return 0, result.Error
    }
    return result.RowsAffected, nil
}
 
func (d *Device) FindAll() (list []Device){
    if err:=db.Table(d.TableName()).Scan(&list).Error;err !=nil{
        return nil
    }
    return list
}
 
type DevClusterGroup struct {
    ClusterId         string         `json:"clusterId"`   //集群id
    ClusterName     string         `json:"clusterName"` //集群名称
    Count             int         `json:"count"`       //数量
}
func (d *Device) GroupByCluster() (list []DevClusterGroup) {
    err := db.Raw("select clusterId,clusterName,count(1) as count from "+d.TableName()+" where clusterId!='' group by clusterId,clusterName order by clusterId asc").Scan(&list).Error
    if err != nil {
        return nil
    }
    return list
}
 
func (d *Device) PageAllDev(page int, size int, status int, inputTxt string) (list []Device, err error) {
    sql := "select * from "+d.TableName()+" where 1=1"
    from := (page-1)*size
    if inputTxt != "" {
        sql += " and devName like '%"+inputTxt+"%'"
    }
    if status == DevStatus_OnLine || status == DevStatus_OffLine {
        sql += " and status="+strconv.Itoa(status)
    }
    sql += " order by createTime limit "+strconv.Itoa(from)+","+strconv.Itoa(size)+""
    err = db.Raw(sql).Scan(&list).Error
    if err != nil {
        return nil, err
    }
    return
}
 
func (d *Device) PageDevByCluster(page int, size int, clusterId string, status int, inputTxt string) (list []Device, err error) {
    sql := "select * from "+d.TableName()+" where 1=1 and clusterId='"+clusterId+"'"
    from := (page-1)*size
    if inputTxt != "" {
        sql += " and devName like '%"+inputTxt+"%'"
    }
    if status == DevStatus_OnLine || status == DevStatus_OffLine {
        sql += " and status="+strconv.Itoa(status)
    }
 
    sql += " order by createTime desc limit "+strconv.Itoa(from)+","+strconv.Itoa(size)+""
    err = db.Raw(sql).Scan(&list).Error
    if err != nil {
        return nil, err
    }
    return
 
}