AlgorithmFacade.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package com.diagbot.facade;
  2. import com.diagbot.dto.AlgorithmDTO;
  3. import com.diagbot.exception.CommonErrorCode;
  4. import com.diagbot.exception.CommonException;
  5. import com.diagbot.util.BigDecimalUtil;
  6. import com.diagbot.util.ListUtil;
  7. import com.diagbot.vo.AlgorithmVO;
  8. import com.diagbot.vo.QcResultAlgVO;
  9. import org.springframework.stereotype.Component;
  10. import java.math.BigDecimal;
  11. import java.util.Arrays;
  12. import java.util.HashMap;
  13. import java.util.List;
  14. import java.util.Map;
  15. /**
  16. * @Description: 评分算法
  17. * @author: gaodm
  18. * @time: 2020/4/14 9:52
  19. */
  20. @Component
  21. public class AlgorithmFacade {
  22. private final static List<Integer> types = Arrays.asList(0, 1, 2, 3);
  23. //获取评分结果和等级
  24. public AlgorithmDTO getAlgorithmRes(AlgorithmVO algorithmVO) {
  25. AlgorithmDTO algorithmDTO = new AlgorithmDTO();
  26. BigDecimal score = this.getScore(algorithmVO);
  27. algorithmDTO.setScore(score);
  28. algorithmDTO.setLevel(this.getLevel(score));
  29. return algorithmDTO;
  30. }
  31. //评结果分数
  32. private BigDecimal getScore(AlgorithmVO algorithmVO) {
  33. if (!types.contains(algorithmVO.getType())) {
  34. throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "操作类型只有0,1,2,3!");
  35. }
  36. //处理数据
  37. dataDeal(algorithmVO);
  38. //计算得分
  39. BigDecimal res = cal(algorithmVO);
  40. return res;
  41. }
  42. //评等级
  43. private String getLevel(BigDecimal score) {
  44. String level = "甲";
  45. //得分>90分为甲级
  46. if (BigDecimalUtil.gt(score, new BigDecimal(90))) {
  47. level = "甲";
  48. }
  49. //得分≤90分且得分>80分为乙级
  50. if (BigDecimalUtil.le(score, new BigDecimal(90))
  51. && BigDecimalUtil.gt(score, new BigDecimal(80))) {
  52. level = "乙";
  53. }
  54. //得分≤80分为丙级
  55. if (BigDecimalUtil.le(score, new BigDecimal(80))) {
  56. level = "丙";
  57. }
  58. return level;
  59. }
  60. //处理数据
  61. private void dataDeal(AlgorithmVO algorithmVO) {
  62. List<QcResultAlgVO> qcResultAlgVOList = algorithmVO.getQcResultAlgVOList();
  63. //新增
  64. if (algorithmVO.getType().equals(1)) {
  65. if (null == algorithmVO.getOptResultAlgVO()) {
  66. throw new CommonException(CommonErrorCode.PARAM_IS_NULL, "新增操作条目不能为空!");
  67. }
  68. //验证和操作数据
  69. if (ListUtil.isNotEmpty(qcResultAlgVOList)) {
  70. for (QcResultAlgVO qcResultAlgVO : qcResultAlgVOList) {
  71. if (qcResultAlgVO.getCasesId().equals(algorithmVO.getOptResultAlgVO().getCasesId())
  72. && qcResultAlgVO.getCasesEntryId().equals(algorithmVO.getOptResultAlgVO().getCasesEntryId())) {
  73. throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "不能重复加入已经评分的条目");
  74. } else {
  75. if (algorithmVO.getOptResultAlgVO().getCasesId().equals(qcResultAlgVO.getCasesId())) {
  76. algorithmVO.getOptResultAlgVO().setCasesScore(qcResultAlgVO.getCasesScore());
  77. }
  78. }
  79. }
  80. qcResultAlgVOList.add(algorithmVO.getOptResultAlgVO());
  81. }
  82. }
  83. //删除
  84. else if (algorithmVO.getType().equals(2)) {
  85. if (null == algorithmVO.getOptResultAlgVO()) {
  86. throw new CommonException(CommonErrorCode.PARAM_IS_NULL, "删除操作条目不能为空!");
  87. }
  88. //操作数据
  89. if (ListUtil.isNotEmpty(qcResultAlgVOList)) {
  90. Boolean hasData = false;
  91. Long optId = algorithmVO.getOptResultAlgVO().getId(); // 操作id
  92. for (QcResultAlgVO qcResultAlgVO : qcResultAlgVOList) {
  93. if (qcResultAlgVO.getId().equals(optId)) {
  94. qcResultAlgVOList.remove(qcResultAlgVO);
  95. hasData = true;
  96. break;
  97. }
  98. }
  99. if (!hasData){
  100. throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "删除的条目不存在!");
  101. }
  102. }
  103. }
  104. //修改
  105. else if (algorithmVO.getType().equals(3)) {
  106. if (null == algorithmVO.getOptResultAlgVO()) {
  107. throw new CommonException(CommonErrorCode.PARAM_IS_NULL, "修改操作条目不能为空!");
  108. }
  109. //操作数据
  110. if (ListUtil.isNotEmpty(qcResultAlgVOList)) {
  111. Boolean hasData = false;
  112. for (QcResultAlgVO qcResultAlgVO : qcResultAlgVOList) {
  113. if (qcResultAlgVO.getId().equals(algorithmVO.getOptResultAlgVO().getId())) {
  114. qcResultAlgVOList.remove(qcResultAlgVO);
  115. qcResultAlgVOList.add(algorithmVO.getOptResultAlgVO());
  116. hasData = true;
  117. break;
  118. }
  119. }
  120. if (!hasData){
  121. throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "修改的条目不存在!");
  122. }
  123. }
  124. }
  125. }
  126. //计算
  127. private BigDecimal cal(AlgorithmVO algorithmVO) {
  128. BigDecimal res = new BigDecimal(100);
  129. //模块总分
  130. Map<Long, BigDecimal> casesMap = new HashMap<>();
  131. //单票否决计分
  132. BigDecimal rejectScore = BigDecimal.ZERO;
  133. //模块计分
  134. Map<Long, BigDecimal> casesScoreMap = new HashMap<>();
  135. List<QcResultAlgVO> qcResultAlgVOList = algorithmVO.getQcResultAlgVOList();
  136. if (ListUtil.isNotEmpty(qcResultAlgVOList)) {
  137. for (QcResultAlgVO qcResultAlgVO : qcResultAlgVOList) {
  138. if (!casesMap.containsKey(qcResultAlgVO.getCasesId())) {
  139. casesMap.put(qcResultAlgVO.getCasesId(), qcResultAlgVO.getCasesScore());
  140. }
  141. if (!casesScoreMap.containsKey(qcResultAlgVO.getCasesId())) {
  142. casesScoreMap.put(qcResultAlgVO.getCasesId(), BigDecimal.ZERO);
  143. }
  144. //单票否决计分
  145. if (qcResultAlgVO.getIsReject().equals(1)) {
  146. rejectScore = rejectScore.add(qcResultAlgVO.getScore());
  147. } else {
  148. //非单票否决计分
  149. if (casesScoreMap.containsKey(qcResultAlgVO.getCasesId())) {
  150. BigDecimal casesScore = casesScoreMap.get(qcResultAlgVO.getCasesId());
  151. casesScore = casesScore.add(qcResultAlgVO.getScore());
  152. casesScoreMap.put(qcResultAlgVO.getCasesId(), casesScore);
  153. }
  154. }
  155. }
  156. //结果先减去单票否决计分总和
  157. res = res.subtract(rejectScore);
  158. //结果小于0按0计算
  159. if (BigDecimalUtil.lt(res, BigDecimal.ZERO)) {
  160. return BigDecimal.ZERO;
  161. } else {
  162. //模块计分
  163. for (Map.Entry<Long, BigDecimal> casesScore : casesScoreMap.entrySet()) {
  164. BigDecimal allSccore = casesMap.get(casesScore.getKey());
  165. if (BigDecimalUtil.le(allSccore, casesScore.getValue())) {
  166. //模块标准分小于等于模块减分总和就用模块标准分
  167. res = res.subtract(allSccore);
  168. } else {
  169. //模块标准分大于模块减分总和就用模块减分总和
  170. res = res.subtract(casesScore.getValue());
  171. }
  172. }
  173. }
  174. //结果小于0按0计算
  175. if (BigDecimalUtil.lt(res, BigDecimal.ZERO)) {
  176. res = BigDecimal.ZERO;
  177. }
  178. }
  179. return res;
  180. }
  181. public static void main(String[] args) {
  182. AlgorithmFacade algorithmFacade = new AlgorithmFacade();
  183. System.out.println("100:" + algorithmFacade.getLevel(new BigDecimal(100)));
  184. System.out.println("95:" + algorithmFacade.getLevel(new BigDecimal(95)));
  185. System.out.println("90.1:" + algorithmFacade.getLevel(new BigDecimal(90.1)));
  186. System.out.println("90:" + algorithmFacade.getLevel(new BigDecimal(90)));
  187. System.out.println("85:" + algorithmFacade.getLevel(new BigDecimal(85)));
  188. System.out.println("80.1:" + algorithmFacade.getLevel(new BigDecimal(80.1)));
  189. System.out.println("80:" + algorithmFacade.getLevel(new BigDecimal(80)));
  190. System.out.println("79.9:" + algorithmFacade.getLevel(new BigDecimal(79.9)));
  191. System.out.println("60:" + algorithmFacade.getLevel(new BigDecimal(60)));
  192. System.out.println("0:" + algorithmFacade.getLevel(new BigDecimal(0)));
  193. if (BigDecimalUtil.lt(new BigDecimal(-1), BigDecimal.ZERO)) {
  194. System.out.println(BigDecimal.ZERO);
  195. }
  196. }
  197. }