|  |  | @ -1,6 +1,8 @@ | 
			
		
	
		
			
				
					|  |  |  | package com.epmet.service.indexcal.impl; | 
			
		
	
		
			
				
					|  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  | import com.alibaba.fastjson.JSON; | 
			
		
	
		
			
				
					|  |  |  | import com.epmet.commons.tools.constant.StrConstant; | 
			
		
	
		
			
				
					|  |  |  | import com.epmet.constant.IndexCalConstant; | 
			
		
	
		
			
				
					|  |  |  | import com.epmet.dao.indexcoll.FactIndexPartyAblityCpcMonthlyDao; | 
			
		
	
		
			
				
					|  |  |  | import com.epmet.dao.screen.IndexCodeFieldReDao; | 
			
		
	
		
			
				
					|  |  |  | import com.epmet.dto.indexcal.CalculateCommonFormDTO; | 
			
		
	
	
		
			
				
					|  |  | @ -9,14 +11,26 @@ import com.epmet.eum.IndexCodeEnum; | 
			
		
	
		
			
				
					|  |  |  | import com.epmet.service.indexcal.CpcIndexCalculateService; | 
			
		
	
		
			
				
					|  |  |  | import com.epmet.service.indexcal.IndexCodeFieldReService; | 
			
		
	
		
			
				
					|  |  |  | import com.epmet.service.screen.IndexGroupDetailService; | 
			
		
	
		
			
				
					|  |  |  | import com.epmet.support.normalizing.BigDecimalScoreCalculator; | 
			
		
	
		
			
				
					|  |  |  | import com.epmet.support.normalizing.Correlation; | 
			
		
	
		
			
				
					|  |  |  | import com.epmet.support.normalizing.ScoreCalculator; | 
			
		
	
		
			
				
					|  |  |  | import com.epmet.support.normalizing.ScoreConstants; | 
			
		
	
		
			
				
					|  |  |  | import com.epmet.support.normalizing.batch.BatchScoreCalculator; | 
			
		
	
		
			
				
					|  |  |  | import com.epmet.support.normalizing.batch.IndexInputVO; | 
			
		
	
		
			
				
					|  |  |  | import com.epmet.support.normalizing.batch.SampleValue; | 
			
		
	
		
			
				
					|  |  |  | import lombok.extern.slf4j.Slf4j; | 
			
		
	
		
			
				
					|  |  |  | import org.apache.commons.lang3.StringUtils; | 
			
		
	
		
			
				
					|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
			
		
	
		
			
				
					|  |  |  | import org.springframework.stereotype.Service; | 
			
		
	
		
			
				
					|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
			
		
	
		
			
				
					|  |  |  | import org.springframework.util.CollectionUtils; | 
			
		
	
		
			
				
					|  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  | import java.math.BigDecimal; | 
			
		
	
		
			
				
					|  |  |  | import java.util.ArrayList; | 
			
		
	
		
			
				
					|  |  |  | import java.util.HashMap; | 
			
		
	
		
			
				
					|  |  |  | import java.util.List; | 
			
		
	
		
			
				
					|  |  |  | import java.util.Map; | 
			
		
	
		
			
				
					|  |  |  | import java.util.stream.Collectors; | 
			
		
	
		
			
				
					|  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  | @Slf4j | 
			
		
	
		
			
				
					|  |  |  | @Service | 
			
		
	
	
		
			
				
					|  |  | @ -29,35 +43,144 @@ public class CpcIndexCalculateServiceImpl implements CpcIndexCalculateService { | 
			
		
	
		
			
				
					|  |  |  |     private IndexGroupDetailService getDetailListByParentCode; | 
			
		
	
		
			
				
					|  |  |  |     @Autowired | 
			
		
	
		
			
				
					|  |  |  |     private IndexCodeFieldReService indexCodeFieldReService; | 
			
		
	
		
			
				
					|  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |     @Override | 
			
		
	
		
			
				
					|  |  |  |     public Boolean cpcIndexCalculate(CalculateCommonFormDTO formDTO) { | 
			
		
	
		
			
				
					|  |  |  |         //计算最大最小值
 | 
			
		
	
		
			
				
					|  |  |  |         Map<String, BigDecimal> minAndMaxList = factIndexPartyAblityCpcMonthlyDao.getExtremeValue(formDTO.getCustomerId()); | 
			
		
	
		
			
				
					|  |  |  |         if (CollectionUtils.isEmpty(minAndMaxList)){ | 
			
		
	
		
			
				
					|  |  |  |             log.warn("cpcIndexCalculate customerId:{} have not any fact record",formDTO.getCustomerId()); | 
			
		
	
		
			
				
					|  |  |  |             return false; | 
			
		
	
		
			
				
					|  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |         //获取指标权重信息
 | 
			
		
	
		
			
				
					|  |  |  |         List<IndexGroupDetailEntity> indexDetails = getDetailListByParentCode.getDetailListByParentCode(formDTO.getCustomerId(), IndexCodeEnum.DANG_YUAN_XIANG_GUAN.getCode(),IndexCodeEnum.CAN_YU_YI_SHI.getCode()); | 
			
		
	
		
			
				
					|  |  |  |         if (CollectionUtils.isEmpty(indexDetails)){ | 
			
		
	
		
			
				
					|  |  |  |             log.warn("cpcIndexCalculate customerId:{} have not any indexGroupDetail",formDTO.getCustomerId()); | 
			
		
	
		
			
				
					|  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |         //获取指标权重信息 -参与议事
 | 
			
		
	
		
			
				
					|  |  |  |         List<IndexGroupDetailEntity> indexDetails = getDetailListByParentCode.getDetailListLikeParentCode(formDTO.getCustomerId(), IndexCodeEnum.DANG_YUAN_XIANG_GUAN.getCode()); | 
			
		
	
		
			
				
					|  |  |  |         if (CollectionUtils.isEmpty(indexDetails)) { | 
			
		
	
		
			
				
					|  |  |  |             log.warn("cpcIndexCalculate customerId:{} have not any indexGroupDetail", formDTO.getCustomerId()); | 
			
		
	
		
			
				
					|  |  |  |             return false; | 
			
		
	
		
			
				
					|  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |         canyuyishi(formDTO, indexDetails); | 
			
		
	
		
			
				
					|  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |         //log.info(JSON.toJSONString(minAndMaxList));
 | 
			
		
	
		
			
				
					|  |  |  |         return null; | 
			
		
	
		
			
				
					|  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |     private void canyuyishi(CalculateCommonFormDTO formDTO, List<IndexGroupDetailEntity> indexDetails) { | 
			
		
	
		
			
				
					|  |  |  |         Map<String, String> indexCodeFieldReList = indexCodeFieldReService.getIndexCodeFieldReMap(); | 
			
		
	
		
			
				
					|  |  |  |         if (CollectionUtils.isEmpty(indexCodeFieldReList)){ | 
			
		
	
		
			
				
					|  |  |  |             log.warn("cpcIndexCalculate have any indexcodefieldre"); | 
			
		
	
		
			
				
					|  |  |  |             return false; | 
			
		
	
		
			
				
					|  |  |  |         if (CollectionUtils.isEmpty(indexCodeFieldReList)) { | 
			
		
	
		
			
				
					|  |  |  |             log.error("cpcIndexCalculate have any indexcodefieldre"); | 
			
		
	
		
			
				
					|  |  |  |             return; | 
			
		
	
		
			
				
					|  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |         String indexCode = null; | 
			
		
	
		
			
				
					|  |  |  |         String fieldName = null; | 
			
		
	
		
			
				
					|  |  |  |         for (IndexGroupDetailEntity indexDetail : indexDetails) { | 
			
		
	
		
			
				
					|  |  |  |             indexCode = indexDetail.getIndexCode(); | 
			
		
	
		
			
				
					|  |  |  |             fieldName = indexCodeFieldReService.getFieldNameByIndexCode(indexCode); | 
			
		
	
		
			
				
					|  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |         //计算最大最小值
 | 
			
		
	
		
			
				
					|  |  |  |         Map<String, Object> minAndMaxMap = factIndexPartyAblityCpcMonthlyDao.getExtremeValue(formDTO.getCustomerId()); | 
			
		
	
		
			
				
					|  |  |  |         if (CollectionUtils.isEmpty(minAndMaxMap)) { | 
			
		
	
		
			
				
					|  |  |  |             log.warn("cpcIndexCalculate getExtremeValue customerId:{} have not any fact record", formDTO.getCustomerId()); | 
			
		
	
		
			
				
					|  |  |  |             return; | 
			
		
	
		
			
				
					|  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |         //指标集合
 | 
			
		
	
		
			
				
					|  |  |  |         //对指标进行分组
 | 
			
		
	
		
			
				
					|  |  |  |         Map<String, List<IndexGroupDetailEntity>> groupIndexDetailsMap = indexDetails.stream().collect(Collectors.groupingBy(IndexGroupDetailEntity::getAllParentIndexCode)); | 
			
		
	
		
			
				
					|  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |         log.info(JSON.toJSONString(minAndMaxList)); | 
			
		
	
		
			
				
					|  |  |  |         Map<String, BigDecimal> list = factIndexPartyAblityCpcMonthlyDao.getCountByMonthId(formDTO.getCustomerId(), formDTO.getMonthId()); | 
			
		
	
		
			
				
					|  |  |  |         return null; | 
			
		
	
		
			
				
					|  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |         List<Map<String, Object>> list = null; | 
			
		
	
		
			
				
					|  |  |  |         int pageNo = 1; | 
			
		
	
		
			
				
					|  |  |  |         int pageSize = 10; | 
			
		
	
		
			
				
					|  |  |  |         do { | 
			
		
	
		
			
				
					|  |  |  |             list = factIndexPartyAblityCpcMonthlyDao.getCountByMonthId(formDTO.getCustomerId(), formDTO.getMonthId(), (pageNo - 1) * pageSize, pageSize); | 
			
		
	
		
			
				
					|  |  |  |             if (!CollectionUtils.isEmpty(list)) { | 
			
		
	
		
			
				
					|  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |                 //遍历指标分组 计算分数
 | 
			
		
	
		
			
				
					|  |  |  |                 List<Map<String, Object>> finalList = list; | 
			
		
	
		
			
				
					|  |  |  |                 groupIndexDetailsMap.forEach((parentIndex, indexDetailList)->{ | 
			
		
	
		
			
				
					|  |  |  |                     Map<String, IndexInputVO<BigDecimal>> indexMap = getIndexInputVO(indexDetailList, minAndMaxMap); | 
			
		
	
		
			
				
					|  |  |  |                     //遍历指标 进行计算
 | 
			
		
	
		
			
				
					|  |  |  |                     for (Map.Entry<String, IndexInputVO<BigDecimal>> entry : indexMap.entrySet()) { | 
			
		
	
		
			
				
					|  |  |  |                         String indexCode = entry.getKey(); | 
			
		
	
		
			
				
					|  |  |  |                         IndexInputVO<BigDecimal> value = entry.getValue(); | 
			
		
	
		
			
				
					|  |  |  |                         String fieldName = indexCodeFieldReService.getFieldNameByIndexCode(indexCode); | 
			
		
	
		
			
				
					|  |  |  |                         if (StringUtils.isBlank(fieldName)) { | 
			
		
	
		
			
				
					|  |  |  |                             log.error("cpcIndexCalculate fieldName error"); | 
			
		
	
		
			
				
					|  |  |  |                             continue; | 
			
		
	
		
			
				
					|  |  |  |                         } | 
			
		
	
		
			
				
					|  |  |  |                         for (Map<String, Object> cpcIndex : finalList) { | 
			
		
	
		
			
				
					|  |  |  |                             //对应的数值
 | 
			
		
	
		
			
				
					|  |  |  |                             String sampleValueStr = String.valueOf(cpcIndex.get(indexCodeFieldReService.getFieldNameByIndexCode(fieldName))); | 
			
		
	
		
			
				
					|  |  |  |                             //构造样本值对象
 | 
			
		
	
		
			
				
					|  |  |  |                             SampleValue<BigDecimal> currentGridIndexValue = new SampleValue((String) cpcIndex.get(IndexCalConstant.USER_ID), new BigDecimal(sampleValueStr)); | 
			
		
	
		
			
				
					|  |  |  |                             value.getIndexValueVOs().add(currentGridIndexValue); | 
			
		
	
		
			
				
					|  |  |  |                         } | 
			
		
	
		
			
				
					|  |  |  |                     } | 
			
		
	
		
			
				
					|  |  |  |                     HashMap<String, BigDecimal> result = calculate(indexMap); | 
			
		
	
		
			
				
					|  |  |  |                     // TODO 处理结果
 | 
			
		
	
		
			
				
					|  |  |  |                     saveGridCorreLationDangJian(formDTO, indexDetails, result); | 
			
		
	
		
			
				
					|  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |                     try { | 
			
		
	
		
			
				
					|  |  |  |                         System.out.println( JSON.toJSONString(result)); | 
			
		
	
		
			
				
					|  |  |  |                     } catch (Exception e) { | 
			
		
	
		
			
				
					|  |  |  |                     } | 
			
		
	
		
			
				
					|  |  |  |                 }); | 
			
		
	
		
			
				
					|  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |         } while (!CollectionUtils.isEmpty(list) && pageNo++ > 0); | 
			
		
	
		
			
				
					|  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |     @Transactional(rollbackFor = Exception.class) | 
			
		
	
		
			
				
					|  |  |  |     public void saveGridCorreLationDangJian(CalculateCommonFormDTO formDTO, List<IndexGroupDetailEntity> indexDetails, HashMap<String, BigDecimal> result) { | 
			
		
	
		
			
				
					|  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |     /** | 
			
		
	
		
			
				
					|  |  |  |      * @param indexMap | 
			
		
	
		
			
				
					|  |  |  |      * @return java.util.List<com.epmet.support.normalizing.batch.IndexOutputVO> | 
			
		
	
		
			
				
					|  |  |  |      * @author yinzuomei | 
			
		
	
		
			
				
					|  |  |  |      * @description | 
			
		
	
		
			
				
					|  |  |  |      * @Date 2020/8/30 21:40 | 
			
		
	
		
			
				
					|  |  |  |      **/ | 
			
		
	
		
			
				
					|  |  |  |     private HashMap<String, BigDecimal> calculate(Map<String, IndexInputVO<BigDecimal>> indexMap) { | 
			
		
	
		
			
				
					|  |  |  |         //构造入参
 | 
			
		
	
		
			
				
					|  |  |  |         List<IndexInputVO> indexInputVOS = indexMap.values().stream().collect(Collectors.toList()); | 
			
		
	
		
			
				
					|  |  |  |         BatchScoreCalculator batchScoreCalculator = new BatchScoreCalculator(); | 
			
		
	
		
			
				
					|  |  |  |         return batchScoreCalculator.getScoreCountOfSampleId(indexInputVOS); | 
			
		
	
		
			
				
					|  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |     /** | 
			
		
	
		
			
				
					|  |  |  |      * @param indexList    指标集合 | 
			
		
	
		
			
				
					|  |  |  |      * @param minAndMaxMap 最大值最小值集合 | 
			
		
	
		
			
				
					|  |  |  |      * @return java.util.Map<java.lang.String, com.epmet.support.normalizing.batch.IndexInputVO> | 
			
		
	
		
			
				
					|  |  |  |      * @author yinzuomei | 
			
		
	
		
			
				
					|  |  |  |      * @description | 
			
		
	
		
			
				
					|  |  |  |      * @Date 2020/8/30 15:56 | 
			
		
	
		
			
				
					|  |  |  |      **/ | 
			
		
	
		
			
				
					|  |  |  |     private Map<String, IndexInputVO<BigDecimal>> getIndexInputVO(List<IndexGroupDetailEntity> indexList, Map<String, Object> minAndMaxMap) { | 
			
		
	
		
			
				
					|  |  |  |         Map<String, IndexInputVO<BigDecimal>> map = new HashMap<>(); | 
			
		
	
		
			
				
					|  |  |  |         for (int indexNum = 0; indexNum < indexList.size(); indexNum++) { | 
			
		
	
		
			
				
					|  |  |  |             IndexGroupDetailEntity index = indexList.get(indexNum); | 
			
		
	
		
			
				
					|  |  |  |             IndexInputVO indexInputVO = new IndexInputVO(); | 
			
		
	
		
			
				
					|  |  |  |             // 指标code
 | 
			
		
	
		
			
				
					|  |  |  |             indexInputVO.setIndexId(index.getIndexCode()); | 
			
		
	
		
			
				
					|  |  |  |             //权重
 | 
			
		
	
		
			
				
					|  |  |  |             indexInputVO.setWeight(index.getWeight()); | 
			
		
	
		
			
				
					|  |  |  |             BigDecimal minValue = null; | 
			
		
	
		
			
				
					|  |  |  |             BigDecimal maxValue = null; | 
			
		
	
		
			
				
					|  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |             //获取指标对应的列名
 | 
			
		
	
		
			
				
					|  |  |  |             String fieldName = indexCodeFieldReService.getFieldNameByIndexCode(index.getIndexCode()); | 
			
		
	
		
			
				
					|  |  |  |             //最小值key
 | 
			
		
	
		
			
				
					|  |  |  |             String minValueKey = fieldName.concat(StrConstant.UNDER_LINE).concat(StrConstant.MIN); | 
			
		
	
		
			
				
					|  |  |  |             //最大值key
 | 
			
		
	
		
			
				
					|  |  |  |             String maxValueKey = fieldName.concat(StrConstant.UNDER_LINE).concat(StrConstant.MAX); | 
			
		
	
		
			
				
					|  |  |  |             minValue = new BigDecimal(String.valueOf(minAndMaxMap.get(minValueKey))); | 
			
		
	
		
			
				
					|  |  |  |             maxValue = new BigDecimal(String.valueOf(minAndMaxMap.get(maxValueKey))); | 
			
		
	
		
			
				
					|  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |             //分值计算器
 | 
			
		
	
		
			
				
					|  |  |  |             ScoreCalculator<Integer> scoreCalculator = new BigDecimalScoreCalculator(minValue, | 
			
		
	
		
			
				
					|  |  |  |                     maxValue, | 
			
		
	
		
			
				
					|  |  |  |                     ScoreConstants.MIN_SCORE, | 
			
		
	
		
			
				
					|  |  |  |                     ScoreConstants.MAX_SCORE, | 
			
		
	
		
			
				
					|  |  |  |                     Correlation.getCorrelation(index.getCorrelation()) | 
			
		
	
		
			
				
					|  |  |  |             ); | 
			
		
	
		
			
				
					|  |  |  |             indexInputVO.setScoreCalculator(scoreCalculator); | 
			
		
	
		
			
				
					|  |  |  |             List<SampleValue<BigDecimal>> sampleValueList = new ArrayList<>(); | 
			
		
	
		
			
				
					|  |  |  |             indexInputVO.setIndexValueVOs(sampleValueList); | 
			
		
	
		
			
				
					|  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |             map.put(index.getIndexCode(), indexInputVO); | 
			
		
	
		
			
				
					|  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |         return map; | 
			
		
	
		
			
				
					|  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  | } | 
			
		
	
	
		
			
				
					|  |  | 
 |