/*
 * @Author: Kane
 * @Date: 2023-10-12 10:43:31
 * @LastEditors: Kane
 * @FilePath: /desktop_archievement_backend/src/test/java/com/cpic/xim/DesktopArchievement/test/BatchInsertTest.java
 * @Description: 
 * 
 * Copyright (c) ${2023} by Kane, All Rights Reserved. 
 */
package com.cpic.xim.DesktopArchievement.test;

import java.util.ArrayList;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cpic.xim.mybatis.mapper.ImportBIArchievementDataMapper;
import com.cpic.xim.mybatis.pojo.BIDepartmentAttachingRateRecord;
import com.cpic.xim.mybatis.pojo.BIDepartmentRenewalRateRecord;
import com.cpic.xim.mybatis.pojo.BITelsalerRenewalRateRecord;
import com.cpic.xim.mybatis.utils.MybatisUtils;
import com.cpic.xim.utils.data.ImportBIExcelData;

public class BatchInsertTest
{
    private static Logger logger = LoggerFactory.getLogger(BatchInsertTest.class);
    @Test
    public void testBatchInsertIntoBITelsalerAttachingRate()
    {
        String filePath = "D:/develop/cpicxim/deskop_task_schedule/数据/测试用/经办 (2).xlsx";
        ArrayList<BITelsalerRenewalRateRecord> records = null;
        SqlSession session = null;
        ImportBIArchievementDataMapper mapper = null;

        try
        {
            records = ImportBIExcelData.importBITelsalerRenewalRateFromXlsx( filePath, 0,
                    1 );
            session = MybatisUtils.getSqlSessionBatch();
            mapper = session.getMapper( ImportBIArchievementDataMapper.class );

            mapper.cleanTelsalerRenewalRateData();

            for ( BITelsalerRenewalRateRecord record : records )
            {
                mapper.insertTelsalerRenewalRateDataToDB( record );
            }

            session.commit();

            assert (true);
        }
        catch ( PersistenceException error)
        {
            System.out.println(error);
            assert( false );
        }
        catch(InvalidFormatException error)
        {
            
        }
        catch ( Exception error )
        {
            if ( session != null )
            {
                session.rollback();

                assert (false);
            }
        }
    }

    @Test
    public void testBatchInsertBITeslsalerRenewalRate()
    {
        String filePath = "D:\\develop\\cpicxim\\deskop_task_schedule\\数据\\测试用\\24年报表\\坐席-续保.xlsx";
        ArrayList<BITelsalerRenewalRateRecord> records = null;
        SqlSession session = null;
        ImportBIArchievementDataMapper mapper = null;

        try
        {
            session = MybatisUtils.getSqlSessionBatch();
            mapper = session.getMapper( ImportBIArchievementDataMapper.class );

            records = ImportBIExcelData.importBITelsalerRenewalRateFromXlsx( filePath, 0, 1 );

            // 清理数据
            mapper.cleanTelsalerRenewalRateData();

            for ( BITelsalerRenewalRateRecord record : records )
            {
                mapper.insertTelsalerRenewalRateDataToDB( record );
            }

            session.commit();

            System.out.println( records );
            assert (!records.isEmpty());
        }
        catch ( Exception error )
        {
            logger.error("出现错误", error);
            assert (false);

            if ( session != null )
            {
                session.rollback();
            }
        }
    }

    @Test
    public void testBatchInsertBIDepartmentAttachingRateToDB()
    {
        String filePath = "D:\\develop\\cpicxim\\deskop_task_schedule\\数据\\测试用\\24年报表\\机构-续保.xlsx";

        ArrayList<BIDepartmentAttachingRateRecord> records = null;
        SqlSession session = null;
        ImportBIArchievementDataMapper mapper = null;

        try
        {
            records =
                    ImportBIExcelData.importBIDepartmentAttachingRateRecordsFromXlsx( filePath, 0, 1 );
            session = MybatisUtils.getSqlSessionBatch();
            mapper = session.getMapper( ImportBIArchievementDataMapper.class );
            
            mapper.cleanDepartmentAttachingRateData();

            for ( BIDepartmentAttachingRateRecord record : records )
            {
                mapper.insertDepartmentAttachingRateDataToDB( record );
            }

            session.commit();
        }
        catch ( Exception error )
        {
            if ( session != null )
            {
                session.rollback();
            }

            logger.error("错误", error);
            
            assert( false );
        }
    }

    @Test
    public void testBatchInsertBIDepartmentRenewalRateToDB()
    {
        String filePath = "D:\\develop\\cpicxim\\deskop_task_schedule\\数据\\测试用\\24年报表\\机构-续保.xlsx";

        ArrayList<BIDepartmentRenewalRateRecord> records = null;
        SqlSession session = null;
        ImportBIArchievementDataMapper mapper = null;

        try
        {
            records =
                    ImportBIExcelData.importBIDepartmentRenewalRateRecordsFromXlsx( filePath, 0, 1 );
            session = MybatisUtils.getSqlSessionBatch();
            mapper = session.getMapper( ImportBIArchievementDataMapper.class );
            
            mapper.cleanDepartmentRenewalRateData();

            for ( BIDepartmentRenewalRateRecord record : records )
            {
                mapper.insertDepartmentRenewalRateDataToDB( record );
            }

            session.commit();
        }
        catch ( Exception error )
        {
            if ( session != null )
            {
                session.rollback();
            }

            logger.error("错误", error);
            
            assert( false );
        }
    }
}
