package com.cpxt.biz.orderutil;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.cpxt.biz.domain.BizOrder;
import com.cpxt.biz.domain.BizOrderSub;
import com.cpxt.biz.domain.SysArea;
import com.cpxt.biz.mapper.SysAreaMapper;
import com.cpxt.common.annotation.Anonymous;
import com.cpxt.common.utils.DateUtils;
import com.cpxt.common.utils.Helper;
import com.cpxt.system.mapper.SysDictDataMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @Author Ly
 * @Create 2024/12/4 18:57
 * @Desc
 */
@Service
@Slf4j
public class CityDelivery {

    private static final char[] specialChars = {'\n', '\r', '\t', '\f', '\b', ' '};

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private SysAreaMapper sysAreaMapper;

    // test
    String clientKey = "7480617400984563";//accessKey
    String clientSecret = "91wCEvn82CeyMg3k";
    String sm4Key = "MJy05dcI4mPbGB61";
    String appType = "wxmp";
    String client_type = "other";
    String version = "1.0.0";

    /**
     * 同城急送平台工具，post接口
     */
    public void cityDelivery_signBody22() {
        Long timeStamp = System.currentTimeMillis();
        String nonce = UUID.randomUUID().toString().replace("-", "");
        System.out.println("nonce:" + nonce);
        System.out.println("timeStamp:" + timeStamp);
        //签名
        Map<String, Object> sortedMap = new TreeMap<>();
        //下单接口
        String body = "{\n" +
                "    \"customerCode\": \"HFCP\",\n" +
                "    \"payTypeFirst\": \"03\",\n" +
                "    \"channelOrderNo\": \"Y202501021005-1\",\n" +
                "    \"platformOrderNo\": \"\",\n" +
                "    \"expressNbr\": \"\",\n" +
                "    \"coordinateTransformType\": \"02\",\n" +
                "    \"weight\": \"501\",\n" +
                "    \"expectedTime\": \"2025-01-22,18:12:00\",\n" +
                "    \"postageFee\": \"2000\",\n" +
//                "    \"convertFlag\": false,\n" +
                "    \"orderSource\": \"other\",\n" +
                "    \"orderSourcePlatform\": \"其他\",\n" +
                "    \"costFee\": \"0\",\n" +
                "    \"tipFee\": \"0\",\n" +
                "    \"insuredFee\": \"0\",\n" +
                "    \"otherFee1\": \"0\",\n" +
                "    \"otherFee2\": \"0\",\n" +
                "    \"packageDesc\": \"药品\",\n" +
                "    \"remark\": \"手机尾号1234\",\n" +
                "    \"addressFirst\": {\n" +
                "        \"senderProviceCode\": \"340000\",\n" +
                "        \"senderProviceName\": \"安徽省\",\n" +
                "        \"senderCityCode\": \"340100\",\n" +
                "        \"senderCityName\": \"合肥市\",\n" +
                "        \"senderCountyCode\": \"340102\",\n" +
                "        \"senderCountyName\": \"瑶海区\",\n" +
                "        \"senderContactName\": \"中山三院\",\n" +
                "        \"senderContactPhone\": \"17523029470\",\n" +
                "        \"senderLongitude\": \"117.307179\",\n" +
                "        \"senderLatitude\": \"31.856484\",\n" +
                "        \"senderStreetCode\": \"001\",\n" +
                "        \"senderStreetName\": \"明光路街道\",\n" +
                "        \"senderAddr\": \"明光路188号\",\n" +
                "        \"receiverProviceCode\": \"340000\",\n" +
                "        \"receiverProviceName\": \"安徽省\",\n" +
                "        \"receiverCityCode\": \"340100\",\n" +
                "        \"receiverCityName\": \"合肥市\",\n" +
                "        \"receiverCountyCode\": \"340103\",\n" +
                "        \"receiverCountyName\": \"庐阳区\",\n" +
                "        \"receiverContactName\": \"陈先生\",\n" +
                "        \"receiverContactPhone\": \"13711511111\",\n" +
                "        \"receiverAddr\": \"信源大厦20楼2007\",\n" +
                "        \"receiverLongitude\": \"117.285075\",\n" +
                "        \"receiverLatitude\": \"31.882703\",\n" +
                "        \"receiverStreetCode\": \"002\",\n" +
                "        \"receiverStreetName\": \"双岗街道\"\n" +
                "    },\n" +
                "    \"businessJson\": {\n" +
//                "        \"a\": \"a\",\n" +
//                "        \"b\": \"b\"\n" +
                "    }\n" +
                "}";
        //查询物流轨迹
//        String body = "{\n" +
//                "    \"expressNbr\": \"1158114584567\",\n" +
//                "    \"channelOrderNo\": \"\"\n" +
//                "}";
        //get请求，组装成 k v类型
//        sortedMap.put(paramName,request.getParameter(paramName));
        /**
         * post请求，需要放data
         */
        String bodyjson = JSON.toJSONString(StrUtil.removeAll(body, specialChars));
        System.out.println("bodyjson: " + bodyjson);

        sortedMap.put("data", JSON.toJSONString(StrUtil.removeAll(body, specialChars)));
        sortedMap.put("access_id", clientKey);
        sortedMap.put("nonce", nonce);
        sortedMap.put("timestamp", timeStamp);
        //排序
        Map<String, Object> sortedParams = new TreeMap<>(sortedMap);
        Set<Map.Entry<String, Object>> entrySet = sortedParams.entrySet();
        // 遍历排序后的字典，将所有参数按"key=value"格式拼接在一起
        StringBuilder stringToSign = new StringBuilder();
        for (Map.Entry<String, Object> param : entrySet) {
            stringToSign.append(param.getKey()).append("=").append(param.getValue()).append(",");
        }
        stringToSign.append("accessSecret").append("=").append(clientSecret);
        String genSign = CityDelivery_SM4Util.encryptCBC(stringToSign.toString(), clientSecret, sm4Key);
        System.out.println("sign:" + genSign);
    }

