package com.css.simulation.resource.scheduler.app.repository;

import com.css.simulation.resource.scheduler.infra.db.mysql.mapper.IndexMapper;
import com.css.simulation.resource.scheduler.infra.entity.LeafIndexEntity;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class TaskIndexRepository {

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    public void batchInsertLeafIndex(List<LeafIndexEntity> leafTaskIndexList) {

        try(SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false)){
            IndexMapper indexMapper = sqlSession.getMapper(IndexMapper.class);
            for (LeafIndexEntity leafIndexEntity : leafTaskIndexList) {
                indexMapper.insertLeafIndex(leafIndexEntity);
            }
            sqlSession.commit();
        }
    }
}
