package com.yami.shop.utils;

import com.yami.shop.bean.dto.hb.HBBaseReq;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import static cn.hutool.crypto.SecureUtil.md5;


/**
 * 海博签名工具类
 *
 * @author mr wang
 * @version 1.0.0
 * @since 2025-08-29
 */
@Slf4j
@Configuration
public class HBSignUtil {
    @Getter
    @Value("${hb.host:https://p-open.hiboos.com}")
    private String hBHost;
    @Getter
    @Value("${hb.appId:5347a7077e65428bb9a87510ca7e1ea1}")
    private String appId;
    @Value("${hb.secret:67899367b17e4a8d8a7d10ebe8449b90}")
    private String secret;

    /**
     * 生成海博签名
     *
     * @param req 请求对象 {@link HBBaseReq}
     * @return md5 {@link String}
     */
    public <T> String signMd5(HBBaseReq<T> req) {
        return signMd5(appId, req.getBody(), req.getSignMethod(), req.getTimestamp());
    }

    /**
     * 生成海博签名
     *
     * @param appId      平台appid
     * @param body       具体参数
     * @param signMethod 加密方式，当前平台仅支持md5
     * @param timestamp  当前时间戳
     * @return md5 {@link String}
     */
    public <T> String signMd5(String appId, T body, String signMethod, Long timestamp) {
        String builder = secret + "appId" +
                appId +
                "body" +
                 body +
                "signMethod" +
                signMethod +
                "timestamp" +
                timestamp +
                secret;
        log.info("----------------------");
        log.info(builder);
        return md5(builder).toUpperCase();
    }

    /**
     * 生成海博签名
     *
     * @param appId     平台appid
     * @param body      具体参数
     * @param timestamp 当前时间戳
     * @return md5 {@link String}
     */
    public <T> String signMd5(String appId, T body, Long timestamp) {
        return signMd5(appId, body, "md5", timestamp);
    }

}
