yanghui
2021-06-28 b568d251606f82968b8103c485a0d70027c1d57e
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
package models
 
import (
    "sync"
)
 
const (
    RUNNING_Currupted = 0
    RUNNING_Stopped   = 1
    RUNNING_Started   = 2
)
 
type BeeNode struct {
    Id                 int         `gorm:"column:id;primary_key;unique;not null;autoIncrement" json:"id"`   // 节点id
    Address            string         `gorm:"column:address;not null;default:''" json:"address"`               // 程序目录
    Dir                string         `gorm:"column:dir;not null;default:''" json:"dir"`                       // 程序目录
    ApiPort         uint16      `gorm:"column:api_port" json:"apiPort"`                                  // api port number
    P2PPort         uint16      `gorm:"column:p2p_port" json:"p2pPort"`                                  // p2p port number
    DebugPort       uint16      `gorm:"column:debug_port" json:"debugPort"`                              // debug port number
    Status          uint8       `gorm:"column:status" json:"status"`                                     // status
    Del             uint8       `gorm:"column:del" json:"del"`                                           // del
}
 
var (
    m  sync.RWMutex
)
 
func (BeeNode) TableName() string {
    return "t_node"
}
 
func (a *BeeNode) FindAll() ([]BeeNode, error) {
    var rows []BeeNode
 
    m.RLock()
    defer m.RUnlock()
    if err := db.Table(a.TableName()).Find(&rows).Error; err != nil {
        return nil, err
    }
 
    return rows, nil
}
 
func (a *BeeNode) Save() error {
    m.Lock()
    defer m.Unlock()
 
    return db.Create(a).Error
}
 
func (a *BeeNode) Update(status uint8) error {
    m.Lock()
    defer m.Unlock()
 
    return db.Model(a).Update("Status", status).Error
}