/*
 * @Author: Kane
 * @Date: 2023-06-05 08:55:06
 * @LastEditors: Kane
 * @FilePath: /desktop_archievement_backend/src/main/java/com/cpic/xim/utils/ranking/CallerRankingList.java
 * @Description: 坐席排行榜对象
 * 
 * Copyright (c) ${2022} by Kane, All Rights Reserved. 
 */
package com.cpic.xim.utils.ranking;

import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.ibatis.session.SqlSession;
import com.cpic.xim.mybatis.mapper.RankingListMapper;
import com.cpic.xim.mybatis.utils.MybatisUtils;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;

public class CallerRankingList
{
    // 以后要改
    private static String jdbcURL = "jdbc:oracle:thin:@10.39.0.86:1521:xmcx1";
    private static String userName = "desktop_archievement_admin";
    private static String password = "Cpic123456";

    private CallerRankingList( String departmentCode, String year, String month,
            ArrayList<CallerRankingItem> attachingRateRankingList,
            ArrayList<CallerRankingItem> renewalRateRankingList)
    {
        this.departmentCode = departmentCode;
        this.year = year;
        this.month = month;
        this.attachingRateRankingList = attachingRateRankingList;
        this.renewalRateRankingList = renewalRateRankingList;
    }

    @SuppressWarnings("unchecked")
    public static CallerRankingList getCallerRankingList( String departmentCode, String year,
            String month ) throws IOException
    {
        CallerRankingList rankingList = null;

        // String monthRegx = "(0[1-9])|(1[0-2])";
        // String yearRegx = "202[0-9]";

        SqlSession session = MybatisUtils.getSqlSession();
        RankingListMapper mapper = session.getMapper(RankingListMapper.class);
        HashMap<String,Object> params = new HashMap<>();
        
        params.put("a_department_code", departmentCode);
        params.put("a_year", year);
        params.put("a_month", month);

        mapper.getRankingList(params);

        ArrayList<CallerRankingItem> attachingRateRankingList = (ArrayList<CallerRankingItem>)params.get("a_attaching_ranking_list");
        ArrayList<CallerRankingItem> renewalRateList = (ArrayList<CallerRankingItem>)params.get("a_renewal_ranking_list");
        
        rankingList = new CallerRankingList(departmentCode, year, month, attachingRateRankingList, renewalRateList);
        
        return rankingList;
    }

    /**
     * @brief static方法，用于查询部门坐席排行榜
     * @param departmentCode 部门代码
     * @param year 排行榜年份
     * @param month 排行榜月份
     * @return CallerRankingList的实例对象
     */
    public static CallerRankingList getCallerRankingList2( String departmentCode, String year,
            String month ) throws ClassNotFoundException, SQLException
    {
        CallerRankingList rankingList = null;

        Connection connection = null;
        CallableStatement statement = null;
        ResultSet cur_attaching = null;
        ResultSet cur_renewal = null;
        String sql = """
                {call telsale_archievement_pkg.caller_arch_ranking_list(?,?,?,?,?)}
                """;
        // String monthRegx = "(0[1-9])|(1[0-2])";
        // String yearRegx = "20[0-2][0-0]";

        ArrayList<CallerRankingItem> attachingRateRankingList = new ArrayList<>();
        ArrayList<CallerRankingItem> renewalRateRankingList = new ArrayList<>();

        int index = 1;

        try
        {
            Class.forName( "oracle.jdbc.driver.OracleDriver" );
            connection = DriverManager.getConnection( jdbcURL, userName, password );
            statement = connection.prepareCall( sql );

            statement.setString( 1, departmentCode );
            statement.setString( 2, year );
            statement.setString( 3, month );
            statement.registerOutParameter( 4, OracleTypes.CURSOR );
            statement.registerOutParameter( 5, OracleTypes.CURSOR );

            statement.execute();

            // 车非渗透率
            cur_attaching = ((OracleCallableStatement) statement).getCursor( 4 );
            index = 1;

            while (cur_attaching.next())
            {
                String callerName = cur_attaching.getString( 1 );
                String appraiseValue = cur_attaching.getString( 2 );

                CallerRankingItem caller =
                        new CallerRankingItem( index, callerName, appraiseValue );

                attachingRateRankingList.add( caller );

                index++;
            }

            // 续保率
            cur_renewal = ((OracleCallableStatement) statement).getCursor( 5 );
            index = 1;

            while (cur_renewal.next())
            {
                String callerName = cur_renewal.getString( 1 );
                String appraiseValue = cur_renewal.getString( 2 );

                CallerRankingItem caller =
                        new CallerRankingItem( index, callerName, appraiseValue );

                renewalRateRankingList.add( caller );

                index++;
            }

            rankingList = new CallerRankingList( departmentCode, year, month,
                    attachingRateRankingList, renewalRateRankingList );
        }
        finally
        {
            try
            {
                if ( cur_attaching != null )
                {
                    cur_attaching.close();
                }
            }
            catch ( Exception exception )
            {
                exception.printStackTrace();
            }

            try
            {
                if ( statement != null )
                {
                    statement.close();
                }
            }
            catch ( Exception exception )
            {
                exception.printStackTrace();
            }

            try
            {
                if ( connection != null )
                {
                    connection.close();
                }
            }
            catch ( Exception exception )
            {
                exception.printStackTrace();
            }
        }

        return rankingList;
    }

    public String getDepartmentCode()
    {
        return departmentCode;
    }

    public void setDepartmentCode( String departmentCode )
    {
        this.departmentCode = departmentCode;
    }

    public String getYear()
    {
        return year;
    }

    public void setYear( String year )
    {
        this.year = year;
    }

    public String getMonth()
    {
        return month;
    }

    public void setMonth( String month )
    {
        this.month = month;
    }

    public ArrayList<CallerRankingItem> getAttachingRateRankingList()
    {
        return attachingRateRankingList;
    }

    public void setAttachingRateRankingList( ArrayList<CallerRankingItem> attachingRateRankingList )
    {
        this.attachingRateRankingList = attachingRateRankingList;
    }

    public ArrayList<CallerRankingItem> getRenewalRateRankingList()
    {
        return renewalRateRankingList;
    }

    public void setRenewalRateRankingList( ArrayList<CallerRankingItem> renewalRateRankingList )
    {
        this.renewalRateRankingList = renewalRateRankingList;
    }

    // 部门代码
    private String departmentCode;

    // 排行榜年份
    private String year;

    // 排行榜统计月份
    private String month;

    // 车非融合率排行
    private ArrayList<CallerRankingItem> attachingRateRankingList;

    // 续保率排行
    private ArrayList<CallerRankingItem> renewalRateRankingList;

}