    /**
     * 同城急送平台工具，post接口
     */
    public String addOrder(BizOrder bizOrder,BizOrderSub bizOrderSub) {
        String url = "https://wyjcs.gdwyj.cn/szelec/city-delivery-api/api/base/order/add";
        String goodsType = bizOrderSub.getGoodsType();
        String goodsTypeLabel = sysDictDataMapper.selectDictLabel("sys_goods_type", goodsType);

        String payType = bizOrder.getPayType();
        if (payType == null) payType = "";
        String subOrderSn = bizOrderSub.getSubOrderSn();
        if (subOrderSn == null) subOrderSn = "";
        BigDecimal weight = bizOrderSub.getWeight();
        int intweight = Helper.FInt(weight.multiply(new BigDecimal(1000)));
        String remark = bizOrderSub.getRemark();
        if (remark == null) remark = "";

        BigDecimal fee = bizOrderSub.getOrderFee();
        int intfee = Helper.FInt(fee.multiply(new BigDecimal(100)));

        // 寄件人信息
        String senderArea = bizOrder.getSenderArea(); // 寄件人街道代码
        if (senderArea == null) senderArea = "";
        String senderProviceCode = "";
        if (ObjectUtil.isNotEmpty(senderArea)){
            senderProviceCode = senderArea.substring(0,2) + "0000"; // 寄件人省份代码(6位国标码)
        }
        String senderProviceName = "";
        SysArea sysArea1 = sysAreaMapper.selectById(senderProviceCode + "000000");
        if (ObjectUtil.isNotEmpty(sysArea1)){
            senderProviceName = sysArea1.getName(); // 寄件人省份名称
        }
        String senderCityCode = "";
        if (ObjectUtil.isNotEmpty(senderArea)){
            senderCityCode = senderArea.substring(0,4) + "00"; // 寄件人城市代码(6位国标码)
        }
        String senderCityName = "";
        SysArea sysArea2 = sysAreaMapper.selectById(senderCityCode + "000000");
        if (ObjectUtil.isNotEmpty(sysArea2)){
            senderCityName = sysArea2.getName(); // 寄件人城市名称
        }
        String senderCountyCode = "";
        if (ObjectUtil.isNotEmpty(senderArea)){
            senderCountyCode = senderArea.substring(0,6); // 寄件人区县代码(6位国标码)
        }
        String senderCountyName = "";
        SysArea sysArea3 = sysAreaMapper.selectById(senderCountyCode + "000000");
        if (ObjectUtil.isNotEmpty(sysArea3)){
            senderCountyName = sysArea3.getName(); // 寄件人区县名称
        }
        String senderStreetName = "";
        SysArea sysArea4 = sysAreaMapper.selectById(senderArea);
        if (ObjectUtil.isNotEmpty(sysArea4)){
            senderStreetName = sysArea4.getName(); // 寄件人街道名称
        }
        String senderAddress = bizOrder.getSenderAddress(); // 寄件人详细地址
        if (senderAddress == null) senderAddress = "";
        String senderName = bizOrder.getSenderName(); // 寄件人姓名
        if (senderName == null) senderName = "";
        String senderPhone = bizOrder.getSenderPhone(); // 寄件人手机号
        if (senderPhone == null) senderPhone = "";

        String senderLng = ""; // 寄件人经度
        if (bizOrder.getSenderLng() != null){
            senderLng = bizOrder.getSenderLng().toString();
        }
        String senderLat = ""; // 寄件人纬度
        if (bizOrder.getSenderLat() != null){
            senderLat = bizOrder.getSenderLat().toString();
        }

        // 收件人信息
        String receiverArea = bizOrder.getReceiverArea(); // 收件人街道代码
        if (receiverArea == null) receiverArea = "";
        String receiverProviceCode = "";
        if (ObjectUtil.isNotEmpty(receiverArea)){
            receiverProviceCode = receiverArea.substring(0,2) + "0000"; // 收件人省份代码(6位国标码)
        }
        String receiverProviceName = "";
        SysArea sysArea5 = sysAreaMapper.selectById(receiverProviceCode + "000000");
        if (ObjectUtil.isNotEmpty(sysArea5)){
            receiverProviceName = sysArea5.getName(); // 收件人省份名称
        }
        String receiverCityCode = "";
        if (ObjectUtil.isNotEmpty(receiverArea)){
            receiverCityCode = receiverArea.substring(0,4) + "00"; // 收件人城市代码(6位国标码)
        }
        String receiverCityName = "";
        SysArea sysArea6 = sysAreaMapper.selectById(receiverCityCode + "000000");
        if (ObjectUtil.isNotEmpty(sysArea6)){
            receiverCityName = sysArea6.getName(); // 收件人城市名称
        }
        String receiverCountyCode = "";
        if (ObjectUtil.isNotEmpty(receiverArea)){
            receiverCountyCode = receiverArea.substring(0,6); // 收件人区县代码(6位国标码)
        }
        String receiverCountyName = "";
        SysArea sysArea7 = sysAreaMapper.selectById(receiverCountyCode + "000000");
        if (ObjectUtil.isNotEmpty(sysArea7)){
            receiverCountyName = sysArea7.getName(); // 收件人区县名称
        }
        String receiverStreetName = "";
        SysArea sysArea8 = sysAreaMapper.selectById(receiverArea);
        if (ObjectUtil.isNotEmpty(sysArea8)){
            receiverStreetName = sysArea8.getName(); // 收件人街道名称
        }
        String receiverAddress = bizOrder.getReceiverAddress(); // 收件人详细地址
        String receiverName = bizOrder.getReceiverName(); // 收件人姓名
        String receiverPhone = bizOrder.getReceiverPhone(); // 收件人手机号
        String receiverLng = ""; // 收件人经度
        if (bizOrder.getReceiverLng() != null){
            receiverLng = bizOrder.getReceiverLng().toString();
        }
        String receiverLat = ""; // 收件人纬度
        if (bizOrder.getReceiverLat() != null){
            receiverLat = bizOrder.getReceiverLat().toString();
        }
        Date createtime = bizOrder.getCreateTime();
        Date exptime = new Date();
        if (createtime!=null)
            exptime = DateUtils.addHours(createtime, 5);
        String exptime_str = DateUtils.parseDateToStr("yyyy-MM-dd,HH:mm:ss", exptime);

        Long timeStamp = System.currentTimeMillis();
        String nonce = UUID.randomUUID().toString().replace("-", "");
        System.out.println("nonce:" + nonce);
        System.out.println("timeStamp:" + timeStamp);
        //签名
        Map<String, Object> sortedMap = new TreeMap<>();
        //下单接口
        String body = "{\n" +
                "    \"customerCode\": \"HFCP\",\n" +
                "    \"payTypeFirst\": \"" + payType + "\",\n" +
                "    \"channelOrderNo\": \"" + subOrderSn + "\",\n" +
                "    \"platformOrderNo\": \"\",\n" +
                "    \"expressNbr\": \"\",\n" +
                "    \"coordinateTransformType\": \"02\",\n" +
                "    \"weight\": \"" + intweight + "\",\n" +
                "    \"expectedTime\": \""+exptime_str+"\",\n" +
                "    \"postageFee\": \""+ intfee +"\",\n" +
//                "    \"convertFlag\": false,\n" +
                "    \"orderSource\": \"other\",\n" +
                "    \"orderSourcePlatform\": \"其他\",\n" +
                "    \"costFee\": \"0\",\n" +
                "    \"tipFee\": \"0\",\n" +
                "    \"insuredFee\": \"0\",\n" +
                "    \"otherFee1\": \"0\",\n" +
                "    \"otherFee2\": \"0\",\n" +
                "    \"packageDesc\": \"" + goodsTypeLabel + "\",\n" +
                "    \"remark\": \"" + remark + "\",\n" +
                "    \"addressFirst\": {\n" +
                "        \"senderProviceCode\": \"" + senderProviceCode + "\",\n" +
                "        \"senderProviceName\": \"" + senderProviceName + "\",\n" +
                "        \"senderCityCode\": \"" + senderCityCode + "\",\n" +
                "        \"senderCityName\": \"" + senderCityName + "\",\n" +
                "        \"senderCountyCode\": \"" + senderCountyCode + "\",\n" +
                "        \"senderCountyName\": \"" + senderCountyName + "\",\n" +
                "        \"senderContactName\": \"" + senderName + "\",\n" +
                "        \"senderContactPhone\": \"" + senderPhone + "\",\n" +
                "        \"senderLongitude\": \"" + senderLng + "\",\n" +
                "        \"senderLatitude\": \"" + senderLat + "\",\n" +
                "        \"senderStreetCode\": \"" + senderArea + "\",\n" +
                "        \"senderStreetName\": \"" + senderStreetName + "\",\n" +
                "        \"senderAddr\": \"" + senderAddress + "\",\n" +
                "        \"receiverProviceCode\": \"" + receiverProviceCode + "\",\n" +
                "        \"receiverProviceName\": \"" + receiverProviceName + "\",\n" +
                "        \"receiverCityCode\": \"" + receiverCityCode + "\",\n" +
                "        \"receiverCityName\": \"" + receiverCityName + "\",\n" +
                "        \"receiverCountyCode\": \"" + receiverCountyCode + "\",\n" +
                "        \"receiverCountyName\": \"" + receiverCountyName + "\",\n" +
                "        \"receiverContactName\": \"" + receiverName + "\",\n" +
                "        \"receiverContactPhone\": \"" + receiverPhone + "\",\n" +
                "        \"receiverAddr\": \"" + receiverAddress + "\",\n" +
                "        \"receiverLongitude\": \"" + receiverLng + "\",\n" +
                "        \"receiverLatitude\": \"" + receiverLat + "\",\n" +
                "        \"receiverStreetCode\": \"" + receiverArea + "\",\n" +
                "        \"receiverStreetName\": \"" + receiverStreetName + "\"\n" +
                "    },\n" +
                "    \"businessJson\": {}\n" +
                "}";

        // 获取最新token
        String token = refreshToken();
        log.info("========================================准备请求========================================");
        log.info("body：" + body);
        /**
         * post请求，需要放data
         */
        String bodyjson = JSON.toJSONString(StrUtil.removeAll(body, specialChars));

        sortedMap.put("data", bodyjson);
        sortedMap.put("access_id", clientKey);
        sortedMap.put("nonce", nonce);
        sortedMap.put("timestamp", timeStamp);

        //排序
        Map<String, Object> sortedParams = new TreeMap<>(sortedMap);
        Set<Map.Entry<String, Object>> entrySet = sortedParams.entrySet();
        // 遍历排序后的字典，将所有参数按"key=value"格式拼接在一起
        StringBuilder stringToSign = new StringBuilder();
        for (Map.Entry<String, Object> param : entrySet) {
            stringToSign.append(param.getKey()).append("=").append(param.getValue()).append(",");
        }
        stringToSign.append("accessSecret").append("=").append(clientSecret);
        String genSign = CityDelivery_SM4Util.encryptCBC(stringToSign.toString(), clientSecret, sm4Key);

        log.info("bodyjson：" + bodyjson);
        System.out.println("bodyjson: " + bodyjson);
        log.info("nonce：" + nonce);
        System.out.println("nonce：" + nonce);
        log.info("timestamp：" + timeStamp);
        System.out.println("timestamp：" + timeStamp);
        log.info("sign：" + genSign);
        System.out.println("sign：" + genSign);
        log.info("Authorization：" + token);
        System.out.println("Authorization：" + token);
        log.info("========================================开始请求========================================");
//                .body(JSONObject.toJSONString(body))
        HttpResponse response = HttpRequest.post(url)
                .body(StrUtil.removeAll(body, specialChars))
                .header("Content-Type", "application/json")
                .header("client_type", client_type)
                .header("app_type", appType)
                .header("access_id", clientKey)
                .header("nonce", nonce)
                .header("timestamp", timeStamp.toString())
                .header("sign", genSign)
                .header("Authorization", token)
                .execute();
        log.info("========================================请求结果========================================");
        System.out.println("response：" + response);
        log.info("response：" + response);
        return response.body();
    }

