package models

import "LAPP_LF_MOM_BACKEND/db"

type TaskHost struct {
	Id     int   `json:"id" xorm:"int pk autoincr"`
	TaskId int   `json:"task_id" xorm:"int not null index"`
	HostId int16 `json:"host_id" xorm:"smallint not null index"`
}

type TaskHostDetail struct {
	TaskHost `xorm:"extends"`
	Name     string `json:"name"`
	Port     int    `json:"port"`
	Alias    string `json:"alias"`
}

func (TaskHostDetail) TableName() string {
	return "task_host"
}

func hostTableName() []string {
	return []string{"host", "h"}
}

func (th *TaskHost) Remove(taskId int) error {
	e := db.Eloquent.Master()
	_, err := e.Where("task_id = ?", taskId).Delete(new(TaskHost))

	return err
}

func (th *TaskHost) Add(taskId int, hostIds []int) error {
	e := db.Eloquent.Master()
	err := th.Remove(taskId)
	if err != nil {
		return err
	}

	taskHosts := make([]TaskHost, len(hostIds))
	for i, value := range hostIds {
		taskHosts[i].TaskId = taskId
		taskHosts[i].HostId = int16(value)
	}

	_, err = e.Insert(&taskHosts)

	return err
}

func (th *TaskHost) GetHostIdsByTaskId(taskId int) ([]TaskHostDetail, error) {
	e := db.Eloquent.Master()
	list := make([]TaskHostDetail, 0)
	fields := "th.id,th.host_id,h.alias,h.name,h.port"
	err := e.Alias("th").
		Join("LEFT", hostTableName(), "th.host_id=h.id").
		Where("th.task_id = ?", taskId).
		Cols(fields).
		Find(&list)

	return list, err
}

func (th *TaskHost) GetTaskIdsByHostId(hostId int16) ([]interface{}, error) {
	e := db.Eloquent.Master()
	list := make([]TaskHost, 0)
	err := e.Where("host_id = ?", hostId).Cols("task_id").Find(&list)
	if err != nil {
		return nil, err
	}

	taskIds := make([]interface{}, len(list))
	for i, value := range list {
		taskIds[i] = value.TaskId
	}

	return taskIds, err
}

// 判断主机id是否有引用
func (th *TaskHost) HostIdExist(hostId int16) (bool, error) {
	e := db.Eloquent.Master()
	count, err := e.Where("host_id = ?", hostId).Count(th)

	return count > 0, err
}
