forked from ouqiang/gocron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost.go
94 lines (77 loc) · 2.14 KB
/
host.go
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
package models
import (
"github.com/go-xorm/xorm"
)
// 主机
type Host struct {
Id int16 `xorm:"smallint pk autoincr"`
Name string `xorm:"varchar(64) notnull"` // 主机名称
Alias string `xorm:"varchar(32) notnull default '' "` // 主机别名
Port int `xorm:"notnull default 22"` // 主机端口
Remark string `xorm:"varchar(100) notnull default '' "` // 备注
BaseModel `xorm:"-"`
Selected bool `xorm:"-"`
}
// 新增
func (host *Host) Create() (insertId int16, err error) {
_, err = Db.Insert(host)
if err == nil {
insertId = host.Id
}
return
}
func (host *Host) UpdateBean(id int16) (int64, error) {
return Db.ID(id).Cols("name,alias,port,remark").Update(host)
}
// 更新
func (host *Host) Update(id int, data CommonMap) (int64, error) {
return Db.Table(host).ID(id).Update(data)
}
// 删除
func (host *Host) Delete(id int) (int64, error) {
return Db.Id(id).Delete(new(Host))
}
func (host *Host) Find(id int) error {
_, err := Db.Id(id).Get(host)
return err
}
func (host *Host) NameExists(name string, id int16) (bool, error) {
if id == 0 {
count, err := Db.Where("name = ?", name).Count(host)
return count > 0, err
}
count, err := Db.Where("name = ? AND id != ?", name, id).Count(host)
return count > 0, err
}
func (host *Host) List(params CommonMap) ([]Host, error) {
host.parsePageAndPageSize(params)
list := make([]Host, 0)
session := Db.Desc("id")
host.parseWhere(session, params)
err := session.Limit(host.PageSize, host.pageLimitOffset()).Find(&list)
return list, err
}
func (host *Host) AllList() ([]Host, error) {
list := make([]Host, 0)
err := Db.Cols("name,port").Desc("id").Find(&list)
return list, err
}
func (host *Host) Total(params CommonMap) (int64, error) {
session := Db.NewSession()
host.parseWhere(session, params)
return session.Count(host)
}
// 解析where
func (host *Host) parseWhere(session *xorm.Session, params CommonMap) {
if len(params) == 0 {
return
}
id, ok := params["Id"]
if ok && id.(int) > 0 {
session.And("id = ?", id)
}
name, ok := params["Name"]
if ok && name.(string) != "" {
session.And("name = ?", name)
}
}