package com.enterprise.manage.modules.asset.controller;

import com.enterprise.manage.common.base.BaseController;
import com.enterprise.manage.common.web.request.PageDomain;
import com.enterprise.manage.common.web.response.Result;
import com.enterprise.manage.common.web.response.module.ResultTable;
import com.enterprise.manage.modules.asset.domain.Vehicle;
import com.enterprise.manage.modules.asset.service.IVehicleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 车辆信息 Controller
 *
 * @author QingLi Wang
 * @version 1.0.0
 * 2023-11-28 10:01
 */
@RestController
@RequestMapping("/asset/vehicle/v1")
public class VehicleController extends BaseController {

    private IVehicleService vehicleService;

    @Autowired
    public VehicleController(IVehicleService vehicleService) {
        this.vehicleService = vehicleService;
    }

    @GetMapping
    @PreAuthorize("hasPermission('','asset:vehicle:list')")
    public ResultTable list(PageDomain pageDomain, Vehicle param) {
        PageInfo<Vehicle> vehiclePage = vehicleService.page(pageDomain, param);
        return pageTable(vehiclePage.getList(), vehiclePage.getTotal());
    }

    @GetMapping("{id}")
    @PreAuthorize("hasPermission('','asset:vehicle:list')")
    public Result get(@PathVariable Long id) {
        return success(vehicleService.queryById(id));
    }

    @PostMapping
    @PreAuthorize("hasPermission('','asset:vehicle:add')")
    public Result add(Vehicle vehicle) {
        return decide(vehicleService.save(vehicle));
    }

    @PutMapping
    @PreAuthorize("hasPermission('','asset:vehicle:edit')")
    public Result edit(Vehicle vehicle) {
        return decide(vehicleService.update(vehicle));
    }

    @DeleteMapping("{id}")
    @PreAuthorize("hasPermission('','asset:vehicle:del')")
    public Result remove(@PathVariable Integer id) {
        return decide(vehicleService.removeById(id));
    }

}
