package com.miaxis.test;

import com.miaxis.ZzjsApplication;
import com.miaxis.common.config.WaiConfig;
import com.miaxis.common.sms.MD5Utils;
import com.miaxis.feign.dto.Ai;
import com.miaxis.feign.service.IAiService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

@SpringBootTest(classes = ZzjsApplication.class)
@RunWith(SpringRunner.class)
public class AiTest {

    @Autowired
    private IAiService aiService;
    @Autowired
    private WaiConfig waiConfig;

    private static String appid = "7629";
    private static String secret = "cce08fcab10ff96a863892bb6e396a9e";

    public static String getSign(Map<String, Object> params,String client_secret) {
        Map<String, Object> sortMap = new TreeMap<String, Object>();
        sortMap.putAll(params);
        // 以k1v1k2v2...方式拼接参数
        StringBuilder builder = new StringBuilder();
        for (Map.Entry<String, Object> s : sortMap.entrySet()) {
            String k = s.getKey();
            Object v = s.getValue();
            builder.append(k).append(v);
        }
//        if (!sortMap.isEmpty()) {
//            builder.deleteCharAt(builder.length() - 1);
//        }
        builder.append(client_secret);
//        builder.insert(0,client_secret);
        return builder.toString();
    }

    @Test
    public void test() throws Exception {
        long timeLong = System.currentTimeMillis();
        String timestamp = String.valueOf(timeLong/1000);
        Ai ai = new Ai();

        //公共请救参数
        ai.setAppid(waiConfig.getAppid());
        ai.setC_account("15060063160");
        ai.setFormat("json");
        ai.setMerchant_id("order01");
        ai.setNotify_url("http://218.85.55.253:38080/open-api/film/notify/aiqiyiOrderNotify");
        ai.setTime(timestamp);
        //sign 加密
        Map<String,Object> param = new HashMap<String,Object>();
        //公共参数
        param.put("appid",ai.getAppid());
        param.put("c_account",ai.getC_account());
        param.put("format",ai.getFormat());
        param.put("merchant_id",ai.getMerchant_id());
        param.put("notify_url",ai.getNotify_url());
        param.put("time",ai.getTime());

        String sign = getSign(param,waiConfig.getSecret());
        System.out.println(sign);
        String md5 = MD5Utils.MD5Encode(sign);
        ai.setSign(md5);

        String str = aiService.rechargeAiMonth(ai);
        System.out.println(str);

    }













}
