package com.aegis.web.controller.resource.vehicle;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.aegis.common.annotation.Log;
import com.aegis.common.core.controller.BaseController;
import com.aegis.common.core.domain.AjaxResult;
import com.aegis.common.core.page.TableDataInfo;
import com.aegis.common.enums.BusinessType;
import com.aegis.resource.vehicle.domain.Vehicle;
import com.aegis.resource.vehicle.service.IResourceVehicleService;

/**
 * 车辆Controller
 * 
 * @author aegis
 */
@RestController
@RequestMapping("/resources/vehicles")
public class ResourceVehicleController extends BaseController {

    @Autowired
    private IResourceVehicleService vehicleService;

    /**
     * 查询车辆列表
     */
    @PreAuthorize("@ss.hasPermi('resource:vehicle:list')")
    @GetMapping("/list")
    public TableDataInfo list(Vehicle vehicle) {
        startPage();
        List<Vehicle> list = vehicleService.listByCondition(vehicle);
        return getDataTable(list);
    }

    /**
     * 根据ID获取车辆详情
     */
    @PreAuthorize("@ss.hasPermi('resource:vehicle:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(vehicleService.getById(id));
    }

    /**
     * 根据队伍ID查询车辆列表
     */
    @PreAuthorize("@ss.hasPermi('resource:vehicle:list')")
    @GetMapping("/team/{teamId}")
    public AjaxResult listByTeam(@PathVariable Long teamId) {
        List<Vehicle> list = vehicleService.listByTeamId(teamId);
        return success(list);
    }

    /**
     * 新增车辆
     */
    @PreAuthorize("@ss.hasPermi('resource:vehicle:add')")
    @Log(title = "车辆管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Vehicle vehicle) {
        return toAjax(vehicleService.save(vehicle));
    }

    /**
     * 修改车辆
     */
    @PreAuthorize("@ss.hasPermi('resource:vehicle:edit')")
    @Log(title = "车辆管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Vehicle vehicle) {
        return toAjax(vehicleService.updateById(vehicle));
    }

    /**
     * 删除车辆
     */
    @PreAuthorize("@ss.hasPermi('resource:vehicle:remove')")
    @Log(title = "车辆管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        boolean removed = vehicleService.removeByIds(Arrays.asList(ids));
        return removed ? success() : error("删除失败");
    }

    /**
     * 设置车辆状态
     */
    @PreAuthorize("@ss.hasPermi('resource:vehicle:edit')")
    @Log(title = "车辆管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}/status")
    public AjaxResult updateStatus(@PathVariable Long id, @RequestBody Vehicle vehicle) {
        Vehicle existingVehicle = vehicleService.getById(id);
        if (existingVehicle == null) {
            return error("车辆不存在");
        }
        existingVehicle.setStatus(vehicle.getStatus());
        return toAjax(vehicleService.updateById(existingVehicle));
    }

    /**
     * 更新GPS定位（用于GPS设备上报）
     */
    @PostMapping("/{id}/location")
    public AjaxResult updateLocation(@PathVariable Long id, @RequestBody Map<String, Object> params) {
        BigDecimal gpsLat = params.get("gpsLat") != null ? new BigDecimal(params.get("gpsLat").toString()) : null;
        BigDecimal gpsLng = params.get("gpsLng") != null ? new BigDecimal(params.get("gpsLng").toString()) : null;
        if (gpsLat == null || gpsLng == null) {
            return error("GPS坐标不能为空");
        }
        return toAjax(vehicleService.updateVehicleLocation(id, gpsLat, gpsLng));
    }
}