package service

import (
	"iot_manager_service/app/device/dao"
	"iot_manager_service/app/operation/edge_service"
	"iot_manager_service/app/operation/model"
	"strconv"
)

// CapturePeriodsService 时段统计
var CapturePeriodsService = new(capturePeriodsService)

type capturePeriodsService struct{}

func (s capturePeriodsService) GetPeriodsList(tenantId string, req model.RequestCaptureFilter) (interface{}, error) {
	unit := dao.CaptureUnit{
		TenantId: tenantId,
		ID:       req.CaptureId,
	}
	unit.GetDevice()
	var codes []string
	codes = append(codes, unit.CaptureSn)
	forCaptureIts := edge_service.ForCaptureIts{}
	forData, err := forCaptureIts.VehicleHourTotal(edge_service.ForCaptureItsReq{
		Codes: codes,
		Start: req.StartTime,
		End:   req.EndTime,
	})
	if err != nil {
		return nil, err
	}
	var hours []string
	for i := 0; i < 24; i++ {
		ii := strconv.Itoa(i)
		if len(ii) < 2 {
			ii = "0" + ii
		}
		hours = append(hours, ii)
	}
	datas := make(map[string]int)
	for _, datum := range forData {
		timeStr := datum.Time
		hour := timeStr[11:13]
		_, ok := datas[(hour)]
		if ok {
			datas[(hour)] += datum.Total
		} else {
			datas[(hour)] = datum.Total
		}
	}
	var list []model.ResponseCapturePeriods
	for _, hour := range hours {
		list = append(list, model.ResponseCapturePeriods{Hours: hour, CountT: datas[hour]})
	}
	return list, nil
}
