edb_terminal.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package services
  2. import (
  3. "errors"
  4. "eta/eta_index_lib/models"
  5. "eta/eta_index_lib/utils"
  6. )
  7. // GetTerminal 获取终端信息
  8. func GetTerminal(source int, oldTerminalCode string) (edbTerminal *models.EdbTerminal, err error) {
  9. if source == utils.DATA_SOURCE_WIND && oldTerminalCode == "" {
  10. tmpConfig := new(models.EdbConfig)
  11. tmpConfig, err = models.GetConfigDetailByCode("wind_terminal_code")
  12. if err != nil && err.Error() != utils.ErrNoRow() {
  13. err = errors.New("获取配置的wind地址失败,err:" + err.Error())
  14. return
  15. }
  16. err = nil
  17. if tmpConfig != nil {
  18. oldTerminalCode = tmpConfig.ConfigValue
  19. //return
  20. }
  21. }
  22. if oldTerminalCode != "" {
  23. edbTerminal, err = models.GetEdbTerminalByCode(oldTerminalCode)
  24. if err != nil {
  25. return
  26. }
  27. return
  28. }
  29. // 配置中没有的话,那么就从分组获取,
  30. list, err := models.GetEdbCountGroupByTerminal(source)
  31. if err != nil {
  32. return
  33. }
  34. //windUrlNum := 0
  35. //获取对应的配置url
  36. terminalNumMap := make(map[string]int)
  37. for _, v := range list {
  38. terminalNumMap[v.TerminalCode] = v.Total
  39. }
  40. terminalList, err := models.GetEdbTerminalListBySource(source)
  41. if err != nil {
  42. return
  43. }
  44. if len(terminalList) == 0 {
  45. err = errors.New("终端地址未配置")
  46. return
  47. }
  48. num := 0
  49. for _, v := range terminalList {
  50. tmpNum := terminalNumMap[v.TerminalCode]
  51. if edbTerminal == nil {
  52. edbTerminal = v
  53. num = tmpNum
  54. } else if tmpNum < num {
  55. edbTerminal = v
  56. num = tmpNum
  57. }
  58. }
  59. if edbTerminal == nil {
  60. err = errors.New("获取配置的终端地址失败")
  61. return
  62. }
  63. /*if edbTerminal != nil {
  64. windUrl = edbTerminal.ServerUrl
  65. terminalCode = edbTerminal.TerminalCode
  66. }*/
  67. return
  68. }
  69. func GetFirstTerminal(source int, oldTerminalCode string) (edbTerminal *models.EdbTerminal, err error) {
  70. if oldTerminalCode != "" {
  71. edbTerminal, err = models.GetEdbTerminalByCode(oldTerminalCode)
  72. if err != nil {
  73. return
  74. }
  75. return
  76. }
  77. edbTerminal, err = models.GetEdbTerminalFirstBySource(source)
  78. if err != nil {
  79. if err.Error() == utils.ErrNoRow() {
  80. err = errors.New("终端未配置")
  81. return
  82. }
  83. return
  84. }
  85. return
  86. }