package com.mortals.xhx.common.utils;

import com.mortals.xhx.common.pdu.WeightPdu;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.model.StaffPerformSummaryEntity;

import java.math.BigDecimal;

/**
 * 员工绩效分数计算
 */
public class StaffPerformUtil {

    /** 群众评议总分 **/
    //public static BigDecimal REVIEW_SCORE = new BigDecimal(20);

    public static BigDecimal SCORE100 = new BigDecimal(100);

    public static void computeSummary(StaffPerformSummaryEntity staffPerformSummaryEntity, WeightPdu weightPdu, StaffEntity staffEntity){

        BigDecimal reviewScore = weightPdu.reviewWeight().add(staffPerformSummaryEntity.getReviewScore());  //评价分数默认20分扣完为止
        if(reviewScore.compareTo(BigDecimal.ZERO)< 0){
            reviewScore = BigDecimal.ZERO;
        }
        BigDecimal attendScore = weightPdu.attendWeight().add(staffPerformSummaryEntity.getAttendScore()); //考勤
        if(attendScore.compareTo(BigDecimal.ZERO)< 0){
            attendScore = BigDecimal.ZERO;
        }

        BigDecimal otherScore = staffPerformSummaryEntity.getOtherScore();  //其他绩效加分项

        BigDecimal goworkScore = weightPdu.goworkWeight().add(staffPerformSummaryEntity.getGoworkScore());  //办件
        if(goworkScore.compareTo(BigDecimal.ZERO)< 0){
            goworkScore = BigDecimal.ZERO;
        }

        BigDecimal effectScore = weightPdu.effectWeight().add(staffPerformSummaryEntity.getEffectScore());  //效能
        if(effectScore.compareTo(BigDecimal.ZERO)< 0){
            effectScore = BigDecimal.ZERO;
        }

        BigDecimal complainScore = staffPerformSummaryEntity.getComplainScore();  //自评不用加100
        if(complainScore.compareTo(BigDecimal.ZERO)==0){
            complainScore = SCORE100;
        }
        complainScore = complainScore.multiply(weightPdu.selfWeight());
        BigDecimal summary = new BigDecimal(0);
        if(staffEntity.getReviewCheck()==1) {
            summary = summary.add(reviewScore);
        }else {
            summary = summary.add(weightPdu.reviewWeight());
            staffPerformSummaryEntity.setErrorScore(staffPerformSummaryEntity.getErrorScore().subtract(staffPerformSummaryEntity.getReviewScore()));
            staffPerformSummaryEntity.setReviewScore(BigDecimal.ZERO);
        }
        if(staffEntity.getAttendCheck()==1) {
            summary = summary.add(attendScore);
        }else {
            summary = summary.add(weightPdu.attendWeight());
            staffPerformSummaryEntity.setErrorScore(staffPerformSummaryEntity.getErrorScore().subtract(staffPerformSummaryEntity.getAttendScore()));
            staffPerformSummaryEntity.setAttendScore(BigDecimal.ZERO);
        }

        summary = summary.add(otherScore);

        if(staffEntity.getEffectCheck()==1){
            summary = summary.add(effectScore);
        }else {
            summary = summary.add(weightPdu.effectWeight());
            staffPerformSummaryEntity.setErrorScore(staffPerformSummaryEntity.getErrorScore().subtract(staffPerformSummaryEntity.getEffectScore()));
            staffPerformSummaryEntity.setEffectScore(BigDecimal.ZERO);
        }
        if(staffEntity.getGoworkCheck()==1) {
            summary = summary.add(goworkScore);
        }else {
            summary = summary.add(weightPdu.goworkWeight());
            staffPerformSummaryEntity.setErrorScore(staffPerformSummaryEntity.getErrorScore().subtract(staffPerformSummaryEntity.getGoworkScore()));
            staffPerformSummaryEntity.setGoworkScore(BigDecimal.ZERO);
        }
        if(staffEntity.getComplainCheck()==1){
            summary = summary.add(complainScore);
        }else {
            summary = summary.add(weightPdu.selfWeight());
            BigDecimal complainScoreErro = SCORE100.subtract(staffPerformSummaryEntity.getComplainScore());
            staffPerformSummaryEntity.setErrorScore(staffPerformSummaryEntity.getErrorScore().subtract(BigDecimal.ZERO.subtract(complainScoreErro)));
            staffPerformSummaryEntity.setComplainScore(BigDecimal.ZERO);
        }
        staffPerformSummaryEntity.setTotalScore(summary);
        if(staffPerformSummaryEntity.getAddTotalScore()!=null){
            staffPerformSummaryEntity.setSumScore(staffPerformSummaryEntity.getTotalScore().add(staffPerformSummaryEntity.getAddTotalScore()));
        }
    }
}
