/*
 * @Author: Kane
 * @Date: 2023-05-02 10:11:27
 * @LastEditors: Kane
 * @FilePath: /desktop_archievement_backend/src/main/java/com/cpic/xim/utils/archievement/DepartmentArchievement.java
 * @Description: 
 * 
 * Copyright (c) ${2022} by Kane, All Rights Reserved. 
 */
package com.cpic.xim.utils.archievement;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.CallableStatement;

import java.util.Vector;
import com.fasterxml.jackson.annotation.JsonProperty;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;

public class DepartmentArchievement
{

    // 以后要改
    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";

    /**
     * 默认的构造函数，提供给json使用。
     */
    public DepartmentArchievement()
    {
        this.totalArchievement = 0;
        this.mensualArchievementList = new Vector<>();
        this.insuranceRenewalRate = "";
        this.attachingRate = "";
    }

    /**
     * @brief 构造函数，提供所有成员对象的值。
     * @param totalArchievement
     * @param mensualArchievementList
     * @param insuranceRenewalRate
     * @param attachingRate
     */
    public DepartmentArchievement( long totalArchievement, Vector<Long> mensualArchievementList,
            String insuranceRenewalRate, String attachingRate)
    {
        this.totalArchievement = totalArchievement;
        this.mensualArchievementList = mensualArchievementList;
        this.insuranceRenewalRate = insuranceRenewalRate;
        this.attachingRate = attachingRate;
    }

    /**
     * 静态方法，用于根据部门代码，查询部门业绩，生成一个 DepartmentArchievement 对象。
     * @param departmentCode 部门代码
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static DepartmentArchievement queryDepartmentArchievement( String departmentCode )
            throws SQLException, ClassNotFoundException
    {
        Connection connection = null;
        java.sql.CallableStatement statement = null;
        ResultSet menusual_cursor = null;

        DepartmentArchievement archievement = null;

        String sql = "{call telsale_archievement_pkg.department_archievement(?,?,?,?,?)}";

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


            statement.setString( 1, departmentCode );
            statement.registerOutParameter( 2, java.sql.Types.VARCHAR );
            statement.registerOutParameter( 3, java.sql.Types.VARCHAR );
            statement.registerOutParameter( 4, java.sql.Types.INTEGER );
            statement.registerOutParameter( 5, OracleTypes.CURSOR );

            statement.execute();

            Vector<Long> menusual_list = new Vector<Long>();
            String attach_rate = statement.getString( 2 );
            String renewal_rate = statement.getString( 3 );
            long totalArchievement = statement.getInt( 4 );

            menusual_cursor = ((OracleCallableStatement) statement).getCursor( 5 );

            while ( menusual_cursor.next())
            {
                menusual_list.add( menusual_cursor.getLong( 2 ) );
            }

            archievement = new DepartmentArchievement( totalArchievement, menusual_list,
                    renewal_rate, attach_rate );
        }
        finally
        {
            try
            {
                if ( menusual_cursor != null )
                {
                    menusual_cursor.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 archievement;
    }

    public long getTotalArchievement()
    {
        return totalArchievement;
    }

    public void setTotalArchievement( long totalArchievement )
    {
        this.totalArchievement = totalArchievement;
    }

    public Vector<Long> getMensualArchievementList()
    {
        return mensualArchievementList;
    }

    public void setMensualArchievementList( Vector<Long> mensualArchievementList )
    {
        this.mensualArchievementList = mensualArchievementList;
    }

    public String getInsuranceRenewalRate()
    {
        return insuranceRenewalRate;
    }

    public void setInsuranceRenewalRate( String insuranceRenewalRate )
    {
        this.insuranceRenewalRate = insuranceRenewalRate;
    }

    public String getAttachingRate()
    {
        return attachingRate;
    }

    public void setAttachingRate( String attachingRate )
    {
        this.attachingRate = attachingRate;
    }

    @JsonProperty( "total_archievement" )
    private long totalArchievement; // 总业绩

    // 每月业绩列表
    // 要保证数据是按照月份排序。
    @JsonProperty( "mensual_archievement_list" )
    private Vector<Long> mensualArchievementList;

    @JsonProperty( "insurance_renewal_rate" )
    private String insuranceRenewalRate; // 续保率

    @JsonProperty( "attaching_rate" )
    private String attachingRate; // 车非渗透率
}
