package com.gunshi.project.xyt.service; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.IdWorker; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.gunshi.db.dto.MonthRangeSo; import com.gunshi.project.xyt.entity.so.AssessTaskPageSo; import com.gunshi.project.xyt.entity.vo.AssessRatingVo; import com.gunshi.project.xyt.entity.vo.AssessRectifyVo; import com.gunshi.project.xyt.entity.vo.AssessScoreVo; import com.gunshi.project.xyt.mapper.*; import com.gunshi.project.xyt.model.*; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; /** * 描述: 考核评分详情 * author: xusan * date: 2024-09-05 14:20:04 */ @Service @Slf4j @Transactional(rollbackFor = Exception.class) public class AssessTeamRatingService extends ServiceImpl { @Autowired private FileAssociationsService fileService; @Autowired private AssessObjectMapper objectMapper; @Autowired private AssessTaskMapper taskMapper; @Autowired private AssessTeamMapper teamMapper; @Autowired private AssessTemplateMapper templateMapper; @Autowired private AssessObjectRatingService assessObjectRatingService; @Autowired private AssessIndicatorRatingService indicatorRatingService; public Boolean score(AssessScoreVo vo) { List ratings = vo.getRatings(); Long teamId = ratings.get(0).getTeamId(); this.delData(teamId); for(AssessTeamRating rating : ratings){ rating.setId(IdWorker.getId()); rating.setRectifyStatus(0); fileService.save(rating.getFiles(), rating.getId().toString(), getGroupId(),getScoreType()); } AssessTask task = taskMapper.selectById(vo.getTaskId()); //更新该考核人员对考核对象的状态为已评分 AssessTeam assessTeam = teamMapper.selectById(teamId); assessTeam.setStatus(2); //获取模板信息 AssessTemplate template = templateMapper.selectById(task.getTemplateId()); //根据总得分计算等级 Integer level = calcLevel(template,vo.getScore()); assessTeam.setAssessScore(vo.getScore()); assessTeam.setAssessLevel(level); teamMapper.updateById(assessTeam); Boolean res = this.saveBatch(ratings); updateObjectAndTask(assessTeam.getObjectId(),vo.getTaskId(),template,task); return res; } private void delData(Long teamId) { List teamRatings = this.list(new QueryWrapper().eq("team_id", teamId)); if(CollectionUtils.isNotEmpty(teamRatings)){ List ratingIds = teamRatings.stream().map(AssessTeamRating::getId).map(Objects::toString).collect(Collectors.toList()); fileService.remove(new QueryWrapper().in("business_id",ratingIds)); this.remove(new QueryWrapper().eq("team_id", teamId)); } } private Integer calcLevel(AssessTemplate template, BigDecimal score) { BigDecimal excellentScore = template.getExcellentScore(); BigDecimal goodScore = template.getGoodScore(); BigDecimal passScore = template.getPassScore(); if(score.compareTo(excellentScore) >= 0){ return 1; }else if(score.compareTo(goodScore) >=0 && score.compareTo(excellentScore) < 0){ return 2; }else if(score.compareTo(passScore) >=0 && score.compareTo(goodScore) < 0){ return 3; }else{ return 4; } } private void updateObjectAndTask(Long objectId, Long taskId,AssessTemplate template,AssessTask task) { //先判断该次评分是否是该考核对象的最后一次评分 //如果为最后一次评分,就要计算该考核对象的最终得分 List teams = teamMapper.selectList(new QueryWrapper().eq("object_id", objectId).eq("task_id", taskId)); List finishTeams = teams.stream().filter(o -> o.getStatus() == 2).collect(Collectors.toList()); if(teams.size() == finishTeams.size()){ Integer scoreWay = task.getScoreWay(); List teamIds = finishTeams.stream().map(AssessTeam::getId).collect(Collectors.toList()); BigDecimal assessScore = calcScore(scoreWay,teamIds,objectId); Integer level = calcLevel(template, assessScore); AssessObject object = objectMapper.selectById(objectId); object.setStatus(2); object.setAssessScore(assessScore); object.setAssessLevel(level); objectMapper.updateById(object); //判断该考核任务是否存在未打分的对象,不存在则更新考核任务的状态为已完成 Long taskCount = objectMapper.selectCount(new QueryWrapper().eq("task_id", taskId).eq("status", 1)); if(taskCount == 0){ task.setStatus(2); taskMapper.updateById(task); } } } private BigDecimal calcScore(Integer scoreWay, List teamIds,Long objectId) { final BigDecimal[] score = {new BigDecimal(0)}; List ratings = this.list(new QueryWrapper().in("team_id",teamIds)); Map> map = ratings.stream().collect(Collectors.groupingBy(AssessTeamRating::getIndicatorId,Collectors.mapping(AssessTeamRating::getAssessScore,Collectors.toList()))); Map> standardMap = ratings.stream().collect(Collectors.groupingBy(AssessTeamRating::getIndicatorId,Collectors.mapping(AssessTeamRating::getStandardScore,Collectors.toList()))); List list = new ArrayList<>(); map.entrySet().forEach(o->{ List value = o.getValue(); AssessObjectRating objectRating = new AssessObjectRating(); objectRating.setId(IdWorker.getId()); objectRating.setObjectId(objectId); objectRating.setIndicatorId(o.getKey()); objectRating.setStandardScore(standardMap.get(o.getKey()).get(0)); if (scoreWay == 1){ BigDecimal val = value.stream().min(BigDecimal::compareTo).get(); objectRating.setAssessScore(val); score[0] = score[0].add(val); }else{ BigDecimal vide = value.stream().reduce(BigDecimal.ZERO, BigDecimal::add).divide(BigDecimal.valueOf(value.size()), 2, RoundingMode.HALF_UP); objectRating.setAssessScore(vide); score[0] = score[0].add(vide); } list.add(objectRating); }); assessObjectRatingService.saveBatch(list); return score[0]; } public String getGroupId() { return "assessTeamRating"; } public String getScoreType() { return "assessScore"; } public String getRectifyType() { return "assessRectify"; } public List doDetail(Long teamId) { List list = this.baseMapper.scoreDetail(teamId); fillRating(list); return list; } public Boolean saveScore(AssessScoreVo vo) { List ratings = vo.getRatings(); for(AssessTeamRating rating : ratings){ rating.setId(IdWorker.getId()); rating.setRectifyStatus(0); fileService.save(rating.getFiles(), rating.getId().toString(), getGroupId(),getScoreType()); } Long teamId = ratings.get(0).getTeamId(); this.delData(teamId); AssessTeam assessTeam = teamMapper.selectById(teamId); assessTeam.setAssessScore(vo.getScore()); assessTeam.setStatus(9); teamMapper.updateById(assessTeam); return this.saveOrUpdateBatch(ratings); } public Map> scoreDetail(Long objectId) { List list = this.baseMapper.scoreByObjectId(objectId); fillRating(list); return list.stream().collect(Collectors.groupingBy(AssessTeamRating::getIndicatorId)); } private void fillRating(List list){ List relList = indicatorRatingService.queryRatingList(list.stream().map(AssessRatingVo::getIndicatorId).distinct().collect(Collectors.toList())); Map> map = relList.stream().collect(Collectors.groupingBy(AssessIndicatorRating::getIndicatorId)); for (AssessRatingVo vo : list){ vo.setIndicatorRatings(map.get(vo.getIndicatorId())); if(vo.getIsNeedRectify() == 1){ vo.setFiles(fileService.queryFileList(vo.getId().toString(),getGroupId(),getScoreType())); } } } public Page listPage(AssessTaskPageSo page) { Page res = this.baseMapper.listPage(page.getPageSo().toPage(), page); if (res.getRecords() != null && res.getRecords().size() > 0) { for (AssessRectifyVo record : res.getRecords()) { record.setFiles(fileService.queryFileList(record.getId().toString(),getGroupId(),getScoreType())); record.setRectifyFiles(fileService.queryFileList(record.getId().toString(),getGroupId(),getRectifyType())); } } return res; } public String rectify(AssessTeamRating rating) { rating.setRectifyStatus(1); this.updateById(rating); fileService.save(rating.getRectifyFiles(),rating.getId().toString(),getGroupId(),getRectifyType()); return "整改成功"; } public Map rectifyStat(MonthRangeSo monthRangeSo) { List list = this.baseMapper.rectifyStat(monthRangeSo); return list.stream().collect(Collectors.groupingBy(AssessTeamRating::getRectifyStatus, Collectors.counting())); } }