package nckd.jxccl.hr.hstu.common.bo;

import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import nckd.jxccl.hr.hstu.business.helper.HonorStudentHelper;

/**
* 问卷考评结果BO（Business Object）
* @author W.Y.C
* @date 2025/8/7 17:06
* @version 1.0
*/
public class EvalQuestResultBO {

    private static final Log logger = LogFactory.getLog(HonorStudentHelper.class);

    // ================= 评分人数相关 =================
    /** 同级评分人数 */
    private  Integer peerRatingCount;
    /** 上级评分人数 */
    private  Integer superiorRatingCount;
    /** 领导评分人数 */
    private  Integer leaderRatingCount;
    /** 选择"不了解"选项的人数 */
    private  Integer notUnderstandCount;


    // ================= 评分总分相关 =================
    /** 同级评分总分 */
    private  Integer peerRatingTotal;
    /** 上级评分总分 */
    private  Integer superiorRatingTotal;
    /** 领导评分总分 */
    private  Integer leaderRatingTotal;
    /** 已评分总分 */
    private  Integer ratedTotal;

    // ================= 评分状态相关 =================
    /** 未评分人员 */
    private  Integer unratedPersonnel;
    /** 已评分人数 */
    private  Integer ratedCount;

    /** 优秀生评价积分*/
    private  Integer sumScore;
    /** 活动规则ID */
    private Long evaluationRuleId;

    /** 公司排名人数*/
    private Integer comPersons;
    /** 所在单位排名人数*/
    private Integer hrPersons;

    /** 公司排名*/
    private Integer comRanking;
    /** 所在单位排名*/
    private Integer hrRanking;


    /**
     * 计算综合测评得分
     * @param leaderWeight 领导评分权重占比（百分比值，如50表示50%）
     * @param superiorWeight 上级评分权重占比（百分比值）
     * @param peerWeight 同级评分权重占比（百分比值）
     * @param comprehensiveWeight 综合测评权重占比（百分比值）
     * @param maxScore 综合测评封顶分
     * @return 最终的综合测评得分
     */
    public double calculateComprehensiveScore(
            int leaderWeight,
            int superiorWeight,
            int peerWeight,
            int comprehensiveWeight,
            int maxScore) {

        /*
         * 综合测评得分的计算方法
         *
         * 1.计算各角色综合评分贡献分。
         *     公式：(各角色（领导、上级、同级）总分 / 对应角色评分人数) * 考评规则.各角色评分权重占比 * 0.01
         *         计算各角色的平均分（总分除以评分人数） ，然后乘以该角色的权重占比，最后乘以0.01将百分比转换为小数
         *     例如：
         *         领导评分平均分90分，权重50%，贡献分 = 90 * 50% * 0.01 = 45分
         *         上级评分平均分80分，权重40%，贡献分 = 80 * 40% * 0.01 = 32分
         *         同级评分平均分70分，权重30%，贡献分 = 70 * 30% * 0.01 = 21分
         * 2.计算最终的综合测评得分
         *     公式：综合测评得分 = (各角色贡献分之和) * 综合测评权重占比 * 0.01
         *     将所有角色的综合评分贡献分相加，再乘以考评规则中的综合测评权重占比，再乘以0.01：
         * 3.封顶处理
         *     公式：最终得分 = (综合测评得分 > 封顶分) ? 封顶分 : 综合测评得分
         *     如果计算出的综合测评得分超过了考评规则中设定的综合测评封顶分，则最终得分取封顶分值：
         */

        logger.info("【优秀生考评】-更新问卷测评结果-计算综合测评得分-领导评分权重占比：{},上级评分权重占比：{},同级评分权重占比：{}，综合测评权重占比：{}，综合测评封顶分：{}", leaderWeight,superiorWeight,peerWeight,comprehensiveWeight,maxScore);

        // 1. 计算各角色综合评分贡献分
        double leaderContribution = calculateRoleContribution(leaderRatingTotal, leaderRatingCount, leaderWeight);
        double superiorContribution = calculateRoleContribution(superiorRatingTotal, superiorRatingCount, superiorWeight);
        double peerContribution = calculateRoleContribution(peerRatingTotal, peerRatingCount, peerWeight);

        // 2. 计算综合测评得分
        double comprehensiveScore = (leaderContribution + superiorContribution + peerContribution)
                * comprehensiveWeight * 0.01;

        // 3. 封顶处理
        return Math.min(comprehensiveScore, maxScore);

    }