    /**
     * 同城急送平台工具，获取token
     */
//    @Anonymous
//    @GetMapping("/getToken2")
    public void getTokenPostMan() {
        Long timeStamp = System.currentTimeMillis();
        String nonce = UUID.randomUUID().toString().replace("-", "");
        System.out.println("timeStamp：" + timeStamp);
        System.out.println("nonce：" + nonce);
        //body
        Map map = new HashMap();
        map.put("clientKey", clientKey);
        map.put("clientSecret", clientSecret);
        map.put("appType", appType);
        map.put("clientType", client_type);
        map.put("version", version);

        Map<String, Object> sortedParams1 = new TreeMap<>(map);
        Set<Map.Entry<String, Object>> entrySet1 = sortedParams1.entrySet();
        StringBuilder stringToSign = new StringBuilder();
        for (Map.Entry<String, Object> param : entrySet1) {
            stringToSign.append(param.getKey()).append("=").append(param.getValue()).append(",");
        }

        String encryptBase64 = CityDelivery_SM4Util.encryptCBC(stringToSign.toString(), clientSecret, sm4Key);
        System.out.println("body:" + encryptBase64);
        //签名
        Map<String, Object> sortedMap2 = new TreeMap<>();

        //get请求，组装成 k v类型
//        sortedMap2.put(paramName,request.getParameter(paramName));
        /**
         * post请求，需要放data
         */
        Map bodyMap = new HashMap();
        bodyMap.put("body", encryptBase64);
        String bodyJson = JSON.toJSONString(bodyMap);
        sortedMap2.put("data", JSON.toJSONString(bodyJson));
        sortedMap2.put("access_id", clientKey);
        sortedMap2.put("nonce", nonce);
        sortedMap2.put("timestamp", timeStamp);
        //排序
        Map<String, Object> sortedParams2 = new TreeMap<>(sortedMap2);
        Set<Map.Entry<String, Object>> entrySet2 = sortedParams2.entrySet();
        // 遍历排序后的字典，将所有参数按"key=value"格式拼接在一起
        StringBuilder stringToSign2 = new StringBuilder();
        for (Map.Entry<String, Object> param : entrySet2) {
            stringToSign2.append(param.getKey()).append("=").append(param.getValue()).append(",");
        }
        stringToSign2.append("accessSecret").append("=").append(clientSecret);
//        String genSign = DigestUtils.md5Hex(stringToSign2.toString());
        String genSign = CityDelivery_SM4Util.encryptCBC(stringToSign2.toString(), clientSecret, sm4Key);
        System.out.println("------------");
        System.out.println("sign:" + genSign);
    }

