content: /*
   * Copyright 2023-2025 Licensed under the Dual Licensing
   * website: https://aizuda.com
   */
  package test.mysql.config;
  
  import com.aizuda.bpm.engine.FlowLongEngine;
  import com.aizuda.bpm.spring.autoconfigure.FlowLongAutoConfiguration;
  import com.baomidou.mybatisplus.core.MybatisConfiguration;
  import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
  import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
  import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
  import org.apache.ibatis.session.SqlSessionFactory;
  import org.apache.ibatis.type.JdbcType;
  import org.mybatis.spring.annotation.MapperScan;
  import org.springframework.beans.factory.ObjectProvider;
  import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.core.env.Environment;
  import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  import org.springframework.jdbc.support.JdbcTransactionManager;
  import org.springframework.transaction.TransactionManager;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  
  import javax.sql.DataSource;
  
  /**
   * 测试 Mysql 配置文件
   */
  @Configuration
  @EnableTransactionManagement
  @MapperScan("com.aizuda.bpm.mybatisplus.mapper")
  public class MysqlConfig extends FlowLongAutoConfiguration {
  
      @Bean("mybatisSqlSession")
      public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
          MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
          /* 数据源 */
          sqlSessionFactory.setDataSource(dataSource);
          /* 扫描 typeHandler */
          MybatisConfiguration configuration = new MybatisConfiguration();
          configuration.setJdbcTypeForNull(JdbcType.NULL);
          /* 驼峰转下划线 */
          configuration.setMapUnderscoreToCamelCase(true);
          MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
          mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
          sqlSessionFactory.setPlugins(mybatisPlusInterceptor);
          sqlSessionFactory.setConfiguration(configuration);
          return sqlSessionFactory.getObject();
      }
  
      @Bean
      @ConditionalOnMissingBean(TransactionManager.class)
      DataSourceTransactionManager transactionManager(Environment environment, DataSource dataSource,
                                                      ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
          DataSourceTransactionManager transactionManager = createTransactionManager(environment, dataSource);
          transactionManagerCustomizers.ifAvailable((customizers) -> customizers.customize(transactionManager));
          return transactionManager;
      }
  
      private DataSourceTransactionManager createTransactionManager(Environment environment, DataSource dataSource) {
          return environment.getProperty("spring.dao.exceptiontranslation.enabled", Boolean.class, Boolean.TRUE)
                  ? new JdbcTransactionManager(dataSource) : new DataSourceTransactionManager(dataSource);
      }
  
      @Bean
      public TestTaskAccessStrategy taskAccessStrategy() {
          return new TestTaskAccessStrategy();
      }
  
      @Bean
      public TestTaskActorProvider testTaskActorProvider() {
          return new TestTaskActorProvider();
      }
  
      @Bean
      public TestTaskListener testTaskListener(FlowLongEngine flowLongEngine) {
          return new TestTaskListener(flowLongEngine);
      }
  }
  