edb_terminal.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package data_stat
  2. import (
  3. "errors"
  4. "eta/eta_api/models/data_manage"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "strings"
  8. "time"
  9. )
  10. // SaveEdbTerminal 保存数据源终端信息
  11. func SaveEdbTerminal(req *data_manage.AddEdbTerminalListReq) (errMsg string, err error) {
  12. nowTime := time.Now().Local()
  13. // 更新
  14. if req.TerminalId > 0 {
  15. item, e := data_manage.GetEdbTerminalById(req.TerminalId)
  16. if e != nil {
  17. if e.Error() == utils.ErrNoRow() {
  18. errMsg = "终端不存在"
  19. err = errors.New(errMsg)
  20. return
  21. }
  22. errMsg = "获取终端信息失败"
  23. err = errors.New("获取终端信息失败, Err:" + e.Error())
  24. return
  25. }
  26. updateCols := make([]string, 0)
  27. updateCols = append(updateCols, "Source", "Name", "ServerUrl", "DirPath", "Num", "Value", "ModifyTime")
  28. item.Source = req.Source
  29. item.Name = req.Name
  30. item.ServerUrl = req.ServerUrl
  31. item.DirPath = req.DirPath
  32. item.Num = req.Num
  33. item.Value = req.Value
  34. // todo 状态是否可修改
  35. //item.Status = 1
  36. item.ModifyTime = nowTime
  37. if e := item.Update(updateCols); e != nil {
  38. errMsg = "更新失败"
  39. err = errors.New("更新终端失败, Err:" + e.Error())
  40. }
  41. return
  42. }
  43. terminalCode := createTerminalCode(req.Source, req.Name)
  44. //item.TerminalCode = req.TerminalCode
  45. // 新增
  46. newTerminal := &data_manage.EdbTerminal{
  47. Source: req.Source,
  48. Name: req.Name,
  49. TerminalCode: terminalCode,
  50. ServerUrl: req.ServerUrl,
  51. DirPath: req.DirPath,
  52. Num: req.Num,
  53. Status: 1,
  54. Value: req.Value,
  55. CreateTime: nowTime,
  56. ModifyTime: nowTime,
  57. }
  58. if e := newTerminal.Add(); e != nil {
  59. errMsg = "新增失败"
  60. err = errors.New("新增终端失败, Err:" + e.Error())
  61. }
  62. return
  63. }
  64. // SetEdbTerminalStatus 启用/禁用 终端
  65. func SetEdbTerminalStatus(id int, status int) (errMsg string, err error) {
  66. nowTime := time.Now().Local()
  67. // 更新
  68. item, e := data_manage.GetEdbTerminalById(id)
  69. if e != nil {
  70. if e.Error() == utils.ErrNoRow() {
  71. errMsg = "终端不存在"
  72. err = errors.New(errMsg)
  73. return
  74. }
  75. errMsg = "获取终端信息失败"
  76. err = errors.New("获取终端信息失败, Err:" + e.Error())
  77. return
  78. }
  79. updateCols := make([]string, 0)
  80. updateCols = append(updateCols, "Status", "ModifyTime")
  81. item.Status = status
  82. item.ModifyTime = nowTime
  83. if e := item.Update(updateCols); e != nil {
  84. errMsg = "更新失败"
  85. err = errors.New("更新终端失败, Err:" + e.Error())
  86. }
  87. return
  88. }
  89. func createTerminalCode(source int, sourceName string) (terminalCode string) {
  90. randInt := utils.GetRandNumber(1000)
  91. terminalCode = utils.MD5(fmt.Sprintf("%d_%s_%s_%d", source, sourceName, time.Now(), randInt))
  92. return
  93. }
  94. // GetEdbTerminalDirInfo 获取指标的终端信息
  95. func GetEdbTerminalDirInfo(indexId, source int) (info *data_manage.EdbTerminalDirInfo, err error) {
  96. // 查询指标信息
  97. var terminalCode, filePath, name, dirPath string
  98. switch source {
  99. case utils.DATA_SOURCE_MYSTEEL_CHEMICAL:
  100. // 查询钢联指标信息
  101. indexInfo, e := data_manage.GetBaseFromMysteelChemicalIndexByIndexId(indexId)
  102. if e != nil {
  103. err = errors.New("获取指标信息失败, Err:" + e.Error())
  104. return
  105. }
  106. terminalCode = indexInfo.TerminalCode
  107. filePath = indexInfo.MergeFilePath
  108. if filePath == "" && indexInfo.FilePath != "" {
  109. filePath = indexInfo.FilePath
  110. }
  111. break
  112. case utils.DATA_SOURCE_YS:
  113. // 查询有色指标信息
  114. indexInfo, e := data_manage.GetBaseFromSmmIndexByIndexId(indexId)
  115. if e != nil {
  116. err = errors.New("获取指标信息失败, Err:" + e.Error())
  117. return
  118. }
  119. terminalCode = indexInfo.TerminalCode
  120. filePath = indexInfo.RenameFileName
  121. break
  122. case utils.DATA_SOURCE_SCI:
  123. // 查询SCI指标信息
  124. indexInfo, e := data_manage.GetBaseFromSciIndexByIndexId(indexId)
  125. if e != nil {
  126. err = errors.New("获取指标信息失败, Err:" + e.Error())
  127. return
  128. }
  129. terminalCode = indexInfo.TerminalCode
  130. filePath = indexInfo.FilePath
  131. break
  132. default:
  133. err = errors.New("不支持其他来源")
  134. return
  135. }
  136. // 查询终端信息
  137. if terminalCode == "" {
  138. err = errors.New("获取终端信息失败, Err:终端编码为空")
  139. return
  140. }
  141. terminalInfo, err := data_manage.GetEdbTerminalByCode(terminalCode)
  142. if err != nil {
  143. err = errors.New("获取终端信息失败, Err:" + err.Error())
  144. return
  145. }
  146. if terminalInfo.Source != source {
  147. err = errors.New("获取终端信息失败, Err:指标来源与终端来源不一致")
  148. return
  149. }
  150. name = terminalInfo.Name
  151. // 截取最后的文件名称
  152. if filePath != "" {
  153. // 使用 strings.LastIndex 和切片操作获取文件路径(不包括文件名)
  154. lastIndex := strings.LastIndex(filePath, `\`)
  155. if lastIndex != -1 {
  156. dirPath = filePath[:lastIndex]
  157. //fmt.Println("文件路径:", dirPath)
  158. fileName := filePath[lastIndex+1:]
  159. //fmt.Println("文件名:", fileName)
  160. filePath = fileName
  161. }
  162. }
  163. info = &data_manage.EdbTerminalDirInfo{
  164. TerminalCode: terminalCode,
  165. FilePath: filePath,
  166. Name: name,
  167. DirPath: dirPath,
  168. }
  169. return
  170. }