package com.bjtdba.order.util.gong;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bjtdba.order.util.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Component
public class WeixinUtil {
    @Resource
    private RedisUtil redisUtilConfig;
    @Autowired
    private static RedisUtil redisUtil;


    @PostConstruct
    public  void init(){
        redisUtil=this.redisUtilConfig;
    }

    public static String get_access_token(){
        if(redisUtil.hasKey("java_wx9909df446471d46b_access_token")){
            return redisUtil.get("java_wx9909df446471d46b_access_token").toString();
        }

//        if ($access_token)
//            return $access_token;

        String url = "https://api.weixin.qq.com/cgi-bin/token";
        String query="grant_type=client_credential&appid=wx9909df446471d46b&secret=9f1bb3a062b163fa5a780c976092f009";
        String res =HttpUtil.doGet(url,query);
        System.out.println(res);
        JSONObject jsonObject= JSON.parseObject(res);
        if(jsonObject.get("errcode")==null||jsonObject.get("errcode").equals("")) {
            redisUtil.set("java_wx9909df446471d46b_access_token", jsonObject.get("access_token"), Long.parseLong(jsonObject.get("expires_in") + ""));
            return jsonObject.get("access_token") + "";
        }else{
            return  null;
        }
//        Cache::set('wx9909df446471d46b_access_token', $res['access_token'], $res['expires_in']);

    }


    public static String get_ticket(){
//        $ticket = Cache::get('wx9909df446471d46b_ticket');
//        if($ticket)
//            return $ticket;
        if(redisUtil.hasKey("java_wx9909df446471d46b_ticket")){
            String  res= redisUtil.get("java_wx9909df446471d46b_ticket").toString();

            return res;
        }

        String access_token = get_access_token();
        if(access_token!=null) {
            String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
            String query = "access_token=" + access_token + "&type=jsapi";
            String res = HttpUtil.doGet(url, query);
            ;
            JSONObject jsonObject = JSON.parseObject(res);
            redisUtil.set("java_wx9909df446471d46b_ticket", jsonObject.get("ticket"), Long.parseLong(jsonObject.get("expires_in") + ""));
//        Cache::set('wx9909df446471d46b_ticket', $res['ticket'], $res['expires_in']);
            return jsonObject.get("ticket") + "";
        }else{
            return  null;
        }
    }


    public static void main(String[] args) {
       Double a=625d;

        int num=1;
        Double cc=5d;
        for (int i=1;i<100;i++){
            cc=cc*5;
            System.out.println(cc+"=====");
            num++;

            if(cc<a){
                System.out.println( "=====111===="+num);


            }else if(cc.compareTo(a)==0){
                break;
            }

        }
        System.out.println( "========="+num);
//       Math.sqrt(625);
    }

}
