package com.miaxis.job;


import com.alibaba.fastjson.JSONObject;
import com.miaxis.carousel.service.IHomePageDataInfoService;
import com.miaxis.carousel.vo.HomePageDataInfoVo;
import com.miaxis.common.config.MeiConfig;
import com.miaxis.common.config.PddConfig;
import com.miaxis.common.sms.MD5Utils;
import com.miaxis.common.utils.DateUtils;
import com.miaxis.feign.dto.Mei;
import com.miaxis.feign.dto.Pdd;
import com.miaxis.feign.service.IMeiService;
import com.miaxis.feign.service.IPddService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.unit.DataUnit;

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

@Slf4j
@Component
public class MenuJob {

    @Autowired
    private IPddService pddService;
    @Autowired
    private IMeiService meiService;
    @Autowired
    private IHomePageDataInfoService homePageDataInfoService;
    @Autowired
    private PddConfig pddConfig;
    @Autowired
    private MeiConfig meiConfig;

//    @Scheduled(cron = "0/30 * * * * ?")
    @Scheduled(cron = "0 0 10 1/7 * ?")
    public void autoGetUrl() throws Exception {
        getPddPhoneUrl();
        getPddTrainTicketsUrl();
        getMeiUrl();
        homePageDataInfoService.synchroData();
        log.info("--------更新美团，拼多多URL完成！~---------------");
    }


    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);
        }
        builder.append(client_secret);
        builder.insert(0,client_secret);
        System.out.println(builder.toString());
        return builder.toString();
    }

    public void getPddPhoneUrl() throws Exception {

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

        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());
        //     pdd.setResource_type(39996);
        pdd.setResource_type(39997);
        //sign 加密
        Map<String,Object> param = new HashMap<String,Object>();
        //公共参数
        param.put("type",pdd.getType());
        param.put("client_id",pdd.getClient_id());
        param.put("timestamp",pdd.getTimestamp());
        //请求参数
        param.put("generate_we_app",pdd.getGenerate_we_app());
        param.put("pid",pdd.getPid());
        param.put("resource_type",pdd.getResource_type());
        String sign = getSign(param,pddConfig.getClientSecret());
        String md5 = MD5Utils.MD5Encode(sign).toUpperCase();
        pdd.setSign(md5);

        String result = pddService.getPdd(pdd);

        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();

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


    public void getPddTrainTicketsUrl() 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());
        //     pdd.setResource_type(39996);
        pdd.setResource_type(50005);
        //sign 加密
        Map<String,Object> param = new HashMap<String,Object>();
        //公共参数
        param.put("type",pdd.getType());
        param.put("client_id",pdd.getClient_id());
        param.put("timestamp",pdd.getTimestamp());
        //请求参数
        param.put("generate_we_app",pdd.getGenerate_we_app());
        param.put("pid",pdd.getPid());
        param.put("resource_type",pdd.getResource_type());
        String sign = getSign(param,pddConfig.getClientSecret());
        String md5 = MD5Utils.MD5Encode(sign).toUpperCase();
        pdd.setSign(md5);

        String result = pddService.getPdd(pdd);

        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();

        HomePageDataInfoVo hp = new HomePageDataInfoVo();
        hp.setPictureName("火车票");
        hp.setJumpUrl("{\"appId\":\""+appId+"\",\"path\":\""+pagePath+"\"}");
        homePageDataInfoService.updateHomePageDataByPictureName(hp);
    }


    public void getMeiUrl() throws Exception {

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

        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.getSign(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);
    }




}
