package com.sf.service.gateway.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.sf.service.gateway.domain.GatewayRoute;
import com.sf.service.gateway.domain.GatewayStrategy;
import com.sf.service.gateway.domain.dto.InsertOrUpdateGatewayRouteDTO;
import com.sf.service.gateway.domain.dto.QueryGatewayRouteDTO;
import com.sf.service.gateway.domain.dto.QueryGatewayStrategyDTO;
import com.sf.service.gateway.domain.vo.GatewayRouteListVO;
import com.sf.service.gateway.domain.vo.GatewayStrategyListVO;
import com.sf.service.gateway.enums.GatewayDataStatus;
import com.sf.service.gateway.service.IGatewayRouteService;
import com.sf.service.index.util.AppUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sf.common.annotation.Log;
import com.sf.common.core.controller.BaseController;
import com.sf.common.core.domain.AjaxResult;
import com.sf.common.enums.BusinessType;
import com.sf.common.utils.poi.ExcelUtil;
import com.sf.common.core.page.TableDataInfo;

/**
 * 路由管理Controller
 * 
 * @author 邹坤
 * @date 2024-04-22
 */
@RestController
@RequestMapping("/gateway/route")
public class GatewayRouteController extends BaseController
{
    @Autowired
    private IGatewayRouteService gatewayRouteService;

    /**
     * 查询路由管理列表
     */
    @PreAuthorize("@ss.hasPermi('gateway:route:list')")
    @GetMapping("/list")
    public TableDataInfo list(GatewayRoute gatewayRoute)
    {
        startPage();
        List<GatewayRoute> list = gatewayRouteService.selectGatewayRouteList(gatewayRoute);
        List<GatewayRouteListVO> voList = list.stream().map(item ->{
            GatewayRouteListVO vo = new GatewayRouteListVO();
            vo.setId(item.getId());
            vo.setRouteName(item.getRouteName());
            vo.setRequestProtocol(item.getRequestProtocol());
            vo.setRouteType(item.getRouteType());
            vo.setRouteContent(JSON.parseArray(item.getRouteContent(),GatewayRoute.RouteContent.class));
            vo.setRouteStatusActiveMonitoring(item.getRouteStatusActiveMonitoring());
            vo.setRouteActiveMonitoringPath(item.getRouteActiveMonitoringPath());
            vo.setRouteActiveMonitoringTimeout(item.getRouteActiveMonitoringTimeout());
            vo.setCreated(item.getCreated());
            vo.setCreateTime(item.getCreateTime());
            vo.setRouteActiveStatus(item.getRouteActiveStatus());
            return vo;
        }).collect(Collectors.toList());

        return getDataTable(voList);
    }

    /**
     * 导出路由管理列表
     */
    @PreAuthorize("@ss.hasPermi('gateway:route:export')")
    @Log(title = "路由管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GatewayRoute gatewayRoute)
    {
        List<GatewayRoute> list = gatewayRouteService.selectGatewayRouteList(gatewayRoute);
        ExcelUtil<GatewayRoute> util = new ExcelUtil<GatewayRoute>(GatewayRoute.class);
        util.exportExcel(response, list, "路由管理数据");
    }

    /**
     * 获取路由管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('gateway:route:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(gatewayRouteService.selectGatewayRouteById(id));
    }

    /**
     * 新增路由管理
     */
    @PreAuthorize("@ss.hasPermi('gateway:route:add')")
    @Log(title = "路由管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody InsertOrUpdateGatewayRouteDTO dto)
    {
        return toAjax(gatewayRouteService.insertGatewayRoute(dto));
    }

    /**
     * 修改路由管理
     */
    @PreAuthorize("@ss.hasPermi('gateway:route:edit')")
    @Log(title = "路由管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody InsertOrUpdateGatewayRouteDTO dto)
    {
        return toAjax(gatewayRouteService.updateGatewayRoute(dto));
    }

    /**
     * 删除路由管理
     */
    @PreAuthorize("@ss.hasPermi('gateway:route:remove')")
    @Log(title = "路由管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gatewayRouteService.deleteGatewayRouteByIds(ids));
    }

    /**
     * 查询可绑定的路由列表（不分页）
     */
    @GetMapping("/bindableList")
    public List<GatewayRouteListVO> bindableList(QueryGatewayRouteDTO dto)
    {
        GatewayRoute gatewayRoute = new GatewayRoute();
        gatewayRoute.setRouteName(dto.getRouteName());
        gatewayRoute.setAppCode(AppUtils.getAppCodeFromRequestHeader());
        Map<String, Object> params = new HashMap<>(8);
        params.put("ids",dto.getIds());
        params.put("excludedIds",dto.getExcludedIds());
        gatewayRoute.setParams(params);
        List<GatewayRoute> list = gatewayRouteService.selectGatewayRouteList(gatewayRoute);
        return list.stream().map(item ->{
            GatewayRouteListVO vo = new GatewayRouteListVO();
            vo.setId(item.getId());
            vo.setRouteName(item.getRouteName());
            vo.setRouteType(item.getRouteType());
            return vo;
        }).collect(Collectors.toList());
    }
}
