package com.zcxk.evaluation.service.impl;

import com.zcxk.evaluation.dao.EvaluationResultDetailsMapper;
import com.zcxk.evaluation.dao.EvaluationResultMapper;
import com.zcxk.evaluation.dto.*;
import com.zcxk.evaluation.enums.EvaluationResultStatus;
import com.zcxk.evaluation.service.EvaluationGradeService;
import com.zcxk.evaluation.service.EvaluationResultDetailsService;
import com.zcxk.exception.ServiceException;
import com.zcxk.model.LoginUser;
import com.zcxk.util.IntegerUtil;
import com.zcxk.util.UserUtil;
import com.zcxk.utils.OperactionStringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName EvaluationResultServiceImpl
 * @Description: 考评结果业务处理
 * @Author lihui
 * @Date 2021/5/7
 * @Version V1.0
 **/
@Slf4j
@Service
public class EvaluationResultDetailsServiceImpl  implements EvaluationResultDetailsService {

    @Autowired
    private EvaluationResultDetailsMapper evaluationResultDetailsMapper;

    @Autowired
    private EvaluationResultMapper evaluationResultMapper;

    @Autowired
    private EvaluationGradeService evaluationGradeService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEvaluateScore(EvaluationResultDetailsDto dto) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        Integer resultId = dto.getResultId();
        if (IntegerUtil.isNullOrZero(resultId)){
            throw new ServiceException(EvaluationResultStatus.PARAM_ERROR.getStatus(), EvaluationResultStatus.PARAM_ERROR.getMessage());
        }
        Integer state = evaluationResultMapper.findEvaluationState(resultId);
        if (state == null){
            throw new ServiceException(EvaluationResultStatus.DATA_NOT_FOUND.getStatus(), EvaluationResultStatus.DATA_NOT_FOUND.getMessage());
        }
        if (state == 1) {
            throw new ServiceException(EvaluationResultStatus.IS_ASSESSED.getStatus(), EvaluationResultStatus.IS_ASSESSED.getMessage());
        }
        List<Integer> ids = evaluationResultDetailsMapper.findResultDetailsIds(resultId);
        List<EvaluationDetailsDto>  evaluationDetailsDtos =  dto.getDetailsDtoList();
        List<Integer> detailIds = evaluationDetailsDtos.stream().map(EvaluationDetailsDto::getDetailId).collect(Collectors.toList());
        if (!OperactionStringUtils.checkDifferent(ids, detailIds)){
            throw new ServiceException(EvaluationResultStatus.PARAM_ERROR.getStatus(), EvaluationResultStatus.PARAM_ERROR.getMessage());
        }
        // 记录总得分
        BigDecimal score = new BigDecimal(0);
        // 修改得分详情
        for (EvaluationDetailsDto detailsDto : evaluationDetailsDtos) {
             evaluationResultDetailsMapper.updateEvaluateScore(detailsDto);
             score = score.add(detailsDto.getScore());
        }
        // 获取分数对应的等级
        String evaluationGrade = evaluationGradeService.getScoreGrade(dto.getTenantId(), dto.getCompanyOrgId(), score, false);
        EvaluationResultUpdateDto updateDto = new EvaluationResultUpdateDto();
        updateDto.setUpdateBy(currentUser.getUsername());
        updateDto.setEvaluationGrade(StringUtils.isEmpty(evaluationGrade) ? null : Integer.parseInt(evaluationGrade));
        updateDto.setEvaluationResultId(resultId);
        updateDto.setDateEvaluation(new Date());
        updateDto.setDateUpdate(new Date());
        updateDto.setScore(score);
        updateDto.setEvaluationBy(currentUser.getUsername());
        // 修改最终得分
        evaluationResultMapper.updateEvaluationScore(updateDto);
    }



}
