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.constraints.*;
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.tunnelDevice.*;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.TunnelDeviceDO;
import com.cico.nest.lcdp.boot.core.qxsd.application.service.TunnelDeviceService;

@Tag(name = "管理后台 - 隧道设备信息")
@RestController
@RequestMapping("/qxsd/device")
@Validated
public class TunnelDeviceController {

    @Resource
    private TunnelDeviceService tunnelDeviceService;

    @PostMapping("/create")
    @Operation(summary = "创建隧道设备信息")
    @PreAuthorize("@ss.hasPermission('qxsd:tunnel-device:create')")
    public CommonResult<String> createTunnelDevice(@Valid @RequestBody TunnelDeviceSaveReqVO createReqVO) {
        return success(tunnelDeviceService.createTunnelDevice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新隧道设备信息")
    @PreAuthorize("@ss.hasPermission('qxsd:tunnel-device:update')")
    public CommonResult<Boolean> updateTunnelDevice(@Valid @RequestBody TunnelDeviceSaveReqVO updateReqVO) {
        tunnelDeviceService.updateTunnelDevice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除隧道设备信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qxsd:tunnel-device:delete')")
    public CommonResult<Boolean> deleteTunnelDevice(@RequestParam("id") String id) {
        tunnelDeviceService.deleteTunnelDevice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得隧道设备信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qxsd:tunnel-device:query')")
    public CommonResult<TunnelDeviceRespVO> getTunnelDevice(@RequestParam("id") String id) {
        TunnelDeviceDO tunnelDevice = tunnelDeviceService.getTunnelDevice(id);
        return success(BeanUtils.toBean(tunnelDevice, TunnelDeviceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得隧道设备信息分页")
    @PreAuthorize("@ss.hasPermission('qxsd:tunnel-device:query')")
    public CommonResult<PageResult<TunnelDeviceRespVO>> getTunnelDevicePage(@Valid TunnelDevicePageReqVO pageReqVO) {
        PageResult<TunnelDeviceRespVO> pageResult = tunnelDeviceService.getTunnelDevicePageV2(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/listByHole")
    @Operation(summary = "获得隧道洞关联的设备列表")
    @PreAuthorize("@ss.hasPermission('qxsd:tunnel-device:query')")
    public CommonResult<List<TunnelDeviceRespVO>> getTunnelDeviceByHole(TunnelDeviceRespVO param) {
        List<TunnelDeviceDO> listDo = tunnelDeviceService.listByHole(param);
        List<TunnelDeviceRespVO> list = BeanUtils.toBean(listDo, TunnelDeviceRespVO.class);
        return success(list);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出隧道设备信息 Excel")
    @PreAuthorize("@ss.hasPermission('qxsd:tunnel-device:export')")
    @OperateLog(type = EXPORT)
    public void exportTunnelDeviceExcel(@Valid TunnelDevicePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TunnelDeviceDO> list = tunnelDeviceService.getTunnelDevicePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "隧道设备信息.xls", "数据", TunnelDeviceRespVO.class,
                        BeanUtils.toBean(list, TunnelDeviceRespVO.class));
    }

}
