package com.cpxt.web.controller.biz;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.cpxt.biz.orderutil.CityDelivery_SM4Util;
import com.cpxt.common.annotation.Anonymous;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.UUID;

@RestController
@Slf4j
@RequestMapping("/test")
public class TestContrller {
    private static final char[] specialChars = {'\n', '\r', '\t', '\f', '\b', ' '};

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

    @Anonymous
    @GetMapping("/getOrder")
    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\": \"02\",\n" +
                "    \"channelOrderNo\": \"Y202501221001-1\",\n" +
                "    \"platformOrderNo\": \"\",\n" +
                "    \"expressNbr\": \"\",\n" +
                "    \"coordinateTransformType\": \"02\",\n" +
                "    \"weight\": \"1000\",\n" +
                "    \"expectedTime\": \"2025-02-10,21:00:00\",\n" +
                "    \"postageFee\": \"800\",\n" +
                "    \"orderSource\": \"other\",\n" +
                "    \"orderSourcePlatform\": \"其他\",\n" +
                "    \"costFee\": \"0\",\n" +
                "    \"tipFee\": \"0\",\n" +
                "    \"insuredFee\": \"0\",\n" +
                "    \"otherFee1\": \"0\",\n" +
                "    \"otherFee2\": \"0\",\n" +
                "    \"packageDesc\": \"食品\",\n" +
                "    \"remark\": \"\",\n" +
                "    \"addressFirst\": {\n" +
                "        \"senderProviceCode\": \"340000\",\n" +
                "        \"senderProviceName\": \"安徽省\",\n" +
                "        \"senderCityCode\": \"340100\",\n" +
                "        \"senderCityName\": \"合肥市\",\n" +
                "        \"senderCountyCode\": \"340121\",\n" +
                "        \"senderCountyName\": \"长丰县\",\n" +
                "        \"senderContactName\": \"巴莉甜甜工厂\",\n" +
                "        \"senderContactPhone\": \"15856235263\",\n" +
                "        \"senderLongitude\": \"117.30488400\",\n" +
                "        \"senderLatitude\": \"31.87844600\",\n" +
                "        \"senderStreetCode\": \"340121400000\",\n" +
                "        \"senderStreetName\": \"双凤开发区\",\n" +
                "        \"senderAddr\": \"合肥市长丰县凤麟路10号\",\n" +
                "        \"receiverProviceCode\": \"340000\",\n" +
                "        \"receiverProviceName\": \"安徽省\",\n" +
                "        \"receiverCityCode\": \"340100\",\n" +
                "        \"receiverCityName\": \"合肥市\",\n" +
                "        \"receiverCountyCode\": \"340122\",\n" +
                "        \"receiverCountyName\": \"肥东县\",\n" +
                "        \"receiverContactName\": \"舒城港汇\",\n" +
                "        \"receiverContactPhone\": \"15856235263\",\n" +
                "        \"receiverAddr\": \"巴莉甜甜（舒城港汇店）\",\n" +
                "        \"receiverLongitude\": \"116.93721100\",\n" +
                "        \"receiverLatitude\": \"31.47053900\",\n" +
                "        \"receiverStreetCode\": \"340122402000\",\n" +
                "        \"receiverStreetName\": \"合肥循环经济示范园\"\n" +
                "    },\n" +
                "    \"businessJson\": {\n" +
                "    }\n" +
                "}";

        String bodyjson = JSON.toJSONString(StrUtil.removeAll(body, specialChars));
        System.out.println("bodyjson: " + bodyjson);

        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);
        System.out.println("sign:" + genSign);

        log.info("bodyjson：" + bodyjson);
        log.info("nonce：" + nonce);
        log.info("timestamp：" + timeStamp);
        log.info("sign：" + genSign);
    }
}
