package com.xindazhou.vehicle.controller.admin.vehicle;

import com.xindazhou.framework.common.pojo.CommonResult;
import com.xindazhou.framework.common.pojo.PageResult;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleArchiveChangeOwnerReqVO;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleArchiveLockReqVO;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleArchivePageReqVO;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleArchiveRespVO;
import com.xindazhou.vehicle.service.archive.VehicleArchiveService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.xindazhou.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 车辆档案 Controller
 *
 * @author xindazhou
 */
@Tag(name = "管理后台 - 车辆档案")
@RestController
@RequestMapping("/admin/vehicle/archive")
@Validated
public class VehicleArchiveController {

    @Resource
    private VehicleArchiveService vehicleArchiveService;

    @GetMapping("/page")
    @Operation(summary = "获得车辆档案分页")
    @PreAuthorize("@ss.hasPermission('vehicle:archive:query')")
    public CommonResult<PageResult<VehicleArchiveRespVO>> getVehicleArchivePage(@Valid VehicleArchivePageReqVO pageReqVO) {
        PageResult<VehicleArchiveRespVO> pageResult = vehicleArchiveService.getVehicleArchivePage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/get")
    @Operation(summary = "获得车辆档案")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('vehicle:archive:query')")
    public CommonResult<VehicleArchiveRespVO> getVehicleArchive(@RequestParam("id") Long id) {
        VehicleArchiveRespVO archive = vehicleArchiveService.getVehicleArchive(id);
        return success(archive);
    }

    @PutMapping("/changeOwner")
    @Operation(summary = "更换车主")
    @PreAuthorize("@ss.hasPermission('vehicle:archive:update')")
    public CommonResult<Boolean> changeVehicleOwner(@Valid @RequestBody VehicleArchiveChangeOwnerReqVO reqVO) {
        vehicleArchiveService.changeVehicleOwner(reqVO);
        return success(true);
    }

    @PostMapping("/activate")
    @Operation(summary = "激活车辆")
    @Parameter(name = "id", description = "车辆ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('vehicle:archive:update')")
    public CommonResult<Boolean> activateVehicle(@RequestParam("id") Long id) {
        vehicleArchiveService.activateVehicle(id);
        return success(true);
    }

    @PostMapping("/lock")
    @Operation(summary = "锁定/解锁车辆")
    @PreAuthorize("@ss.hasPermission('vehicle:archive:update')")
    public CommonResult<Boolean> lockVehicle(@Valid @RequestBody VehicleArchiveLockReqVO reqVO) {
        vehicleArchiveService.lockVehicle(reqVO);
        return success(true);
    }

}

