package org.xyzh.common.jdbc.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;
import org.xyzh.common.jdbc.handler.EncryptedStringTypeHandler;
import org.xyzh.common.utils.crypto.AesEncryptUtil;

import jakarta.annotation.PostConstruct;

/**
 * TypeHandler 配置类
 * 用于初始化 EncryptedStringTypeHandler 中的 AesEncryptUtil
 * 
 * @author yslg
 * @since 2025-12-11
 */
@Configuration
@ConditionalOnClass(AesEncryptUtil.class)
public class TypeHandlerConfig {

    private final AesEncryptUtil aesEncryptUtil;

    @Autowired(required = false)
    public TypeHandlerConfig(AesEncryptUtil aesEncryptUtil) {
        this.aesEncryptUtil = aesEncryptUtil;
    }

    @PostConstruct
    public void init() {
        // 初始化 TypeHandler 中的静态 AesEncryptUtil 实例
        if (aesEncryptUtil != null) {
            EncryptedStringTypeHandler.setAesEncryptUtil(aesEncryptUtil);
            System.out.println("✓ TypeHandler 已初始化 AES 加密工具");
        } else {
            System.err.println("警告: AesEncryptUtil 未能注入，加密功能可能无法正常工作");
        }
    }
}
