package com.dxhy.erp.utils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;

import com.dxhy.common.utils.IPUtils;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.hutool.core.codec.Base64;
import lombok.extern.slf4j.Slf4j;

/**
 * @author dxhy
 */
@Slf4j
public class OpenApiUtils {

    public static Map<String, String> getRequestParams(HttpServletRequest request, ObjectMapper mapper)
        throws IOException {
        Map<String, String> paramterMap = new HashMap<>(16);

        String globalInfoJson = request.getParameter("globalInfo");
        String dataJson = null;
        // 代码中不应该出现system.out字样,不受日志控制
        log.info(request.getContentType());
        if (CheckUtils.isNullOrEmpty(globalInfoJson)) {
            InputStream in = request.getInputStream();
            if (in != null) {
                byte[] reqByte = IOUtils.toByteArray(in);
                String requestMessage = new String(reqByte, StandardCharsets.UTF_8);
                if (log.isDebugEnabled()) {
                    log.debug("接收报文:{}", requestMessage);
                }

                try {

                    Map<String, Object> map = mapper.readValue(requestMessage, Map.class);
                    globalInfoJson = mapper.writeValueAsString(map.get("globalInfo"));
                    dataJson = mapper.writeValueAsString(map.get("data"));
                } catch (Exception e) {
                   log.info("业务处理异常：{}", e);
                }
            }
        } else {
            dataJson = request.getParameter("data");
        }
        paramterMap.put("globalInfo", globalInfoJson);
        paramterMap.put("data", dataJson);
        return paramterMap;
    }

    public static Map<String, String> getRequestParam(HttpServletRequest request, ObjectMapper mapper)
        throws IOException {
        Map<String, String> parameter = new HashMap<>(16);
        String globalInfoJson = request.getParameter("globalInfo");
        String dataJson = null;
        String authorizeJson = request.getParameter("authorize");
        if (CheckUtils.isNullOrEmpty(globalInfoJson)) {
            InputStream in = request.getInputStream();
            if (in != null) {
                byte[] reqByte = IOUtils.toByteArray(in);
                String requestMessage = new String(reqByte, StandardCharsets.UTF_8);
                if (log.isDebugEnabled()) {
                    log.debug("报文发送ip：{}", IPUtils.getIpAddr(request));
                    log.debug("接收报文:{}", requestMessage);
                }

                Map<String, Object> map = mapper.readValue(requestMessage, Map.class);
                globalInfoJson = mapper.writeValueAsString(map.get("globalInfo"));
                authorizeJson = mapper.writeValueAsString(map.get("authorize"));
                dataJson = mapper.writeValueAsString(map.get("data"));
            }
        } else {
            dataJson = request.getParameter("data");
        }
        parameter.put("globalInfo", globalInfoJson);
        parameter.put("authorize", authorizeJson);
        parameter.put("data", Base64.decodeStr(dataJson));
        return parameter;
    }

}
