/*
 * @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);

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

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

    /**
     * 静态方法，用于根据部门代码，查询部门业绩，生成一个 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);

            mapper.getDepartmentArchievement(params);

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

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

            logger.error(errorMessage, error);

            throw error;
        }

        return archievement;
    }

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

    public String getDepartmentCode()
    {
        return departmentCode;
    }

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

    @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( "attaching_rate" )
    private String attachingRate; // 车非渗透率
}
