package controller

import (
	"github.com/gin-gonic/gin"
	"iot_manager_service/app/middleware"
	"iot_manager_service/app/operation/model"
	"iot_manager_service/app/operation/service"
	"iot_manager_service/util/common"
	"net/http"
	"strconv"
)

// Capture 抓拍单元
var Capture = new(captureCtl)

type captureCtl struct{}

// CountList 取车流量 月日
func (c captureCtl) CountList(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	var req model.RequestCaptureFilter
	err := ctx.ShouldBindQuery(&req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	var records interface{}
	err = nil
	if req.QueryType == "day" {
		records, err = service.CaptureService.GetDayVehicleTotal(claims.TenantId, req)
	} else {
		records, err = service.CaptureService.GetMonthVehicleTotal(claims.TenantId, req)
	}
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, records))
}

// SuggestSpeed 取抓拍单元 方位
func (c captureCtl) SuggestSpeed(ctx *gin.Context) {
	captureId, err := strconv.Atoi(ctx.Query("captureId"))
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	var records interface{}
	records, err = service.CaptureService.GetSuggestSpeed(claims.TenantId, captureId)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, records))
}

// AreaList 归属地统计 list
func (c captureCtl) AreaList(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	var req model.RequestCaptureFilter
	err := ctx.ShouldBindQuery(&req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	var records interface{}
	records, err = service.CaptureAreaService.GetAreaList(claims.TenantId, req)

	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, records))
}

// AreaTypeList 归属地统计 line图表
func (c captureCtl) AreaTypeList(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	var req model.RequestCaptureFilter
	err := ctx.ShouldBindQuery(&req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	var records interface{}
	records, err = service.CaptureAreaService.GetAreaTypeList(claims.TenantId, req)

	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, records))
}

// VehicleTypeEx 车型 列表 查询
func (c captureCtl) VehicleTypeEx(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	var req model.RequestCaptureFilter
	err := ctx.ShouldBindQuery(&req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	var records interface{}
	err = nil
	records, err = service.CaptureVehicleTypeExService.GetVehicleList(claims.TenantId, req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, records))
}

// VehicleTypeList 车型 line线图数据
func (c captureCtl) VehicleTypeList(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	var req model.RequestCaptureFilter
	err := ctx.ShouldBindQuery(&req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	var records interface{}
	err = nil
	records, err = service.CaptureVehicleTypeExService.GetVehicleTypeList(claims.TenantId, req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, records))
}

// SpeedList 车速统计
func (c captureCtl) SpeedList(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	var req model.RequestCaptureFilter
	err := ctx.ShouldBindQuery(&req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	var records interface{}
	records, err = service.CaptureSpeedService.GetList(claims.TenantId, req)

	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, records))
}

// OverSpeedRecord 超速明细
func (c captureCtl) OverSpeedRecord(ctx *gin.Context) {
	var req model.RequestCaptureOverSpeed
	_ = ctx.ShouldBindQuery(&req)
	var records interface{}
	records, err := service.CaptureSpeedService.GetOverSpeedRecord(req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, records))
}

// OverSpeedRecordSync 同步超速明细记录
func (c captureCtl) OverSpeedRecordSync(ctx *gin.Context) {
	var req model.RequestCaptureFilter
	_ = ctx.ShouldBindQuery(&req)
	go func() {
		service.CaptureSpeedService.SyncOverSpeedRecord(req.StartTime, req.EndTime)
	}()
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, nil))
}

// PeriodsList 时段统计
func (c captureCtl) PeriodsList(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	var req model.RequestCaptureFilter
	err := ctx.ShouldBindQuery(&req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	records, err := service.CapturePeriodsService.GetPeriodsList(claims.TenantId, req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, records))
}

// CarrecordCount 货车统计
func (c captureCtl) CarrecordCount(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	var req model.RequestCarRecordCountFilter
	err := ctx.ShouldBindQuery(&req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	var records interface{}
	err = nil
	records, err = service.CaptureCarRecordService.GetCarRecordCount(claims.TenantId, req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, records))
}

// ReportList 报表统计
func (c captureCtl) ReportList(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	var req model.RequestCaptureReportFilter
	err := ctx.ShouldBindQuery(&req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	var records interface{}
	err = nil
	records, err = service.CaptureReportService.GetList(claims.TenantId, req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, records))
}
