package com.zt.plat.module.qms.framework.mybatis;


import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import com.zt.plat.module.qms.framework.datapermission.QMSMultiDataPermissionHandler;
import jakarta.annotation.PostConstruct;
import org.aspectj.lang.annotation.After;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
public class QMSDataPermissionConfig implements SmartInitializingSingleton {
    @Autowired
    private MybatisPlusInterceptor mybatisPlusInterceptor;

    @Autowired
    private QMSMultiDataPermissionHandler qmsMultiDataPermissionHandler;

    @Override
    public void afterSingletonsInstantiated() {
        List<InnerInterceptor> interceptors = mybatisPlusInterceptor.getInterceptors();
        // 避免重复注册
        boolean exists = interceptors.stream()
                .filter(i -> i instanceof DataPermissionInterceptor)
                .map(i -> (DataPermissionInterceptor) i)
                .anyMatch(i -> i.getDataPermissionHandler() == qmsMultiDataPermissionHandler);
        if (!exists) {
            mybatisPlusInterceptor.addInnerInterceptor(new DataPermissionInterceptor(qmsMultiDataPermissionHandler));
        }
    }

//    @PostConstruct
//    public void addDataPermissionInterceptor() {
//
//    }

}
