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

152 lines
6.1 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.Comparator;
2024-09-06 17:37:31 +08:00
import java.util.List;
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;
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);
updateObjectAndTask(assessTeam.getObjectId(),vo.getTaskId(),template,task);
return this.saveBatch(ratings);
}
private void delData(Long teamId) {
List<AssessTeamRating> teamRatings = this.list(new QueryWrapper<AssessTeamRating>().eq("team_id", teamId));
if(CollectionUtils.isNotEmpty(teamRatings)){
List<Long> ratingIds = teamRatings.stream().map(AssessTeamRating::getId).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();
BigDecimal assessScore;
if(scoreWay == 1){
assessScore = finishTeams.stream().min(Comparator.comparing(AssessTeam::getAssessScore)).get().getAssessScore();
}else {
assessScore = finishTeams.stream().map(AssessTeam::getAssessScore).reduce(BigDecimal.ZERO,BigDecimal::add).divide(BigDecimal.valueOf(finishTeams.size()),2, RoundingMode.HALF_UP);
}
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
}
public String getGroupId() {
return "assessTeamRating";
}
public List<AssessRatingVo> detail(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-06 17:37:31 +08:00
}