host.go 3.9 KB
Newer Older
7
3.0.0  
710leo 已提交
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
package models

import (
	"strings"
	"time"

	"xorm.io/xorm"

	"github.com/toolkits/pkg/str"
)

type Host struct {
	Id     int64  `json:"id"`
	SN     string `json:"sn" xorm:"'sn'"`
	IP     string `json:"ip" xorm:"'ip'"`
	Ident  string `json:"ident"`
	Name   string `json:"name"`
	Note   string `json:"note"`
	CPU    string `json:"cpu" xorm:"'cpu'"`
	Mem    string `json:"mem"`
	Disk   string `json:"disk"`
	Cate   string `json:"cate"`
	Clock  int64  `json:"clock"`
	Tenant string `json:"tenant"`
}

func (h *Host) Save() error {
	_, err := DB["ams"].Insert(h)
	return err
}

qd_lm's avatar
qd_lm 已提交
32
func HostNew(sn, ip, ident, name, cate string, fields map[string]interface{}) (*Host, error) {
7
3.0.0  
710leo 已提交
33 34 35 36 37 38 39 40 41 42 43 44
	host := new(Host)
	host.SN = sn
	host.IP = ip
	host.Ident = ident
	host.Name = name
	host.Cate = cate
	host.Clock = time.Now().Unix()

	session := DB["ams"].NewSession()
	defer session.Close()

	if err := session.Begin(); err != nil {
qd_lm's avatar
qd_lm 已提交
45
		return nil, err
7
3.0.0  
710leo 已提交
46 47 48 49
	}

	if _, err := session.Insert(host); err != nil {
		session.Rollback()
qd_lm's avatar
qd_lm 已提交
50
		return nil, err
7
3.0.0  
710leo 已提交
51 52 53 54 55
	}

	if len(fields) > 0 {
		if _, err := session.Table(new(Host)).ID(host.Id).Update(fields); err != nil {
			session.Rollback()
qd_lm's avatar
qd_lm 已提交
56
			return nil, err
7
3.0.0  
710leo 已提交
57 58 59
		}
	}

qd_lm's avatar
qd_lm 已提交
60 61 62
	err := session.Commit()

	return host, err
7
3.0.0  
710leo 已提交
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
}

func (h *Host) Update(fields map[string]interface{}) error {
	_, err := DB["ams"].Table(new(Host)).ID(h.Id).Update(fields)
	return err
}

func (h *Host) Del() error {
	_, err := DB["ams"].Where("id=?", h.Id).Delete(new(Host))
	return err
}

func HostUpdateNote(ids []int64, note string) error {
	_, err := DB["ams"].Exec("UPDATE host SET note=? WHERE id in ("+str.IdsString(ids)+")", note)
	return err
}

func HostUpdateCate(ids []int64, cate string) error {
	_, err := DB["ams"].Exec("UPDATE host SET cate=? WHERE id in ("+str.IdsString(ids)+")", cate)
	return err
}

func HostUpdateTenant(ids []int64, tenant string) error {
	_, err := DB["ams"].Exec("UPDATE host SET tenant=? WHERE id in ("+str.IdsString(ids)+")", tenant)
	return err
}

func HostGet(where string, args ...interface{}) (*Host, error) {
	var obj Host
	has, err := DB["ams"].Where(where, args...).Get(&obj)
	if err != nil {
		return nil, err
	}

	if !has {
		return nil, nil
	}

	return &obj, nil
}

func HostGets(where string, args ...interface{}) (hosts []Host, err error) {
	if where != "" {
		err = DB["ams"].Where(where, args...).Find(&hosts)
	} else {
		err = DB["ams"].Find(&hosts)
	}
	return hosts, err
}

func HostByIds(ids []int64) (hosts []Host, err error) {
	if len(ids) == 0 {
		return
	}

	err = DB["ams"].In("id", ids).Find(&hosts)
	return
}

U
Ulric Qin 已提交
122 123 124 125 126
func HostIdsByIps(ips []string) (ids []int64, err error) {
	err = DB["ams"].Table(new(Host)).In("ip", ips).Select("id").Find(&ids)
	return
}

7
3.0.0  
710leo 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
func HostSearch(batch, field string) ([]Host, error) {
	arr := str.ParseLines(strings.Replace(batch, ",", "\n", -1))
	if len(arr) == 0 {
		return []Host{}, nil
	}

	var objs []Host
	err := DB["ams"].In(field, arr).Find(&objs)
	return objs, err
}

func HostTotalForAdmin(tenant, query, batch, field string) (int64, error) {
	return buildHostWhere(tenant, query, batch, field).Count()
}

func HostGetsForAdmin(tenant, query, batch, field string, limit, offset int) ([]Host, error) {
	var objs []Host
	err := buildHostWhere(tenant, query, batch, field).Limit(limit, offset).Find(&objs)
	return objs, err
}

func buildHostWhere(tenant, query, batch, field string) *xorm.Session {
	session := DB["ams"].Table(new(Host)).OrderBy("ident")

7
710leo 已提交
151 152 153
	if tenant == "0" {
		session = session.Where("tenant=?", "")
	} else if tenant != "" {
7
3.0.0  
710leo 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
		session = session.Where("tenant=?", tenant)
	}

	if batch == "" && query != "" {
		arr := strings.Fields(query)
		for i := 0; i < len(arr); i++ {
			q := "%" + arr[i] + "%"
			session = session.Where("cate=? or sn=? or ident like ? or ip like ? or name like ? or note like ?", arr[i], arr[i], q, q, q, q)
		}
	}

	if batch != "" {
		arr := str.ParseLines(strings.Replace(batch, ",", "\n", -1))
		if len(arr) > 0 {
			session = session.In(field, arr)
		}
	}

	return session
}