package com.ruoyi.interfaces.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.interfaces.domain.GatewayRoutes;
import com.ruoyi.interfaces.service.GatewayRoutesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lql
 * @date 2025-8-19
 */
@Api(value = "路由配置", tags = "路由配置")
@RestController
@RequestMapping("/gateway/routes")
public class GatewayRoutesController extends BaseController {

    @Autowired
    private GatewayRoutesService gatewayRoutesService;


    @ApiOperation(value = "新增/修改路由配置")
    @PostMapping("")
    public AjaxResult save(@ApiParam(name = "gatewayRoutes", value = "GatewayRoutes", required = true) @RequestBody GatewayRoutes gatewayRoutes) {
        boolean result = gatewayRoutesService.saveOrUpdate(gatewayRoutes);
        if (result) {
            gatewayRoutesService.refreshRoutes();
        }
        return AjaxResult.success(result);
    }

    @ApiOperation(value = "根据ID删除路由配置")
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@ApiParam(name = "id", value = "id", required = true) @PathVariable String id) {
        return AjaxResult.success(gatewayRoutesService.removeById(id));
    }

    @ApiOperation(value = "根据ID获取路由配置（单表）")
    @GetMapping("/{id}")
    public AjaxResult get(@ApiParam(name = "id", value = "id", required = true) @PathVariable String id) {
        return AjaxResult.success(gatewayRoutesService.getById(id));
    }


    @ApiOperation("根据主键获取数据")
    @GetMapping(value = "/listOfPage")
    public TableDataInfo listOfPage(GatewayRoutes gatewayRoutes) {
        startPage();
        QueryWrapper<GatewayRoutes> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(gatewayRoutes.getStatus()), "status", gatewayRoutes.getStatus())
                .like(StringUtils.isNotBlank(gatewayRoutes.getServiceName()), "service_name", gatewayRoutes.getServiceName());
        List<GatewayRoutes> list = gatewayRoutesService.list(queryWrapper);
        return getDataTable(list);
    }

}
