2017-08-06 14:49:24 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
type TaskHost struct {
|
2017-09-16 09:58:33 +00:00
|
|
|
Id int `xorm:"int pk autoincr"`
|
|
|
|
TaskId int `xorm:"int not null index"`
|
|
|
|
HostId int16 `xorm:"smallint not null index"`
|
2017-08-06 14:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TaskHostDetail struct {
|
2017-09-16 09:58:33 +00:00
|
|
|
TaskHost `xorm:"extends"`
|
|
|
|
Name string
|
|
|
|
Port int
|
|
|
|
Alias string
|
2017-08-06 14:49:24 +00:00
|
|
|
}
|
|
|
|
|
2017-09-16 09:58:33 +00:00
|
|
|
func (TaskHostDetail) TableName() string {
|
|
|
|
return TablePrefix + "task_host"
|
2017-08-06 14:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func hostTableName() []string {
|
2017-09-16 09:58:33 +00:00
|
|
|
return []string{TablePrefix + "host", "h"}
|
2017-08-06 14:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (th *TaskHost) Remove(taskId int) error {
|
2017-09-16 09:58:33 +00:00
|
|
|
_, err := Db.Where("task_id = ?", taskId).Delete(new(TaskHost))
|
2017-08-06 14:49:24 +00:00
|
|
|
|
2017-09-16 09:58:33 +00:00
|
|
|
return err
|
2017-08-06 14:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (th *TaskHost) Add(taskId int, hostIds []int) error {
|
|
|
|
|
2017-09-16 09:58:33 +00:00
|
|
|
err := th.Remove(taskId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-06 14:49:24 +00:00
|
|
|
|
2017-09-16 09:58:33 +00:00
|
|
|
taskHosts := make([]TaskHost, len(hostIds))
|
|
|
|
for i, value := range hostIds {
|
|
|
|
taskHosts[i].TaskId = taskId
|
|
|
|
taskHosts[i].HostId = int16(value)
|
|
|
|
}
|
2017-08-06 14:49:24 +00:00
|
|
|
|
2017-09-16 09:58:33 +00:00
|
|
|
_, err = Db.Insert(&taskHosts)
|
2017-08-06 14:49:24 +00:00
|
|
|
|
2017-09-16 09:58:33 +00:00
|
|
|
return err
|
2017-08-06 14:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (th *TaskHost) GetHostIdsByTaskId(taskId int) ([]TaskHostDetail, error) {
|
2017-09-16 09:58:33 +00:00
|
|
|
list := make([]TaskHostDetail, 0)
|
|
|
|
fields := "th.id,th.host_id,h.alias,h.name,h.port"
|
|
|
|
err := Db.Alias("th").
|
|
|
|
Join("LEFT", hostTableName(), "th.host_id=h.id").
|
|
|
|
Where("th.task_id = ?", taskId).
|
|
|
|
Cols(fields).
|
|
|
|
Find(&list)
|
|
|
|
|
|
|
|
return list, err
|
2017-08-06 14:49:24 +00:00
|
|
|
}
|
|
|
|
|
2017-09-16 09:58:33 +00:00
|
|
|
func (th *TaskHost) GetTaskIdsByHostId(hostId int16) ([]interface{}, error) {
|
|
|
|
list := make([]TaskHost, 0)
|
|
|
|
err := Db.Where("host_id = ?", hostId).Cols("task_id").Find(&list)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-06 14:49:24 +00:00
|
|
|
|
2017-09-16 09:58:33 +00:00
|
|
|
taskIds := make([]interface{}, len(list))
|
|
|
|
for i, value := range list {
|
|
|
|
taskIds[i] = value.TaskId
|
|
|
|
}
|
2017-08-06 14:49:24 +00:00
|
|
|
|
2017-09-16 09:58:33 +00:00
|
|
|
return taskIds, err
|
2017-08-06 14:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 判断主机id是否有引用
|
|
|
|
func (th *TaskHost) HostIdExist(hostId int16) (bool, error) {
|
2017-09-16 09:58:33 +00:00
|
|
|
count, err := Db.Where("host_id = ?", hostId).Count(th)
|
2017-08-06 14:49:24 +00:00
|
|
|
|
2017-09-16 09:58:33 +00:00
|
|
|
return count > 0, err
|
|
|
|
}
|