    //获取token
//    @Anonymous
//    @GetMapping("/getToken")
    public void getToken() {
        System.out.println("获取token");
        String url = "https://wyjcs.gdwyj.cn/szelec/city-delivery-api/api/mini/app/login/token";
        String nonce = UUID.randomUUID().toString().replace("-", "");
        Long timestamp = System.currentTimeMillis();
        //body
        Map map = new HashMap();
        map.put("clientKey", clientKey);
        map.put("clientSecret", clientSecret);
        map.put("appType", appType);
        map.put("clientType", client_type);
        map.put("version", version);

        Map<String, Object> sortedParams1 = new TreeMap<>(map);
        Set<Map.Entry<String, Object>> entrySet1 = sortedParams1.entrySet();
        StringBuilder stringToSign = new StringBuilder();
        for (Map.Entry<String, Object> param : entrySet1) {
            stringToSign.append(param.getKey()).append("=").append(param.getValue()).append(",");
        }

        String encryptBase64 = CityDelivery_SM4Util.encryptCBC(stringToSign.toString(), clientSecret, sm4Key);
        System.out.println("body:" + encryptBase64);
        //签名
        Map<String, Object> sortedMap2 = new TreeMap<>();

        //get请求，组装成 k v类型
//        sortedMap2.put(paramName,request.getParameter(paramName));
        /**
         * post请求，需要放data
         */
        Map bodyMap = new HashMap();
        bodyMap.put("body", encryptBase64);
        String bodyJson = JSON.toJSONString(bodyMap);
        sortedMap2.put("data", JSON.toJSONString(bodyJson));
        sortedMap2.put("access_id", clientKey);
        sortedMap2.put("nonce", nonce);
        sortedMap2.put("timestamp", timestamp);
        //排序
        Map<String, Object> sortedParams2 = new TreeMap<>(sortedMap2);
        Set<Map.Entry<String, Object>> entrySet2 = sortedParams2.entrySet();
        // 遍历排序后的字典，将所有参数按"key=value"格式拼接在一起
        StringBuilder stringToSign2 = new StringBuilder();
        for (Map.Entry<String, Object> param : entrySet2) {
            stringToSign2.append(param.getKey()).append("=").append(param.getValue()).append(",");
        }
        stringToSign2.append("accessSecret").append("=").append(clientSecret);
//        String genSign = DigestUtils.md5Hex(stringToSign2.toString());
        String genSign = CityDelivery_SM4Util.encryptCBC(stringToSign2.toString(), clientSecret, sm4Key);
        System.out.println("------------");
        System.out.println("sign:" + genSign);

        HttpResponse response = HttpRequest.post(url)
                .body(JSON.toJSONString(bodyMap))
                .header("Content-Type", "application/json")
                .header("client_type", client_type)
                .header("app_type", appType)
                .header("access_id", clientKey)
                .header("nonce", nonce)
                .header("timestamp", timestamp.toString())
                .header("sign", genSign)
                .execute();
        log.info("response：" + response);
        System.out.println("response：" + response);
        log.info("responseBody：" + response.body());
        System.out.println("responseBody：" + response.body());

        JSONObject jsonObject = JSON.parseObject(response.body());
        Map<String, Object> result = (Map<String, Object>) jsonObject.get("result");
        String token = (String) result.get("token");
        Long expirationTimestamp = (Long) result.get("expirationTimestamp");
        redisTemplate.opsForValue().set("city_delivery_token", token);
        redisTemplate.opsForValue().set("city_delivery_token_expirationTimestamp", expirationTimestamp);
    }

