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.sf.service.gateway.domain.GatewayStrategy;
import com.sf.service.gateway.domain.dto.QueryGatewayStrategyDTO;
import com.sf.service.gateway.domain.dto.UpdateDataStatusDTO;
import com.sf.service.gateway.domain.vo.GatewayInterfaceInfoListVO;
import com.sf.service.gateway.domain.vo.GatewayStrategyListVO;
import com.sf.service.gateway.enums.GatewayDataStatus;
import com.sf.service.gateway.service.IGatewayStrategyService;
import com.sf.service.index.util.AppUtils;
import io.swagger.annotations.ApiOperation;
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 zoukun
 * @date 2024-04-22
 */
@RestController
@RequestMapping("/gateway/strategy")
public class GatewayStrategyController extends BaseController
{
    @Autowired
    private IGatewayStrategyService gatewayStrategyService;

    /**
     * 查询策略管理列表
     */
    @PreAuthorize("@ss.hasPermi('gateway:strategy:list')")
    @GetMapping("/list")
    public TableDataInfo list(GatewayStrategy gatewayStrategy)
    {
        gatewayStrategy.setAppCode(AppUtils.getAppCodeFromRequestHeader());
        startPage();
        List<GatewayStrategy> list = gatewayStrategyService.selectGatewayStrategyList(gatewayStrategy);
        return getDataTable(list);
    }

    /**
     * 导出策略管理列表
     */
    @PreAuthorize("@ss.hasPermi('gateway:strategy:export')")
    @Log(title = "策略管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GatewayStrategy gatewayStrategy)
    {
        List<GatewayStrategy> list = gatewayStrategyService.selectGatewayStrategyList(gatewayStrategy);
        ExcelUtil<GatewayStrategy> util = new ExcelUtil<GatewayStrategy>(GatewayStrategy.class);
        util.exportExcel(response, list, "策略管理数据");
    }

    /**
     * 获取策略管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('gateway:strategy:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(gatewayStrategyService.selectGatewayStrategyById(id));
    }

    /**
     * 新增策略管理
     */
    @PreAuthorize("@ss.hasPermi('gateway:strategy:add')")
    @Log(title = "策略管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody GatewayStrategy gatewayStrategy)
    {
        gatewayStrategy.setAppCode(AppUtils.getAppCodeFromRequestHeader());
        String username = getUsername();
        gatewayStrategy.setCreated(username);
        gatewayStrategy.setModified(username);
        return toAjax(gatewayStrategyService.insertGatewayStrategy(gatewayStrategy));
    }

    /**
     * 修改策略管理
     */
    @PreAuthorize("@ss.hasPermi('gateway:strategy:edit')")
    @Log(title = "策略管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody GatewayStrategy gatewayStrategy)
    {
        gatewayStrategy.setModified(getUsername());
        return toAjax(gatewayStrategyService.updateGatewayStrategy(gatewayStrategy));
    }

    /**
     * 删除策略管理
     */
    @PreAuthorize("@ss.hasPermi('gateway:strategy:remove')")
    @Log(title = "策略管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gatewayStrategyService.deleteGatewayStrategyByIds(ids));
    }

    @ApiOperation("启用/停用网关策略")
    @Log(title = "启用/停用网关策略", businessType = BusinessType.UPDATE)
    @PutMapping("/status")
    public AjaxResult updateStatus(@Validated @RequestBody UpdateDataStatusDTO dto)
    {
        return toAjax(gatewayStrategyService.updateGatewayStrategyStatusByIds(dto.getIds(), GatewayDataStatus.getByCode(dto.getStatus())));
    }

    /**
     * 查询可用的策略列表（状态为启用的,不分页）
     */
    @GetMapping("/bindableList")
    public List<GatewayStrategyListVO> bindableList(QueryGatewayStrategyDTO dto)
    {
        GatewayStrategy gatewayStrategy = new GatewayStrategy();
        gatewayStrategy.setStatus(GatewayDataStatus.ENABLE.getCode());
        gatewayStrategy.setStrategyName(dto.getStrategyName());
        gatewayStrategy.setStrategyType(dto.getStrategyType());
        gatewayStrategy.setAppCode(AppUtils.getAppCodeFromRequestHeader());
        Map<String, Object> params = new HashMap<>(8);
        params.put("ids",dto.getIds());
        params.put("excludedIds",dto.getExcludedIds());
        gatewayStrategy.setParams(params);
        List<GatewayStrategy> list = gatewayStrategyService.selectGatewayStrategyList(gatewayStrategy);
        return list.stream().map(item ->{
            GatewayStrategyListVO vo = new GatewayStrategyListVO();
            vo.setId(item.getId());
            vo.setStrategyName(item.getStrategyName());
            vo.setStrategyType(item.getStrategyType());
            vo.setDescription(item.getDescription());
            vo.setStatus(item.getStatus());
            return vo;
        }).collect(Collectors.toList());
    }
}
