package com.bonus.canteen.core.auth.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.ListIterator;

@Configuration
public class LeNiuMybatisPlusConfigRunner implements ApplicationRunner {
   private static final Logger log = LoggerFactory.getLogger(LeNiuMybatisPlusConfigRunner.class);
   @Autowired
   private MybatisPlusInterceptor mybatisPlusInterceptor;
//   @Autowired
//   private DbProperties dbProperties;

   public void run(ApplicationArguments args) throws Exception {
      List<InnerInterceptor> interceptors = Lists.newArrayList();

      InnerInterceptor interceptor;
      for(ListIterator<InnerInterceptor> iterator = this.mybatisPlusInterceptor.getInterceptors().listIterator(); iterator.hasNext(); interceptors.add(interceptor)) {
         interceptor = (InnerInterceptor)iterator.next();
         if (interceptor instanceof PaginationInnerInterceptor) {
            LeNiuDataPermissionInterceptor leNiuDataPermissionInterceptor = new LeNiuDataPermissionInterceptor();
            leNiuDataPermissionInterceptor.setDataPermissionHandler(new LeNiuDataPermissionHandler());
            leNiuDataPermissionInterceptor.setDatabaseType("mysql");
            interceptors.add(leNiuDataPermissionInterceptor);
//            QueryExtensionInterceptor queryExtensionInterceptor = new QueryExtensionInterceptor();
//            interceptors.add(queryExtensionInterceptor);
         }
      }

      this.mybatisPlusInterceptor.setInterceptors(interceptors);
   }
}