    /*@GetMapping("/111")
    @Anonymous
    public void test(){
        String body = "{\"success\":true,\"message\":\"\",\"code\":200,\"result\":{\"expirationTimestamp\":1737624154641,\"token\":\"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiIxMDAwMiIsInJuU3RyIjoiRDA5VGtjUTZ4Y3dobjdMNDFISXdWTjM1bGdScWt1cVkiLCJjbGllbnRUeXBlIjoib3RoZXIiLCJ1c2VySWQiOiIxMDAwMiJ9.yA3zq8e4uDZCLBlMpL0dfyIfkP3zoegcNt9iHSjB-jM\"},\"timestamp\":1737620554641}\n";
        JSONObject jsonObject = JSON.parseObject(body);
        System.out.println("jsonObject = " + jsonObject);

    }*/

    /**
     * 刷新token
     *
     * @return token
     */
    public String refreshToken() {
        String cacheToken = (String) redisTemplate.opsForValue().get("city_delivery_token");
        if (ObjectUtil.isNotEmpty(cacheToken)) {
            long expirationTimestamp = (long)redisTemplate.opsForValue().get("city_delivery_token_expirationTimestamp");
            long currentTimeMillis = System.currentTimeMillis();
            if (expirationTimestamp > currentTimeMillis) { // 没过期
                return cacheToken;
            } else { // 过期，重新获取
                getToken();
                return (String) redisTemplate.opsForValue().get("city_delivery_token");
            }
        } else {
            getToken();
            return (String) redisTemplate.opsForValue().get("city_delivery_token");
        }
    }
}
