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.MeiConfig;
import com.miaxis.common.sms.MD5Utils;
import com.miaxis.feign.dto.Mei;
import com.miaxis.feign.service.IMeiService;
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 NormalTest5 {

    @Autowired
    private IMeiService meiService;
    @Autowired
    private IHomePageDataInfoService homePageDataInfoService;

    @Autowired
    private MeiConfig meiConfig;


//    private String actId = "2";  // 必填  	活动id，可以在联盟活动列表中查看获取 2
//    private String key = "610077fffb51712efaa686d9ffb623da"; //必填 appkey 514202a9238958b16cb13805d6f79273
//    private String secret = "514202a9238958b16cb13805d6f79273";
//    private String sid = "jsshq001"; //必填
//
//    private String appId = "wxde8ac0a21135c07d";

    public static String getMeiSign(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);
        Mei mei = new Mei();
        mei.setActId(meiConfig.getActId());
        mei.setKey(meiConfig.getKey());
        mei.setSid(meiConfig.getSid());
        mei.setLinkType("4");

        Map<String,Object> param = new HashMap<String,Object>();
        param.put("actId",meiConfig.getActId());
        param.put("key",meiConfig.getKey());
        param.put("sid",meiConfig.getSid());

        param.put("linkType",4);
        String sign = this.getMeiSign(param,meiConfig.getSecret());
        System.out.println(sign);
        String md5 = MD5Utils.MD5Encode(sign);
        mei.setSign(md5);

        System.out.println(md5);
        String result = meiService.geMeiUrl(mei);
        System.out.println(result);


        JSONObject json = JSONObject.parseObject(result);
        String status = json.get("status").toString();
        String des = json.get("des").toString();
        String pagePath = json.get("data").toString();

        System.out.println(pagePath);

        HomePageDataInfoVo hp = new HomePageDataInfoVo();
        hp.setPictureName("美团外卖");
        hp.setJumpUrl("{\"appId\":\""+meiConfig.getAppId()+"\",\"path\":\""+pagePath+"\"}");
        homePageDataInfoService.updateHomePageDataByPictureName(hp);
    }













}
