@ -1,11 +1,13 @@
package com.epmet.service.indexcal.impl ;
import com.alibaba.fastjson.JSON ;
import com.epmet.commons.tools.constant.StrConstant ;
import com.epmet.commons.tools.utils.DateUtils ;
import com.epmet.constant.IndexCalConstant ;
import com.epmet.dao.indexcoll.FactIndexPartyAblityCpcMonthlyDao ;
import com.epmet.dao.indexscore.CpcScoreDao ;
import com.epmet.dao.screen.IndexCodeFieldReDao ;
import com.epmet.dto.indexcal.CalculateCommonFormDTO ;
import com.epmet.entity.indexscore.CpcScoreEntity ;
import com.epmet.entity.screen.IndexGroupDetailEntity ;
import com.epmet.eum.IndexCodeEnum ;
import com.epmet.service.indexcal.CpcIndexCalculateService ;
@ -18,6 +20,7 @@ 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 ;
@ -26,10 +29,7 @@ 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.* ;
import java.util.stream.Collectors ;
@Slf4j
@ -43,24 +43,53 @@ public class CpcIndexCalculateServiceImpl implements CpcIndexCalculateService {
private IndexGroupDetailService getDetailListByParentCode ;
@Autowired
private IndexCodeFieldReService indexCodeFieldReService ;
@Autowired
private CpcScoreDao cpcScoreDao ;
@Override
public Boolean cpcIndexCalculate ( CalculateCommonFormDTO formDTO ) {
//获取指标权重信息 -参与议事
List < IndexGroupDetailEntity > indexDetails = getDetailListByParentCode . getDetailListLikeParentCode ( formDTO . getCustomerId ( ) , IndexCodeEnum . DANG_YUAN_XIANG_GUAN . getCode ( ) ) ;
if ( CollectionUtils . isEmpty ( indexDetails ) ) {
calculatePartScore ( formDTO ) ;
calculateTotalScore ( formDTO ) ;
return null ;
}
/ * *
* desc : 计算总分
* @param formDTO
* /
private void calculateTotalScore ( CalculateCommonFormDTO formDTO ) {
//获取指标权重
List < IndexGroupDetailEntity > 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 false ;
return ;
}
//获取数据
List < Map < String , Object > > list = null ;
int pageNo = 1 ;
int pageSize = 10 ;
do {
canyuyishi ( formDTO , indexDetails ) ;
list = cpcScoreDao . getPartScoreByPage ( formDTO . getCustomerId ( ) , formDTO . getMonthId ( ) , ( pageNo - 1 ) * pageSize , pageSize ) ;
if ( ! CollectionUtils . isEmpty ( list ) ) {
list . forEach ( partScore - > {
partScore . get ( IndexCalConstant . USER_ID )
} ) ;
//遍历指标分组 计算分数
List < Map < String , Object > > finalList = list ;
groupIndexDetailsMap . forEach ( ( parentIndexCode , details ) - > {
calculate ( formDTO , details , finalList , minAndMaxMap , parentIndexCode ) ;
} ) ;
}
} while ( ! CollectionUtils . isEmpty ( list ) & & pageNo + + > 0 ) ;
//log.info(JSON.toJSONString(minAndMaxList));
return null ;
}
private void canyuyishi ( CalculateCommonFormDTO formDTO , List < IndexGroupDetailEntity > indexDetails ) {
private void calculatePartScore ( CalculateCommonFormDTO formDTO ) {
Map < String , String > indexCodeFieldReList = indexCodeFieldReService . getIndexCodeFieldReMap ( ) ;
if ( CollectionUtils . isEmpty ( indexCodeFieldReList ) ) {
log . error ( "cpcIndexCalculate have any indexcodefieldre" ) ;
@ -74,9 +103,29 @@ public class CpcIndexCalculateServiceImpl implements CpcIndexCalculateService {
}
//指标集合
//对指标进行分组
Map < String , List < IndexGroupDetailEntity > > groupIndexDetailsMap = indexDetails . stream ( ) . collect ( Collectors . groupingBy ( IndexGroupDetailEntity : : getAllParentIndexCode ) ) ;
//Map<String, List<IndexGroupDetailEntity>> groupIndexDetailsMap = indexDetails.stream().collect(Collectors.groupingBy(IndexGroupDetailEntity::getAllParentIndexCode));
//获取指标权重信息 -参与议事
List < IndexGroupDetailEntity > 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 < IndexGroupDetailEntity > 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 < IndexGroupDetailEntity > 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 ( ) ) ;
return ;
}
Map < String , List < IndexGroupDetailEntity > > 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 < Map < String , Object > > list = null ;
int pageNo = 1 ;
@ -87,42 +136,65 @@ public class CpcIndexCalculateServiceImpl implements CpcIndexCalculateService {
//遍历指标分组 计算分数
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 ) {
}
groupIndexDetailsMap . forEach ( ( parentIndexCode , details ) - > {
calculate ( formDTO , details , finalList , minAndMaxMap , parentIndexCode ) ;
} ) ;
}
} while ( ! CollectionUtils . isEmpty ( list ) & & pageNo + + > 0 ) ;
}
@Transactional ( rollbackFor = Exception . class )
public void saveGridCorreLationDangJian ( CalculateCommonFormDTO formDTO , List < IndexGroupDetailEntity > indexDetails , HashMap < String , BigDecimal > result ) {
private void calculate ( CalculateCommonFormDTO formDTO , List < IndexGroupDetailEntity > indexDetailList , List < Map < String , Object > > finalList , Map < String , Object > minAndMaxMap , String parentIndexCode ) {
Map < String , IndexInputVO < BigDecimal > > indexMap = getIndexInputVO ( indexDetailList , minAndMaxMap ) ;
Map < String , CpcScoreEntity > scoreEntityMap = new HashMap < > ( ) ;
//遍历指标 进行计算
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 > cpcCount : finalList ) {
//对应的数值
String userId = String . valueOf ( cpcCount . get ( IndexCalConstant . USER_ID ) ) ;
String sampleValueStr = String . valueOf ( cpcCount . get ( fieldName ) ) ;
CpcScoreEntity cpcScoreEntity = new CpcScoreEntity ( ) ;
cpcScoreEntity . setYearId ( DimIdGenerator . getYearDimId ( DateUtils . addDateMonths ( new Date ( ) , - 1 ) ) ) ;
cpcScoreEntity . setCustomerId ( formDTO . getCustomerId ( ) ) ;
cpcScoreEntity . setAgencyId ( String . valueOf ( cpcCount . get ( IndexCalConstant . AGENCY_ID ) ) ) ;
cpcScoreEntity . setGridId ( String . valueOf ( cpcCount . get ( IndexCalConstant . GRID_ID ) ) ) ;
cpcScoreEntity . setUserId ( userId ) ;
cpcScoreEntity . setMonthId ( formDTO . getMonthId ( ) ) ;
cpcScoreEntity . setScore ( new BigDecimal ( 0 ) ) ;
cpcScoreEntity . setIndexCode ( parentIndexCode ) ;
scoreEntityMap . put ( userId , cpcScoreEntity ) ;
//构造样本值对象
SampleValue < BigDecimal > currentGridIndexValue = new SampleValue ( ( String ) cpcCount . get ( IndexCalConstant . USER_ID ) , new BigDecimal ( sampleValueStr ) ) ;
value . getIndexValueVOs ( ) . add ( currentGridIndexValue ) ;
}
}
log . warn ( "计算的参数:{}" , indexMap ) ;
HashMap < String , BigDecimal > result = calculate ( indexMap ) ;
//处理结果
saveCpcScore ( formDTO , scoreEntityMap , parentIndexCode , result ) ;
}
@Transactional ( rollbackFor = Exception . class )
public void saveCpcScore ( CalculateCommonFormDTO formDTO , Map < String , CpcScoreEntity > indexDetails , String parentIndexCode , HashMap < String , BigDecimal > result ) {
cpcScoreDao . deleteByMonthId ( formDTO . getCustomerId ( ) , formDTO . getMonthId ( ) , parentIndexCode ) ;
result . forEach ( ( userId , score ) - > {
CpcScoreEntity cpcScoreEntity = indexDetails . get ( userId ) ;
cpcScoreEntity . setScore ( score ) ;
cpcScoreDao . insert ( cpcScoreEntity ) ;
} ) ;
}
/ * *