2017-03-10 09:24:06 +00:00
|
|
|
package models
|
|
|
|
|
2017-04-22 15:39:33 +00:00
|
|
|
import (
|
2017-09-16 09:58:33 +00:00
|
|
|
"github.com/go-xorm/xorm"
|
2017-04-22 15:39:33 +00:00
|
|
|
)
|
2017-04-20 01:36:42 +00:00
|
|
|
|
2017-03-10 09:24:06 +00:00
|
|
|
// 主机
|
|
|
|
type Host struct {
|
2017-09-16 09:58:33 +00:00
|
|
|
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:"-"`
|
2017-03-10 09:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 新增
|
2017-04-02 02:19:52 +00:00
|
|
|
func (host *Host) Create() (insertId int16, err error) {
|
2017-09-16 09:58:33 +00:00
|
|
|
_, err = Db.Insert(host)
|
|
|
|
if err == nil {
|
|
|
|
insertId = host.Id
|
|
|
|
}
|
2017-03-24 09:55:44 +00:00
|
|
|
|
2017-09-16 09:58:33 +00:00
|
|
|
return
|
2017-03-10 09:24:06 +00:00
|
|
|
}
|
|
|
|
|
2017-09-16 09:58:33 +00:00
|
|
|
func (host *Host) UpdateBean(id int16) (int64, error) {
|
|
|
|
return Db.ID(id).Cols("name,alias,port,remark").Update(host)
|
2017-04-20 01:36:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 09:24:06 +00:00
|
|
|
// 更新
|
2017-04-02 02:19:52 +00:00
|
|
|
func (host *Host) Update(id int, data CommonMap) (int64, error) {
|
2017-09-16 09:58:33 +00:00
|
|
|
return Db.Table(host).ID(id).Update(data)
|
2017-03-10 09:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 删除
|
2017-04-02 02:19:52 +00:00
|
|
|
func (host *Host) Delete(id int) (int64, error) {
|
2017-09-16 09:58:33 +00:00
|
|
|
return Db.Id(id).Delete(new(Host))
|
2017-03-10 09:24:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-16 08:24:09 +00:00
|
|
|
func (host *Host) Find(id int) error {
|
2017-09-16 09:58:33 +00:00
|
|
|
_, err := Db.Id(id).Get(host)
|
2017-04-16 08:24:09 +00:00
|
|
|
|
2017-09-16 09:58:33 +00:00
|
|
|
return err
|
2017-04-16 08:24:09 +00:00
|
|
|
}
|
|
|
|
|
2017-09-16 09:58:33 +00:00
|
|
|
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
|
|
|
|
}
|
2017-04-13 09:35:59 +00:00
|
|
|
|
2017-09-16 09:58:33 +00:00
|
|
|
count, err := Db.Where("name = ? AND id != ?", name, id).Count(host)
|
|
|
|
return count > 0, err
|
2017-04-13 09:35:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-22 15:39:33 +00:00
|
|
|
func (host *Host) List(params CommonMap) ([]Host, error) {
|
2017-09-16 09:58:33 +00:00
|
|
|
host.parsePageAndPageSize(params)
|
|
|
|
list := make([]Host, 0)
|
|
|
|
session := Db.Desc("id")
|
|
|
|
host.parseWhere(session, params)
|
|
|
|
err := session.Limit(host.PageSize, host.pageLimitOffset()).Find(&list)
|
2017-03-10 09:24:06 +00:00
|
|
|
|
2017-09-16 09:58:33 +00:00
|
|
|
return list, err
|
2017-03-10 09:24:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 09:15:30 +00:00
|
|
|
func (host *Host) AllList() ([]Host, error) {
|
2017-09-16 09:58:33 +00:00
|
|
|
list := make([]Host, 0)
|
|
|
|
err := Db.Cols("name,port").Desc("id").Find(&list)
|
2017-04-08 09:15:30 +00:00
|
|
|
|
2017-09-16 09:58:33 +00:00
|
|
|
return list, err
|
2017-04-08 09:15:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-23 06:11:53 +00:00
|
|
|
func (host *Host) Total(params CommonMap) (int64, error) {
|
2017-09-16 09:58:33 +00:00
|
|
|
session := Db.NewSession()
|
|
|
|
host.parseWhere(session, params)
|
|
|
|
return session.Count(host)
|
2017-03-10 09:24:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-22 15:39:33 +00:00
|
|
|
// 解析where
|
2017-09-16 09:58:33 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|