package com.ruoyi.web.app;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.bst.device.domain.DeviceQuery;
import com.ruoyi.bst.device.domain.DeviceVO;
import com.ruoyi.bst.device.domain.enums.DeviceStatus;
import com.ruoyi.bst.device.service.DeviceAssembler;
import com.ruoyi.bst.device.service.DeviceService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/app/device")
public class AppDeviceController extends BaseController {

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private DeviceAssembler deviceAssembler;

    @ApiOperation("获取附近可用车辆列表")
    @GetMapping("/listNearBy")
    @Anonymous
    public AjaxResult listNearBy(DeviceQuery query) {
        if (query.getRadius() == null || query.getRadius().compareTo(BigDecimal.ZERO) <= 0 || query.getRadius().compareTo(BigDecimal.valueOf(10000)) > 0) {
            return error("半径必须在0-10000之间");
        }
        if (query.getCenter() == null) {
            return error("中心坐标不能为空");
        }
        if (query.getCenter().size() != 2) {
            return error("中心坐标格式错误，必须包含经度和纬度");
        }
        query.setStatus(DeviceStatus.AVAILABLE.getCode());
        List<DeviceVO> list = deviceService.selectDeviceList(query);
        return success(list);
    }

    @ApiOperation("获取可用车辆详情")
    @GetMapping("/availableDetail")
    @Anonymous
    public AjaxResult getAvailableDetail(@RequestParam(required = false) Long id, @RequestParam(required = false) String sn) {
        DeviceVO device = deviceService.selectAvaliableDevice(id, sn);
        return success(device);
    }

}
