edb_terminal.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package data_stat
  2. import (
  3. "errors"
  4. "eta_gn/eta_api/models/data_manage"
  5. "eta_gn/eta_api/utils"
  6. "fmt"
  7. "time"
  8. )
  9. // SaveEdbTerminal 保存数据源终端信息
  10. func SaveEdbTerminal(req *data_manage.AddEdbTerminalListReq) (errMsg string, err error) {
  11. nowTime := time.Now().Local()
  12. // 更新
  13. if req.TerminalId > 0 {
  14. item, e := data_manage.GetEdbTerminalById(req.TerminalId)
  15. if e != nil {
  16. if utils.IsErrNoRow(e) {
  17. errMsg = "终端不存在"
  18. err = errors.New(errMsg)
  19. return
  20. }
  21. errMsg = "获取终端信息失败"
  22. err = errors.New("获取终端信息失败, Err:" + e.Error())
  23. return
  24. }
  25. updateCols := make([]string, 0)
  26. updateCols = append(updateCols, "Source", "Name", "ServerUrl", "DirPath", "Num", "Value", "ModifyTime")
  27. item.Source = req.Source
  28. item.Name = req.Name
  29. item.ServerUrl = req.ServerUrl
  30. item.DirPath = req.DirPath
  31. item.Num = req.Num
  32. item.Value = req.Value
  33. // todo 状态是否可修改
  34. //item.Status = 1
  35. item.ModifyTime = nowTime
  36. if e := item.Update(updateCols); e != nil {
  37. errMsg = "更新失败"
  38. err = errors.New("更新终端失败, Err:" + e.Error())
  39. }
  40. return
  41. }
  42. terminalCode := createTerminalCode(req.Source, req.Name)
  43. //item.TerminalCode = req.TerminalCode
  44. // 新增
  45. newTerminal := &data_manage.EdbTerminal{
  46. Source: req.Source,
  47. Name: req.Name,
  48. TerminalCode: terminalCode,
  49. ServerUrl: req.ServerUrl,
  50. DirPath: req.DirPath,
  51. Num: req.Num,
  52. Status: 1,
  53. Value: req.Value,
  54. CreateTime: nowTime,
  55. ModifyTime: nowTime,
  56. }
  57. if e := newTerminal.Add(); e != nil {
  58. errMsg = "新增失败"
  59. err = errors.New("新增终端失败, Err:" + e.Error())
  60. }
  61. return
  62. }
  63. // SetEdbTerminalStatus 启用/禁用 终端
  64. func SetEdbTerminalStatus(id int, status int) (errMsg string, err error) {
  65. nowTime := time.Now().Local()
  66. // 更新
  67. item, e := data_manage.GetEdbTerminalById(id)
  68. if e != nil {
  69. if utils.IsErrNoRow(e) {
  70. errMsg = "终端不存在"
  71. err = errors.New(errMsg)
  72. return
  73. }
  74. errMsg = "获取终端信息失败"
  75. err = errors.New("获取终端信息失败, Err:" + e.Error())
  76. return
  77. }
  78. updateCols := make([]string, 0)
  79. updateCols = append(updateCols, "Status", "ModifyTime")
  80. item.Status = status
  81. item.ModifyTime = nowTime
  82. if e := item.Update(updateCols); e != nil {
  83. errMsg = "更新失败"
  84. err = errors.New("更新终端失败, Err:" + e.Error())
  85. }
  86. return
  87. }
  88. func createTerminalCode(source int, sourceName string) (terminalCode string) {
  89. randInt := utils.GetRandNumber(1000)
  90. terminalCode = utils.MD5(fmt.Sprintf("%d_%s_%s_%d", source, sourceName, time.Now(), randInt))
  91. return
  92. }