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" ) // CaptureAreaService 归属地统计 var CaptureAreaService = new(captureAreaService) type captureAreaService struct{} // GetAreaList 得到下方前10的详细数据 list func (s captureAreaService) GetAreaList(tenantId string, req model.RequestCaptureFilter) (interface{}, error) { //得到边缘数据 flag, 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) } //将所有省份或城市 数据组装成 map {time:[{Name:"xxx", Value:11}]} mdata := make(map[string][]model.TmpCapture) for _, datum := range forData { time := datum.Time if flag == 1 { time += "-01" } name := datum.City if req.Type == 1 { name = datum.Province } mm := model.TmpCapture{ Name: name, Value: datum.Total, } mdata[time] = append(mdata[time], mm) } //时间范围内的月份或天的明细列表 var countTimes []string if flag == 1 { countTimes = common.GetTimeMonths(req.StartTime, req.EndTime) //时间范围 月 } else { countTimes = common.GetTimeDays(req.StartTime, req.EndTime) //时间范围 天 } //每日|每月 前10的数据 组装 var list []model.TmpCaptures for _, countTime := range countTimes { //得到总排名前10的,当天|当月 数据 var tmpsData []model.TmpCapture 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, model.TmpCapture{ Name: area.Name, Value: val, }) } //按日期 组装 前10名的每天或每月数据 list = append(list, model.TmpCaptures{ CountTime: countTime, TmpCapturesData: tmpsData, }) } //按前端要求的格式,组装数据 var list2 []model.TmpCaptures for _, areas := range list { data := model.ViewData(areas) if len(data.TmpCapturesData) > 0 { list2 = append(list2, data) } } if err != nil { return nil, err } return list2, nil } // GetAreaTypeList 前10的地区 和 数量 line 图表 func (s captureAreaService) GetAreaTypeList(tenantId string, req model.RequestCaptureFilter) (interface{}, error) { //得到全部数据 _, forData, err := s.getData(tenantId, req) // 得到排名前10的省或城市 getTop10 := s.getTop10(forData, req.Type) var tmp model.TmpCaptures tmp.TmpCapturesData = getTop10 //按前端要求的格式,组装数据 data := model.ViewData(tmp) return data, err } //得到边缘接口 时间范围数据 func (s captureAreaService) getData(tenantId string, req model.RequestCaptureFilter) (int, []edge_service.ForCaptureItsData, error) { //区分按月或按日 flag := 0 if req.QueryType == "month" { flag = 1 } //查出抓拍单元sn unit := dao.CaptureUnit{ TenantId: tenantId, ID: req.CaptureId, } unit.GetDevice() var codes []string codes = append(codes, unit.CaptureSn) 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, forData, err } //得到 前10 func (s captureAreaService) getTop10(data []edge_service.ForCaptureItsData, typeInt int) model.PairList { //拼全部数据 allData := make(map[string]int) for _, datum := range data { if typeInt == 1 { allData[datum.Province] += datum.Total } else { allData[datum.City] += datum.Total } } //对数据排序 top10Data := model.RankByWordCount(allData) //取前10 if len(top10Data) >= 10 { top10Data = top10Data[:10] } return top10Data }