package jnpf.personnels.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import jnpf.base.vo.PageListVO;
import jnpf.model.personnels.dto.analysis.FtbPersonnlesAnalysisDTO;
import jnpf.model.personnels.vo.analysis.*;
import jnpf.model.personnels.vo.analysis.PersonnelsOverviewAgeDetailsVO;
import jnpf.model.personnels.vo.analysis.PersonnelsOverviewCompanyDetailsVO;
import jnpf.model.personnels.vo.analysis.PersonnelsOverviewSeniorityDetailsVO;
import jnpf.model.personnels.vo.analysis.PersonnelsOverviewInsuranceDetailsVO;
import jnpf.model.personnels.vo.analysis.PersonnelsOverviewEducationDetailsVO;
import jnpf.model.personnels.vo.analysis.PersonnelsOverviewDepartmentDetailsVO;
import jnpf.model.personnels.vo.analysis.PersonnelsOverviewStoreDetailsVO;
import jnpf.model.personnels.vo.analysis.PersonnelsOverviewJobDetailsVO;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

public interface FtbPersonnelsOverviewAnalysisService {
    /**
     * 在职员工
     */
    PersonnelsOverviewEmployeesVO currentEmployees(FtbPersonnlesAnalysisDTO dto);

    /**
     * 平均年龄
     */
    PersonnelsOverviewEmployeesVO compositeLife(FtbPersonnlesAnalysisDTO dto);

    /**
     * 平均司龄
     */
    PersonnelsOverviewEmployeesAgeVO averageAge(FtbPersonnlesAnalysisDTO dto);

    /**
     * 性别分布
     */
    List<PersonnelsOverviewProportionVO> genderDistribution(FtbPersonnlesAnalysisDTO dto);

    /**
     * 员工状态占比
     */
    List<PersonnelsOverviewProportionVO> employeeStatusProportion(FtbPersonnlesAnalysisDTO dto);

    /**
     * 用工类型占比
     */
    List<PersonnelsOverviewProportionVO> proportionOfEmploymentTypes(FtbPersonnlesAnalysisDTO dto);

    /**
     * 年龄分布占比
     */
    List<PersonnelsOverviewProportionVO> ageDistributionProportion(FtbPersonnlesAnalysisDTO dto);

    /**
     * 司龄分布
     */
    List<PersonnelsOverviewProportionVO> ageDistribution(FtbPersonnlesAnalysisDTO dto);

    /**
     * 学历占比
     */
    List<PersonnelsOverviewProportionVO> educationalBackground(FtbPersonnlesAnalysisDTO dto);

    /**
     * 部门人数占比
     */
    List<PersonnelsOverviewProportionVO> departmentHeadcountRatio(FtbPersonnlesAnalysisDTO dto);

    /**
     * 岗位人数占比
     */
    List<PersonnelsOverviewProportionVO> proportionOfJobHeadcount(FtbPersonnlesAnalysisDTO dto);

    /**
     * 在职员工柱状图
     */
    List<PersonnelsOverviewProportionVO> statusOfCurrentEmployees(FtbPersonnlesAnalysisDTO dto);


    /**
     * 百分比计算（保留两位小数）
     *
     * @param a 分子
     * @param b 分母
     * @return {@link BigDecimal }
     */
    @SuppressWarnings("Duplicates")
    static BigDecimal computeDivision(Object a, Object b) {
        BigDecimal result = computeDivisionNo(a, b);
        return result.multiply(BigDecimal.valueOf(100));
    }

    /**
     * 非百分比计算
     */
    @SuppressWarnings("Duplicates")
    static BigDecimal computeDivisionNo(Object a, Object b) {
        if (a == null || b == null) {
            return BigDecimal.ZERO;
        }
        // a/b 除数为0
        if (b instanceof Integer) {
            Integer b0 = (Integer) b;
            if (b0 == 0) {
                return BigDecimal.ZERO;
            }
        }
        if (b instanceof Long) {
            Long b0 = (Long) b;
            if (b0 == 0) {
                return BigDecimal.ZERO;
            }
        }
        if (b instanceof BigDecimal) {
            if (((BigDecimal) b).compareTo(BigDecimal.ZERO) == 0) {
                return BigDecimal.ZERO;
            }
        }
        BigDecimal a0 = new BigDecimal(String.valueOf(a));
        BigDecimal b0 = new BigDecimal(String.valueOf(b));
        return a0.divide(b0, 4, RoundingMode.HALF_UP);
    }

    /**
     * 非百分比自定义计算
     */
    static BigDecimal percentCustomCalculation(Object a, Object b, int scale) {
        return computeDivisionSb(a, b, scale).divide(BigDecimal.valueOf(100), scale, RoundingMode.HALF_UP);
    }

