alarmController.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "rtzh_elec_temperature/rtelec_app_public_lib/service"
  5. "strings"
  6. "time"
  7. "github.com/spf13/cast"
  8. "git.rtzhtech.cn/iss/public-lib/dto"
  9. )
  10. //告警相关服务
  11. type AlarmController struct {
  12. BaseController
  13. }
  14. //告警策略数据列表
  15. // @router /list [post,get]
  16. func (c *AlarmController) AlarmList() {
  17. level, _ := c.GetInt("level")
  18. name := c.GetString("name")
  19. pageIndex, _ := c.GetInt("pageindex", 1)
  20. pageSize, _ := c.GetInt("pagesize", 20)
  21. list, err := new(service.AlarmService).AlarmList(level, pageIndex, pageSize, name)
  22. if err == nil {
  23. c.Data["json"] = c.ApiOK(list)
  24. } else {
  25. c.Data["json"] = c.ApiError(err.Error())
  26. }
  27. c.ServeJSON()
  28. }
  29. //编辑告警策略数据记录(可以带告警策略与测点关联)
  30. // @router /edit [post]
  31. func (c *AlarmController) EditAlarm() {
  32. var AlarmObject dto.AddAlarmStrategyRes
  33. AlarmObject.Strategyid, _ = c.GetInt64("strategyid")
  34. AlarmObject.Strategyname = c.GetString("strategyname")
  35. AlarmObject.Alarmlevel, _ = c.GetInt32("alarmlevel")
  36. AlarmObject.Appid = cast.ToInt32(service.RtelecManageApp().RegAppID)
  37. relationParameter := c.GetString("relation")
  38. var relationData []map[string]interface{}
  39. if relationParameter != "" {
  40. bytes := []byte(relationParameter)
  41. json_err := json.Unmarshal(bytes, &relationData)
  42. if json_err != nil {
  43. c.Data["json"] = c.ApiError("告警策略与测点关联参数不是标准的json格式!")
  44. c.ServeJSON()
  45. return
  46. }
  47. }
  48. err := new(service.AlarmService).EditAlarm(&AlarmObject, relationData)
  49. if err == nil {
  50. c.Data["json"] = c.ApiOK("编辑告警策略成功!")
  51. } else {
  52. c.Data["json"] = c.ApiError(err.Error())
  53. }
  54. c.ServeJSON()
  55. }
  56. //编辑告警策略详细
  57. // @router /detail [get]
  58. func (c *AlarmController) AlarmDetail() {
  59. strategyid, _ := c.GetInt64("strategyid")
  60. if strategyid == 0 {
  61. c.Data["json"] = c.ApiError("策略ID不允许为空或0!")
  62. } else {
  63. list, err := new(service.AlarmService).AlarmDetail(strategyid)
  64. if err == nil {
  65. c.Data["json"] = c.ApiOK(list)
  66. } else {
  67. c.Data["json"] = c.ApiError(err.Error())
  68. }
  69. }
  70. c.ServeJSON()
  71. }
  72. //编辑告警策略数据记录(可以带告警策略与测点关联)
  73. // @router /delete [post]
  74. func (c *AlarmController) DeleteAlarm() {
  75. strategyid, _ := c.GetInt64("strategyid")
  76. if strategyid == 0 {
  77. c.Data["json"] = c.ApiError("策略ID不允许为空!")
  78. } else {
  79. err := new(service.AlarmService).DeleteAlarm(strategyid)
  80. if err == nil {
  81. c.Data["json"] = c.ApiOK("删除告警策略成功!")
  82. } else {
  83. c.Data["json"] = c.ApiError(err.Error())
  84. }
  85. }
  86. c.ServeJSON()
  87. }
  88. //告警数据列表
  89. // @router /data_list [get]
  90. func (c *AlarmController) AlarmDataList() {
  91. alarmLevel, _ := c.GetInt("alarmlevel")
  92. area := c.GetString("area")
  93. pageIndex, _ := c.GetInt("pageindex", 1)
  94. pageSize, _ := c.GetInt("pagesize", 20)
  95. alarmType, _ := c.GetInt("alarmtype")
  96. starttime := c.GetString("starttime")
  97. var startDate, endDate = 0, 0
  98. if starttime != "" {
  99. starttime = strings.TrimLeft(starttime, " ")
  100. starttime = strings.TrimRight(starttime, " ")
  101. if strings.Index(starttime, " ") == -1 {
  102. starttime += " 00:00:00"
  103. }
  104. start_date, parseErr := time.ParseInLocation("2006-01-02 15:04:05", starttime, time.Local)
  105. if parseErr == nil {
  106. startDate = int(start_date.Unix())
  107. }
  108. }
  109. endtime := c.GetString("endtime")
  110. if endtime != "" {
  111. endtime = strings.TrimLeft(endtime, " ")
  112. endtime = strings.TrimRight(endtime, " ")
  113. if strings.Index(endtime, " ") == -1 {
  114. endtime += " 00:00:00"
  115. }
  116. end_date, parseErr := time.ParseInLocation("2006-01-02 15:04:05", endtime, time.Local)
  117. if parseErr == nil {
  118. endDate = int(end_date.Unix())
  119. }
  120. }
  121. appid := cast.ToInt(service.RtelecManageApp().RegAppID)
  122. list, err := new(service.AlarmService).AlarmDataList(appid, startDate, endDate, area, alarmType, alarmLevel, pageIndex, pageSize)
  123. if err == nil {
  124. c.Data["json"] = c.ApiOK(list)
  125. } else {
  126. c.Data["json"] = c.ApiError(err.Error())
  127. }
  128. c.ServeJSON()
  129. }
  130. //告警数据列表
  131. // @router /now [get]
  132. func (c *AlarmController) AlarmNewList() {
  133. pageIndex, _ := c.GetInt("pageindex", 1)
  134. pageSize, _ := c.GetInt("pagesize", 20)
  135. curDate := time.Now().Local().Format("2006-01-02") + " 00:00:00"
  136. formatTime, _ := time.ParseInLocation("2006-01-02 15:04:05", curDate, time.Local)
  137. var startDate = int(formatTime.Unix())
  138. appid := cast.ToInt(service.RtelecManageApp().RegAppID)
  139. list, err := new(service.AlarmService).AlarmDataList(appid, startDate, 0, "", 0, 0, pageIndex, pageSize)
  140. if err == nil {
  141. c.Data["json"] = c.ApiOK(list)
  142. } else {
  143. c.Data["json"] = c.ApiError(err.Error())
  144. }
  145. c.ServeJSON()
  146. }
  147. // 确认告警 godoc
  148. // @Summary 对告警记录进行确认
  149. // @Description 对告警记录进行确认,确认时要以填确认意见
  150. // @Tags api
  151. // @Accept x-www-form-urlencoded
  152. // @Produce json
  153. // @Param id query int true "需要确认的告警记录ID"
  154. // @Param content query string false "确认的用户填写的内容"
  155. // @Param eventid query int true "需要确认的告警事件ID"
  156. // @Success 200 {object} ApiOK 成功
  157. // @Failure 500 status 失败
  158. // @router /confirm [post]
  159. func (c *AlarmController) AlarmConfirm() {
  160. id, _ := c.GetInt32("id")
  161. content := c.GetString("content")
  162. eventid, _ := c.GetInt64("eventid")
  163. err := new(service.AlarmService).AlarmConfirm(id, eventid, content)
  164. if err == nil {
  165. c.Data["json"] = c.ApiOK("确认告警成功!")
  166. } else {
  167. c.Data["json"] = c.ApiError(err.Error())
  168. }
  169. c.ServeJSON()
  170. }