/*
 * @Author: Kane
 * @Date: 2023-11-24 11:09:46
 * @LastEditors: Kane
 * @FilePath: /desktop_archievement_backend/src/main/java/com/cpic/xim/utils/data/ImportTWRTelsalerData.java
 * @Description: 
 * 
 * Copyright (c) ${2023} by Kane, All Rights Reserved. 
 */
package com.cpic.xim.utils.data;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cpic.xim.mybatis.pojo.TWrTelsalerRecord;
import com.cpic.xim.utils.poi.MyPOIUtils;

public class ImportTWRTelsalerData
{
    private static Logger logger = LoggerFactory.getLogger( ImportTWRTelsalerData.class );
    private static String[] TwrTelsalerListExcelTitle = new String[]
    { "人员姓名", "人员工号", "籍 贯", "参加工作时间", "入职日期", "入司日期", "业务类型", "办公地省", "办公地市", "人员类别", "现任岗位",
            "现任职级", "机构名称", "职场分类", "片区名称", "团队名称", "对口分公司", "展业地区", "招聘渠道", "渠道明细", "全日制最高学历",
            "学历类型", "用工性质名称", "合同种类", "合同类型", "合同性质", "合同签订次数", "合同生效日期", "合同到期日期", "人员属性", "保代员工号",
            "职场属性", "办公性质", "保代人员属性", "人员状态",};

