package com.yd.csf.service.config;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.yd.auth.core.dto.AuthUserDto;
import com.yd.auth.core.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.time.LocalDateTime;

@Slf4j
@Configuration
public class CsfMybatisPlusConfig {

    /**
     * 分页插件配置（必须）
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 使用正确的枚举值 DbType.MYSQL
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    /**
     * 自动填充字段配置（如创建人、更新人等）
     */
    @Bean
    @Primary
    public MetaObjectHandler metaObjectHandler() {
        log.info("MetaObjectHandler Bean 正在创建...","MetaObjectHandler Bean 正在创建==========");
        return new MetaObjectHandler() {
            @Override
            public void insertFill(MetaObject metaObject) {
                //获取Security上下文当前用户的登录信息
                AuthUserDto authUserDto = null;
                try {
                    authUserDto = SecurityUtil.getCurrentLoginUser();
                }catch (Exception e) {
                    log.info("获取Security上下文当前用户的登录信息失败，{}",e.getMessage());
                }
                log.info("metaObjectHandler=======insertFill自动填充获取用户信息：{}", JSON.toJSONString(authUserDto));
                if (authUserDto != null) {
                    //自动注入创建人和创建用户名
                    this.strictInsertFill(metaObject, "creatorId", String.class, authUserDto.getUserBizId());
                    this.strictInsertFill(metaObject, "creatorName", String.class, authUserDto.getRealName());
                }
            }

            @Override
            public void updateFill(MetaObject metaObject) {
                //获取Security上下文当前用户的登录信息
                AuthUserDto authUserDto = null;
                try {
                    authUserDto = SecurityUtil.getCurrentLoginUser();
                }catch (Exception e) {
                    log.info("获取Security上下文当前用户的登录信息失败，{}",e.getMessage());
                }
                log.info("metaObjectHandler=======updateFill自动填充获取用户信息：{}", JSON.toJSONString(authUserDto));
                if (authUserDto != null) {
                    //自动注入更新人和更新用户名
                    this.strictUpdateFill(metaObject, "updaterId", String.class, authUserDto.getUserBizId());
                    this.strictUpdateFill(metaObject, "updaterName", String.class, authUserDto.getRealName());
                }
            }
        };
    }
}
