captureSpeedService.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package service
  2. import (
  3. "iot_manager_service/app/device/dao"
  4. operationDao "iot_manager_service/app/operation/dao"
  5. "iot_manager_service/app/operation/edge_service"
  6. "iot_manager_service/app/operation/model"
  7. "iot_manager_service/util/common"
  8. "math"
  9. "strconv"
  10. )
  11. var CaptureSpeedService = new(captureSpeedService)
  12. type captureSpeedService struct{}
  13. // GetList 车速统计
  14. func (s captureSpeedService) GetList(tenantId string, req model.RequestCaptureFilter) (map[string]interface{}, error) {
  15. //sectionSpeed 区间车速数据
  16. data := make(map[string]interface{})
  17. speed, err := s.getSectionSpeed(tenantId, req)
  18. data["sectionSpeed"] = s.viewData(req, speed, "sectionSpeed")
  19. //overSpeed //超速数据
  20. speed, err = s.getOverSpeed(tenantId, req)
  21. data["overSpeed"] = s.viewData(req, speed, "overSpeed")
  22. return data, err
  23. }
  24. // 区间车速数据
  25. func (s captureSpeedService) getSectionSpeed(tenantId string, req model.RequestCaptureFilter) ([]edge_service.ForCaptureItsData, error) {
  26. //区分按月或按日
  27. flag := 0
  28. if req.QueryType == "month" {
  29. flag = 1
  30. }
  31. //查出抓拍单元sn
  32. unit := dao.CaptureUnit{
  33. TenantId: tenantId,
  34. ID: req.CaptureId,
  35. }
  36. unit.GetDevice()
  37. var codes []string
  38. codes = append(codes, unit.CaptureSn)
  39. forCaptureIts := edge_service.ForCaptureIts{}
  40. speedTotal, err := forCaptureIts.VehicleSpeedTotal(edge_service.ForCaptureItsReq{
  41. Codes: codes,
  42. Start: req.StartTime,
  43. End: req.EndTime,
  44. Flag: flag,
  45. })
  46. if err != nil {
  47. return nil, err
  48. }
  49. return speedTotal, nil
  50. }
  51. // 超速数据
  52. func (s captureSpeedService) getOverSpeed(tenantId string, req model.RequestCaptureFilter) ([]edge_service.ForCaptureItsData, error) {
  53. //区分按月或按日
  54. flag := 0
  55. if req.QueryType == "month" {
  56. flag = 1
  57. }
  58. //查出抓拍单元sn
  59. unit := dao.CaptureUnit{
  60. TenantId: tenantId,
  61. ID: req.CaptureId,
  62. }
  63. unit.GetDevice()
  64. var codes []string
  65. codes = append(codes, unit.CaptureSn)
  66. forCaptureIts := edge_service.ForCaptureIts{}
  67. speedTotal, err := forCaptureIts.VehicleOverSpeedTotal(edge_service.ForCaptureItsReq{
  68. Codes: codes,
  69. Start: req.StartTime,
  70. End: req.EndTime,
  71. Flag: flag,
  72. })
  73. if err != nil {
  74. return nil, err
  75. }
  76. return speedTotal, nil
  77. }
  78. // 前端数据格式渲染
  79. func (s captureSpeedService) viewData(req model.RequestCaptureFilter, speeds []edge_service.ForCaptureItsData, cate string) []model.ResponseCaptureSpeed {
  80. //区分按月或按日
  81. flag := 0
  82. if req.QueryType == "month" {
  83. flag = 1
  84. }
  85. data := make(map[string]int)
  86. for _, speed := range speeds {
  87. time := speed.Time
  88. if flag == 1 {
  89. time += "-01"
  90. }
  91. sflag := strconv.Itoa(speed.Sflag)
  92. data[time+"--"+sflag] += speed.Total
  93. }
  94. //时间范围内的月份或天的明细列表
  95. var countTimes []string
  96. if flag == 1 {
  97. countTimes = common.GetTimeMonths(req.StartTime, req.EndTime) //时间范围 月
  98. } else {
  99. countTimes = common.GetTimeDays(req.StartTime, req.EndTime) //时间范围 天
  100. }
  101. var list []model.ResponseCaptureSpeed
  102. for _, time := range countTimes {
  103. if cate == "sectionSpeed" {
  104. list = append(list, model.ResponseCaptureSpeed{
  105. CountTime: time,
  106. V1: data[time+"--0"],
  107. V2: data[time+"--1"],
  108. V3: data[time+"--2"],
  109. V4: data[time+"--3"],
  110. V5: data[time+"--4"],
  111. V6: data[time+"--5"],
  112. V7: data[time+"--6"],
  113. })
  114. } else {
  115. list = append(list, model.ResponseCaptureSpeed{
  116. CountTime: time,
  117. V11: data[time+"--100"],
  118. V13: data[time+"--101"],
  119. V14: data[time+"--102"],
  120. V15: data[time+"--103"],
  121. V16: data[time+"--104"],
  122. })
  123. }
  124. }
  125. return list
  126. }
  127. // SyncOverSpeedRecord 同步超速车牌记录
  128. func (s captureSpeedService) SyncOverSpeedRecord(start, end string) error {
  129. speed := operationDao.CaptureOverSpeed{}
  130. maxId, maxTime := speed.GetMaxIdAndUpDateTime()
  131. forCaptureIts := edge_service.ForCaptureIts{}
  132. record, err := forCaptureIts.SyncOverSpeedRecord(maxId, maxTime, start, end)
  133. if err != nil {
  134. return err
  135. }
  136. var list []operationDao.CaptureOverSpeed
  137. for _, data := range record {
  138. list = append(list, operationDao.CaptureOverSpeed{
  139. ID: data.ID,
  140. CaptureSn: data.Code,
  141. Time: common.Time(common.TimeStringToGoTime(data.Time)),
  142. Vtype: data.Vtype,
  143. Plate: data.Plate,
  144. Speed: data.Speed,
  145. Sflag: data.Flag,
  146. })
  147. }
  148. return speed.BatchCreate(list)
  149. }
  150. func (s captureSpeedService) GetOverSpeedRecord(req model.RequestCaptureOverSpeed) (*model.ResponseCaptureOverSpeedRecord, error) {
  151. //查出抓拍单元sn
  152. unit := dao.CaptureUnit{
  153. ID: req.CaptureId,
  154. }
  155. if req.Size <= 0 {
  156. req.Size = 10
  157. }
  158. unit.GetDevice()
  159. speed := operationDao.CaptureOverSpeed{}
  160. records, total, err := speed.Gets(unit.CaptureSn, req)
  161. if err != nil {
  162. return nil, err
  163. }
  164. pages := math.Ceil(float64(total) / float64(req.Size))
  165. return &model.ResponseCaptureOverSpeedRecord{
  166. Records: records,
  167. Current: req.Current,
  168. Size: req.Size,
  169. Total: total,
  170. Pages: int(pages),
  171. }, nil
  172. }