package com.zhgd.jeecg.common.util.pass;


import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpMethod;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
 * http 工具类 获取请求中的参数
 *
 * @author show
 * @date 14:23 2019/5/29
 */
@Slf4j
public class HttpUtils {
    /**
     * 将URL的参数和body参数合并
     *
     * @param request
     * @author show
     * @date 14:24 2019/5/29
     */
    public static SortedMap<String, Object> getAllParams(HttpServletRequest request) throws IOException {

        SortedMap<String, Object> result = new TreeMap<>();
        //获取URL上的参数
        Map<String, Object> urlParams = getUrlParams(request);
        for (Map.Entry entry : urlParams.entrySet()) {
            result.put((String) entry.getKey(), entry.getValue());
        }
        Map<String, String> allRequestParam = new HashMap<>(16);
        // get请求不需要拿body参数
        if (!HttpMethod.GET.name().equals(request.getMethod())) {
            allRequestParam = getAllRequestParam(request);
        }
        //将URL的参数和body参数进行合并
        if (allRequestParam != null) {
            for (Map.Entry entry : allRequestParam.entrySet()) {
                result.put((String) entry.getKey(), (String) entry.getValue());
            }
        }
        return result;
    }

    /**
     * 获取 Body 参数
     *
     * @param request
     * @author show
     * @date 15:04 2019/5/30
     */
    public static Map<String, String> getAllRequestParam(final HttpServletRequest request) throws IOException {
        InputStreamReader inputStreamReader = null;
        StringBuilder wholeStr = new StringBuilder();
        BufferedReader reader = null;
        try {
            inputStreamReader = new InputStreamReader(request.getInputStream());
            //BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
            reader = new BufferedReader(inputStreamReader);
            String str = "";
            //一行一行的读取body体里面的内容；
            while ((str = reader.readLine()) != null) {
                wholeStr.append(str);
            }
        } finally {
            try {
                inputStreamReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //转化成json对象
        return JSONObject.parseObject(StringUtils.isNotEmpty(wholeStr.toString()) ? wholeStr.toString() : "{}", Map.class);
    }

    /**
     * 将URL请求参数转换成Map
     *
     * @param request
     * @author show
     */
    public static Map<String, Object> getUrlParams(HttpServletRequest request) {

        Enumeration enu = request.getParameterNames();
        Map<String, Object> result = new HashMap<>(16);
        while (enu.hasMoreElements()) {
            String paraName = (String) enu.nextElement();
            log.info(paraName + ": " + request.getParameter(paraName));
            result.put(paraName, request.getParameter(paraName));
        }
        return result;
    }
}