statisticsLogic.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package logic
  2. import (
  3. "fmt"
  4. "git.rtzhtech.cn/iss/public-lib/dao"
  5. "git.rtzhtech.cn/iss/public-lib/dto"
  6. "git.rtzhtech.cn/iss/public-lib/model"
  7. "git.rtzhtech.cn/iss/public-lib/svc"
  8. "time"
  9. )
  10. // NewStatisticsLogic
  11. // @函数名:NewStatisticsLogic
  12. // @函数功能描述: 初始化统计数据
  13. // @返回值:*Statistics
  14. //
  15. func NewStatisticsLogic() *Statistics {
  16. return &Statistics{
  17. SvcCtx: svc.SvcCtx,
  18. }
  19. }
  20. type Statistics struct {
  21. SvcCtx *svc.ServiceContext
  22. }
  23. // GetStatisticInfo
  24. // @函数名:GetStatisticInfo
  25. // @函数功能描述: 获取统计信息
  26. // @对象名:c
  27. // @参数定义:req
  28. // @返回值:*dto.GetStatisticInfoResponse
  29. // @返回值:error
  30. //
  31. func (c *Statistics) GetStatisticInfo(req *dto.GetStatisticInfoRequest) (*dto.GetStatisticInfoResponse, error) {
  32. var (
  33. resp = new(dto.GetStatisticInfoResponse)
  34. )
  35. deviceCond := new(dao.Condition)
  36. devicewhere := map[string]any{}
  37. if req.AppId != 0 {
  38. devicewhere[c.SvcCtx.DevDevinfo.Appid.String()] = req.AppId
  39. }
  40. deviceCond.Where = devicewhere
  41. //设备总数、在线、离线
  42. err := c.SvcCtx.DevDevinfo.Base.Count(deviceCond, &resp.DeviceCount)
  43. if err != nil {
  44. return resp, err
  45. }
  46. if resp.DeviceCount != 0 {
  47. deviceCond.Where[c.SvcCtx.DevDevinfo.Online.String()] = 2
  48. c.SvcCtx.DevDevinfo.Base.Count(deviceCond, &resp.DeviceOnlineCount)
  49. resp.DeviceDownCount = resp.DeviceCount - resp.DeviceOnlineCount
  50. }
  51. //总告警数、未处理告警数
  52. alarmCond := new(dao.Condition)
  53. alarmWhere := map[string]any{}
  54. var ids []int64
  55. if req.AppId != 0 {
  56. strategyidCond := new(dao.Condition)
  57. strategyidWhere := map[string]any{}
  58. strategyidWhere[c.SvcCtx.AlarmStrategy.Appid.String()] = req.AppId
  59. strategyidCond.Where = strategyidWhere
  60. strategyidCond.Fields = []string{"strategyid"}
  61. var md []*model.AlarmStrategy
  62. err := c.SvcCtx.AlarmStrategy.Base.Find(strategyidCond, &md)
  63. if err != nil {
  64. return resp, err
  65. }
  66. for _, v := range md {
  67. ids = append(ids, v.Strategyid)
  68. }
  69. }
  70. if len(ids) > 0 {
  71. alarmWhere["strategyid"] = ids
  72. }
  73. alarmCond.Where = alarmWhere
  74. err = c.SvcCtx.AlarmEvent.Base.Count(alarmCond, &resp.AlarmCount)
  75. if err != nil {
  76. return resp, err
  77. }
  78. if resp.AlarmCount != 0 {
  79. alarmCond.Where["confirm"] = 1
  80. err = c.SvcCtx.AlarmEvent.Base.Count(alarmCond, &resp.UntreatedAlarmCount)
  81. if err != nil {
  82. return resp, err
  83. }
  84. }
  85. //联动事件
  86. linkCond := new(dao.Condition)
  87. linkWhere := map[string]any{}
  88. var linkIds []int64
  89. if req.AppId != 0 {
  90. linkstrateCond := dao.Condition{
  91. Where: map[string]any{
  92. "appid": req.AppId,
  93. },
  94. }
  95. linkstrateCond.Fields = []string{"strategyid"}
  96. var md []*model.LinkStrategy
  97. err = c.SvcCtx.LinkStrategy.Base.Find(&linkstrateCond, &md)
  98. if err != nil {
  99. return resp, err
  100. }
  101. for _, val := range md {
  102. linkIds = append(linkIds, val.Strategyid)
  103. }
  104. }
  105. if len(linkIds) > 0 {
  106. linkWhere["strategyid"] = linkIds
  107. }
  108. linkCond.Where = linkWhere
  109. err = c.SvcCtx.LinkEvent.Base.Count(linkCond, &resp.LinkCount)
  110. if err != nil {
  111. return resp, err
  112. }
  113. if resp.LinkCount > 0 {
  114. linkCond.Where["create_at > ?"] = time.Now().Format("2006-01-02 00:00:00")
  115. err = c.SvcCtx.LinkEvent.Base.Count(linkCond, &resp.NowDateLinkCount)
  116. if err != nil {
  117. return resp, err
  118. }
  119. }
  120. return resp, nil
  121. }
  122. // GetDevStatus
  123. // @函数名:GetDevStatus
  124. // @函数功能描述: 获取设备统计信息
  125. // @对象名:c
  126. // @参数定义:req
  127. // @返回值:*dto.GetDevStatusResponse
  128. // @返回值:error
  129. //
  130. func (c *Statistics) GetDevStatus(req *dto.GetDevStatusRequest) (*dto.GetDevStatusResponse, error) {
  131. var (
  132. resp = new(dto.GetDevStatusResponse)
  133. devStatusInfo []dto.DevStatusInfo
  134. )
  135. //算总数
  136. cond := dao.Condition{}
  137. if req.AppId != 0 {
  138. cond.Where = map[string]any{
  139. "appid": req.AppId,
  140. }
  141. }
  142. err := c.SvcCtx.DevDevinfo.Base.Count(&cond, &resp.Total)
  143. if err != nil {
  144. return resp, err
  145. }
  146. if req.Page < 1 {
  147. req.Page = 1
  148. }
  149. if req.Limit < 1 {
  150. req.Limit = 20
  151. }
  152. where := `1 = 1`
  153. if req.AppId != 0 {
  154. where = fmt.Sprintf(`%s%s%d`, where, ` and appid = `, req.AppId)
  155. }
  156. sql := "SELECT \ndev_info.deviceid as deviceId,\ndev_info.devicename as deviceName,\n( SELECT sys_station.`name` FROM sys_station WHERE dev_info.stationid = sys_station.stationid LIMIT 1 ) AS station_name,\n(select app.appname from sys_app as app where app.appid = dev_info.appid limit 1) AS app_name,\ndev_info.`online` as online\nFROM\n\tdev_devinfo AS dev_info \nWHERE\n\t %s ORDER BY id desc limit %d,%d;"
  157. sql = fmt.Sprintf(sql, where, (req.Page-1)*req.Limit, req.Limit)
  158. err = c.SvcCtx.DevDevinfo.Base.Raw(sql, &devStatusInfo)
  159. if err != nil {
  160. return resp, err
  161. }
  162. for key, val := range devStatusInfo {
  163. cond := dao.Condition{
  164. Where: map[string]any{
  165. "deviceid": val.DeviceId,
  166. },
  167. }
  168. var md []model.DevMpinfo
  169. err = c.SvcCtx.DevMpinfo.Base.Find(&cond, &md)
  170. if err != nil {
  171. return resp, err
  172. }
  173. for _, v := range md {
  174. var tem dto.MpinfoList
  175. tem.MpName = v.Mpname
  176. tem.MpZone = v.Zonename
  177. tem.MpPosition = v.Positionname
  178. devStatusInfo[key].Mpinfo = append(devStatusInfo[key].Mpinfo, tem)
  179. }
  180. }
  181. resp.List = devStatusInfo
  182. return resp, nil
  183. }