
package org.jeecg.modules.workFlow.flowlong.spring.autoconfigure;

import org.apache.ibatis.session.SqlSessionFactory;
import org.jeecg.modules.workFlow.flowlong.engine.dao.*;
import org.jeecg.modules.workFlow.flowlong.mybatisplus.impl.*;
import org.jeecg.modules.workFlow.flowlong.mybatisplus.mapper.*;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;

/**
 * FlowLong MybatisPlus 加载配置处理类
 *

 */
@Configuration
public class FlowLongMybatisPlusConfiguration {

    @Bean
    public MapperFactoryBean<?>[] registerMappers(SqlSessionFactory sqlSessionFactory) throws Exception {
        List<Class<?>> mapperClasses = Arrays.asList(
                FlwExtInstanceMapper.class,
                FlwHisInstanceMapper.class,
                FlwHisTaskActorMapper.class,
                FlwHisTaskMapper.class,
                FlwInstanceMapper.class,
                FlwProcessMapper.class,
                FlwTaskActorMapper.class,
                FlwTaskMapper.class
        );

        MapperFactoryBean<?>[] beans = new MapperFactoryBean[mapperClasses.size()];
        for (int i = 0; i < mapperClasses.size(); i++) {
            MapperFactoryBean<?> factoryBean = new MapperFactoryBean<>(mapperClasses.get(i));
            factoryBean.setSqlSessionFactory(sqlSessionFactory);
            // Spring 会根据方法返回值注册 Bean 名称（默认首字母小写类名）
            factoryBean.afterPropertiesSet();
            beans[i] = factoryBean;
        }
        return beans;
    }

    @Bean
    @ConditionalOnMissingBean
    public FlwExtInstanceDao extInstanceDao(FlwExtInstanceMapper extInstanceMapper) {
        return new FlwExtInstanceDaoImpl(extInstanceMapper);
    }

    @Bean
    @ConditionalOnMissingBean
    public FlwHisInstanceDao hisInstanceDao(FlwHisInstanceMapper hisInstanceMapper) {
        return new FlwHisInstanceDaoImpl(hisInstanceMapper);
    }

    @Bean
    @ConditionalOnMissingBean
    public FlwHisTaskActorDao hisTaskActorDao(FlwHisTaskActorMapper hisInstanceMapper) {
        return new FlwHisTaskActorDaoImpl(hisInstanceMapper);
    }

    @Bean
    @ConditionalOnMissingBean
    public FlwHisTaskDao hisTaskDao(FlwHisTaskMapper hisTaskMapper) {
        return new FlwHisTaskDaoImpl(hisTaskMapper);
    }

    @Bean
    @ConditionalOnMissingBean
    public FlwInstanceDao instanceDao(FlwInstanceMapper instanceMapper) {
        return new FlwInstanceDaoImpl(instanceMapper);
    }

    @Bean
    @ConditionalOnMissingBean
    public FlwProcessDao processDao(FlwProcessMapper processMapper) {
        return new FlwProcessDaoImpl(processMapper);
    }

    @Bean
    @ConditionalOnMissingBean
    public FlwTaskActorDao taskActorDao(FlwTaskActorMapper taskActorMapper) {
        return new FlwTaskActorDaoImpl(taskActorMapper);
    }

    @Bean
    @ConditionalOnMissingBean
    public FlwTaskDao taskDao(FlwTaskMapper taskMapper) {
        return new FlwTaskDaoImpl(taskMapper);
    }

}
