Explorar el Código

智慧交流-归属地统计 代码优化

sixian hace 2 años
padre
commit
c7a237bb2d
Se han modificado 1 ficheros con 45 adiciones y 31 borrados
  1. 45 31
      app/operation/service/captureAreaService.go

+ 45 - 31
app/operation/service/captureAreaService.go

@@ -7,21 +7,24 @@ import (
 	"iot_manager_service/util/common"
 )
 
-// 归属地统计
+// CaptureAreaService 归属地统计
 var CaptureAreaService = new(captureAreaService)
 
 type captureAreaService struct{}
 
-//得到下方前10的详细数据
+// GetAreaList 得到下方前10的详细数据 list
 func (s captureAreaService) GetAreaList(tenantId int, req model.RequestCaptureFilter) (interface{}, error) {
-	//得到全部数据
-	flag, countTimes, forData, err := s.getData(tenantId, req)
+	//得到边缘数据
+	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
@@ -38,10 +41,19 @@ func (s captureAreaService) GetAreaList(tenantId int, req model.RequestCaptureFi
 		}
 		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
-	//fmt.Printf("mdata = %v \n", mdata)
-	//fmt.Printf("getTop10 = %v \n", getTop10)
 	for _, countTime := range countTimes {
+		//得到总排名前10的,当天|当月 数据
 		var tmpsData []model.TmpCapture
 		for _, area := range getTop10 {
 			value := mdata[countTime]
@@ -57,11 +69,14 @@ func (s captureAreaService) GetAreaList(tenantId int, req model.RequestCaptureFi
 				Value: val,
 			})
 		}
+		//按日期 组装 前10名的每天或每月数据
 		list = append(list, model.TmpCaptures{
 			CountTime:       countTime,
 			TmpCapturesData: tmpsData,
 		})
 	}
+
+	//按前端要求的格式,组装数据
 	var list2 []model.TmpCaptures
 	for _, areas := range list {
 		data := model.ViewData(areas)
@@ -75,24 +90,30 @@ func (s captureAreaService) GetAreaList(tenantId int, req model.RequestCaptureFi
 	return list2, nil
 }
 
-//前10的地区 和 数量
+// GetAreaTypeList 前10的地区 和 数量 line 图表
 func (s captureAreaService) GetAreaTypeList(tenantId int, req model.RequestCaptureFilter) (interface{}, error) {
 	//得到全部数据
-	_, _, forData, err := s.getData(tenantId, req)
+	_, 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 int, req model.RequestCaptureFilter) (int, []string, []edge_service.ForCaptureItsData, error) {
+//得到边缘接口 时间范围数据
+func (s captureAreaService) getData(tenantId int, 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,
@@ -100,25 +121,18 @@ func (s captureAreaService) getData(tenantId int, req model.RequestCaptureFilter
 	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) //时间范围 天
-	}
+
 	forCaptureIts := edge_service.ForCaptureIts{}
 	var forData []edge_service.ForCaptureItsData
 	var err error
-	if req.Type == 1 {
-		//按省
+	if req.Type == 1 { //按省
 		forData, err = forCaptureIts.Province(edge_service.ForCaptureItsReq{
 			Codes: codes,
 			Start: req.StartTime,
 			End:   req.EndTime,
 			Flag:  flag,
 		})
-	} else {
-		//按城市
+	} else { //按城市
 		forData, err = forCaptureIts.City(edge_service.ForCaptureItsReq{
 			Codes: codes,
 			Start: req.StartTime,
@@ -126,25 +140,25 @@ func (s captureAreaService) getData(tenantId int, req model.RequestCaptureFilter
 			Flag:  flag,
 		})
 	}
-	return flag, countTimes, forData, err
+	return flag, forData, err
 }
 
 //得到 前10
 func (s captureAreaService) getTop10(data []edge_service.ForCaptureItsData, typeInt int) model.PairList {
-	//fmt.Printf("typeInt = %v \n", typeInt)
-	//fmt.Printf("data = %v \n", data)
-	topData := make(map[string]int)
+	//拼全部数据
+	allData := make(map[string]int)
 	for _, datum := range data {
 		if typeInt == 1 {
-			topData[datum.Province] += datum.Total
+			allData[datum.Province] += datum.Total
 		} else {
-			topData[datum.City] += datum.Total
+			allData[datum.City] += datum.Total
 		}
 	}
-	count := model.RankByWordCount(topData)
-	//fmt.Printf("count = %v \n", count)
-	if len(count) >= 10 {
-		count = count[:10]
+	//对数据排序
+	top10Data := model.RankByWordCount(allData)
+	//取前10
+	if len(top10Data) >= 10 {
+		top10Data = top10Data[:10]
 	}
-	return count
+	return top10Data
 }