GAAS GFrame项目web后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
1.9 KiB

  1. package models
  2. import "LAPP_GAAS_GFrame_BACKEND/db"
  3. type TaskHost struct {
  4. Id int `json:"id" xorm:"int pk autoincr"`
  5. TaskId int `json:"task_id" xorm:"int not null index"`
  6. HostId int16 `json:"host_id" xorm:"smallint not null index"`
  7. }
  8. type TaskHostDetail struct {
  9. TaskHost `xorm:"extends"`
  10. Name string `json:"name"`
  11. Port int `json:"port"`
  12. Alias string `json:"alias"`
  13. }
  14. func (TaskHostDetail) TableName() string {
  15. return "task_host"
  16. }
  17. func hostTableName() []string {
  18. return []string{"host", "h"}
  19. }
  20. func (th *TaskHost) Remove(taskId int) error {
  21. e := db.Eloquent.Master()
  22. _, err := e.Where("task_id = ?", taskId).Delete(new(TaskHost))
  23. return err
  24. }
  25. func (th *TaskHost) Add(taskId int, hostIds []int) error {
  26. e := db.Eloquent.Master()
  27. err := th.Remove(taskId)
  28. if err != nil {
  29. return err
  30. }
  31. taskHosts := make([]TaskHost, len(hostIds))
  32. for i, value := range hostIds {
  33. taskHosts[i].TaskId = taskId
  34. taskHosts[i].HostId = int16(value)
  35. }
  36. _, err = e.Insert(&taskHosts)
  37. return err
  38. }
  39. func (th *TaskHost) GetHostIdsByTaskId(taskId int) ([]TaskHostDetail, error) {
  40. e := db.Eloquent.Master()
  41. list := make([]TaskHostDetail, 0)
  42. fields := "th.id,th.host_id,h.alias,h.name,h.port"
  43. err := e.Alias("th").
  44. Join("LEFT", hostTableName(), "th.host_id=h.id").
  45. Where("th.task_id = ?", taskId).
  46. Cols(fields).
  47. Find(&list)
  48. return list, err
  49. }
  50. func (th *TaskHost) GetTaskIdsByHostId(hostId int16) ([]interface{}, error) {
  51. e := db.Eloquent.Master()
  52. list := make([]TaskHost, 0)
  53. err := e.Where("host_id = ?", hostId).Cols("task_id").Find(&list)
  54. if err != nil {
  55. return nil, err
  56. }
  57. taskIds := make([]interface{}, len(list))
  58. for i, value := range list {
  59. taskIds[i] = value.TaskId
  60. }
  61. return taskIds, err
  62. }
  63. // 判断主机id是否有引用
  64. func (th *TaskHost) HostIdExist(hostId int16) (bool, error) {
  65. e := db.Eloquent.Master()
  66. count, err := e.Where("host_id = ?", hostId).Count(th)
  67. return count > 0, err
  68. }