package com.cyksj.web.controller.post;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.cyksj.common.util.IoKit;
import com.cyksj.common.util.J11HttpC;
import com.cyksj.common.util.Jsons;
import com.cyksj.common.util.StringUtil;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.redis.RedisService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.URL;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * 数据回传
 * @author chan
 * @date 2023/10/18 13:49
 */
@Slf4j
@RestController
@RequestMapping("/applets/oceanengine")
@RequiredArgsConstructor
public class OceanengineController {
    private final RedisService redisService;

    @RequestMapping("/monitor")
    private Result<String> monitor(HttpServletRequest request, String callback_url) throws Exception {
        Map<String, String[]> parameterMap = request.getParameterMap();

        //https://analytics,oceanengine.com/api/v2/conversion
        //customer_effective
        log.info("巨量监控地址 参数:\n{}", Jsons.toJson(parameterMap));
        if(StringUtils.isNotBlank(callback_url) && !"__CALLBACK_URL__".equals(callback_url)){
            Map<String, String> paramMap1 = HttpUtil.decodeParamMap(callback_url, StandardCharsets.UTF_8);
                redisService.sSetAndTime(RedisService.key.OCEANENGINE_MONITOR_CALLBCAK.getName(), RedisService.key.OCEANENGINE_MONITOR_CALLBCAK.getTimeout(), StringUtil.getString(paramMap1.get("callback")));
            }
        return GatewayResponse.SUCCESS.newBuilder().toResult();
    }
}