    /**
     * 百分比计算自定义计算
     */
    @SuppressWarnings("Duplicates")
    static BigDecimal computeDivisionSb(Object a, Object b, int scale) {
        if (a == null || b == null) {
            return BigDecimal.ZERO;
        }
        // a/b 除数为0
        if (b instanceof Integer) {
            Integer b0 = (Integer) b;
            if (b0 == 0) {
                return BigDecimal.ZERO;
            }
        }
        if (b instanceof Long) {
            Long b0 = (Long) b;
            if (b0 == 0) {
                return BigDecimal.ZERO;
            }
        }
        if (b instanceof BigDecimal) {
            if (((BigDecimal) b).compareTo(BigDecimal.ZERO) == 0) {
                return BigDecimal.ZERO;
            }
        }
        BigDecimal a0 = new BigDecimal(String.valueOf(a));
        BigDecimal b0 = new BigDecimal(String.valueOf(b));
        return a0.divide(b0, scale, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
    }

    /**
     * 总年龄计算
     */
    default BigDecimal totalAgeCalculation(List<String> ages) {
        Integer chainValue = ages.stream()
                .filter(StrUtil::isNotBlank)
                .map(DateUtil::ageOfNow)
                .reduce(0, Integer::sum);
        return BigDecimal.valueOf(chainValue);
    }

    List<PersonnelsOverviewProportionVO> seniorityDistribution(FtbPersonnlesAnalysisDTO dto);

    /**
     * 保险购买分析
     */
    List<PersonnelsOverviewProportionVO> insurancePurchaseAnalysis(FtbPersonnlesAnalysisDTO dto);

    List<PersonnelsOverviewProportionVO> proportionOfStoreNumbers(FtbPersonnlesAnalysisDTO dto);

    PageListVO<PersonnelsOverviewEmployeesDetailsVO> statusOfCurrentEmployeesDetails(FtbPersonnlesAnalysisDTO dto);

    void statusOfCurrentEmployeesDetailsExport(FtbPersonnlesAnalysisDTO dto) throws IOException;

    PageListVO<PersonnelsOverviewProportionDetailsVO> genderDistributionDetails(FtbPersonnlesAnalysisDTO dto);

    void genderDistributionDetailsExport(FtbPersonnlesAnalysisDTO dto) throws IOException;

    PageListVO<PersonnelsOverviewSituationDetailsVO> employeeStatusProportionDetails(FtbPersonnlesAnalysisDTO dto);

    void employeeStatusProportionDetailsExport(FtbPersonnlesAnalysisDTO dto) throws IOException;

    PageListVO<PersonnelsOverviewTypeDetailsVO> proportionOfEmploymentTypesDetails(FtbPersonnlesAnalysisDTO dto);

    void proportionOfEmploymentTypesDetailsExport(FtbPersonnlesAnalysisDTO dto) throws IOException;

    PageListVO<PersonnelsOverviewAgeDetailsVO> ageDistributionProportionDetails(FtbPersonnlesAnalysisDTO dto);

    void ageDistributionProportionDetailsExport(FtbPersonnlesAnalysisDTO dto) throws IOException;

    PageListVO<PersonnelsOverviewCompanyDetailsVO> companyDistributionDetails(FtbPersonnlesAnalysisDTO dto);

    void companyDistributionDetailsExport(FtbPersonnlesAnalysisDTO dto);

    PageListVO<PersonnelsOverviewSeniorityDetailsVO> seniorityDistributionDetails(FtbPersonnlesAnalysisDTO dto);

    void seniorityDistributionDetailsExport(FtbPersonnlesAnalysisDTO dto) throws IOException;

    PageListVO<PersonnelsOverviewInsuranceDetailsVO> insurancePurchaseAnalysisDetails(FtbPersonnlesAnalysisDTO dto);

    void insurancePurchaseAnalysisDetailsExport(FtbPersonnlesAnalysisDTO dto) throws IOException;

    PageListVO<PersonnelsOverviewEducationDetailsVO> educationalBackgroundDetails(FtbPersonnlesAnalysisDTO dto);

    void educationalBackgroundDetailsExport(FtbPersonnlesAnalysisDTO dto) throws IOException;

    PageListVO<PersonnelsOverviewDepartmentDetailsVO> departmentHeadcountRatioDetails(FtbPersonnlesAnalysisDTO dto);

    void departmentHeadcountRatioDetailsExport(FtbPersonnlesAnalysisDTO dto) throws IOException;

    PageListVO<PersonnelsOverviewStoreDetailsVO> proportionOfStoreNumbersDetails(FtbPersonnlesAnalysisDTO dto);

    void proportionOfStoreNumbersDetailsExport(FtbPersonnlesAnalysisDTO dto) throws IOException;

    PageListVO<PersonnelsOverviewJobDetailsVO> proportionOfJobHeadcountDetails(FtbPersonnlesAnalysisDTO dto);

    void proportionOfJobHeadcountDetailsExport(FtbPersonnlesAnalysisDTO dto) throws IOException;
}
