alarmEventLogic.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package logic
  2. import (
  3. "git.rtzhtech.cn/iss/public-lib/dao"
  4. "git.rtzhtech.cn/iss/public-lib/dto"
  5. "git.rtzhtech.cn/iss/public-lib/model"
  6. "git.rtzhtech.cn/iss/public-lib/svc"
  7. "git.rtzhtech.cn/iss/public-lib/utils"
  8. "github.com/gogf/gf/v2/util/gconv"
  9. "gorm.io/gen/field"
  10. "time"
  11. )
  12. type AlarmEventLogic struct {
  13. SvcCtx *svc.ServiceContext
  14. }
  15. // NewAlarmEventLogic
  16. // @Description: 告警事件
  17. // @return *AlarmEventLogic
  18. func NewAlarmEventLogic() *AlarmEventLogic {
  19. return &AlarmEventLogic{
  20. SvcCtx: svc.SvcCtx,
  21. }
  22. }
  23. // AddAlarmEvent
  24. // @Description: 添加
  25. // @receiver l
  26. // @param in
  27. // @return error
  28. func (l *AlarmEventLogic) AddAlarmEvent(in *model.AlarmEvent) error {
  29. in.ID = 0
  30. err := l.SvcCtx.AlarmEvent.Create(in)
  31. return err
  32. }
  33. // DelAlarmEvent
  34. // @Description: 删除
  35. // @receiver l
  36. // @param id
  37. // @return error
  38. func (l *AlarmEventLogic) DelAlarmEvent(id int) error {
  39. do := l.SvcCtx.AlarmEvent
  40. _, err := do.Where(do.ID.Eq(int32(id))).Delete()
  41. return err
  42. }
  43. // ModifyAlarmEvent
  44. // @Description: 修改
  45. // @receiver l
  46. // @param in
  47. // @param fields 更新字段,当field为null时,更新in的全部字段
  48. // @return error
  49. func (l *AlarmEventLogic) ModifyAlarmEvent(in *model.AlarmEvent, fields ...string) error {
  50. do := l.SvcCtx.AlarmEvent
  51. var err error
  52. _do := do.Omit(do.ID).Where(do.Eventid.Eq(in.Eventid))
  53. if fields == nil {
  54. m := gconv.Map(in)
  55. _, err = _do.Updates(m)
  56. return err
  57. }
  58. var fieldList []field.Expr
  59. for _, v := range fields {
  60. _f, ok := do.GetFieldByName(v)
  61. if ok {
  62. fieldList = append(fieldList, _f)
  63. }
  64. }
  65. _, err = _do.Select(fieldList...).Updates(in)
  66. return err
  67. }
  68. func (l *AlarmEventLogic) getAlarmEventCond(req *dto.GetAlarmEventReq) (*dao.Condition, error) {
  69. _dao := l.SvcCtx.AlarmEvent
  70. _strategy := l.SvcCtx.AlarmStrategy
  71. _app := l.SvcCtx.SysApp
  72. _relation := l.SvcCtx.AlarmRelation
  73. _mpinfo := l.SvcCtx.DevMpinfo
  74. cond := NewCondition(&req.LimitPage)
  75. if req.EventId > 0 {
  76. cond.Where[_dao.Eventid.String()] = req.EventId
  77. return cond, nil
  78. }
  79. if req.SubEventId > 0 {
  80. cond.Where[_dao.Subeventid.String()] = req.SubEventId
  81. return cond, nil
  82. }
  83. var strategyId []int64
  84. isStrategyIdFirstQ := true //是否是第一次查询StrategyId标示
  85. if req.AppName != "" {
  86. var appId int32
  87. err := _app.Select(_app.Appid).Where(_app.Appname.Eq(req.AppName)).Scan(&appId)
  88. if err == nil {
  89. cond.Where[_dao.Appid.String()] = appId
  90. }
  91. //var id []int64
  92. //err := _strategy.Select(_strategy.Strategyid).Where(_strategy.Columns(_strategy.Appid).Eq(_app.Select(_app.Appid).Where(_app.Appname.Eq(req.AppName)))).
  93. // Scan(&id)
  94. //if err != nil {
  95. // return nil, err
  96. //}
  97. //strategyId = removeSingleStrategyId(strategyId, id, &isStrategyIdFirstQ)
  98. //if len(strategyId) == 0 {
  99. // return nil, nil
  100. //}
  101. }
  102. if req.AppId > 0 {
  103. cond.Where[_dao.Appid.String()] = req.AppId
  104. //var id []int64
  105. ////测点告警
  106. //err := _strategy.Select(_strategy.Strategyid).Where(_strategy.Appid.Eq(req.AppId)).Scan(&id)
  107. //if err != nil {
  108. // return nil, err
  109. //}
  110. //strategyId = removeSingleStrategyId(strategyId, id, &isStrategyIdFirstQ)
  111. //if len(strategyId) == 0 {
  112. // return nil, nil
  113. //}
  114. }
  115. if req.StartTime != "" {
  116. cond.Where[_dao.CreateAt.String()+" >= ?"] = req.StartTime
  117. }
  118. if req.EndTime != "" {
  119. cond.Where[_dao.CreateAt.String()+" <= ?"] = req.EndTime
  120. }
  121. if req.EventType > 0 {
  122. var id []int64
  123. cond.Where[_dao.Eventtype.String()] = req.EventType
  124. //测点告警
  125. if req.EventType == 1 && req.PositionName != "" {
  126. err := _relation.Select(_relation.Strategyid).Where(_relation.Columns(_relation.Mpid).Eq(_mpinfo.Select(_mpinfo.Mpid).Where(_mpinfo.Positionname.Eq(req.PositionName)))).
  127. Scan(&id)
  128. if err != nil {
  129. return nil, err
  130. }
  131. strategyId = removeSingleStrategyId(strategyId, id, &isStrategyIdFirstQ)
  132. if len(strategyId) == 0 {
  133. return nil, nil
  134. }
  135. }
  136. } else {
  137. cond.Where[_dao.Eventtype.String()+" !=?"] = 3
  138. }
  139. if len(req.AlarmLevel) > 0 {
  140. var id []int64
  141. err := _strategy.Select(_strategy.Strategyid).Where(_strategy.Alarmlevel.In(req.AlarmLevel...)).Scan(&id)
  142. if err != nil {
  143. return nil, err
  144. }
  145. strategyId = removeSingleStrategyId(strategyId, id, &isStrategyIdFirstQ)
  146. if len(strategyId) == 0 {
  147. return nil, nil
  148. }
  149. }
  150. if req.Confirm > 0 {
  151. cond.Where[_dao.Confirm.String()] = req.Confirm
  152. }
  153. if len(strategyId) > 0 {
  154. cond.Where[_dao.Strategyid.String()] = utils.RemoveRep(strategyId)
  155. }
  156. return cond, nil
  157. }
  158. // GetAlarmEvent
  159. // @Description: 查询
  160. // @receiver l
  161. // @param req
  162. // @return *dto.GetAlarmEventResp
  163. // @return error
  164. func (l *AlarmEventLogic) GetAlarmEvent(req *dto.GetAlarmEventReq) (*dto.GetAlarmEventResp, error) {
  165. _dao := l.SvcCtx.AlarmEvent
  166. _strategy := l.SvcCtx.AlarmStrategy
  167. _app := l.SvcCtx.SysApp
  168. _relation := l.SvcCtx.AlarmRelation
  169. _mpinfo := l.SvcCtx.DevMpinfo
  170. cond, err := l.getAlarmEventCond(req)
  171. if cond == nil && err == nil {
  172. return &dto.GetAlarmEventResp{}, err
  173. }
  174. if err != nil {
  175. return nil, err
  176. }
  177. var list []*model.AlarmEvent
  178. err = _dao.Base.Find(cond, &list)
  179. if err != nil {
  180. return nil, err
  181. }
  182. resp := &dto.GetAlarmEventResp{}
  183. for _, v := range list {
  184. tmp := &dto.AlarmEventDto{}
  185. alarmStrategy, err := _strategy.Where(_strategy.Strategyid.Eq(v.Strategyid)).Last()
  186. if err == nil {
  187. tmp.AlarmStrategy = *alarmStrategy
  188. tmp.AlarmLevel = alarmStrategy.Alarmlevel
  189. //err = _app.Select(_app.Nickname).Where(_app.Appid.Eq(alarmStrategy.Appid)).Scan(&tmp.AppName)
  190. _ = _mpinfo.Select(_mpinfo.Positionname).Where(_mpinfo.Columns(_mpinfo.Mpid).In(
  191. _relation.Select(_relation.Mpid).Where(_relation.Strategyid.Eq(v.Strategyid)),
  192. )).Scan(&tmp.PositionName)
  193. //if err == nil {
  194. // return nil, err
  195. //}
  196. }
  197. _app.Select(_app.Nickname).Where(_app.Appid.Eq(v.Appid)).Scan(&tmp.AppName)
  198. tmp.AlarmEvent = *v
  199. resp.List = append(resp.List, tmp)
  200. }
  201. err = _dao.Base.Count(cond, &resp.Total)
  202. if err != nil {
  203. return nil, err
  204. }
  205. return resp, err
  206. }
  207. // GetAlarmEventCount
  208. // @Description:查询满足条件的总数
  209. // @receiver l
  210. // @param where 查询条件
  211. // @return int64
  212. // @return error
  213. //func (l *AlarmEventLogic) GetAlarmEventCount(where map[string]any) (int64, error) {
  214. // cond := &dao.Condition{}
  215. // cond.Where = where
  216. // var count int64
  217. // err := l.SvcCtx.AlarmEvent.Base.Count(cond, &count)
  218. // if err != nil {
  219. // return 0, err
  220. // }
  221. // return count, err
  222. //}
  223. // SetAlarmEvent
  224. // @Description: 设置告警事件
  225. // @receiver l
  226. // @param id 告警ID
  227. // @param confirmTime 确认时间
  228. // @param result 处理结果
  229. // @return error
  230. func (l *AlarmEventLogic) SetAlarmEvent(id int32, confirmTime time.Time, result string) error {
  231. do := l.SvcCtx.AlarmEvent
  232. var in model.AlarmEvent
  233. in.ID = id
  234. in.Confirm = 2
  235. in.Confirmtime = confirmTime
  236. in.Result = result
  237. _, err := do.Updates(in)
  238. return err
  239. }