    public static ArrayList<TWrTelsalerRecord> importTWrTelsalerDataFromExcel( String filePath, int sheetIndex,
            int firstRow ) throws IOException, InvalidFormatException
    {
        ArrayList<TWrTelsalerRecord> records = new ArrayList<>( 200 );

        Workbook wb = null;
        Sheet sheet = null;

        try
        {
            wb = WorkbookFactory.create( new File( filePath ) );
            sheet = wb.getSheetAt( sheetIndex );

            // 先验证格式，不对就抛出错误
            if ( !checkExcelFormat( sheet, null, 0, TwrTelsalerListExcelTitle, 0 ) )
            {
                throw new InvalidFormatException( "Excel文件格式错误，请检查报表内容！" );
            }

            for ( Row row : sheet )
            {
                int rowIndex = row.getRowNum();

                // 从数据行开始
                if ( row.getRowNum() < firstRow )
                {
                    continue;
                }

                try
                {
                    String 人员姓名 = MyPOIUtils.getStringCellValue( row, 0 );
                    String 人员工号 = MyPOIUtils.getStringCellValue( row, 1 );
                    String 籍贯 = MyPOIUtils.getStringCellValue( row, 2 );
                    String 参加工作时间 = MyPOIUtils.getStringCellValue( row, 3 );
                    String 入职日期 = MyPOIUtils.getStringCellValue( row, 4 );
                    String 入司日期 = MyPOIUtils.getStringCellValue( row, 5 );
                    String 业务类型 = MyPOIUtils.getStringCellValue( row, 6 );
                    String 办公地省 = MyPOIUtils.getStringCellValue( row, 7 );
                    String 办公地市 = MyPOIUtils.getStringCellValue( row, 8 );
                    String 人员类别 = MyPOIUtils.getStringCellValue( row, 9 );
                    String 现任岗位 = MyPOIUtils.getStringCellValue( row, 10 );
                    String 现任职级 = MyPOIUtils.getStringCellValue( row, 11 );
                    String 机构名称 = MyPOIUtils.getStringCellValue( row, 12 );
                    String 职场分类 = MyPOIUtils.getStringCellValue( row, 13 );
                    String 片区名称 = MyPOIUtils.getStringCellValue( row, 14 );
                    String 团队名称 = MyPOIUtils.getStringCellValue( row, 15 );
                    String 对口分公司 = MyPOIUtils.getStringCellValue( row, 16 );
                    String 展业地区 = MyPOIUtils.getStringCellValue( row, 17 );
                    String 招聘渠道 = MyPOIUtils.getStringCellValue( row, 18 );
                    String 渠道明细 = MyPOIUtils.getStringCellValue( row, 19 );
                    String 全日制最高学历 = MyPOIUtils.getStringCellValue( row, 20 );
                    String 学历类型 = MyPOIUtils.getStringCellValue( row, 21 );
                    String 用工性质名称 = MyPOIUtils.getStringCellValue( row, 22 );
                    String 合同种类 = MyPOIUtils.getStringCellValue( row, 23 );
                    String 合同类型 = MyPOIUtils.getStringCellValue( row, 24 );
                    String 合同性质 = MyPOIUtils.getStringCellValue( row, 25 );
                    int 合同签订次数 = (int)MyPOIUtils.getNumbericCellValue( row, 26 );
                    String 合同生效日期 = MyPOIUtils.getStringCellValue( row, 27 );
                    String 合同到期日期 = MyPOIUtils.getStringCellValue( row, 28 );
                    String 人员属性 = MyPOIUtils.getStringCellValue( row, 29 );
                    String 保代员工号 = MyPOIUtils.getStringCellValue( row, 30 );
                    String 职场属性 = MyPOIUtils.getStringCellValue( row, 31 );
                    String 办公性质 = MyPOIUtils.getStringCellValue( row, 32 );
                    String 保代人员属性 = MyPOIUtils.getStringCellValue( row, 33 );
                    String 人员状态 = MyPOIUtils.getStringCellValue( row, 34 );

                    TWrTelsalerRecord record = new TWrTelsalerRecord(人员姓名, 人员工号, 籍贯, 参加工作时间, 入职日期, 入司日期, 业务类型, 办公地省, 办公地市, 人员类别, 现任岗位, 现任职级, 机构名称, 职场分类, 片区名称, 团队名称, 对口分公司, 展业地区, 招聘渠道, 渠道明细, 全日制最高学历, 学历类型, 用工性质名称, 合同种类, 合同类型, 合同性质, 合同签订次数, 合同生效日期, 合同到期日期, 人员属性, 保代员工号, 职场属性, 办公性质, 保代人员属性, 人员状态);
                
                    records.add(record);
                }
                catch ( IllegalStateException error )
                {
                    error.printStackTrace();
                }
                catch ( NumberFormatException error )
                {
                    error.printStackTrace();
                }
                catch ( NullPointerException error )
                {
                    String message = "第" + String.valueOf( rowIndex ) + "行出现NullPointerException异常";
                    logger.error( message, error );
                }
            }
        }
        finally
        {
            try
            {
                if ( wb != null )
                {
                    wb.close();
                }
            }
            catch ( IOException error )
            {
                error.printStackTrace();
            }
        }

        return records;
    }

    private static boolean checkExcelFormat( Sheet sheet, String caption, int captionRowIndex,
            String[] title, int titleRowIndex )
    {
        boolean result = true;

        Row captionRow = sheet.getRow( captionRowIndex );
        Row titleRow = sheet.getRow( titleRowIndex );

        if ( caption != null && !caption.isEmpty() )
        {
            try
            {
                int captionCellNum = captionRow.getFirstCellNum();
                String captionString = MyPOIUtils.getStringCellValue( captionRow, captionCellNum );

                if ( !caption.equals( captionString ) )
                {
                    result = false;
                }
            }
            catch ( NullPointerException error )
            {
                result = false;
            }
        }

        if ( title.length != 0 )
        {
            int cellIndex = 0;

            try
            {
                for ( Cell cell : titleRow )
                {
                    String cellString = MyPOIUtils.getStringCellValue( cell );

                    if ( !cellString.equals( title[cellIndex] ) )
                    {
                        result = false;
                        break;
                    }

                    cellIndex++;
                }
            }
            catch ( NullPointerException error )
            {
                result = false;
            }
        }

        return result;
    }
}
