/*
 * @Author: Kane
 * @Date: 2023-05-02 09:52:15
 * @LastEditors: Kane
 * @FilePath: /desktop_archievement_backend/src/main/java/com/cpic/xim/utils/archievement/CallerArchievement.java
 * @Description: 
 * 
 * Copyright (c) ${2022} by Kane, All Rights Reserved. 
 */

package com.cpic.xim.utils.archievement;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cpic.xim.mybatis.mapper.ArchievementMapper;
import com.cpic.xim.mybatis.pojo.MensualArchievementItem;
import com.cpic.xim.mybatis.utils.MybatisUtils;
import com.fasterxml.jackson.annotation.JsonProperty;

public class CallerArchievement
{
    private static Logger logger = LoggerFactory.getLogger( CallerArchievement.class );
    private static ArrayList<MensualArchievementItem> monthArray = null;

    // 静态代码块
    static
    {
        // 初始化月份
        monthArray = new ArrayList<MensualArchievementItem>( 12 );

        monthArray.add( new MensualArchievementItem( 1, "0" ) );
        monthArray.add( new MensualArchievementItem( 2, "0" ) );
        monthArray.add( new MensualArchievementItem( 3, "0" ) );
        monthArray.add( new MensualArchievementItem( 4, "0" ) );
        monthArray.add( new MensualArchievementItem( 5, "0" ) );
        monthArray.add( new MensualArchievementItem( 6, "0" ) );
        monthArray.add( new MensualArchievementItem( 7, "0" ) );
        monthArray.add( new MensualArchievementItem( 8, "0" ) );
        monthArray.add( new MensualArchievementItem( 9, "0" ) );
        monthArray.add( new MensualArchievementItem( 10, "0" ) );
        monthArray.add( new MensualArchievementItem( 11, "0" ) );
        monthArray.add( new MensualArchievementItem( 12, "0" ) );
    }

    public static CallerArchievement getCallerArchievement( String callerCode ) throws IOException
    {
        CallerArchievement archievement = null;

        SqlSession session = MybatisUtils.getSqlSession();
        ArchievementMapper mapper = session.getMapper( ArchievementMapper.class );
        HashMap<String, Object> params = new HashMap<String, Object>();
        Integer totalArchievement = null;
        double motoPremiumPresentMonth = 0;
        String attachingRate = null;
        String renewalRate = null;
        ArrayList<MensualArchievementItem> mensual = null;

        params.put( "a_caller_code", callerCode );

        session.clearCache();
        mapper.getCallerArchievement( params );

        // 总业绩
        if ( params.get( "a_total" ) instanceof Integer )
        {
            totalArchievement = (Integer) params.get( "a_total" );
        }
        else
        {
            totalArchievement = Integer.valueOf( 0 );
        }

        // 当月车险保费
        motoPremiumPresentMonth = ((Double) params.get( "a_present_month" )).doubleValue();


        // 车非融合
        if ( params.get( "a_attaching_rate" ) instanceof String )
        {
            attachingRate = (String) params.get( "a_attaching_rate" );
        }
        else
        {
            attachingRate = "0.0";
        }

        // 续保率
        if ( params.get( "a_renewal_rate" ) instanceof String )
        {
            renewalRate = (String) params.get( "a_renewal_rate" );
        }
        else
        {
            renewalRate = "0.0";
        }

        // 处理每月业绩
        // ArrayList<MensualArchievementItem> mensualList = new
        // ArrayList<>(CallerArchievement.monthArray);
        // 数据库接收的每月业绩，要检查是否有缺漏
        if ( params.get( "a_mensual_cur" ) instanceof ArrayList )
        {
            mensual = (ArrayList<MensualArchievementItem>) params.get( "a_mensual_cur" );
        }

        archievement = new CallerArchievement( callerCode, totalArchievement,
                motoPremiumPresentMonth, mensual, renewalRate, attachingRate );

        return archievement;
    }

    public static Logger getLogger()
    {
        return logger;
    }

    public static void setLogger( Logger logger )
    {
        CallerArchievement.logger = logger;
    }

    public static ArrayList<MensualArchievementItem> getMonthArray()
    {
        return monthArray;
    }

    public static void setMonthArray( ArrayList<MensualArchievementItem> monthArray )
    {
        CallerArchievement.monthArray = monthArray;
    }

