package com.qaup.web.controller.system;

import java.util.Date;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.qaup.common.annotation.Log;
import com.qaup.common.core.controller.BaseController;
import com.qaup.common.core.domain.AjaxResult;
import com.qaup.common.enums.BusinessType;
import com.qaup.system.domain.SysVehicleLocation;
import com.qaup.system.domain.vo.VehicleTrajectoryVO;
import com.qaup.system.domain.vo.VehicleLocationPlaybackVO;
import com.qaup.system.domain.vo.TrajectoryStatistics;
import com.qaup.system.domain.dto.TrajectoryQueryRequest;
import com.qaup.system.service.ISysVehicleLocationService;
import com.qaup.common.utils.poi.ExcelUtil;
import com.qaup.common.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 车辆运动信息Controller
 * 
 * @author qaup
 * @date 2025-01-16
 */
@Tag(name = "车辆运动信息管理")
@RestController
@RequestMapping("/system/vehicle_location")
public class SysVehicleLocationController extends BaseController
{
    @Autowired
    private ISysVehicleLocationService sysVehicleLocationService;

    /**
     * 查询车辆运动信息列表
     */
    @Operation(summary = "查询车辆运动信息列表", description = "支持按车辆ID、车牌号、车辆类型、品牌、所属单位、数据质量、时间段等条件查询。车牌号、车辆类型等基础信息通过关联sys_vehicle_info表获取")
    @GetMapping("/list")
    public TableDataInfo list(SysVehicleLocation sysVehicleLocation)
    {
        startPage();
        List<SysVehicleLocation> list = sysVehicleLocationService.selectSysVehicleLocationList(sysVehicleLocation);
        return getDataTable(list);
    }

