/*
 * @Author: Kane
 * @Date: 2023-10-08 15:02:15
 * @LastEditors: Kane
 * @FilePath: /desktop_archievement_backend/src/main/java/com/cpic/xim/utils/data/TelsalerArchievementData.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.time.LocalDate;
import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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 com.cpic.xim.mybatis.pojo.TelsalerAttachingRateRecord;
import com.cpic.xim.utils.poi.MyPOIUtils;

/**
 * 坐席业绩相关的数据操作方法。
 */
public class TelsalerArchievementData
{
    private static Logger logger = LoggerFactory.getLogger( TelsalerArchievementData.class );

    /**
     * 
     * @param filePath 文件路径
     * @param summaryDate 统计日期
     * @return 返回一个ArrayList保存的TelsalerAttachingRateRecord记录数组。
     * @throws IOException 打开excel文件错误时抛出。
     * @throws InvalidFormatException excel单元格格式错误时抛出
     */
    public static ArrayList<TelsalerAttachingRateRecord> importTelsalerAttachingRateRecordFromXlsx(
            String filePath, LocalDate summaryDate, boolean hasCaptionRow ) throws IOException, InvalidFormatException
    {
        ArrayList<TelsalerAttachingRateRecord> records = new ArrayList<>( 200 );

        Workbook wb = null;
        Sheet sheet = null;

        try
        {
            wb = WorkbookFactory.create( new File( filePath ) );
            sheet = wb.getSheet( "经办" );

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

                // 判断是否要跳过标题行
                if ( hasCaptionRow == true && row.getRowNum() == 0 )
                {
                    continue;
                }

                try
                {
                    // 坐席名称
                    name = MyPOIUtils.getStringCellValue( row, 1 );

                    if ( name.equals( "全流程" ) || name.equals( "其他" ) || name.isEmpty() )
                    {
                        continue;
                    }

                    // 车险保费
                    double motoPremium = MyPOIUtils.getNumbericCellValue( row, 2 );
                    // 非车险保费
                    double nomotoPremium = MyPOIUtils.getNumbericCellValue( row, 4 );
                    // 车险保费占比
                    double motoPremiumProPortion = MyPOIUtils.getNumbericCellValue( row, 3 );
                    // 当月保费渗透率
                    double attachingRate = MyPOIUtils.getNumbericCellValue( row, 5 );
                    // 保费手头率环比上月
                    double attachingRateChange = MyPOIUtils.getNumbericCellValue( row, 6 );
                    // 当月客户渗透率
                    double customerHandleRateCell = MyPOIUtils.getNumbericCellValue( row, 7 );
                    // 客户渗透率环比上月
                    double customerHandleRateChangeCell = MyPOIUtils.getNumbericCellValue( row, 8 );
                    // 当月车非客均保费
                    double noMotoPremiumPerCustomerCell = MyPOIUtils.getNumbericCellValue( row, 9 );
                    // 客均保费环比上月
                    double noMotoPremiumPerCustomerChangeCell =
                            MyPOIUtils.getNumbericCellValue( row, 10 );

                    TelsalerAttachingRateRecord record = new TelsalerAttachingRateRecord(
                            summaryDate, name, motoPremium, nomotoPremium, motoPremiumProPortion,
                            attachingRate, attachingRateChange, customerHandleRateCell,
                            customerHandleRateChangeCell, noMotoPremiumPerCustomerCell,
                            noMotoPremiumPerCustomerChangeCell );

                    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;
    }
}
