package service

import (
	"iot_manager_service/app/device/dao"
	"iot_manager_service/app/operation/edge_service"
	"iot_manager_service/app/operation/model"
	systemService "iot_manager_service/app/system/service"
	"iot_manager_service/util/common"
	"strconv"
)

// CaptureVehicleTypeExService 车型统计
var CaptureVehicleTypeExService = new(captureVehicleTypeExService)

type captureVehicleTypeExService struct{}

// GetVehicleList 得到下方前10的详细数据 list
func (s captureVehicleTypeExService) GetVehicleList(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 _, Vehicle := range getTop10 {
		topArr = append(topArr, Vehicle.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 := s.getVtypeName(datum.Vtype)

		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 _, Vehicle := range getTop10 {
			value := mdata[countTime]
			val := 0
			for _, i := range value {
				if Vehicle.Name == i.Name {
					val += i.Value
				}
				i.Value = 0
			}
			tmpsData = append(tmpsData, model.TmpCapture{
				Name:  Vehicle.Name,
				Value: val,
			})
		}
		//按日期 组装 前10名的每天或每月数据
		list = append(list, model.TmpCaptures{
			CountTime:       countTime,
			TmpCapturesData: tmpsData,
		})
	}

	//按前端要求的格式，组装数据
	var list2 []model.TmpCaptures
	for _, Vehicles := range list {
		data := model.ViewData(Vehicles)
		if len(data.TmpCapturesData) > 0 {
			list2 = append(list2, data)
		}
	}
	if err != nil {
		return nil, err
	}
	return list2, nil
}

// GetVehicleTypeList 前10的地区 和 数量 line 图表
func (s captureVehicleTypeExService) GetVehicleTypeList(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 captureVehicleTypeExService) 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
	forData, err = forCaptureIts.Vehicletypeex(edge_service.ForCaptureItsReq{
		Codes: codes,
		Start: req.StartTime,
		End:   req.EndTime,
		Flag:  flag,
	})
	return flag, forData, err
}

//得到 前10
func (s captureVehicleTypeExService) getTop10(data []edge_service.ForCaptureItsData, typeInt int) model.PairList {
	//拼全部数据
	allData := make(map[string]int)
	for _, datum := range data {
		name := s.getVtypeName(datum.Vtype)
		allData[name] += datum.Total
	}
	//对数据排序
	top10Data := model.RankByWordCount(allData)
	//取前10
	if len(top10Data) >= 10 {
		top10Data = top10Data[:10]
	}
	return top10Data
}

// getVtypeName 根据车型编号  取字典中的名称
func (s captureVehicleTypeExService) getVtypeName(vtype int) string {
	name := strconv.Itoa(vtype)
	if vtype > 34 {
		return "其它"
	}
	dict := systemService.DictService.GetCacheDict("car_model_type"+name, "car_model_type", vtype)
	return dict
}
