package com.cico.nest.lcdp.boot.core.qxsd.adapter.web;

import org.springframework.web.bind.annotation.*;
import jakarta.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 jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.cico.nest.lcdp.framework.common.pojo.PageParam;
import com.cico.nest.lcdp.framework.common.pojo.PageResult;
import com.cico.nest.lcdp.framework.common.pojo.CommonResult;
import com.cico.nest.lcdp.framework.common.util.object.BeanUtils;
import static com.cico.nest.lcdp.framework.common.pojo.CommonResult.success;

import com.cico.nest.lcdp.framework.excel.core.util.ExcelUtils;

import com.cico.nest.lcdp.framework.operatelog.core.annotations.OperateLog;
import static com.cico.nest.lcdp.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.tunnelHole.*;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.TunnelHoleV2DO;
import com.cico.nest.lcdp.boot.core.qxsd.application.service.TunnelHoleService;

@Tag(name = "管理后台 - 隧道设施上下行洞")
@RestController
@RequestMapping("/qxsd/tunnel-hole")
@Validated
public class TunnelHoleController {

    @Resource
    private TunnelHoleService tunnelHoleService;

    @PostMapping("/create")
    @Operation(summary = "创建隧道设施上下行洞")
    @PreAuthorize("@ss.hasPermission('qxsd:tunnel-hole:create')")
    public CommonResult<String> createTunnelHole(@Valid @RequestBody TunnelHoleSaveReqVO createReqVO) {
        return success(tunnelHoleService.createTunnelHole(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新隧道设施上下行洞")
    @PreAuthorize("@ss.hasPermission('qxsd:tunnel-hole:update')")
    public CommonResult<Boolean> updateTunnelHole(@Valid @RequestBody TunnelHoleSaveReqVO updateReqVO) {
        tunnelHoleService.updateTunnelHole(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除隧道设施上下行洞")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qxsd:tunnel-hole:delete')")
    public CommonResult<Boolean> deleteTunnelHole(@RequestParam("id") String id) {
        tunnelHoleService.deleteTunnelHole(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得隧道设施上下行洞")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qxsd:tunnel-hole:query')")
    public CommonResult<TunnelHoleRespVO> getTunnelHole(@RequestParam("id") String id) {
        TunnelHoleV2DO tunnelHole = tunnelHoleService.getTunnelHole(id);
        return success(BeanUtils.toBean(tunnelHole, TunnelHoleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得隧道设施上下行洞分页")
    @PreAuthorize("@ss.hasPermission('qxsd:tunnel-hole:query')")
    public CommonResult<PageResult<TunnelHoleRespVO>> getTunnelHolePage(@Valid TunnelHolePageReqVO pageReqVO) {
        PageResult<TunnelHoleV2DO> pageResult = tunnelHoleService.getTunnelHolePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TunnelHoleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出隧道设施上下行洞 Excel")
    @PreAuthorize("@ss.hasPermission('qxsd:tunnel-hole:export')")
    @OperateLog(type = EXPORT)
    public void exportTunnelHoleExcel(@Valid TunnelHolePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TunnelHoleV2DO> list = tunnelHoleService.getTunnelHolePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "隧道设施上下行洞.xls", "数据", TunnelHoleRespVO.class,
                        BeanUtils.toBean(list, TunnelHoleRespVO.class));
    }

}
