/*
 * @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.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 DepartmentArchievement
{
    // log4j2
    private static Logger logger = LoggerFactory.getLogger(DepartmentArchievement.class);

    /**
     * 静态方法，用于根据部门代码，查询部门业绩，生成一个 DepartmentArchievement 对象。
     * @param departmentCode 部门代码
     * @return
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    public static DepartmentArchievement getDepartmentArchievement( String departmentCode )
            throws IOException
    {
        DepartmentArchievement archievement = null;

        try
        {
            SqlSession session = MybatisUtils.getSqlSession();
            ArchievementMapper mapper = session.getMapper(ArchievementMapper.class);

            // 参数集合
            HashMap<String, Object> params = new HashMap<>(5);
            
            params.put("a_department_code", departmentCode);

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

            Integer totalArchievement = (Integer)params.get("a_total");
            String attachingRate = (String)params.get("a_attaching_rate");
            String attachingRateTarget = (String)params.get("a_attaching_rate_target");
            String renewalRate = (String)params.get("a_renewal_rate");
            String renewalRateTarget = (String)params.get("a_renewal_rate_target");
            ArrayList<MensualArchievementItem> mensual = (ArrayList<MensualArchievementItem>)params.get("a_mensual_cur");

            archievement = new DepartmentArchievement(
                departmentCode, 
                totalArchievement.intValue(), 
                mensual, 
                renewalRate, renewalRateTarget,
                attachingRate, attachingRateTarget);
        }
        catch ( IOException error )
        {
            String errorMessage = "查询" + departmentCode + "业绩时出现错误。";

            logger.error(errorMessage, error);

            throw error;
        }

        return archievement;
    }

    @JsonProperty( "department_code" )
    private String departmentCode;

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

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

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

    @JsonProperty( "insurance_renewal_rate_target" )
    private String insuranceRenewalRateTarget; // 续保率目标值

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

    @JsonProperty( "attaching_rate_target" )
    private String attachingRateTarget; // 车非渗透率目标值

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

    /**
     * @brief 构造函数，提供所有成员对象的值。
     * @param departmentCode 部门代码
     * @param totalArchievement 总业绩
     * @param mensualArchievementList 每月业绩表
     * @param insuranceRenewalRate 续保率
     * @param insuranceRenewalRateTarget 续保率目标值
     * @param attachingRate 车非渗透率
     * @param attachingRateTarget 车非渗透率目标值
     */
    public DepartmentArchievement( String departmentCode, long totalArchievement,
            ArrayList<MensualArchievementItem> mensualArchievementList, String insuranceRenewalRate,
            String insuranceRenewalRateTarget, String attachingRate, String attachingRateTarget )
    {
        this.departmentCode = departmentCode;
        this.totalArchievement = totalArchievement;
        this.mensualArchievementList = mensualArchievementList;
        this.insuranceRenewalRate = insuranceRenewalRate;
        this.insuranceRenewalRateTarget = insuranceRenewalRateTarget;
        this.attachingRate = attachingRate;
        this.attachingRateTarget = attachingRateTarget;
    }

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

    @Override
    public String toString()
    {
        return "DepartmentArchievement [departmentCode=" + departmentCode + ", totalArchievement="
                + totalArchievement + ", mensualArchievementList=" + mensualArchievementList
                + ", insuranceRenewalRate=" + insuranceRenewalRate + ", insuranceRenewalRateTarget="
                + insuranceRenewalRateTarget + ", attachingRate=" + attachingRate
                + ", attachingRateTarget=" + attachingRateTarget + "]";
    }

    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((departmentCode == null) ? 0 : departmentCode.hashCode());
        result = prime * result + (int) (totalArchievement ^ (totalArchievement >>> 32));
        result = prime * result
                + ((mensualArchievementList == null) ? 0 : mensualArchievementList.hashCode());
        result = prime * result
                + ((insuranceRenewalRate == null) ? 0 : insuranceRenewalRate.hashCode());
        result = prime * result + ((insuranceRenewalRateTarget == null) ? 0
                : insuranceRenewalRateTarget.hashCode());
        result = prime * result + ((attachingRate == null) ? 0 : attachingRate.hashCode());
        result = prime * result
                + ((attachingRateTarget == null) ? 0 : attachingRateTarget.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;
        DepartmentArchievement other = (DepartmentArchievement) obj;
        if ( departmentCode == null )
        {
            if ( other.departmentCode != null )
                return false;
        }
        else if ( !departmentCode.equals( other.departmentCode ) )
            return false;
        if ( totalArchievement != other.totalArchievement )
            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 ( insuranceRenewalRateTarget == null )
        {
            if ( other.insuranceRenewalRateTarget != null )
                return false;
        }
        else if ( !insuranceRenewalRateTarget.equals( other.insuranceRenewalRateTarget ) )
            return false;
        if ( attachingRate == null )
        {
            if ( other.attachingRate != null )
                return false;
        }
        else if ( !attachingRate.equals( other.attachingRate ) )
            return false;
        if ( attachingRateTarget == null )
        {
            if ( other.attachingRateTarget != null )
                return false;
        }
        else if ( !attachingRateTarget.equals( other.attachingRateTarget ) )
            return false;
        return true;
    }

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

    public String getInsuranceRenewalRateTarget()
    {
        return insuranceRenewalRateTarget;
    }

    public void setInsuranceRenewalRateTarget( String insuranceRenewalRateTarget )
    {
        this.insuranceRenewalRateTarget = insuranceRenewalRateTarget;
    }

    public String getAttachingRateTarget()
    {
        return attachingRateTarget;
    }

    public void setAttachingRateTarget( String attachingRateTarget )
    {
        this.attachingRateTarget = attachingRateTarget;
    }

    public String getDepartmentCode()
    {
        return departmentCode;
    }

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