package com.bjtdba.order.util.gong;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bjtdba.order.util.redis.RedisUtil;
import com.bjtdba.utils.DateUtils;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class GongApiRequest {
    private static RedisUtil redisUtil;


    private static String path = "https://www.douhuomall.com";
    private static String app_id = "YS112May13bb673b251a";
    private static String app_secret = "38971362a8965c9135cb147d53e52706";
    private static String mobile = "18226705337";


    public static String getSign(String time){
        //按照以下顺序将字符串拼接起来 app_id+mobile+timestamp+app_secret 再用md5加密
        return DigestUtils.md5DigestAsHex((app_id+mobile+time+app_secret).getBytes(StandardCharsets.UTF_8));
    }

    public static JSONObject get_access_token(){
        Map<String,String> params=new HashMap<String,String>();
        String url = "/api/noAuth/access_token";
        String time=DateUtils.getNowTime();
        params.put("app_id",app_id);
        params.put("sign",getSign(time));
        params.put("mobile",mobile);
        params.put("timestamp",time);
        String resStr=HttpUtil.doPost(path+url,params);
        JSONObject res= JSON.parseObject(resStr);

//        $body = [
//        'app_id' => self::$app_id,
//                'sign' => self::getSign($time),
//                'mobile' => self::$mobile,
//                'timestamp' => $time
//        ];
//        $res = self::curl_post(self::$path.$url,$body);
        if(res.get("'error_code").equals("0")){
            redisUtil.set("gong_access_token",res.get("access_token"),23*60*60);
        }

        return res;
    }

    public static String  access_token(){
        String gong_access_token = redisUtil.get("gong_access_token").toString();
        String url = "/api/category/get_list";
        Map<String,String> params=new HashMap<String,String>();
        params.put("access_token",gong_access_token);

        String resStr=HttpUtil.doPost(path+url,params);
        JSONObject res=JSON.parseObject(resStr);
        if(gong_access_token.equals("")||gong_access_token==null||res.get("error_code").equals("1001")){
            redisUtil.del("gong_access_token");
        }
        return gong_access_token;

    }


    /**
     * 商品是否有货
     * @param  sku_id 商品sku_id
     * @param  address_id 地址id（区id或者接到id）
     * @param  goods_num 商品数量
     * @return array|boolean
     * @author lin
     * @created 2022年5月19日11:31:59
     */
    public static JSONObject stock(String sku_id, String address_id, String goods_num){
        String url = "/api/goods/stock";
        Map<String,String> params=new HashMap<String,String>();
        params.put("'access_token'",access_token());
        params.put("sku_id",sku_id);
        params.put("address_id",address_id);
        params.put("goods_num",goods_num);

        String resStr=HttpUtil.doPost(path+url,params);
        JSONObject res=JSON.parseObject(resStr);

        if(res.get("error_code").equals("0")){
            return res;
        }
        return null;
    }

    public static void main(String[] args) {
        GongApiRequest ss=new GongApiRequest();
        ss.access_token();
    }

}