    /**
     * 计算单个角色的贡献分
     * @param totalScore 该角色评分总分
     * @param raterCount 该角色评分人数
     * @param weight 该角色权重占比（百分比值）
     * @return 贡献分
     */
    private double calculateRoleContribution(Integer totalScore, Integer raterCount, int weight) {
        if (totalScore == null || raterCount == null || raterCount == 0) {
            return 0.0;
        }
        double averageScore = (double) totalScore / raterCount;
        return averageScore * weight * 0.01;
    }






    /**
     * 获取同级评分人数
     * @return 同级评分人数
     */
    public Integer getLeaderRatingCount() {
        return leaderRatingCount;
    }
    public void setLeaderRatingCount(Integer leaderRatingCount) {
        this.leaderRatingCount = leaderRatingCount;
    }
    public void addLeaderRatingCount(Integer leaderRatingCount) {
        this.leaderRatingCount = (this.leaderRatingCount == null) ? leaderRatingCount : this.leaderRatingCount + leaderRatingCount;
    }

    /**
     * 获取上级评分人数
     * @return 上级评分人数
     */
    public Integer getLeaderRatingTotal() {
        return leaderRatingTotal;
    }
    public void setLeaderRatingTotal(Integer leaderRatingTotal) {
        this.leaderRatingTotal = leaderRatingTotal;
    }
    public void addLeaderRatingTotal(Integer leaderRatingTotal) {
        this.leaderRatingTotal = (this.leaderRatingTotal == null) ? leaderRatingTotal : this.leaderRatingTotal + leaderRatingTotal;
    }

    /**
     * 获取选择"不了解"选项的人数
     * @return 选择"不了解"选项的人数
     */
    public Integer getNotUnderstandCount() {
        return notUnderstandCount;
    }

    public void setNotUnderstandCount(Integer notUnderstandCount) {
        this.notUnderstandCount = notUnderstandCount;
    }
    public void addNotUnderstandCount(Integer notUnderstandCount) {
        this.notUnderstandCount = (this.notUnderstandCount == null) ? notUnderstandCount : this.notUnderstandCount + notUnderstandCount;
    }

    /**
     * 获取同级评分人数
     * @return 同级评分人数
     */
    public Integer getPeerRatingCount() {
        return peerRatingCount;
    }
    public void setPeerRatingCount(Integer peerRatingCount) {
        this.peerRatingCount = peerRatingCount;
    }

    public void addPeerRatingCount(Integer peerRatingCount) {
        this.peerRatingCount = (this.peerRatingCount == null) ? peerRatingCount : this.peerRatingCount + peerRatingCount;
    }

    /**
     * 获取同级评分总分
     * @return 同级评分总分
     */
    public Integer getPeerRatingTotal() {
        return peerRatingTotal;
    }
    public void setPeerRatingTotal(Integer peerRatingTotal) {
        this.peerRatingTotal = peerRatingTotal;
    }
    public void addPeerRatingTotal(Integer peerRatingTotal) {
        this.peerRatingTotal = (this.peerRatingTotal == null) ? peerRatingTotal : this.peerRatingTotal + peerRatingTotal;
    }

    /**
     * 获取已评分人数
     * @return 已评分人数
     */
    public Integer getRatedCount() {
        return ratedCount;
    }
    public void setRatedCount(Integer ratedCount) {
        this.ratedCount = ratedCount;
    }
    public void addRatedCount(Integer ratedCount) {
        this.ratedCount = (this.ratedCount == null) ? ratedCount : this.ratedCount + ratedCount;
    }

    /**
     * 获取已评分总分
     * @return 已评分总分
     */
    public Integer getRatedTotal() {
        return ratedTotal;
    }
    public void setRatedTotal(Integer ratedTotal) {
        this.ratedTotal = ratedTotal;
    }
    public void addRatedTotal(Integer ratedTotal) {
        this.ratedTotal = (this.ratedTotal == null) ? ratedTotal : this.ratedTotal + ratedTotal;
    }