    /**
     * 导出车辆运动信息列表
     */
    @Operation(summary = "导出车辆运动信息列表", description = "导出包含关联的车辆基础信息和位置数据的Excel文件")
    @Log(title = "车辆运动信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysVehicleLocation sysVehicleLocation)
    {
        List<SysVehicleLocation> list = sysVehicleLocationService.selectSysVehicleLocationList(sysVehicleLocation);
        ExcelUtil<SysVehicleLocation> util = new ExcelUtil<SysVehicleLocation>(SysVehicleLocation.class);
        util.exportExcel(response, list, "车辆运动信息数据");
    }

    /**
     * 根据车辆ID查询车辆运动信息列表
     */
    @Operation(summary = "根据车辆ID查询车辆运动信息列表", description = "查询指定车辆的所有位置记录，包含关联的车辆基础信息")
    @Parameter(name = "vehicleId", description = "车辆ID", required = true)
    @GetMapping("/vehicle/{vehicleId}")
    public AjaxResult getByVehicleId(@PathVariable("vehicleId") Long vehicleId)
    {
        List<SysVehicleLocation> list = sysVehicleLocationService.selectSysVehicleLocationByVehicleId(vehicleId);
        return success(list);
    }

    /**
     * 根据车牌号查询车辆运动信息列表
     */
    @Operation(summary = "根据车牌号查询车辆运动信息列表", description = "通过车牌号关联查询车辆的所有位置记录")
    @Parameter(name = "licensePlate", description = "车牌号", required = true)
    @GetMapping("/plate/{licensePlate}")
    public AjaxResult getByLicensePlate(@PathVariable("licensePlate") String licensePlate)
    {
        List<SysVehicleLocation> list = sysVehicleLocationService.selectSysVehicleLocationByLicensePlate(licensePlate);
        return success(list);
    }

    /**
     * 根据车辆ID查询车辆最新位置信息
     */
    @Operation(summary = "根据车辆ID查询车辆最新位置信息", description = "获取指定车辆的最新位置记录，包含关联的车辆基础信息")
    @Parameter(name = "vehicleId", description = "车辆ID", required = true)
    @GetMapping("/latest/vehicle/{vehicleId}")
    public AjaxResult getLatestByVehicleId(@PathVariable("vehicleId") Long vehicleId)
    {
        SysVehicleLocation sysVehicleLocation = sysVehicleLocationService.selectLatestSysVehicleLocationByVehicleId(vehicleId);
        return success(sysVehicleLocation);
    }

    /**
     * 根据车牌号查询车辆最新位置信息
     */
    @Operation(summary = "根据车牌号查询车辆最新位置信息", description = "通过车牌号关联查询车辆的最新位置记录")
    @Parameter(name = "licensePlate", description = "车牌号", required = true)
    @GetMapping("/latest/plate/{licensePlate}")
    public AjaxResult getLatestByLicensePlate(@PathVariable("licensePlate") String licensePlate)
    {
        SysVehicleLocation sysVehicleLocation = sysVehicleLocationService.selectLatestSysVehicleLocationByLicensePlate(licensePlate);
        return success(sysVehicleLocation);
    }

    /**
     * 车辆轨迹查询接口
     */
    @Operation(summary = "车辆轨迹查询", description = "查询指定车辆在指定时间段内的运行轨迹，支持数据简化和采样优化")
    @GetMapping("/trajectory/{vehicleId}")
    public AjaxResult getVehicleTrajectory(
            @PathVariable("vehicleId") Long vehicleId,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
            @RequestParam(defaultValue = "false") Boolean simplified,
            @RequestParam(defaultValue = "1000") Integer maxPoints)
    {
        VehicleTrajectoryVO trajectory = sysVehicleLocationService
            .getVehicleTrajectory(vehicleId, startTime, endTime, simplified, maxPoints);
        return success(trajectory);
    }

    /**
     * 多车辆轨迹查询接口
     */
    @Operation(summary = "多车辆轨迹查询", description = "批量查询多个车辆的运行轨迹，支持复杂查询条件")
    @PostMapping("/trajectory/batch")
    public AjaxResult getBatchVehicleTrajectory(@Valid @RequestBody TrajectoryQueryRequest request)
    {
        List<VehicleTrajectoryVO> trajectories = sysVehicleLocationService
            .getBatchVehicleTrajectory(request);
        return success(trajectories);
    }

    /**
     * 轨迹回放数据接口
     */
    @Operation(summary = "轨迹回放数据查询", description = "获取用于轨迹回放的数据，按指定时间间隔采样，包含回放所需的序列和时间信息")
    @GetMapping("/trajectory/{vehicleId}/playback")
    public AjaxResult getTrajectoryPlaybackData(
            @PathVariable("vehicleId") Long vehicleId,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
            @RequestParam(defaultValue = "10") Integer intervalSeconds)
    {
        List<VehicleLocationPlaybackVO> playbackData = sysVehicleLocationService
            .getTrajectoryPlaybackData(vehicleId, startTime, endTime, intervalSeconds);
        return success(playbackData);
    }

    /**
     * 区域内轨迹查询接口
     */
    @Operation(summary = "区域内轨迹查询", description = "查询车辆在指定区域内的运行轨迹")
    @GetMapping("/trajectory/{vehicleId}/area")
    public AjaxResult getVehicleTrajectoryInArea(
            @PathVariable("vehicleId") Long vehicleId,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
            @RequestParam String areaWkt)
    {
        VehicleTrajectoryVO trajectory = sysVehicleLocationService
            .getVehicleTrajectoryInArea(vehicleId, startTime, endTime, areaWkt);
        return success(trajectory);
    }

    /**
     * 轨迹统计信息查询接口
     */
    @Operation(summary = "轨迹统计信息查询", description = "获取车辆轨迹的统计信息，如总距离、平均速度、行驶时长等")
    @GetMapping("/trajectory/{vehicleId}/statistics")
    public AjaxResult getTrajectoryStatistics(
            @PathVariable("vehicleId") Long vehicleId,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime)
    {
        TrajectoryStatistics statistics = sysVehicleLocationService
            .getTrajectoryStatistics(vehicleId, startTime, endTime);
        return success(statistics);
    }
}