From cbf7212c686f0f1bc2acc03462abaa8d1b6de4bc Mon Sep 17 00:00:00 2001 From: jianjun Date: Mon, 31 Aug 2020 22:09:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/epmet/constant/IndexCalConstant.java | 1 + .../impl/CpcIndexCalculateServiceImpl.java | 184 ++++++++++-------- .../FactIndexPartyAblityCpcMonthlyDao.xml | 3 + 3 files changed, 108 insertions(+), 80 deletions(-) diff --git a/epmet-module/data-statistical/data-statistical-client/src/main/java/com/epmet/constant/IndexCalConstant.java b/epmet-module/data-statistical/data-statistical-client/src/main/java/com/epmet/constant/IndexCalConstant.java index 79321c6026..13b27760a5 100644 --- a/epmet-module/data-statistical/data-statistical-client/src/main/java/com/epmet/constant/IndexCalConstant.java +++ b/epmet-module/data-statistical/data-statistical-client/src/main/java/com/epmet/constant/IndexCalConstant.java @@ -19,4 +19,5 @@ public interface IndexCalConstant { String MONTH_ID="MONTH_ID"; String USER_ID="USER_ID"; + String YEAR_ID = "YEAR_ID"; } diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/indexcal/impl/CpcIndexCalculateServiceImpl.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/indexcal/impl/CpcIndexCalculateServiceImpl.java index 214076fc6a..c15620850d 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/indexcal/impl/CpcIndexCalculateServiceImpl.java +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/indexcal/impl/CpcIndexCalculateServiceImpl.java @@ -2,8 +2,8 @@ package com.epmet.service.indexcal.impl; import com.epmet.commons.tools.constant.NumConstant; import com.epmet.commons.tools.constant.StrConstant; +import com.epmet.commons.tools.exception.RenException; import com.epmet.commons.tools.utils.ConvertUtils; -import com.epmet.commons.tools.utils.DateUtils; import com.epmet.constant.IndexCalConstant; import com.epmet.dao.indexcoll.FactIndexPartyAblityCpcMonthlyDao; import com.epmet.dao.indexscore.CpcScoreDao; @@ -22,7 +22,6 @@ 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 com.epmet.util.DimIdGenerator; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -50,11 +49,8 @@ public class CpcIndexCalculateServiceImpl implements CpcIndexCalculateService { @Override public Boolean cpcIndexCalculate(CalculateCommonFormDTO formDTO) { - - calculatePartScore(formDTO); calculateTotalScore(formDTO); - return true; } @@ -68,36 +64,35 @@ public class CpcIndexCalculateServiceImpl implements CpcIndexCalculateService { List parentIndexDetails = getDetailListByParentCode.getDetailListByParentCode(formDTO.getCustomerId(), IndexCodeEnum.DANG_YUAN_XIANG_GUAN.getCode()); if (CollectionUtils.isEmpty(parentIndexDetails)) { log.warn("cpcIndexCalculate customerId:{} have not any indexGroupDetail", formDTO.getCustomerId()); - return; + throw new RenException("客户【党员相关】指标权重信息不存在"); } - //获取数据 - List list = null; Map cpcScoreTotalMap = new HashMap<>(); Map indexWeightMap = parentIndexDetails.stream().collect(Collectors.toMap(IndexGroupDetailEntity::getIndexCode, o -> o)); - list = cpcScoreDao.getPartScore(formDTO.getCustomerId(), formDTO.getMonthId()); - if (!CollectionUtils.isEmpty(list)) { - Map> userGroupMap = list.stream().collect(Collectors.groupingBy(CpcScoreEntity::getUserId)); - userGroupMap.forEach((userId, partScoreList) -> { - CpcScoreEntity totalEntity = null; - for (CpcScoreEntity part : partScoreList) { - IndexGroupDetailEntity indexGroupDetailEntity = indexWeightMap.get(part.getIndexCode()); - if (totalEntity == null) { - totalEntity = ConvertUtils.sourceToTarget(part, CpcScoreEntity.class); - totalEntity.setIsTotal(NumConstant.ONE_STR); - totalEntity.setIndexCode(indexGroupDetailEntity.getIndexCode()); - cpcScoreTotalMap.put(userId, totalEntity); - } - BigDecimal total = part.getScore().multiply(indexGroupDetailEntity.getWeight()); - log.warn("userId:{},分数:{},权重:{},total:{}", userId, part.getScore(), indexGroupDetailEntity.getWeight(), total); - totalEntity.setScore(totalEntity.getScore().add(total)); - } - }); + //获取数据 + List list = cpcScoreDao.getPartScore(formDTO.getCustomerId(), formDTO.getMonthId()); + if (CollectionUtils.isEmpty(list)) { + log.error("calculateTotalScore cpcScoreDao.getPartScore return empty,customerId:{},monthId:{}", formDTO.getCustomerId(), formDTO.getMonthId()); + throw new RenException("客户四级指标分值记录不存在"); } - + Map> userGroupMap = list.stream().collect(Collectors.groupingBy(CpcScoreEntity::getUserId)); + userGroupMap.forEach((userId, partScoreList) -> { + CpcScoreEntity totalEntity = null; + for (CpcScoreEntity part : partScoreList) { + IndexGroupDetailEntity indexGroupDetailEntity = indexWeightMap.get(part.getIndexCode()); + if (totalEntity == null) { + totalEntity = ConvertUtils.sourceToTarget(part, CpcScoreEntity.class); + totalEntity.setIsTotal(NumConstant.ONE_STR); + totalEntity.setIndexCode(indexGroupDetailEntity.getIndexCode()); + cpcScoreTotalMap.put(userId, totalEntity); + } + //todo 自建群活跃度——议题转项目率 有阈值 >60%按60%算 + BigDecimal total = part.getScore().multiply(indexGroupDetailEntity.getWeight()); + log.debug("userId:{},分数:{},权重:{},total:{}", userId, part.getScore(), indexGroupDetailEntity.getWeight(), total); + totalEntity.setScore(totalEntity.getScore().add(total)); + } + }); insertBatch(cpcScoreTotalMap.values().stream().collect(Collectors.toList())); - - } private void insertBatch(Collection values) { @@ -113,56 +108,44 @@ public class CpcIndexCalculateServiceImpl implements CpcIndexCalculateService { //计算最大最小值 Map minAndMaxMap = factIndexPartyAblityCpcMonthlyDao.getExtremeValue(formDTO.getCustomerId()); if (CollectionUtils.isEmpty(minAndMaxMap)) { - log.warn("cpcIndexCalculate getExtremeValue customerId:{} have not any fact record", formDTO.getCustomerId()); + log.error("cpcIndexCalculate getExtremeValue customerId:{} have not any fact record", formDTO.getCustomerId()); return; } //指标集合 - //对指标进行分组 - //Map> groupIndexDetailsMap = indexDetails.stream().collect(Collectors.groupingBy(IndexGroupDetailEntity::getAllParentIndexCode)); - - //获取指标权重信息 -参与议事 - List canyuyishiIndexDetails = getDetailListByParentCode.getDetailListByParentCode(formDTO.getCustomerId(), IndexCodeEnum.DANG_YUAN_XIANG_GUAN.getCode(), IndexCodeEnum.CAN_YU_YI_SHI.getCode()); - if (CollectionUtils.isEmpty(canyuyishiIndexDetails)) { - log.warn("cpcIndexCalculate customerId:{} have not any indexGroupDetail", formDTO.getCustomerId()); - return; - } - List dangwuhongdongIndexDetails = getDetailListByParentCode.getDetailListByParentCode(formDTO.getCustomerId(), IndexCodeEnum.DANG_YUAN_XIANG_GUAN.getCode(), IndexCodeEnum.DANG_WU_HUO_DONG.getCode()); - if (CollectionUtils.isEmpty(canyuyishiIndexDetails)) { - log.warn("cpcIndexCalculate customerId:{} have not any indexGroupDetail", formDTO.getCustomerId()); - return; - } - List lianxiqunzhongIndexDetails = getDetailListByParentCode.getDetailListByParentCode(formDTO.getCustomerId(), IndexCodeEnum.DANG_YUAN_XIANG_GUAN.getCode(), IndexCodeEnum.LIAN_XI_QUN_ZHONG.getCode()); - if (CollectionUtils.isEmpty(canyuyishiIndexDetails)) { - log.warn("cpcIndexCalculate customerId:{} have not any indexGroupDetail", formDTO.getCustomerId()); + Map> groupIndexDetailsMap = getIndexDetailMap(formDTO); + if (groupIndexDetailsMap == null) { return; } - Map> groupIndexDetailsMap = new HashMap<>(); - groupIndexDetailsMap.put(IndexCodeEnum.CAN_YU_YI_SHI.getCode(), canyuyishiIndexDetails); - groupIndexDetailsMap.put(IndexCodeEnum.DANG_WU_HUO_DONG.getCode(), dangwuhongdongIndexDetails); - groupIndexDetailsMap.put(IndexCodeEnum.LIAN_XI_QUN_ZHONG.getCode(), lianxiqunzhongIndexDetails); - - List> list = null; int pageNo = 1; int pageSize = 10; + //分页查询 要计算的原始数据 + List> list = null; do { list = factIndexPartyAblityCpcMonthlyDao.getCountByMonthId(formDTO.getCustomerId(), formDTO.getMonthId(), (pageNo - 1) * pageSize, pageSize); if (!CollectionUtils.isEmpty(list)) { //遍历指标分组 计算分数 List> finalList = list; groupIndexDetailsMap.forEach((parentIndexCode, details) -> { - calculate(formDTO, details, finalList, minAndMaxMap, parentIndexCode); + calculateScore(formDTO, details, finalList, minAndMaxMap, parentIndexCode); }); } } while (!CollectionUtils.isEmpty(list) && pageNo++ > 0); - } - private void calculate(CalculateCommonFormDTO formDTO, List indexDetailList, List> finalList, Map minAndMaxMap, String parentIndexCode) { - Map> indexMap = getIndexInputVO(indexDetailList, minAndMaxMap); + /** + * desc:计算并保存中间结果 + * + * @param formDTO + * @param indexDetailList + * @param finalList + * @param minAndMaxMap + * @param parentIndexCode remark:1.遍历指标和原始数据;2.构建指标归一算法计算器,计算分值;3.将分值保存到中间表 + */ + private void calculateScore(CalculateCommonFormDTO formDTO, List indexDetailList, List> finalList, Map minAndMaxMap, String parentIndexCode) { + Map> indexMap = buildIndexInputVO(indexDetailList, minAndMaxMap); Map scoreEntityMap = new HashMap<>(); //遍历指标 进行计算 - for (Map.Entry> entry : indexMap.entrySet()) { String indexCode = entry.getKey(); IndexInputVO value = entry.getValue(); @@ -175,10 +158,10 @@ public class CpcIndexCalculateServiceImpl implements CpcIndexCalculateService { for (Map cpcCount : finalList) { //对应的数值 String userId = String.valueOf(cpcCount.get(IndexCalConstant.USER_ID)); - String sampleValueStr = String.valueOf(cpcCount.get(fieldName)); + BigDecimal sampleValue = new BigDecimal(String.valueOf(cpcCount.get(fieldName))); CpcScoreEntity cpcScoreEntity = new CpcScoreEntity(); - cpcScoreEntity.setYearId(DimIdGenerator.getYearDimId(DateUtils.addDateMonths(new Date(), -1))); + cpcScoreEntity.setYearId(String.valueOf(cpcCount.get(IndexCalConstant.YEAR_ID))); cpcScoreEntity.setCustomerId(formDTO.getCustomerId()); cpcScoreEntity.setAgencyId(String.valueOf(cpcCount.get(IndexCalConstant.AGENCY_ID))); cpcScoreEntity.setGridId(String.valueOf(cpcCount.get(IndexCalConstant.GRID_ID))); @@ -187,22 +170,30 @@ public class CpcIndexCalculateServiceImpl implements CpcIndexCalculateService { cpcScoreEntity.setMonthId(formDTO.getMonthId()); cpcScoreEntity.setScore(new BigDecimal(0)); cpcScoreEntity.setIndexCode(parentIndexCode); + scoreEntityMap.put(userId, cpcScoreEntity); //构造样本值对象 - SampleValue currentGridIndexValue = new SampleValue((String) cpcCount.get(IndexCalConstant.USER_ID), new BigDecimal(sampleValueStr)); - value.getIndexValueVOs().add(currentGridIndexValue); + SampleValue currentUserIndexValue = new SampleValue(userId, sampleValue); + value.getIndexValueVOs().add(currentUserIndexValue); } } log.warn("计算的参数:{}", indexMap); - HashMap result = calculate(indexMap); + HashMap result = calculateScore(indexMap); log.warn("计算的结果:{}", result); //处理结果 saveCpcScore(formDTO, scoreEntityMap, parentIndexCode, result); } - + /** + * desc:保存分值 到中间表 + * + * @param formDTO + * @param indexDetails + * @param parentIndexCode + * @param result + */ @Transactional(rollbackFor = Exception.class) public void saveCpcScore(CalculateCommonFormDTO formDTO, Map indexDetails, String parentIndexCode, HashMap result) { cpcScoreDao.deleteByMonthId(formDTO.getCustomerId(), formDTO.getMonthId(), parentIndexCode); @@ -215,21 +206,10 @@ public class CpcIndexCalculateServiceImpl implements CpcIndexCalculateService { this.insertBatch(list); } - /** - * @param indexMap - * @return java.util.List - * @author yinzuomei - * @description - * @Date 2020/8/30 21:40 - **/ - private HashMap calculate(Map> indexMap) { - //构造入参 - List indexInputVOS = indexMap.values().stream().collect(Collectors.toList()); - BatchScoreCalculator batchScoreCalculator = new BatchScoreCalculator(); - return batchScoreCalculator.getScoreCountOfSampleId(indexInputVOS); - } /** + * desc:构建分值计算器 + * * @param indexList 指标集合 * @param minAndMaxMap 最大值最小值集合 * @return java.util.Map @@ -237,10 +217,9 @@ public class CpcIndexCalculateServiceImpl implements CpcIndexCalculateService { * @description * @Date 2020/8/30 15:56 **/ - private Map> getIndexInputVO(List indexList, Map minAndMaxMap) { + private Map> buildIndexInputVO(List indexList, Map minAndMaxMap) { Map> map = new HashMap<>(); - for (int indexNum = 0; indexNum < indexList.size(); indexNum++) { - IndexGroupDetailEntity index = indexList.get(indexNum); + for (IndexGroupDetailEntity index : indexList) { IndexInputVO indexInputVO = new IndexInputVO(); // 指标code indexInputVO.setIndexId(index.getIndexCode()); @@ -273,4 +252,49 @@ public class CpcIndexCalculateServiceImpl implements CpcIndexCalculateService { } return map; } + + /** + * desc:调用计算器计算每个人的分数值 + * + * @param indexMap + * @return + */ + private HashMap calculateScore(Map> indexMap) { + //构造入参 + List indexInputVOS = indexMap.values().stream().collect(Collectors.toList()); + BatchScoreCalculator batchScoreCalculator = new BatchScoreCalculator(); + return batchScoreCalculator.getScoreCountOfSampleId(indexInputVOS); + } + + + /** + * desc:构建党员相关4级指标明细map + * + * @param formDTO + * @return + */ + private Map> getIndexDetailMap(CalculateCommonFormDTO formDTO) { + //获取指标权重信息 -参与议事 + List canyuyishiIndexDetails = getDetailListByParentCode.getDetailListByParentCode(formDTO.getCustomerId(), IndexCodeEnum.DANG_YUAN_XIANG_GUAN.getCode(), IndexCodeEnum.CAN_YU_YI_SHI.getCode()); + if (CollectionUtils.isEmpty(canyuyishiIndexDetails)) { + log.error("cpcIndexCalculate customerId:{} have not any indexGroupDetail", formDTO.getCustomerId()); + return null; + } + List dangwuhongdongIndexDetails = getDetailListByParentCode.getDetailListByParentCode(formDTO.getCustomerId(), IndexCodeEnum.DANG_YUAN_XIANG_GUAN.getCode(), IndexCodeEnum.DANG_WU_HUO_DONG.getCode()); + if (CollectionUtils.isEmpty(canyuyishiIndexDetails)) { + log.error("cpcIndexCalculate customerId:{} have not any indexGroupDetail", formDTO.getCustomerId()); + return null; + } + List lianxiqunzhongIndexDetails = getDetailListByParentCode.getDetailListByParentCode(formDTO.getCustomerId(), IndexCodeEnum.DANG_YUAN_XIANG_GUAN.getCode(), IndexCodeEnum.LIAN_XI_QUN_ZHONG.getCode()); + if (CollectionUtils.isEmpty(canyuyishiIndexDetails)) { + log.error("cpcIndexCalculate customerId:{} have not any indexGroupDetail", formDTO.getCustomerId()); + return null; + } + + Map> groupIndexDetailsMap = new HashMap<>(); + groupIndexDetailsMap.put(IndexCodeEnum.CAN_YU_YI_SHI.getCode(), canyuyishiIndexDetails); + groupIndexDetailsMap.put(IndexCodeEnum.DANG_WU_HUO_DONG.getCode(), dangwuhongdongIndexDetails); + groupIndexDetailsMap.put(IndexCodeEnum.LIAN_XI_QUN_ZHONG.getCode(), lianxiqunzhongIndexDetails); + return groupIndexDetailsMap; + } } diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/resources/mapper/indexcoll/FactIndexPartyAblityCpcMonthlyDao.xml b/epmet-module/data-statistical/data-statistical-server/src/main/resources/mapper/indexcoll/FactIndexPartyAblityCpcMonthlyDao.xml index beb5b42a4e..c9f807d881 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/resources/mapper/indexcoll/FactIndexPartyAblityCpcMonthlyDao.xml +++ b/epmet-module/data-statistical/data-statistical-server/src/main/resources/mapper/indexcoll/FactIndexPartyAblityCpcMonthlyDao.xml @@ -101,6 +101,9 @@ AGENCY_ID, PARENT_ID, GRID_ID, + MONTH_ID, + QUARTER_ID, + YEAR_ID, USER_ID, CREATE_TOPIC_COUNT, JOIN_TOPIC_COUNT,