    /**
     * 获取上级评分人数
     * @return 上级评分人数
     */
    public Integer getSuperiorRatingCount() {
        return superiorRatingCount;
    }
    public void setSuperiorRatingCount(Integer superiorRatingCount) {
        this.superiorRatingCount = superiorRatingCount;
    }
    public void addSuperiorRatingCount(Integer superiorRatingCount) {
        this.superiorRatingCount = (this.superiorRatingCount == null) ? superiorRatingCount : this.superiorRatingCount + superiorRatingCount;
    }

    /**
     * 获取上级评分总分
     * @return 上级评分总分
     */
    public Integer getSuperiorRatingTotal() {
        return superiorRatingTotal;
    }
    public void setSuperiorRatingTotal(Integer superiorRatingTotal) {
        this.superiorRatingTotal = superiorRatingTotal;
    }
    public void addSuperiorRatingTotal(Integer superiorRatingTotal) {
        this.superiorRatingTotal = (this.superiorRatingTotal == null) ? superiorRatingTotal : this.superiorRatingTotal + superiorRatingTotal;
    }

    /**
     * 获取未评分人员
     * @return 未评分人员
     */
    public Integer getUnratedPersonnel() {
        return unratedPersonnel;
    }
    public void setUnratedPersonnel(Integer unratedPersonnel) {
        this.unratedPersonnel = unratedPersonnel;
    }
    public void addUnratedPersonnel(Integer unratedPersonnel) {
        this.unratedPersonnel = (this.unratedPersonnel == null) ? unratedPersonnel : this.unratedPersonnel + unratedPersonnel;
    }


    public Long getEvaluationRuleId() {
        return evaluationRuleId;
    }

    public void setEvaluationRuleId(Long evaluationRuleId) {
        this.evaluationRuleId = evaluationRuleId;
    }

    public Integer getSumScore() {
        return sumScore;
    }

    public void setSumScore(Integer sumScore) {
        this.sumScore = sumScore;
    }

    public Integer getComPersons() {
        return comPersons;
    }

    public void setComPersons(Integer comPersons) {
        this.comPersons = comPersons;
    }

    public Integer getComRanking() {
        return comRanking;
    }

    public void setComRanking(Integer comRanking) {
        this.comRanking = comRanking;
    }

    public Integer getHrPersons() {
        return hrPersons;
    }

    public void setHrPersons(Integer hrPersons) {
        this.hrPersons = hrPersons;
    }

    public Integer getHrRanking() {
        return hrRanking;
    }

    public void setHrRanking(Integer hrRanking) {
        this.hrRanking = hrRanking;
    }

    /**
     * 公司排名百分比 = (公司排名 / 公司总人数) * 100%
     * @return: java.lang.Double
     * @author W.Y.C
     * @date: 2025/08/12 12:22
     */
    public Double calculateComPercentage(){
        Integer comRanking = this.getComRanking();
        Integer comPersons = this.getComPersons();
        if (comRanking == null || comPersons == null || comPersons == 0) {
            return 0.0;
        }
        double result = (double) comRanking / comPersons * 100;
        return Double.valueOf(String.format("%.2f", result));
    }

    /**
     * 所在单位排名百分比 = (所在单位排名 / 所在单位总人数) * 100%
     * @return: java.lang.Double
     * @author W.Y.C
     * @date: 2025/08/12 12:22
     */
    public Double calculateHrPercentage(){
        Integer hrRanking = this.getHrRanking();
        Integer hrPersons = this.getHrPersons();
        if (hrRanking == null || hrPersons == null || hrPersons == 0) {
            return 0.0;
        }
        double result = (double) hrRanking / hrPersons * 100;
        return Double.valueOf(String.format("%.2f", result));
    }


    @Override
    public String toString() {
        return "EvalQuestResultDTO [leaderRatingCount=" + leaderRatingCount
                + ", leaderRatingTotal=" + leaderRatingTotal
                + ", notUnderstandCount=" + notUnderstandCount
                + ", peerRatingCount=" + peerRatingCount
                + ", peerRatingTotal=" + peerRatingTotal
                + ", ratedCount=" + ratedCount
                + ", ratedTotal=" + ratedTotal
                + ", superiorRatingCount=" + superiorRatingCount
                + ", superiorRatingTotal=" + superiorRatingTotal
                + ", unratedPersonnel=" + unratedPersonnel + "]";
    }
}