captureAreaService.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package service
  2. import (
  3. "iot_manager_service/app/device/dao"
  4. "iot_manager_service/app/operation/edge_service"
  5. "iot_manager_service/app/operation/model"
  6. "iot_manager_service/util/common"
  7. )
  8. // CaptureAreaService 归属地统计
  9. var CaptureAreaService = new(captureAreaService)
  10. type captureAreaService struct{}
  11. // GetAreaList 得到下方前10的详细数据 list
  12. func (s captureAreaService) GetAreaList(tenantId string, req model.RequestCaptureFilter) (interface{}, error) {
  13. //得到边缘数据
  14. flag, forData, err := s.getData(tenantId, req)
  15. //得到排名前10的省或城市
  16. getTop10 := s.getTop10(forData, req.Type)
  17. var topArr []string
  18. for _, area := range getTop10 {
  19. topArr = append(topArr, area.Name)
  20. }
  21. //将所有省份或城市 数据组装成 map {time:[{Name:"xxx", Value:11}]}
  22. mdata := make(map[string][]model.TmpCapture)
  23. for _, datum := range forData {
  24. time := datum.Time
  25. if flag == 1 {
  26. time += "-01"
  27. }
  28. name := datum.City
  29. if req.Type == 1 {
  30. name = datum.Province
  31. }
  32. mm := model.TmpCapture{
  33. Name: name,
  34. Value: datum.Total,
  35. }
  36. mdata[time] = append(mdata[time], mm)
  37. }
  38. //时间范围内的月份或天的明细列表
  39. var countTimes []string
  40. if flag == 1 {
  41. countTimes = common.GetTimeMonths(req.StartTime, req.EndTime) //时间范围 月
  42. } else {
  43. countTimes = common.GetTimeDays(req.StartTime, req.EndTime) //时间范围 天
  44. }
  45. //每日|每月 前10的数据 组装
  46. var list []model.TmpCaptures
  47. for _, countTime := range countTimes {
  48. //得到总排名前10的,当天|当月 数据
  49. var tmpsData []model.TmpCapture
  50. for _, area := range getTop10 {
  51. value := mdata[countTime]
  52. val := 0
  53. for _, i := range value {
  54. if area.Name == i.Name {
  55. val += i.Value
  56. }
  57. i.Value = 0
  58. }
  59. tmpsData = append(tmpsData, model.TmpCapture{
  60. Name: area.Name,
  61. Value: val,
  62. })
  63. }
  64. //按日期 组装 前10名的每天或每月数据
  65. list = append(list, model.TmpCaptures{
  66. CountTime: countTime,
  67. TmpCapturesData: tmpsData,
  68. })
  69. }
  70. //按前端要求的格式,组装数据
  71. var list2 []model.TmpCaptures
  72. for _, areas := range list {
  73. data := model.ViewData(areas)
  74. if len(data.TmpCapturesData) > 0 {
  75. list2 = append(list2, data)
  76. }
  77. }
  78. if err != nil {
  79. return nil, err
  80. }
  81. return list2, nil
  82. }
  83. // GetAreaTypeList 前10的地区 和 数量 line 图表
  84. func (s captureAreaService) GetAreaTypeList(tenantId string, req model.RequestCaptureFilter) (interface{}, error) {
  85. //得到全部数据
  86. _, forData, err := s.getData(tenantId, req)
  87. // 得到排名前10的省或城市
  88. getTop10 := s.getTop10(forData, req.Type)
  89. var tmp model.TmpCaptures
  90. tmp.TmpCapturesData = getTop10
  91. //按前端要求的格式,组装数据
  92. data := model.ViewData(tmp)
  93. return data, err
  94. }
  95. //得到边缘接口 时间范围数据
  96. func (s captureAreaService) getData(tenantId string, req model.RequestCaptureFilter) (int, []edge_service.ForCaptureItsData, error) {
  97. //区分按月或按日
  98. flag := 0
  99. if req.QueryType == "month" {
  100. flag = 1
  101. }
  102. //查出抓拍单元sn
  103. unit := dao.CaptureUnit{
  104. TenantId: tenantId,
  105. ID: req.CaptureId,
  106. }
  107. unit.GetDevice()
  108. var codes []string
  109. codes = append(codes, unit.CaptureSn)
  110. forCaptureIts := edge_service.ForCaptureIts{}
  111. var forData []edge_service.ForCaptureItsData
  112. var err error
  113. if req.Type == 1 { //按省
  114. forData, err = forCaptureIts.Province(edge_service.ForCaptureItsReq{
  115. Codes: codes,
  116. Start: req.StartTime,
  117. End: req.EndTime,
  118. Flag: flag,
  119. })
  120. } else { //按城市
  121. forData, err = forCaptureIts.City(edge_service.ForCaptureItsReq{
  122. Codes: codes,
  123. Start: req.StartTime,
  124. End: req.EndTime,
  125. Flag: flag,
  126. })
  127. }
  128. return flag, forData, err
  129. }
  130. //得到 前10
  131. func (s captureAreaService) getTop10(data []edge_service.ForCaptureItsData, typeInt int) model.PairList {
  132. //拼全部数据
  133. allData := make(map[string]int)
  134. for _, datum := range data {
  135. if typeInt == 1 {
  136. allData[datum.Province] += datum.Total
  137. } else {
  138. allData[datum.City] += datum.Total
  139. }
  140. }
  141. //对数据排序
  142. top10Data := model.RankByWordCount(allData)
  143. //取前10
  144. if len(top10Data) >= 10 {
  145. top10Data = top10Data[:10]
  146. }
  147. return top10Data
  148. }