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

import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.tunnelBase.TunnelBaseSaveReqVO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.TunnelFacilityDO;
import com.cico.nest.lcdp.framework.common.exception.ServiceException;
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.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
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.tunnelBase.*;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.TunnelBaseDO;
import com.cico.nest.lcdp.boot.core.qxsd.application.service.TunnelBaseService;

@Tag(name = "管理后台 - 隧道设施基础信息")
@RestController
@RequestMapping("/qxsd/tunnelbase")
@Validated
public class TunnelBaseController {

    @Resource
    private TunnelBaseService tunnelBaseService;

    @PostMapping("/create")
    @Operation(summary = "创建隧道设施信息")
    @PreAuthorize("@ss.hasPermission('qxsd:tunnel-base:create')")
    public CommonResult<String> createTunnelBase(@Valid @RequestBody TunnelInfoVo createReqVO) {
        return success(tunnelBaseService.createTunnel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新隧道设施信息")
    @PreAuthorize("@ss.hasPermission('qxsd:tunnel-base:update')")
    public CommonResult<Boolean> updateTunnelBase(@Valid @RequestBody TunnelInfoVo updateReqVO) {
        tunnelBaseService.updateTunnel(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得隧道设施基础信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qxsd:tunnel-base:query')")
    public CommonResult<TunnelInfoVo> getTunnelBase(@RequestParam("id") String id) {
        TunnelInfoVo tunnelInfo = tunnelBaseService.getTunnel(id);
        return success(tunnelInfo);
    }

    @GetMapping("/page")
    @Operation(summary = "获得隧道设施信息分页")
    @PreAuthorize("@ss.hasPermission('qxsd:tunnel-base:query')")
    public CommonResult<PageResult<TunnelBaseRespVO>> getTunnelBasePage(@Valid TunnelBasePageReqVO pageReqVO) {
        PageResult<TunnelBaseRespVO> pageResult = tunnelBaseService.getTunnelPage(pageReqVO);
        return success(pageResult);
    }

    /**
     * 启用设施
     * @param id 设施id
     * @return
     */
    @GetMapping("/enable")
    @Operation(summary = "启用设施")
    public CommonResult<String> enable(@RequestParam("id") String id) {
        tunnelBaseService.enable(id);
        return success("操作成功");
    }

    /**
     * 禁用设施
     * @param id 设施id
     * @return
     */
    @GetMapping("/disEnable")
    @Operation(summary = "禁用设施")
    public CommonResult<String> disEnable(@RequestParam("id") String id) {
        tunnelBaseService.disEnable(id);
        return success("操作成功");
    }

    @GetMapping("/enableList")
    @Operation(summary = "启用设施列表")
    public CommonResult<List<TunnelBaseRespVO>> enableList(String deptId) {
        return success(tunnelBaseService.getEnableList(deptId));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出隧道设施基础信息 Excel")
    @PreAuthorize("@ss.hasPermission('qxsd:tunnel-base:export')")
    @OperateLog(type = EXPORT)
    public void exportTunnelBaseExcel(@Valid TunnelBasePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TunnelBaseDO> list = tunnelBaseService.getTunnelBasePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "隧道设施基础信息.xls", "数据", TunnelBaseRespVO.class,
                        BeanUtils.toBean(list, TunnelBaseRespVO.class));
    }

}
