gunshi-project-ss/src/main/java/com/gunshi/project/xyt/service/AssessTeamRatingService.java

187 lines
8.0 KiB
Java
Raw Normal View History

2024-09-06 17:37:31 +08:00
package com.gunshi.project.xyt.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
2024-09-06 17:37:31 +08:00
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gunshi.project.xyt.entity.vo.AssessRatingVo;
import com.gunshi.project.xyt.entity.vo.AssessScoreVo;
import com.gunshi.project.xyt.mapper.*;
import com.gunshi.project.xyt.model.*;
2024-09-06 17:37:31 +08:00
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;
2024-09-06 17:37:31 +08:00
import java.util.List;
2024-09-10 09:27:05 +08:00
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
2024-09-06 17:37:31 +08:00
/**
* :
* author: xusan
* date: 2024-09-05 14:20:04
*/
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class AssessTeamRatingService extends ServiceImpl<AssessTeamRatingMapper, AssessTeamRating>
{
@Autowired
private FileAssociationsService fileService;
@Autowired
private AssessObjectMapper objectMapper;
@Autowired
private AssessTaskMapper taskMapper;
2024-09-06 17:37:31 +08:00
@Autowired
private AssessTeamMapper teamMapper;
@Autowired
private AssessTemplateMapper templateMapper;
@Autowired
private AssessObjectRatingService assessObjectRatingService;
2024-09-06 17:37:31 +08:00
public Boolean score(AssessScoreVo vo) {
List<AssessTeamRating> ratings = vo.getRatings();
Long teamId = ratings.get(0).getTeamId();
this.delData(teamId);
for(AssessTeamRating rating : ratings){
2024-09-06 17:37:31 +08:00
rating.setId(IdWorker.getId());
fileService.saveFile(rating.getFiles(), getGroupId(), rating.getId().toString());
}
AssessTask task = taskMapper.selectById(vo.getTaskId());
2024-09-06 17:37:31 +08:00
//更新该考核人员对考核对象的状态为已评分
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);
2024-09-06 17:37:31 +08:00
teamMapper.updateById(assessTeam);
Boolean res = this.saveBatch(ratings);
updateObjectAndTask(assessTeam.getObjectId(),vo.getTaskId(),template,task);
return res;
}
private void delData(Long teamId) {
List<AssessTeamRating> teamRatings = this.list(new QueryWrapper<AssessTeamRating>().eq("team_id", teamId));
if(CollectionUtils.isNotEmpty(teamRatings)){
List<String> ratingIds = teamRatings.stream().map(AssessTeamRating::getId).map(Objects::toString).collect(Collectors.toList());
fileService.remove(new QueryWrapper<FileAssociations>().in("business_id",ratingIds));
this.remove(new QueryWrapper<AssessTeamRating>().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;
}
2024-09-06 17:37:31 +08:00
}
private void updateObjectAndTask(Long objectId, Long taskId,AssessTemplate template,AssessTask task) {
2024-09-06 17:37:31 +08:00
//先判断该次评分是否是该考核对象的最后一次评分
//如果为最后一次评分,就要计算该考核对象的最终得分
List<AssessTeam> teams = teamMapper.selectList(new QueryWrapper<AssessTeam>().eq("object_id", objectId).eq("task_id", taskId));
List<AssessTeam> finishTeams = teams.stream().filter(o -> o.getStatus() == 2).collect(Collectors.toList());
if(teams.size() == finishTeams.size()){
Integer scoreWay = task.getScoreWay();
2024-09-10 09:27:05 +08:00
List<Long> 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<AssessObject>().eq("task_id", taskId).eq("status", 1));
if(taskCount == 0){
task.setStatus(2);
taskMapper.updateById(task);
}
}
2024-09-06 17:37:31 +08:00
}
private BigDecimal calcScore(Integer scoreWay, List<Long> teamIds,Long objectId) {
final BigDecimal[] score = {new BigDecimal(0)};
2024-09-10 09:27:05 +08:00
List<AssessTeamRating> ratings = this.list(new QueryWrapper<AssessTeamRating>().in("team_id",teamIds));
Map<Long, List<BigDecimal>> map = ratings.stream().collect(Collectors.groupingBy(AssessTeamRating::getIndicatorId,Collectors.mapping(AssessTeamRating::getAssessScore,Collectors.toList())));
Map<Long, List<Integer>> standardMap = ratings.stream().collect(Collectors.groupingBy(AssessTeamRating::getIndicatorId,Collectors.mapping(AssessTeamRating::getStandardScore,Collectors.toList())));
List<AssessObjectRating> list = new ArrayList<>();
2024-09-10 09:27:05 +08:00
map.entrySet().forEach(o->{
List<BigDecimal> 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));
2024-09-10 09:27:05 +08:00
if (scoreWay == 1){
BigDecimal val = value.stream().min(BigDecimal::compareTo).get();
objectRating.setAssessScore(val);
score[0] = score[0].add(val);
2024-09-10 09:27:05 +08:00
}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);
2024-09-10 09:27:05 +08:00
}
list.add(objectRating);
2024-09-10 09:27:05 +08:00
});
assessObjectRatingService.saveBatch(list);
return score[0];
2024-09-10 09:27:05 +08:00
}
2024-09-06 17:37:31 +08:00
public String getGroupId() {
return "assessTeamRating";
}
2024-09-10 09:27:05 +08:00
public List<AssessRatingVo> doDetail(Long teamId) {
List<AssessRatingVo> list = this.baseMapper.scoreDetail(teamId);
for (AssessRatingVo vo : list){
if(vo.getIsNeedRectify() == 1){
vo.setFiles(fileService.getFiles(getGroupId(),vo.getId().toString()));
}
}
return list;
}
public Boolean saveScore(AssessScoreVo vo) {
List<AssessTeamRating> ratings = vo.getRatings();
for(AssessTeamRating rating : ratings){
rating.setId(IdWorker.getId());
fileService.saveFile(rating.getFiles(), getGroupId(), rating.getId().toString());
}
Long teamId = ratings.get(0).getTeamId();
AssessTeam assessTeam = teamMapper.selectById(teamId);
assessTeam.setStatus(9);
teamMapper.updateById(assessTeam);
return this.saveBatch(ratings);
}
2024-09-10 09:27:05 +08:00
public Map<Long,List<AssessRatingVo>> scoreDetail(Long objectId) {
List<AssessRatingVo> list = this.baseMapper.scoreByObjectId(objectId);
return list.stream().collect(Collectors.groupingBy(AssessTeamRating::getIndicatorId));
}
2024-09-06 17:37:31 +08:00
}