package com.ydl.ydlcommon.utils;

import com.ydl.ydlcommon.data.http.BaseCommand;
import com.ydl.ydlcommon.data.http.FormatText;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by haorui on 2019-09-19.
 * Des:
 */
public class NetworkParamsUtils {

    public static List<FormatText> getPostList(BaseCommand crq) {
        List<FormatText> formTextList = new ArrayList<>();
        Field[] fields = crq.getClass().getFields();
        for (Field field : fields) {
            try {
                boolean accessFlag = field.isAccessible();
                field.setAccessible(true);
                Object o = field.get(crq);
                if (o != null) {
                    FormatText formatText;
                    formatText = new FormatText(field.getName(), String.valueOf(o));
                    formTextList.add(formatText);
                }
                field.setAccessible(accessFlag);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return formTextList;
    }


    //获取网络请求表
    public static Map<String, String> getMaps(List<FormatText> list) {
        Map<String, String> map = new HashMap<>();
        int size = list.size();
        for (int i = 0; i < size; i++) {
            FormatText formatText = list.get(i);
            map.put(formatText.getmKey(), String.valueOf(formatText.getmValue()));
        }
        return map;
    }


}
