diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java index e1fa2fa2c7..786067c3f0 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java @@ -295,25 +295,36 @@ public class RedisKeys { * @return */ public static String getExternalAppSecretKey(String appId) { - return String.format(rootPrefix+"externalapp:secret:%s",appId); + return String.format(rootPrefix + "externalapp:secret:%s", appId); } /** * 计算指标时获取指标code和fields关系缓存Key + * * @return */ public static String getIndexCodeFieldReKey() { return rootPrefix.concat("stats:indexcal:indexcode:field"); } + /** + * 计算指标时获取指标权重缓存Key + * + * @return + */ + public static String getIndexCodeWeightKey(String customerId) { + return rootPrefix.concat("stats:indexcal:indexcode:weight").concat(customerId); + } + /** * 客户统计的计算标记。 * 0:未在计算 * 1:正在计算 + * * @param customerId * @return */ public static String getCustomerStatsCalFlag(String customerId) { - return String.format(rootPrefix+"stats:calflag:%s", customerId); + return String.format(rootPrefix + "stats:calflag:%s", customerId); } } diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/dao/evaluationindex/screen/IndexGroupDetailDao.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/dao/evaluationindex/screen/IndexGroupDetailDao.java index 17e7a7eb82..d67b86e887 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/dao/evaluationindex/screen/IndexGroupDetailDao.java +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/dao/evaluationindex/screen/IndexGroupDetailDao.java @@ -34,4 +34,6 @@ import java.util.List; public interface IndexGroupDetailDao extends BaseDao { List getDetailListByParentCode(@Param("customerId") String customerId, @Param("indexCode") String indexCode); + + List getAllIndexWeightList(String customerId); } \ No newline at end of file diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/redis/IndexCodeFieldReRedis.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/redis/IndexCalRedis.java similarity index 67% rename from epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/redis/IndexCodeFieldReRedis.java rename to epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/redis/IndexCalRedis.java index 8a99d62890..8e1c769d14 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/redis/IndexCodeFieldReRedis.java +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/redis/IndexCalRedis.java @@ -24,12 +24,13 @@ import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; +import java.math.BigDecimal; import java.util.Map; import java.util.concurrent.TimeUnit; @Slf4j @Component -public class IndexCodeFieldReRedis { +public class IndexCalRedis { @Autowired private RedisTemplate redisTemplate; @@ -54,9 +55,37 @@ public class IndexCodeFieldReRedis { /** * desc:删除指标字段关系缓存 + * * @return */ public Boolean deleteIndexCodeFromRedis() { return redisTemplate.delete(RedisKeys.getIndexCodeFieldReKey()); } + + /** + * @Description 获取指标权重 + */ + public Map getIndexCodeWeightMap(String customerId) { + HashOperations hashOperations = redisTemplate.opsForHash(); + Map values = hashOperations.entries(RedisKeys.getIndexCodeWeightKey(customerId)); + return values; + } + + /** + * @Description 存入指标权重 + */ + public void setIndexCodeWeightMap(String customerId, Map records) { + HashOperations hashOperations = redisTemplate.opsForHash(); + hashOperations.putAll(RedisKeys.getIndexCodeWeightKey(customerId), records); + redisTemplate.expire(RedisKeys.getIndexCodeFieldReKey(), 1, TimeUnit.DAYS); + } + + /** + * desc:删除指标权重缓存 + * + * @return + */ + public Boolean deleteIndexCodeWeightFromRedis(String customerId) { + return redisTemplate.delete(RedisKeys.getIndexCodeWeightKey(customerId)); + } } \ No newline at end of file diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcal/impl/IndexCalculateServiceImpl.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcal/impl/IndexCalculateServiceImpl.java index 48b8b46e31..c4fef95f7b 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcal/impl/IndexCalculateServiceImpl.java +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcal/impl/IndexCalculateServiceImpl.java @@ -8,7 +8,7 @@ import com.epmet.commons.tools.utils.Result; import com.epmet.constant.DataSourceConstant; import com.epmet.dto.indexcal.CalculateCommonFormDTO; import com.epmet.feign.EpmetCommonServiceOpenFeignClient; -import com.epmet.redis.IndexCodeFieldReRedis; +import com.epmet.redis.IndexCalRedis; import com.epmet.service.evaluationindex.indexcal.*; import com.epmet.service.evaluationindex.indexcoll.FactIndexCollectService; import com.epmet.util.DimIdGenerator; @@ -36,7 +36,7 @@ public class IndexCalculateServiceImpl implements IndexCalculateService { @Autowired private CpcIndexCalculateService cpcIndexCalculateService; @Autowired - private IndexCodeFieldReRedis indexCodeFieldReRedis; + private IndexCalRedis indexCalRedis; @Autowired private IndexCalculateCommunityService indexCalculateCommunityService; @Autowired @@ -70,90 +70,106 @@ public class IndexCalculateServiceImpl implements IndexCalculateService { } Boolean flag = false; for (String customerId : customerIds) { - CalculateCommonFormDTO calculateCommonFormDTO = new CalculateCommonFormDTO(customerId, formDTO.getMonthId()); - long start = System.currentTimeMillis(); + CalculateCommonFormDTO param = new CalculateCommonFormDTO(); + param.setCustomerId(customerId); + param.setMonthId(formDTO.getMonthId()); + flag = calulateCustomerIndexScore(param); + } + return flag; + } catch (Exception e) { + log.error("indexCalculate exception,param:{}", JSON.toJSONString(formDTO)); + } finally { + //清除缓存 + indexCalRedis.deleteIndexCodeFromRedis(); + } + return false; + } + + private Boolean calulateCustomerIndexScore(CalculateCommonFormDTO formDTO) { + try { + String customerId = formDTO.getCustomerId(); + Boolean flag; + long start = System.currentTimeMillis(); + try { + //计算党员相关的 try { - //计算党员相关的 - try { - CalculateCommonFormDTO param = new CalculateCommonFormDTO(customerId, formDTO.getMonthId()); - flag = cpcIndexCalculateService.cpcIndexCalculate(param); - log.info("客户Id:{}【党员相关】计算完毕,总耗时:{}秒,result:{}", customerId, (System.currentTimeMillis() - start) / 1000, flag); - } catch (Exception e) { - log.error("indexCalculate cpcIndexCalculate exception", e); - throw new RenException("indexCalculate cpcIndexCalculate exception", e); - } - //测试用 - //计算网格 - start = System.currentTimeMillis(); - try { - flag = gridCorreLationService.calculateGridCorreLation(calculateCommonFormDTO); - log.info("客户Id:{}【网格相关】计算完毕,总耗时:{}秒,result:{},result:{}", customerId, (System.currentTimeMillis() - start) / 1000, flag); - } catch (Exception e) { - log.error("indexCalculate calculateGridCorreLation exception", e); - throw new RenException("indexCalculate calculateGridCorreLation exception", e); - } - //计算社区 - start = System.currentTimeMillis(); - try { - flag = indexCalculateCommunityService.calCommunityAll(customerId, formDTO.getMonthId()); - log.info("客户Id:{}【社区相关】计算完毕,总耗时:{}秒,result:{},result:{}", customerId, (System.currentTimeMillis() - start) / 1000, flag); - } catch (Exception e) { - log.error("indexCalculate calCommunityAll exception", e); - throw new RenException("indexCalculate calAll exception", e); - } + flag = cpcIndexCalculateService.cpcIndexCalculate(formDTO); + log.info("客户Id:{}【党员相关】计算完毕,总耗时:{}秒,result:{}", customerId, (System.currentTimeMillis() - start) / 1000, flag); + } catch (Exception e) { + log.error("indexCalculate cpcIndexCalculate exception", e); + throw new RenException("indexCalculate cpcIndexCalculate exception", e); + } + //测试用 + + //计算网格 + start = System.currentTimeMillis(); + try { + flag = gridCorreLationService.calculateGridCorreLation(formDTO); + log.info("客户Id:{}【网格相关】计算完毕,总耗时:{}秒,result:{},result:{}", customerId, (System.currentTimeMillis() - start) / 1000, flag); + } catch (Exception e) { + log.error("indexCalculate calculateGridCorreLation exception", e); + throw new RenException("indexCalculate calculateGridCorreLation exception", e); + } + //计算社区 + start = System.currentTimeMillis(); + try { + flag = indexCalculateCommunityService.calCommunityAll(customerId, formDTO.getMonthId()); + log.info("客户Id:{}【社区相关】计算完毕,总耗时:{}秒,result:{},result:{}", customerId, (System.currentTimeMillis() - start) / 1000, flag); + } catch (Exception e) { + log.error("indexCalculate calCommunityAll exception", e); + throw new RenException("indexCalculate calAll exception", e); + } - //计算街道 - start = System.currentTimeMillis(); - try { - flag = indexCalculateStreetService.calStreetAll(customerId, formDTO.getMonthId()); - log.info("客户Id:{}【街道相关】计算完毕,总耗时:{}秒,result:{}", customerId, (System.currentTimeMillis() - start) / 1000, flag); - } catch (Exception e) { - log.error("indexCalculate calStreetAll exception", e); - throw new RenException("indexCalculate calStreetAll exception", e); - } + //计算街道 + start = System.currentTimeMillis(); + try { + flag = indexCalculateStreetService.calStreetAll(customerId, formDTO.getMonthId()); + log.info("客户Id:{}【街道相关】计算完毕,总耗时:{}秒,result:{}", customerId, (System.currentTimeMillis() - start) / 1000, flag); + } catch (Exception e) { + log.error("indexCalculate calStreetAll exception", e); + throw new RenException("indexCalculate calStreetAll exception", e); + } - //计算区直属 - start = System.currentTimeMillis(); - try { - flag = deptScoreService.calculateDeptCorreLation(calculateCommonFormDTO); - log.info("客户Id:{}【区直部门】计算完毕,总耗时:{}秒,result:{}", customerId, (System.currentTimeMillis() - start) / 1000, flag); - } catch (Exception e) { - log.error("indexCalculate calculateDeptCorreLation exception", e); - throw new RenException("indexCalculate calculateDeptCorreLation exception", e); - } + //计算区直属 + start = System.currentTimeMillis(); + try { + flag = deptScoreService.calculateDeptCorreLation(formDTO); + log.info("客户Id:{}【区直部门】计算完毕,总耗时:{}秒,result:{}", customerId, (System.currentTimeMillis() - start) / 1000, flag); + } catch (Exception e) { + log.error("indexCalculate calculateDeptCorreLation exception", e); + throw new RenException("indexCalculate calculateDeptCorreLation exception", e); + } - //计算全区 - start = System.currentTimeMillis(); - try { - indexCalculateDistrictService.calDistrictAll(customerId, formDTO.getMonthId()); - log.info("客户Id:{}【全区相关】计算完毕,总耗时:{}秒,result:{}", customerId, (System.currentTimeMillis() - start) / 1000, flag); - } catch (Exception e) { - log.error("indexCalculate calDistrictAll exception", e); - throw new RenException("indexCalculate calDistrictAll exception", e); - } - } catch (RenException e) { - flag = false; + //计算全区 + start = System.currentTimeMillis(); + try { + indexCalculateDistrictService.calDistrictAll(customerId, formDTO.getMonthId()); + log.info("客户Id:{}【全区相关】计算完毕,总耗时:{}秒,result:{}", customerId, (System.currentTimeMillis() - start) / 1000, flag); + } catch (Exception e) { + log.error("indexCalculate calDistrictAll exception", e); + throw new RenException("indexCalculate calDistrictAll exception", e); } + } catch (RenException e) { + flag = false; + } - //计算完毕后 将结果插入大屏相关数据表 - if (flag) { - start = System.currentTimeMillis(); - try { - factIndexCollectService.insertScreenIndexDataMonthlyAndYearly(formDTO.getMonthId(), formDTO.getCustomerId()); - log.info("客户Id:{}分数插入到大屏显示库完毕,总耗时:{}秒", customerId, (System.currentTimeMillis() - start) / 1000); - } catch (Exception e) { - log.error("indexCalculate insertScreenIndexDataMonthlyAndYearly exception", e); - flag = false; - } + //计算完毕后 将结果插入大屏相关数据表 + if (flag) { + start = System.currentTimeMillis(); + try { + factIndexCollectService.insertScreenIndexDataMonthlyAndYearly(formDTO.getMonthId(), formDTO.getCustomerId()); + log.info("客户Id:{}分数插入到大屏显示库完毕,总耗时:{}秒", customerId, (System.currentTimeMillis() - start) / 1000); + } catch (Exception e) { + log.error("indexCalculate insertScreenIndexDataMonthlyAndYearly exception", e); + flag = false; } } return flag; } catch (Exception e) { - log.error("indexCalculate exception,param:{}", JSON.toJSONString(formDTO)); + log.error("calulateCustomerIndexScore exception", e); } finally { - //清除缓存 - indexCodeFieldReRedis.deleteIndexCodeFromRedis(); + indexCalRedis.deleteIndexCodeWeightFromRedis(formDTO.getCustomerId()); } return false; } diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcal/impl/IndexCodeFieldReServiceImpl.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcal/impl/IndexCodeFieldReServiceImpl.java index 3e89bee003..009dc1dc42 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcal/impl/IndexCodeFieldReServiceImpl.java +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcal/impl/IndexCodeFieldReServiceImpl.java @@ -4,7 +4,7 @@ import com.epmet.commons.dynamic.datasource.annotation.DataSource; import com.epmet.constant.DataSourceConstant; import com.epmet.dao.evaluationindex.screen.IndexCodeFieldReDao; import com.epmet.entity.evaluationindex.screen.IndexCodeFieldReEntity; -import com.epmet.redis.IndexCodeFieldReRedis; +import com.epmet.redis.IndexCalRedis; import com.epmet.service.evaluationindex.indexcal.IndexCodeFieldReService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -25,18 +25,18 @@ public class IndexCodeFieldReServiceImpl implements IndexCodeFieldReService { private IndexCodeFieldReDao indexCodeFieldReDao; @Autowired - private IndexCodeFieldReRedis indexCodeFieldReRedis; + private IndexCalRedis indexCalRedis; @Override public Map getIndexCodeFieldReMap() { - Map indexCodeFiledReMap = indexCodeFieldReRedis.getIndexCodeFiledReMap(); + Map indexCodeFiledReMap = indexCalRedis.getIndexCodeFiledReMap(); if (CollectionUtils.isEmpty(indexCodeFiledReMap)) { List allData = indexCodeFieldReDao.getAllData(); if (CollectionUtils.isEmpty(allData)){ return new HashMap<>(); } indexCodeFiledReMap = allData.stream().collect(Collectors.toMap(IndexCodeFieldReEntity::getIndexCode, o -> o.getFieldId())); - indexCodeFieldReRedis.setIndexCodeFiledReMap(indexCodeFiledReMap); + indexCalRedis.setIndexCodeFiledReMap(indexCodeFiledReMap); } return indexCodeFiledReMap; } diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/screen/IndexGroupDetailService.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/screen/IndexGroupDetailService.java index f202beca1b..eb77f2cf63 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/screen/IndexGroupDetailService.java +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/screen/IndexGroupDetailService.java @@ -20,6 +20,7 @@ package com.epmet.service.evaluationindex.screen; import com.epmet.commons.mybatis.service.BaseService; import com.epmet.entity.evaluationindex.screen.IndexGroupDetailEntity; +import java.math.BigDecimal; import java.util.List; /** @@ -31,8 +32,18 @@ import java.util.List; public interface IndexGroupDetailService extends BaseService { /** * desc:根据all_parent_index_code 获取指标明细 + * * @param customerId * @param indexCode */ - List getDetailListByParentCode(String customerId,String... indexCode); + List getDetailListByParentCode(String customerId, String... indexCode); + + /** + * desc:根据all_parent_index_code 获取指标权重 + * 如果获取不到 返回-1 表明数据有误 + * + * @param customerId + * @param allPathIndexCode + */ + BigDecimal getWeightByAllPathIndexCode(String customerId, String allPathIndexCode); } \ No newline at end of file diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/screen/impl/IndexGroupDetailServiceImpl.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/screen/impl/IndexGroupDetailServiceImpl.java index 7a8935bd02..26fb5b8cc2 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/screen/impl/IndexGroupDetailServiceImpl.java +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/screen/impl/IndexGroupDetailServiceImpl.java @@ -19,15 +19,23 @@ package com.epmet.service.evaluationindex.screen.impl; import com.epmet.commons.dynamic.datasource.annotation.DataSource; import com.epmet.commons.mybatis.service.impl.BaseServiceImpl; +import com.epmet.commons.tools.constant.NumConstant; import com.epmet.commons.tools.constant.StrConstant; import com.epmet.commons.tools.exception.RenException; import com.epmet.constant.DataSourceConstant; import com.epmet.dao.evaluationindex.screen.IndexGroupDetailDao; import com.epmet.entity.evaluationindex.screen.IndexGroupDetailEntity; +import com.epmet.redis.IndexCalRedis; import com.epmet.service.evaluationindex.screen.IndexGroupDetailService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; +import java.math.BigDecimal; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; /** * 客户指标详情 @@ -35,20 +43,40 @@ import java.util.List; * @author generator generator@elink-cn.com * @since v1.0.0 2020-08-19 */ +@Slf4j @Service @DataSource(DataSourceConstant.EVALUATION_INDEX) public class IndexGroupDetailServiceImpl extends BaseServiceImpl implements IndexGroupDetailService { - + @Autowired + private IndexCalRedis indexCalRedis; @Override - public List getDetailListByParentCode(String customerId,String... indexCode) { - if (indexCode == null || indexCode.length == 0){ + public List getDetailListByParentCode(String customerId, String... indexCode) { + if (indexCode == null || indexCode.length == 0) { throw new RenException("参数错误"); } StringBuilder sb = new StringBuilder(); - for (String code:indexCode){ + for (String code : indexCode) { sb.append(code).append(StrConstant.COLON); } - return baseDao.getDetailListByParentCode(customerId,sb.deleteCharAt(sb.length()-1).toString()); + return baseDao.getDetailListByParentCode(customerId, sb.deleteCharAt(sb.length() - 1).toString()); } + + @Override + public BigDecimal getWeightByAllPathIndexCode(String customerId, String allPathIndexCode) { + Map indexCodeFiledReMap = indexCalRedis.getIndexCodeWeightMap(customerId); + if (CollectionUtils.isEmpty(indexCodeFiledReMap)) { + List allData = baseDao.getAllIndexWeightList(customerId); + if (CollectionUtils.isEmpty(allData)) { + log.warn("getAllIndexWeightList return empty"); + return null; + } + indexCodeFiledReMap = allData.stream().collect(Collectors.toMap(IndexGroupDetailEntity::getIndexCode, o -> o.getWeight())); + indexCalRedis.setIndexCodeWeightMap(customerId, indexCodeFiledReMap); + } + BigDecimal weight = indexCodeFiledReMap.get(allPathIndexCode); + + return weight == null ? new BigDecimal(NumConstant.ONE_NEG) : weight; + } + } \ No newline at end of file diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/resources/mapper/evaluationindex/screen/IndexGroupDetailDao.xml b/epmet-module/data-statistical/data-statistical-server/src/main/resources/mapper/evaluationindex/screen/IndexGroupDetailDao.xml index c2ab3e07c6..76bec7b257 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/resources/mapper/evaluationindex/screen/IndexGroupDetailDao.xml +++ b/epmet-module/data-statistical/data-statistical-server/src/main/resources/mapper/evaluationindex/screen/IndexGroupDetailDao.xml @@ -12,4 +12,10 @@ CUSTOMER_ID = #{customerId,jdbcType=VARCHAR} and STATUS = 'enable' and DEL_FLAG = '0' and ALL_PARENT_INDEX_CODE = #{indexCode,jdbcType=VARCHAR} + + \ No newline at end of file diff --git a/epmet-module/data-statistical/data-statistical-server/src/test/java/com/epmet/stats/test/normalizing/DemoScoreCal.java b/epmet-module/data-statistical/data-statistical-server/src/test/java/com/epmet/stats/test/normalizing/DemoScoreCal.java index 57969c12e0..15e029b3e6 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/test/java/com/epmet/stats/test/normalizing/DemoScoreCal.java +++ b/epmet-module/data-statistical/data-statistical-server/src/test/java/com/epmet/stats/test/normalizing/DemoScoreCal.java @@ -1,5 +1,6 @@ package com.epmet.stats.test.normalizing; +import com.alibaba.fastjson.JSON; import com.epmet.support.normalizing.*; import com.epmet.support.normalizing.batch.*; import org.junit.Test; @@ -233,6 +234,7 @@ public class DemoScoreCal { List indexInputVOS = Arrays.asList(index1VO, index2VO, index3VO, index4VO); BatchScoreCalculator batchScoreCalculator = new BatchScoreCalculator(); + System.out.println("param:" + JSON.toJSONString(indexInputVOS)); HashMap result = batchScoreCalculator.getScoreCountOfSampleId(indexInputVOS); HashMap result2 = batchScoreCalculator.getScoreTotalOfSampleId(indexInputVOS);