package com.miaxis.test;

import com.alibaba.fastjson.JSONObject;
import com.miaxis.ZzjsApplication;
import com.miaxis.carousel.service.IHomePageDataInfoService;
import com.miaxis.carousel.vo.HomePageDataInfoVo;
import com.miaxis.common.config.PddConfig;
import com.miaxis.common.sms.MD5Utils;
import com.miaxis.feign.dto.Pdd;
import com.miaxis.feign.service.IPddService;
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 NormalTest3 {

    @Autowired
    private IPddService pddService;
    @Autowired
    private IHomePageDataInfoService homePageDataInfoService;

    @Autowired
    private PddConfig pddConfig;

//    private static String client_id = "fa0f2debb5b0444b9719919d7b8204d1";
//    private static String client_secret = "3305b6bb37252495848e67fd991134651e308495";
//    private static String pid = "1642187_203462988";


    public static String getPddSign(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);
        }
        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);
        Pdd pdd = new Pdd();
        //公共请救参数
        pdd.setType("pdd.ddk.resource.url.gen");
        pdd.setClient_id(pddConfig.getClientId());
        pdd.setTimestamp(timestamp);
        //请求参数
        pdd.setGenerate_we_app(true);
        pdd.setPid(pddConfig.getPid());
        System.out.println("---"+pddConfig.getPid()+"-----");
   //     pdd.setResource_type(39996);
        pdd.setResource_type(50005);
//        pdd.setResource_type(50005);
        //sign 加密
        Map<String,Object> param = new HashMap<String,Object>();
        //公共参数
        param.put("type",pdd.getType());
        param.put("client_id",pddConfig.getClientId());
        param.put("timestamp",pdd.getTimestamp());
        //请求参数
        param.put("generate_we_app",pdd.getGenerate_we_app());
        param.put("pid",pddConfig.getPid());
        param.put("resource_type",pdd.getResource_type());
        String sign = getPddSign(param,pddConfig.getClientSecret());
        String md5 = MD5Utils.MD5Encode(sign).toUpperCase();
        pdd.setSign(md5);

        String result = pddService.getPdd(pdd);
        System.out.println(result);


        JSONObject json = JSONObject.parseObject(result);
        String resourceUrlResponseJson = json.get("resource_url_response").toString();
        JSONObject json2  = JSONObject.parseObject(resourceUrlResponseJson);
        String waAppInfo = json2.get("we_app_info").toString();

        JSONObject waAppInfoJson = JSONObject.parseObject(waAppInfo);

        String pagePath = waAppInfoJson.get("page_path").toString();
        String appId = waAppInfoJson.get("app_id").toString();

        System.out.println(pagePath);
        System.out.println(pagePath);
        System.out.println(pagePath);

//        HomePageDataInfoVo hp = new HomePageDataInfoVo();
//        hp.setPictureName("话费充值");
//        hp.setJumpUrl("{\"appId\":\""+appId+"\",\"path\":\""+pagePath+"\"}");
//        homePageDataInfoService.updateHomePageDataByPictureName(hp);
//        homePageDataInfoService.synchroData();
    }
















}
