Browse Source

添加指标权重 缓存

dev_shibei_match
jianjun 5 years ago
parent
commit
b53fd85ae9
  1. 15
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java
  2. 2
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/dao/evaluationindex/screen/IndexGroupDetailDao.java
  3. 31
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/redis/IndexCalRedis.java
  4. 38
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcal/impl/IndexCalculateServiceImpl.java
  5. 8
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcal/impl/IndexCodeFieldReServiceImpl.java
  6. 13
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/screen/IndexGroupDetailService.java
  7. 38
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/screen/impl/IndexGroupDetailServiceImpl.java
  8. 6
      epmet-module/data-statistical/data-statistical-server/src/main/resources/mapper/evaluationindex/screen/IndexGroupDetailDao.xml
  9. 2
      epmet-module/data-statistical/data-statistical-server/src/test/java/com/epmet/stats/test/normalizing/DemoScoreCal.java

15
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);
}
}

2
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<IndexGroupDetailEntity> {
List<IndexGroupDetailEntity> getDetailListByParentCode(@Param("customerId") String customerId, @Param("indexCode") String indexCode);
List<IndexGroupDetailEntity> getAllIndexWeightList(String customerId);
}

31
epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/redis/IndexCodeFieldReRedis.java → 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<String, BigDecimal> getIndexCodeWeightMap(String customerId) {
HashOperations hashOperations = redisTemplate.opsForHash();
Map<String, BigDecimal> values = hashOperations.entries(RedisKeys.getIndexCodeWeightKey(customerId));
return values;
}
/**
* @Description 存入指标权重
*/
public void setIndexCodeWeightMap(String customerId, Map<String, BigDecimal> 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));
}
}

38
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,13 +70,31 @@ public class IndexCalculateServiceImpl implements IndexCalculateService {
}
Boolean flag = false;
for (String customerId : customerIds) {
CalculateCommonFormDTO calculateCommonFormDTO = new CalculateCommonFormDTO(customerId, formDTO.getMonthId());
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 {
CalculateCommonFormDTO param = new CalculateCommonFormDTO(customerId, formDTO.getMonthId());
flag = cpcIndexCalculateService.cpcIndexCalculate(param);
flag = cpcIndexCalculateService.cpcIndexCalculate(formDTO);
log.info("客户Id:{}【党员相关】计算完毕,总耗时:{}秒,result:{}", customerId, (System.currentTimeMillis() - start) / 1000, flag);
} catch (Exception e) {
log.error("indexCalculate cpcIndexCalculate exception", e);
@ -87,7 +105,7 @@ public class IndexCalculateServiceImpl implements IndexCalculateService {
//计算网格
start = System.currentTimeMillis();
try {
flag = gridCorreLationService.calculateGridCorreLation(calculateCommonFormDTO);
flag = gridCorreLationService.calculateGridCorreLation(formDTO);
log.info("客户Id:{}【网格相关】计算完毕,总耗时:{}秒,result:{},result:{}", customerId, (System.currentTimeMillis() - start) / 1000, flag);
} catch (Exception e) {
log.error("indexCalculate calculateGridCorreLation exception", e);
@ -116,7 +134,7 @@ public class IndexCalculateServiceImpl implements IndexCalculateService {
//计算区直属
start = System.currentTimeMillis();
try {
flag = deptScoreService.calculateDeptCorreLation(calculateCommonFormDTO);
flag = deptScoreService.calculateDeptCorreLation(formDTO);
log.info("客户Id:{}【区直部门】计算完毕,总耗时:{}秒,result:{}", customerId, (System.currentTimeMillis() - start) / 1000, flag);
} catch (Exception e) {
log.error("indexCalculate calculateDeptCorreLation exception", e);
@ -147,13 +165,11 @@ public class IndexCalculateServiceImpl implements IndexCalculateService {
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;
}

8
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<String, String> getIndexCodeFieldReMap() {
Map<String, String> indexCodeFiledReMap = indexCodeFieldReRedis.getIndexCodeFiledReMap();
Map<String, String> indexCodeFiledReMap = indexCalRedis.getIndexCodeFiledReMap();
if (CollectionUtils.isEmpty(indexCodeFiledReMap)) {
List<IndexCodeFieldReEntity> 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;
}

13
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<IndexGroupDetailEntity> {
/**
* desc根据all_parent_index_code 获取指标明细
*
* @param customerId
* @param indexCode
*/
List<IndexGroupDetailEntity> getDetailListByParentCode(String customerId,String... indexCode);
List<IndexGroupDetailEntity> getDetailListByParentCode(String customerId, String... indexCode);
/**
* desc根据all_parent_index_code 获取指标权重
* 如果获取不到 返回-1 表明数据有误
*
* @param customerId
* @param allPathIndexCode
*/
BigDecimal getWeightByAllPathIndexCode(String customerId, String allPathIndexCode);
}

38
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<IndexGroupDetailDao, IndexGroupDetailEntity> implements IndexGroupDetailService {
@Autowired
private IndexCalRedis indexCalRedis;
@Override
public List<IndexGroupDetailEntity> getDetailListByParentCode(String customerId,String... indexCode) {
if (indexCode == null || indexCode.length == 0){
public List<IndexGroupDetailEntity> 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<String, BigDecimal> indexCodeFiledReMap = indexCalRedis.getIndexCodeWeightMap(customerId);
if (CollectionUtils.isEmpty(indexCodeFiledReMap)) {
List<IndexGroupDetailEntity> 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;
}
}

6
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}
</select>
<select id="getAllIndexWeightList" resultType="com.epmet.entity.evaluationindex.screen.IndexGroupDetailEntity">
select ID, concat(ALL_PARENT_INDEX_CODE,':',INDEX_CODE) INDEX_CODE, WEIGHT, THRESHOLD FROM index_group_detail
WHERE
CUSTOMER_ID = #{customerId,jdbcType=VARCHAR} and STATUS = 'enable' and DEL_FLAG = '0'
</select>
</mapper>

2
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<IndexInputVO> indexInputVOS = Arrays.asList(index1VO, index2VO, index3VO, index4VO);
BatchScoreCalculator batchScoreCalculator = new BatchScoreCalculator();
System.out.println("param:" + JSON.toJSONString(indexInputVOS));
HashMap<String, BigDecimal> result = batchScoreCalculator.getScoreCountOfSampleId(indexInputVOS);
HashMap<String, CalculateResult> result2 = batchScoreCalculator.getScoreTotalOfSampleId(indexInputVOS);

Loading…
Cancel
Save