captureAreaService.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. "sort"
  8. )
  9. // 归属地统计
  10. var CaptureAreaService = new(captureAreaService)
  11. type captureAreaService struct{}
  12. type tmpCaptureArea struct {
  13. Name string `json:"name"`
  14. Value int `json:"value"`
  15. }
  16. type TypeList struct {
  17. Name1 string `json:"name1"`
  18. Name2 string `json:"name2"`
  19. Name3 string `json:"name3"`
  20. Name4 string `json:"name4"`
  21. Name5 string `json:"name5"`
  22. Name6 string `json:"name6"`
  23. Name7 string `json:"name7"`
  24. Name8 string `json:"name8"`
  25. Name9 string `json:"name9"`
  26. Name10 string `json:"name10"`
  27. Value1 int `json:"value1"`
  28. Value2 int `json:"value2"`
  29. Value3 int `json:"value3"`
  30. Value4 int `json:"value4"`
  31. Value5 int `json:"value5"`
  32. Value6 int `json:"value6"`
  33. Value7 int `json:"value7"`
  34. Value8 int `json:"value8"`
  35. Value9 int `json:"value9"`
  36. Value10 int `json:"value10"`
  37. }
  38. type tmpCaptureAreas struct {
  39. tmpCaptureAreasData []tmpCaptureArea
  40. CountTime string `json:"time"`
  41. TypeList
  42. }
  43. type IntSlice []tmpCaptureArea
  44. func (p IntSlice) Len() int { return len(p) }
  45. func (p IntSlice) Less(i, j int) bool { return p[i].Value < p[j].Value }
  46. func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  47. //得到下方前10的详细数据
  48. func (s captureAreaService) GetAreaList(tenantId int, req model.RequestCaptureFilter) (interface{}, error) {
  49. //得到全部数据
  50. flag, countTimes, list, forData, err := s.getData(tenantId, req)
  51. //得到排名前10的省或城市
  52. getTop10 := s.getTop10(forData, req.Type)
  53. var topArr []string
  54. for _, area := range getTop10 {
  55. topArr = append(topArr, area.Name)
  56. }
  57. mdata := make(map[string][]tmpCaptureArea)
  58. m := map[string]int{}
  59. for _, datum := range forData {
  60. time := datum.Time
  61. if flag == 1 {
  62. time += "-01"
  63. }
  64. name := datum.City
  65. if req.Type == 1 {
  66. name = datum.Province
  67. }
  68. m[name] = datum.Total
  69. mm := tmpCaptureArea{
  70. Name: name,
  71. Value: m[name],
  72. }
  73. mdata[time] = append(mdata[time], mm)
  74. }
  75. //fmt.Printf("mdata = %v \n", mdata)
  76. //fmt.Printf("getTop10 = %v \n", getTop10)
  77. for _, countTime := range countTimes {
  78. var tmpsData []tmpCaptureArea
  79. for _, area := range getTop10 {
  80. value := mdata[countTime]
  81. val := 0
  82. for _, i := range value {
  83. if area.Name == i.Name {
  84. val += i.Value
  85. }
  86. i.Value = 0
  87. }
  88. tmpsData = append(tmpsData, tmpCaptureArea{
  89. Name: area.Name,
  90. Value: val,
  91. })
  92. }
  93. list = append(list, tmpCaptureAreas{
  94. CountTime: countTime,
  95. tmpCaptureAreasData: tmpsData,
  96. })
  97. }
  98. var list2 []tmpCaptureAreas
  99. for _, areas := range list {
  100. data := s.viewData(areas)
  101. if len(data.tmpCaptureAreasData) > 0 {
  102. list2 = append(list2, data)
  103. }
  104. }
  105. if err != nil {
  106. return nil, err
  107. }
  108. return list2, nil
  109. }
  110. //得到全部数据
  111. func (s captureAreaService) getData(tenantId int, req model.RequestCaptureFilter) (int, []string, []tmpCaptureAreas, []edge_service.ForCaptureItsData, error) {
  112. flag := 0
  113. if req.QueryType == "month" {
  114. flag = 1
  115. }
  116. unit := dao.CaptureUnit{
  117. TenantId: tenantId,
  118. ID: req.CaptureId,
  119. }
  120. unit.GetDevice()
  121. var codes []string
  122. codes = append(codes, unit.CaptureSn)
  123. var countTimes []string
  124. if flag == 1 {
  125. countTimes = common.GetTimeMonths(req.StartTime, req.EndTime) //时间范围 月
  126. } else {
  127. countTimes = common.GetTimeDays(req.StartTime, req.EndTime) //时间范围 天
  128. }
  129. var list []tmpCaptureAreas
  130. forCaptureIts := edge_service.ForCaptureIts{}
  131. var forData []edge_service.ForCaptureItsData
  132. var err error
  133. if req.Type == 1 {
  134. //按省
  135. forData, err = forCaptureIts.Province(edge_service.ForCaptureItsReq{
  136. Codes: codes,
  137. Start: req.StartTime,
  138. End: req.EndTime,
  139. Flag: flag,
  140. })
  141. } else {
  142. //按城市
  143. forData, err = forCaptureIts.City(edge_service.ForCaptureItsReq{
  144. Codes: codes,
  145. Start: req.StartTime,
  146. End: req.EndTime,
  147. Flag: flag,
  148. })
  149. }
  150. return flag, countTimes, list, forData, err
  151. }
  152. //渲染出10个前10的数据
  153. func (s captureAreaService) viewData(tmp tmpCaptureAreas) tmpCaptureAreas {
  154. tmpNew := tmp
  155. for i, datum := range tmpNew.tmpCaptureAreasData {
  156. switch i + 1 {
  157. case 1:
  158. tmpNew.Name1 = datum.Name
  159. tmpNew.Value1 = datum.Value
  160. case 2:
  161. tmpNew.Name2 = datum.Name
  162. tmpNew.Value2 = datum.Value
  163. case 3:
  164. tmpNew.Name3 = datum.Name
  165. tmpNew.Value3 = datum.Value
  166. case 4:
  167. tmpNew.Name4 = datum.Name
  168. tmpNew.Value4 = datum.Value
  169. case 5:
  170. tmpNew.Name5 = datum.Name
  171. tmpNew.Value5 = datum.Value
  172. case 6:
  173. tmpNew.Name6 = datum.Name
  174. tmpNew.Value6 = datum.Value
  175. case 7:
  176. tmpNew.Name7 = datum.Name
  177. tmpNew.Value7 = datum.Value
  178. case 8:
  179. tmpNew.Name8 = datum.Name
  180. tmpNew.Value8 = datum.Value
  181. case 9:
  182. tmpNew.Name9 = datum.Name
  183. tmpNew.Value9 = datum.Value
  184. case 10:
  185. tmpNew.Name10 = datum.Name
  186. tmpNew.Value10 = datum.Value
  187. }
  188. }
  189. return tmpNew
  190. }
  191. //前10的地区 和 数量
  192. func (s captureAreaService) GetAreaTypeList(tenantId int, req model.RequestCaptureFilter) (interface{}, error) {
  193. //得到全部数据
  194. _, _, _, forData, err := s.getData(tenantId, req)
  195. // 得到排名前10的省或城市
  196. getTop10 := s.getTop10(forData, req.Type)
  197. var tmp tmpCaptureAreas
  198. tmp.tmpCaptureAreasData = getTop10
  199. data := s.viewData(tmp)
  200. return data, err
  201. }
  202. //得到 前10
  203. func (s captureAreaService) getTop10(data []edge_service.ForCaptureItsData, typeInt int) PairList {
  204. //fmt.Printf("typeInt = %v \n", typeInt)
  205. //fmt.Printf("data = %v \n", data)
  206. topData := make(map[string]int)
  207. for _, datum := range data {
  208. if typeInt == 1 {
  209. topData[datum.Province] += datum.Total
  210. } else {
  211. topData[datum.City] += datum.Total
  212. }
  213. }
  214. count := rankByWordCount(topData)
  215. //fmt.Printf("count = %v \n", count)
  216. if len(count) > 0 {
  217. count = count[:10]
  218. }
  219. return count
  220. }
  221. //排序功能
  222. func rankByWordCount(wordFrequencies map[string]int) PairList {
  223. pl := make(PairList, len(wordFrequencies))
  224. i := 0
  225. for k, v := range wordFrequencies {
  226. pl[i] = tmpCaptureArea{k, v}
  227. i++
  228. }
  229. //从小到大排序
  230. //sort.Sort(pl)
  231. //从大到小排序
  232. sort.Sort(sort.Reverse(pl))
  233. return pl
  234. }
  235. type PairList []tmpCaptureArea
  236. func (p PairList) Len() int { return len(p) }
  237. func (p PairList) Less(i, j int) bool { return p[i].Value < p[j].Value }
  238. func (p PairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  239. func (c captureAreaService) IsContain(items []string, item string) bool {
  240. for _, eachItem := range items {
  241. if eachItem == item {
  242. return true
  243. }
  244. }
  245. return false
  246. }