package cn.iocoder.yudao.module.system.controller.admin.parkingspot;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.parkingspot.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.parkingspot.ParkingSpotDO;
import cn.iocoder.yudao.module.system.service.parkingspot.ParkingSpotService;

@Tag(name = "管理后台 - 停车点")
@RestController
@RequestMapping("/system/parking-spot")
@Validated
public class ParkingSpotController {

    @Resource
    private ParkingSpotService parkingSpotService;

    @PostMapping("/create")
    @Operation(summary = "创建停车点")
    @PreAuthorize("@ss.hasPermission('system:parking-spot:create')")
    public CommonResult<Long> createParkingSpot(@Valid @RequestBody ParkingSpotSaveReqVO createReqVO) {
        return success(parkingSpotService.createParkingSpot(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新停车点")
    @PreAuthorize("@ss.hasPermission('system:parking-spot:update')")
    public CommonResult<Boolean> updateParkingSpot(@Valid @RequestBody ParkingSpotSaveReqVO updateReqVO) {
        parkingSpotService.updateParkingSpot(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得停车点")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:parking-spot:query')")
    public CommonResult<ParkingSpotRespVO> getParkingSpot(@RequestParam("id") Long id) {
        ParkingSpotDO parkingSpot = parkingSpotService.getParkingSpot(id);
        return success(BeanUtils.toBean(parkingSpot, ParkingSpotRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得停车点分页")
    @PreAuthorize("@ss.hasPermission('system:parking-spot:query')")
    public CommonResult<PageResult<ParkingSpotRespVO>> getParkingSpotPage(@Valid ParkingSpotPageReqVO pageReqVO) {
        PageResult<ParkingSpotDO> pageResult = parkingSpotService.getParkingSpotPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ParkingSpotRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出停车点 Excel")
    @PreAuthorize("@ss.hasPermission('system:parking-spot:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportParkingSpotExcel(@Valid ParkingSpotPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ParkingSpotDO> list = parkingSpotService.getParkingSpotPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "停车点.xls", "数据", ParkingSpotRespVO.class,
                        BeanUtils.toBean(list, ParkingSpotRespVO.class));
    }

}
