package cn.iocoder.yudao.module.asset.controller.admin.parking;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.asset.controller.admin.parking.vo.*;
import cn.iocoder.yudao.module.asset.dal.dataobject.parking.ParkingDO;
import cn.iocoder.yudao.module.asset.service.parking.ParkingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 停车场管理 Controller
 *
 * @author 芋道源码
 */
@Tag(name = "管理后台 - 停车场管理")
@RestController
@RequestMapping("/asset/parking")
@Validated
public class ParkingController {

    @Resource
    private ParkingService parkingService;

    @PostMapping("/create")
    @Operation(summary = "创建停车场")
    @PreAuthorize("@ss.hasPermission('asset:parking:create')")
    public CommonResult<Long> createParking(@Valid @RequestBody ParkingSaveReqVO createReqVO) {
        return success(parkingService.createParking(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新停车场")
    @PreAuthorize("@ss.hasPermission('asset:parking:update')")
    public CommonResult<Boolean> updateParking(@Valid @RequestBody ParkingSaveReqVO updateReqVO) {
        parkingService.updateParking(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除停车场")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('asset:parking:delete')")
    public CommonResult<Boolean> deleteParking(@RequestParam("id") Long id) {
        parkingService.deleteParking(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得停车场")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('asset:parking:query')")
    public CommonResult<ParkingRespVO> getParking(@RequestParam("id") Long id) {
        ParkingDO parking = parkingService.getParking(id);
        return success(BeanUtils.toBean(parking, ParkingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得停车场分页")
    @PreAuthorize("@ss.hasPermission('asset:parking:query')")
    public CommonResult<PageResult<ParkingRespVO>> getParkingPage(@Valid ParkingPageReqVO pageReqVO) {
        PageResult<ParkingDO> pageResult = parkingService.getParkingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ParkingRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得停车场精简列表", description = "用于下拉选择")
    public CommonResult<List<ParkingSimpleRespVO>> getParkingSimpleList() {
        List<ParkingDO> list = parkingService.getParkingSimpleList();
        return success(BeanUtils.toBean(list, ParkingSimpleRespVO.class));
    }

}
