package com.njzscloud.supervisory.biz.controller;

import com.njzscloud.common.core.utils.R;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.biz.pojo.entity.TruckLocationTrackEntity;
import com.njzscloud.supervisory.biz.service.TruckLocationTrackService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 车辆定位数据
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/truck_location_track")
@RequiredArgsConstructor
public class TruckLocationTrackController {

    private final TruckLocationTrackService truckLocationTrackService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public R<?> add(@RequestBody TruckLocationTrackEntity truckLocationTrackEntity) {
        truckLocationTrackService.add(truckLocationTrackEntity);
        return R.success();
    }

    /**
     * 修改
     */
    @PostMapping("/modify")
    public R<?> modify(@RequestBody TruckLocationTrackEntity truckLocationTrackEntity) {
        truckLocationTrackService.modify(truckLocationTrackEntity);
        return R.success();
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    public R<?> del(@RequestBody List<Long> ids) {
        truckLocationTrackService.del(ids);
        return R.success();
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    public R<TruckLocationTrackEntity> detail(@RequestParam Long id) {
        return R.success(truckLocationTrackService.detail(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/paging")
    public R<PageResult<TruckLocationTrackEntity>> paging(PageParam pageParam, TruckLocationTrackEntity truckLocationTrackEntity) {
        return R.success(truckLocationTrackService.paging(pageParam, truckLocationTrackEntity));
    }

    /**
     * 历史记录
     */
    @GetMapping("/history")
    public SseEmitter history(@RequestParam("orderId") Long orderId,
                              @RequestParam(value = "startTime", required = false) LocalDateTime startTime,
                              @RequestParam(value = "endTime", required = false) LocalDateTime endTime,
                              @RequestParam(value = "speed", defaultValue = "1") Integer speed
    ) {
        if (speed <= 0) {
            speed = 1;
        } else if (speed > 60) {
            speed = 60;
        }
        return truckLocationTrackService.history(orderId, startTime, endTime, speed);
    }

    /**
     * 实时数据
     */
    @GetMapping("/realtime")
    public SseEmitter realtime(@RequestParam("orderId") Long orderId) {
        return truckLocationTrackService.realtime(orderId);
    }


}
