package com.reyun.saas.web.controller.openapi;


import com.reyun.saas.common.exception.CustomException;
import com.reyun.saas.common.framework.OpenapiCpRestController;
import com.reyun.saas.common.restResult.RestResult;
import com.reyun.saas.common.restResult.ResultCode;
import com.reyun.saas.common.restResult.ResultGenerator;
import com.reyun.saas.mob.openapi.domain.CollaborativeProject;
import com.reyun.saas.mob.openapi.dto.RtaIpWhiteDTO;
import com.reyun.saas.mob.openapi.service.CollaborativeProjectService;
import com.reyun.saas.mob.openapi.service.OpenApiRtaService;
import com.reyun.saas.web.aop.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author: hanzepeng
 * @Date: 2022/02/14/10:29
 * @Description: Rta配置控制层
 */
@Slf4j
@Api(value = "/rta", tags = "OpenApi Rta配置")
@OpenapiCpRestController("rta")
public class OpenApiRtaController {

    @Autowired
    private CollaborativeProjectService projectService;

    @Autowired
    private OpenApiRtaService openApiRtaService;

    @Log(value = 3, type = 9, description = "合作项目更新Rta ip白名单", insertDB = true)
    @PostMapping("updateIpWhiteList")
    @ApiOperation(value = "OpenApi更新IP白名单", produces = "application/json")
    public RestResult updateIpWhiteList(@RequestBody(required = false) RtaIpWhiteDTO rtaIpWhiteDTO,
                                         @RequestHeader(value = "token",required = false) String token) {
        try {
            //根据token查询合作项目
            CollaborativeProject project = projectService.findProjectByToken(token);

            Set<String> ips = null;
            if (rtaIpWhiteDTO != null && !CollectionUtils.isEmpty(rtaIpWhiteDTO.getIps())) {
                rtaIpWhiteDTO.checkParam();
                ips = rtaIpWhiteDTO.getIps();
            }
            openApiRtaService.updateIpWhiteList(project, ips);
            log.info("openapi合作项目更新rta ip白名单 token:{} param:{}", token, rtaIpWhiteDTO);
            return ResultGenerator.genSuccessResult();
        } catch (CustomException e) {
            return new RestResult(e.getResultCode());
        } catch (Exception e) {
            log.error("openapi 合作项目更新rta ip白名单失败! token:{} param:{}", token, rtaIpWhiteDTO, e);
            return new RestResult(ResultCode.UNKNOWN_ERROR);
        }

    }

    @Log(value = 4, type = 9, description = "合作项目查询Rta ip白名单", insertDB = false)
    @GetMapping("ipWhiteList")
    @ApiOperation(value = "OpenApi合作项目查询rta ip白名单", produces = "application/json")
    public RestResult ipWhiteList(@RequestHeader(value = "token",required = false) String token) {
        try {
            //根据token查询合作项目
            CollaborativeProject project = projectService.findProjectByToken(token);
            Set<String> ips = openApiRtaService.ipWhiteList(project);
            Map<String, Object> result = null;
            if (!CollectionUtils.isEmpty(ips)) {
                result = new HashMap<String, Object>() {{
                    put("ips", ips);
                }};
            }
            return ResultGenerator.genSuccessResult(result);
        } catch (CustomException e) {
            return new RestResult(e.getResultCode());
        } catch (Exception e) {
            log.error("openapi 查询rta ip白名单失败! token:{}", token, e);
            return new RestResult(ResultCode.UNKNOWN_ERROR);
        }
    }


}
