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

import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.tunnelFacility.TunnelFacilityPageReqVO;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.tunnelFacility.TunnelFacilityRespVO;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.tunnelFacility.TunnelFacilitySaveReqVO;
import com.cico.nest.lcdp.boot.core.qxsd.application.service.TunnelFacilityService;
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 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 java.util.*;

import com.cico.nest.lcdp.framework.common.pojo.PageResult;
import com.cico.nest.lcdp.framework.common.pojo.CommonResult;

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;


@Tag(name = "管理后台 - 隧道设施")
@RestController
@RequestMapping("/qxsd/tunnel-facility")
@Validated
public class TunnelFacilityController {

    @Resource
    private TunnelFacilityService tunnelFacilityService;

    @PostMapping("/create")
    @Operation(summary = "创建隧道设施")
    public CommonResult<String> createTunnelFacility(@Valid @RequestBody TunnelFacilitySaveReqVO createReqVO) {
        return success(tunnelFacilityService.createTunnelFacility(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新隧道设施")
    public CommonResult<Boolean> updateTunnelFacility(@Valid @RequestBody TunnelFacilitySaveReqVO updateReqVO) {
        tunnelFacilityService.updateTunnelFacility(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除隧道设施")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteTunnelFacility(@RequestParam("id") String id) {
        tunnelFacilityService.deleteTunnelFacility(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得隧道设施")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<TunnelFacilityRespVO> getTunnelFacility(@RequestParam("id") String id) {
        TunnelFacilityDO tunnelFacility = tunnelFacilityService.getTunnelFacility(id);
        return success(TunnelFacilityRespVO.convert2VO(tunnelFacility));
    }

    @GetMapping("/page")
    @Operation(summary = "获得隧道设施分页")
    public CommonResult<PageResult<TunnelFacilityRespVO>> getTunnelFacilityPage(@Valid TunnelFacilityPageReqVO pageReqVO) {
        PageResult pageResult = tunnelFacilityService.getTunnelFacilityPage(pageReqVO);
        pageResult.setList(TunnelFacilityRespVO.convert2VO(pageResult.getList()));
        return success(pageResult);
    }

    /**
     * 启用设施
     * @param id 设施id
     * @return
     */
    @GetMapping("/enable")
    @Operation(summary = "启用设施")
    public CommonResult<String> enable(@RequestParam("id") String id) {
        if(tunnelFacilityService.existEnable()){
            throw new ServiceException(INTERNAL_SERVER_ERROR.getCode(),"请先禁用其他设施");
        }
        tunnelFacilityService.enable(id);
        return success("操作成功");
    }

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

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

}