    @JsonProperty( "caller_code" )
    private String callerCode;

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

    @JsonProperty( "motoPremiumPresentMonth" )
    private double motoPremiumPresentMonth; // 当月车险保费

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

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

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

    /**
     * 构造函数
     * @param callerCode
     * @param totalArchievement
     * @param mensualArchievementList
     * @param insuranceRenewalRate
     * @param attachingRate
     */
    public CallerArchievement( String callerCode, long totalArchievement,
            double motoPremiumPresentMonth,
            ArrayList<MensualArchievementItem> mensualArchievementList, String insuranceRenewalRate,
            String attachingRate )
    {
        this.callerCode = callerCode;
        this.totalArchievement = totalArchievement;
        this.motoPremiumPresentMonth = motoPremiumPresentMonth;
        this.mensualArchievementList = mensualArchievementList;
        this.insuranceRenewalRate = insuranceRenewalRate;
        this.attachingRate = attachingRate;
    }

    public CallerArchievement()
    {
        this.totalArchievement = 0;
        this.motoPremiumPresentMonth = 0;
        this.mensualArchievementList = null;
        this.insuranceRenewalRate = "";
        this.attachingRate = "";
        this.callerCode = "";
    }

    @Override
    public String toString()
    {
        return "CallerArchievement [callerCode=" + callerCode + ", totalArchievement="
                + totalArchievement + ", motoPremiumPresentMonth=" + motoPremiumPresentMonth
                + ", mensualArchievementList=" + mensualArchievementList + ", insuranceRenewalRate="
                + insuranceRenewalRate + ", attachingRate=" + attachingRate + "]";
    }

    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((callerCode == null) ? 0 : callerCode.hashCode());
        result = prime * result + (int) (totalArchievement ^ (totalArchievement >>> 32));
        long temp;
        temp = Double.doubleToLongBits( motoPremiumPresentMonth );
        result = prime * result + (int) (temp ^ (temp >>> 32));
        result = prime * result
                + ((mensualArchievementList == null) ? 0 : mensualArchievementList.hashCode());
        result = prime * result
                + ((insuranceRenewalRate == null) ? 0 : insuranceRenewalRate.hashCode());
        result = prime * result + ((attachingRate == null) ? 0 : attachingRate.hashCode());
        return result;
    }

    @Override
    public boolean equals( Object obj )
    {
        if ( this == obj )
            return true;
        if ( obj == null )
            return false;
        if ( getClass() != obj.getClass() )
            return false;
        CallerArchievement other = (CallerArchievement) obj;
        if ( callerCode == null )
        {
            if ( other.callerCode != null )
                return false;
        }
        else if ( !callerCode.equals( other.callerCode ) )
            return false;
        if ( totalArchievement != other.totalArchievement )
            return false;
        if ( Double.doubleToLongBits( motoPremiumPresentMonth ) != Double
                .doubleToLongBits( other.motoPremiumPresentMonth ) )
            return false;
        if ( mensualArchievementList == null )
        {
            if ( other.mensualArchievementList != null )
                return false;
        }
        else if ( !mensualArchievementList.equals( other.mensualArchievementList ) )
            return false;
        if ( insuranceRenewalRate == null )
        {
            if ( other.insuranceRenewalRate != null )
                return false;
        }
        else if ( !insuranceRenewalRate.equals( other.insuranceRenewalRate ) )
            return false;
        if ( attachingRate == null )
        {
            if ( other.attachingRate != null )
                return false;
        }
        else if ( !attachingRate.equals( other.attachingRate ) )
            return false;
        return true;
    }

    public double getMotoPremiumPresentMonth()
    {
        return motoPremiumPresentMonth;
    }

    public void setMotoPremiumPresentMonth( double motoPremiumPresentMonth )
    {
        this.motoPremiumPresentMonth = motoPremiumPresentMonth;
    }

    public String getCallerCode()
    {
        return callerCode;
    }

    public void setCallerCode( String callerCode )
    {
        this.callerCode = callerCode;
    }

    public long getTotalArchievement()
    {
        return totalArchievement;
    }

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

    public ArrayList<MensualArchievementItem> getMensualArchievementList()
    {
        return mensualArchievementList;
    }

    public void setMensualArchievementList(
            ArrayList<MensualArchievementItem> 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;
    }
}
