123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- package com.diagbot.facade;
- import com.diagbot.dto.AlgorithmDTO;
- import com.diagbot.exception.CommonErrorCode;
- import com.diagbot.exception.CommonException;
- import com.diagbot.util.BigDecimalUtil;
- import com.diagbot.util.ListUtil;
- import com.diagbot.vo.AlgorithmVO;
- import com.diagbot.vo.QcResultAlgVO;
- import org.springframework.stereotype.Component;
- import java.math.BigDecimal;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * @Description: 评分算法
- * @author: gaodm
- * @time: 2020/4/14 9:52
- */
- @Component
- public class AlgorithmFacade {
- private final static List<Integer> types = Arrays.asList(0, 1, 2, 3);
- //获取评分结果和等级
- public AlgorithmDTO getAlgorithmRes(AlgorithmVO algorithmVO) {
- AlgorithmDTO algorithmDTO = new AlgorithmDTO();
- BigDecimal score = this.getScore(algorithmVO);
- algorithmDTO.setScore(score);
- algorithmDTO.setLevel(this.getLevel(score));
- return algorithmDTO;
- }
- //评结果分数
- private BigDecimal getScore(AlgorithmVO algorithmVO) {
- if (!types.contains(algorithmVO.getType())) {
- throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "操作类型只有0,1,2,3!");
- }
- //处理数据
- dataDeal(algorithmVO);
- //计算得分
- BigDecimal res = cal(algorithmVO);
- return res;
- }
- //评等级
- private String getLevel(BigDecimal score) {
- String level = "甲";
- //得分>90分为甲级
- if (BigDecimalUtil.gt(score, new BigDecimal(90))) {
- level = "甲";
- }
- //得分≤90分且得分>80分为乙级
- if (BigDecimalUtil.le(score, new BigDecimal(90))
- && BigDecimalUtil.gt(score, new BigDecimal(80))) {
- level = "乙";
- }
- //得分≤80分为丙级
- if (BigDecimalUtil.le(score, new BigDecimal(80))) {
- level = "丙";
- }
- return level;
- }
- //处理数据
- private void dataDeal(AlgorithmVO algorithmVO) {
- List<QcResultAlgVO> qcResultAlgVOList = algorithmVO.getQcResultAlgVOList();
- //新增
- if (algorithmVO.getType().equals(1)) {
- if (null == algorithmVO.getOptResultAlgVO()) {
- throw new CommonException(CommonErrorCode.PARAM_IS_NULL, "新增操作条目不能为空!");
- }
- //验证和操作数据
- if (ListUtil.isNotEmpty(qcResultAlgVOList)) {
- for (QcResultAlgVO qcResultAlgVO : qcResultAlgVOList) {
- if (qcResultAlgVO.getCasesId().equals(algorithmVO.getOptResultAlgVO().getCasesId())
- && qcResultAlgVO.getCasesEntryId().equals(algorithmVO.getOptResultAlgVO().getCasesEntryId())) {
- throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "不能重复加入已经评分的条目");
- } else {
- if (algorithmVO.getOptResultAlgVO().getCasesId().equals(qcResultAlgVO.getCasesId())) {
- algorithmVO.getOptResultAlgVO().setCasesScore(qcResultAlgVO.getCasesScore());
- }
- }
- }
- qcResultAlgVOList.add(algorithmVO.getOptResultAlgVO());
- }
- }
- //删除
- else if (algorithmVO.getType().equals(2)) {
- if (null == algorithmVO.getOptResultAlgVO()) {
- throw new CommonException(CommonErrorCode.PARAM_IS_NULL, "删除操作条目不能为空!");
- }
- //操作数据
- if (ListUtil.isNotEmpty(qcResultAlgVOList)) {
- Boolean hasData = false;
- Long optId = algorithmVO.getOptResultAlgVO().getId(); // 操作id
- for (QcResultAlgVO qcResultAlgVO : qcResultAlgVOList) {
- if (qcResultAlgVO.getId().equals(optId)) {
- qcResultAlgVOList.remove(qcResultAlgVO);
- hasData = true;
- break;
- }
- }
- if (!hasData){
- throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "删除的条目不存在!");
- }
- }
- }
- //修改
- else if (algorithmVO.getType().equals(3)) {
- if (null == algorithmVO.getOptResultAlgVO()) {
- throw new CommonException(CommonErrorCode.PARAM_IS_NULL, "修改操作条目不能为空!");
- }
- //操作数据
- if (ListUtil.isNotEmpty(qcResultAlgVOList)) {
- Boolean hasData = false;
- for (QcResultAlgVO qcResultAlgVO : qcResultAlgVOList) {
- if (qcResultAlgVO.getId().equals(algorithmVO.getOptResultAlgVO().getId())) {
- qcResultAlgVOList.remove(qcResultAlgVO);
- qcResultAlgVOList.add(algorithmVO.getOptResultAlgVO());
- hasData = true;
- break;
- }
- }
- if (!hasData){
- throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "修改的条目不存在!");
- }
- }
- }
- }
- //计算
- private BigDecimal cal(AlgorithmVO algorithmVO) {
- BigDecimal res = new BigDecimal(100);
- //模块总分
- Map<Long, BigDecimal> casesMap = new HashMap<>();
- //单票否决计分
- BigDecimal rejectScore = BigDecimal.ZERO;
- //模块计分
- Map<Long, BigDecimal> casesScoreMap = new HashMap<>();
- List<QcResultAlgVO> qcResultAlgVOList = algorithmVO.getQcResultAlgVOList();
- if (ListUtil.isNotEmpty(qcResultAlgVOList)) {
- for (QcResultAlgVO qcResultAlgVO : qcResultAlgVOList) {
- if (!casesMap.containsKey(qcResultAlgVO.getCasesId())) {
- casesMap.put(qcResultAlgVO.getCasesId(), qcResultAlgVO.getCasesScore());
- }
- if (!casesScoreMap.containsKey(qcResultAlgVO.getCasesId())) {
- casesScoreMap.put(qcResultAlgVO.getCasesId(), BigDecimal.ZERO);
- }
- //单票否决计分
- if (qcResultAlgVO.getIsReject().equals(1)) {
- rejectScore = rejectScore.add(qcResultAlgVO.getScore());
- } else {
- //非单票否决计分
- if (casesScoreMap.containsKey(qcResultAlgVO.getCasesId())) {
- BigDecimal casesScore = casesScoreMap.get(qcResultAlgVO.getCasesId());
- casesScore = casesScore.add(qcResultAlgVO.getScore());
- casesScoreMap.put(qcResultAlgVO.getCasesId(), casesScore);
- }
- }
- }
- //结果先减去单票否决计分总和
- res = res.subtract(rejectScore);
- //结果小于0按0计算
- if (BigDecimalUtil.lt(res, BigDecimal.ZERO)) {
- return BigDecimal.ZERO;
- } else {
- //模块计分
- for (Map.Entry<Long, BigDecimal> casesScore : casesScoreMap.entrySet()) {
- BigDecimal allSccore = casesMap.get(casesScore.getKey());
- if (BigDecimalUtil.le(allSccore, casesScore.getValue())) {
- //模块标准分小于等于模块减分总和就用模块标准分
- res = res.subtract(allSccore);
- } else {
- //模块标准分大于模块减分总和就用模块减分总和
- res = res.subtract(casesScore.getValue());
- }
- }
- }
- //结果小于0按0计算
- if (BigDecimalUtil.lt(res, BigDecimal.ZERO)) {
- res = BigDecimal.ZERO;
- }
- }
- return res;
- }
- public static void main(String[] args) {
- AlgorithmFacade algorithmFacade = new AlgorithmFacade();
- System.out.println("100:" + algorithmFacade.getLevel(new BigDecimal(100)));
- System.out.println("95:" + algorithmFacade.getLevel(new BigDecimal(95)));
- System.out.println("90.1:" + algorithmFacade.getLevel(new BigDecimal(90.1)));
- System.out.println("90:" + algorithmFacade.getLevel(new BigDecimal(90)));
- System.out.println("85:" + algorithmFacade.getLevel(new BigDecimal(85)));
- System.out.println("80.1:" + algorithmFacade.getLevel(new BigDecimal(80.1)));
- System.out.println("80:" + algorithmFacade.getLevel(new BigDecimal(80)));
- System.out.println("79.9:" + algorithmFacade.getLevel(new BigDecimal(79.9)));
- System.out.println("60:" + algorithmFacade.getLevel(new BigDecimal(60)));
- System.out.println("0:" + algorithmFacade.getLevel(new BigDecimal(0)));
- if (BigDecimalUtil.lt(new BigDecimal(-1), BigDecimal.ZERO)) {
- System.out.println(BigDecimal.ZERO);
- }
- }
- }
|