123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- package service
- import (
- "iot_manager_service/app/device/dao"
- "iot_manager_service/app/operation/edge_service"
- "iot_manager_service/app/operation/model"
- "iot_manager_service/util/common"
- "sort"
- )
- // 归属地统计
- var CaptureAreaService = new(captureAreaService)
- type captureAreaService struct{}
- type tmpCaptureArea struct {
- Name string `json:"name"`
- Value int `json:"value"`
- }
- type TypeList struct {
- Name1 string `json:"name1"`
- Name2 string `json:"name2"`
- Name3 string `json:"name3"`
- Name4 string `json:"name4"`
- Name5 string `json:"name5"`
- Name6 string `json:"name6"`
- Name7 string `json:"name7"`
- Name8 string `json:"name8"`
- Name9 string `json:"name9"`
- Name10 string `json:"name10"`
- Value1 int `json:"value1"`
- Value2 int `json:"value2"`
- Value3 int `json:"value3"`
- Value4 int `json:"value4"`
- Value5 int `json:"value5"`
- Value6 int `json:"value6"`
- Value7 int `json:"value7"`
- Value8 int `json:"value8"`
- Value9 int `json:"value9"`
- Value10 int `json:"value10"`
- }
- type tmpCaptureAreas struct {
- tmpCaptureAreasData []tmpCaptureArea
- CountTime string `json:"time"`
- TypeList
- }
- type IntSlice []tmpCaptureArea
- func (p IntSlice) Len() int { return len(p) }
- func (p IntSlice) Less(i, j int) bool { return p[i].Value < p[j].Value }
- func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
- //得到下方前10的详细数据
- func (s captureAreaService) GetAreaList(tenantId int, req model.RequestCaptureFilter) (interface{}, error) {
- //得到全部数据
- flag, countTimes, list, forData, err := s.getData(tenantId, req)
- //得到排名前10的省或城市
- getTop10 := s.getTop10(forData, req.Type)
- var topArr []string
- for _, area := range getTop10 {
- topArr = append(topArr, area.Name)
- }
- mdata := make(map[string][]tmpCaptureArea)
- m := map[string]int{}
- for _, datum := range forData {
- time := datum.Time
- if flag == 1 {
- time += "-01"
- }
- name := datum.City
- if req.Type == 1 {
- name = datum.Province
- }
- m[name] = datum.Total
- mm := tmpCaptureArea{
- Name: name,
- Value: m[name],
- }
- mdata[time] = append(mdata[time], mm)
- }
- //fmt.Printf("mdata = %v \n", mdata)
- //fmt.Printf("getTop10 = %v \n", getTop10)
- for _, countTime := range countTimes {
- var tmpsData []tmpCaptureArea
- for _, area := range getTop10 {
- value := mdata[countTime]
- val := 0
- for _, i := range value {
- if area.Name == i.Name {
- val += i.Value
- }
- i.Value = 0
- }
- tmpsData = append(tmpsData, tmpCaptureArea{
- Name: area.Name,
- Value: val,
- })
- }
- list = append(list, tmpCaptureAreas{
- CountTime: countTime,
- tmpCaptureAreasData: tmpsData,
- })
- }
- var list2 []tmpCaptureAreas
- for _, areas := range list {
- data := s.viewData(areas)
- if len(data.tmpCaptureAreasData) > 0 {
- list2 = append(list2, data)
- }
- }
- if err != nil {
- return nil, err
- }
- return list2, nil
- }
- //得到全部数据
- func (s captureAreaService) getData(tenantId int, req model.RequestCaptureFilter) (int, []string, []tmpCaptureAreas, []edge_service.ForCaptureItsData, error) {
- flag := 0
- if req.QueryType == "month" {
- flag = 1
- }
- unit := dao.CaptureUnit{
- TenantId: tenantId,
- ID: req.CaptureId,
- }
- unit.GetDevice()
- var codes []string
- codes = append(codes, unit.CaptureSn)
- var countTimes []string
- if flag == 1 {
- countTimes = common.GetTimeMonths(req.StartTime, req.EndTime) //时间范围 月
- } else {
- countTimes = common.GetTimeDays(req.StartTime, req.EndTime) //时间范围 天
- }
- var list []tmpCaptureAreas
- forCaptureIts := edge_service.ForCaptureIts{}
- var forData []edge_service.ForCaptureItsData
- var err error
- if req.Type == 1 {
- //按省
- forData, err = forCaptureIts.Province(edge_service.ForCaptureItsReq{
- Codes: codes,
- Start: req.StartTime,
- End: req.EndTime,
- Flag: flag,
- })
- } else {
- //按城市
- forData, err = forCaptureIts.City(edge_service.ForCaptureItsReq{
- Codes: codes,
- Start: req.StartTime,
- End: req.EndTime,
- Flag: flag,
- })
- }
- return flag, countTimes, list, forData, err
- }
- //渲染出10个前10的数据
- func (s captureAreaService) viewData(tmp tmpCaptureAreas) tmpCaptureAreas {
- tmpNew := tmp
- for i, datum := range tmpNew.tmpCaptureAreasData {
- switch i + 1 {
- case 1:
- tmpNew.Name1 = datum.Name
- tmpNew.Value1 = datum.Value
- case 2:
- tmpNew.Name2 = datum.Name
- tmpNew.Value2 = datum.Value
- case 3:
- tmpNew.Name3 = datum.Name
- tmpNew.Value3 = datum.Value
- case 4:
- tmpNew.Name4 = datum.Name
- tmpNew.Value4 = datum.Value
- case 5:
- tmpNew.Name5 = datum.Name
- tmpNew.Value5 = datum.Value
- case 6:
- tmpNew.Name6 = datum.Name
- tmpNew.Value6 = datum.Value
- case 7:
- tmpNew.Name7 = datum.Name
- tmpNew.Value7 = datum.Value
- case 8:
- tmpNew.Name8 = datum.Name
- tmpNew.Value8 = datum.Value
- case 9:
- tmpNew.Name9 = datum.Name
- tmpNew.Value9 = datum.Value
- case 10:
- tmpNew.Name10 = datum.Name
- tmpNew.Value10 = datum.Value
- }
- }
- return tmpNew
- }
- //前10的地区 和 数量
- func (s captureAreaService) GetAreaTypeList(tenantId int, req model.RequestCaptureFilter) (interface{}, error) {
- //得到全部数据
- _, _, _, forData, err := s.getData(tenantId, req)
- // 得到排名前10的省或城市
- getTop10 := s.getTop10(forData, req.Type)
- var tmp tmpCaptureAreas
- tmp.tmpCaptureAreasData = getTop10
- data := s.viewData(tmp)
- return data, err
- }
- //得到 前10
- func (s captureAreaService) getTop10(data []edge_service.ForCaptureItsData, typeInt int) PairList {
- //fmt.Printf("typeInt = %v \n", typeInt)
- //fmt.Printf("data = %v \n", data)
- topData := make(map[string]int)
- for _, datum := range data {
- if typeInt == 1 {
- topData[datum.Province] += datum.Total
- } else {
- topData[datum.City] += datum.Total
- }
- }
- count := rankByWordCount(topData)
- //fmt.Printf("count = %v \n", count)
- if len(count) > 0 {
- count = count[:10]
- }
- return count
- }
- //排序功能
- func rankByWordCount(wordFrequencies map[string]int) PairList {
- pl := make(PairList, len(wordFrequencies))
- i := 0
- for k, v := range wordFrequencies {
- pl[i] = tmpCaptureArea{k, v}
- i++
- }
- //从小到大排序
- //sort.Sort(pl)
- //从大到小排序
- sort.Sort(sort.Reverse(pl))
- return pl
- }
- type PairList []tmpCaptureArea
- func (p PairList) Len() int { return len(p) }
- func (p PairList) Less(i, j int) bool { return p[i].Value < p[j].Value }
- func (p PairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
- func (c captureAreaService) IsContain(items []string, item string) bool {
- for _, eachItem := range items {
- if eachItem == item {
- return true
- }
- }
- return false
- }
|