package com.liquidnet.service;

import com.alibaba.fastjson.JSON;
import com.liquidnet.commons.lang.util.JsonUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author AnJiabin <anjiabin@zhengzai.tv>
 * @version V1.0
 * @Description: TODO
 * @class: DragonPayTest
 * @Package com.liquidnet.service
 * @Copyright: LightNet @ Copyright (c) 2021
 * @date 2021/7/27 13:47
 */
//@RunWith(SpringJUnit4ClassRunner.class)
//@SpringBootTest
public class DragonPayTest {

    public static void testMap(){
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("status", "1"));
        nvps.add(new BasicNameValuePair("type", "TICKET"));
        nvps.add(new BasicNameValuePair("code", "PAY20210727111746415639688"));
        nvps.add(new BasicNameValuePair("paymentId", "4200001121202107274119605305"));
        nvps.add(new BasicNameValuePair("orderCode", "11766977997930496418880635"));
        nvps.add(new BasicNameValuePair("price", "0.01"));
        nvps.add(new BasicNameValuePair("paymentType", "APPWEPAY"));
        nvps.add(new BasicNameValuePair("paymentAt", "2021-07-27 11:18:03"));
        String jsonData = JSON.toJSONString(nvps);
        System.out.println("jsonDate === "+jsonData);
        System.out.println("covertNVPS2Params jsonDate === "+covertNVPS2Params(nvps));
        System.out.println("covertNVPS2Params jsonDate === "+ JsonUtils.toJson(covertNVPS2Params(nvps)));
        System.out.println("covertNVPS2Params jsonDate === "+ covertNVPS2KeyValue(nvps));
    }
    private static ArrayList<NameValuePair> covertParams2NVPS(Map<String, Object> params) {
        ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
        for (Map.Entry<String, Object> param : params.entrySet()) {
            pairs.add(new BasicNameValuePair(param.getKey(), String.valueOf(param.getValue())));
        }

        return pairs;
    }
    private static Map<String, Object> covertNVPS2Params(List<NameValuePair> nvpList) {
        Map<String, Object> rsMap = new HashMap<>();
        for (NameValuePair nameValuePair : nvpList) {
            rsMap.put(nameValuePair.getName(),nameValuePair.getValue());
        }
        return rsMap;
    }

    private static String covertNVPS2KeyValue(List<NameValuePair> nvpList) {
        StringBuffer sb = new StringBuffer();
        for (NameValuePair nameValuePair : nvpList) {
            sb.append(nameValuePair.getName()+":"+nameValuePair.getValue()+"\n");
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        testMap();
    }
}
