Commit 3a704693 by zhangkb

添加单位评分规则功能

parent 5102d20c
package com.keymobile.indicators.model.entity.objscorerule;
import java.io.Serializable;
import lombok.Data;
/**
* author:zhangkb time:2020-6-2 desc:指标类型权重
*/
@Data
public class IndTypeWeight implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String catalogIdPath;
private String weight;//权重,百分比形式(例如:10 代表 10%)
private int index;
}
package com.keymobile.indicators.model.entity.objscorerule;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import lombok.Data;
/**
*author:zhangkb time:2020-6-2 desc:单位评分规则实体
*/
@Data
@Document(collection="obj_score_rule")
public class ScoreRule implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
private String id;
private String ruleName;//规则名称
private String scoreType;//得分类型:0 指标值直接参与计算 1 指标得分分数参与计算(包括综合评分和改善提升)
private String calType;//计算类型:0 计算所有考核指标平均数 1 根据目录类别分类算平均分后根据权重计算考核指标
private List<IndTypeWeight> indTypeWeights = new ArrayList<>();//目录类别权重数据
private String code;//标识编码
public List<IndTypeWeight> getIndTypeWeights(){
return indTypeWeights;
}
public void setIndTypeWeights(List<IndTypeWeight> indTypeWeights){
this.indTypeWeights.addAll(indTypeWeights);
}
}
...@@ -29,4 +29,6 @@ public interface DriveIndCalResultRepository extends MongoRepository<DriveIndCal ...@@ -29,4 +29,6 @@ public interface DriveIndCalResultRepository extends MongoRepository<DriveIndCal
public List<DriveIndCalResult> findByIndIdAndDateAndCompareId(String indId,int date,String compareId,Sort sort); public List<DriveIndCalResult> findByIndIdAndDateAndCompareId(String indId,int date,String compareId,Sort sort);
public List<DriveIndCalResult> findByIndIdAndDateAndCompareId(String indId,int date,String compareId); public List<DriveIndCalResult> findByIndIdAndDateAndCompareId(String indId,int date,String compareId);
public List<DriveIndCalResult> findByCompareObjAndDate(String compareObj,int date);
} }
package com.keymobile.indicators.persistence.hyindicators;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
import com.keymobile.indicators.model.entity.objscorerule.ScoreRule;
public interface ScoreRuleRepository extends MongoRepository<ScoreRule,String>{
List<ScoreRule> findByCode(String code);
}
package com.keymobile.indicators.service.hytobacco;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.keymobile.indicators.model.entity.DriveIndCalResult;
import com.keymobile.indicators.model.entity.indicators.DriveIndDef;
import com.keymobile.indicators.model.entity.objscorerule.IndTypeWeight;
import com.keymobile.indicators.model.entity.objscorerule.ScoreRule;
import com.keymobile.indicators.model.mapper.indicators.DriveIndDefMapper;
import com.keymobile.indicators.persistence.hyindicators.DriveIndCalResultRepository;
import com.keymobile.indicators.persistence.hyindicators.ScoreRuleRepository;
import com.keymobile.indicators.utils.CalculateUtils;
@Service
public class ScoreRuleService {
private Logger logger = LoggerFactory.getLogger(ScoreRuleService.class);
@Autowired
private ScoreRuleRepository scoreRuleRepo;
@Autowired
private DriveIndCalResultRepository driveIndCalResultRepo;
@Autowired
private DriveIndDefMapper driveIndDefMapper;
public String save(ScoreRule scoreRule) {
scoreRule = scoreRuleRepo.save(scoreRule);
return scoreRule.getId();
}
public void delete(String id) {
scoreRuleRepo.deleteById(id);
}
public List<ScoreRule> getAll(String code){
return scoreRuleRepo.findByCode(code);
}
public ScoreRule getById(String id) {
Optional<ScoreRule> scoreRule = scoreRuleRepo.findById(id);
if(scoreRule.isPresent()) {
return scoreRule.get();
}
return null;
}
public void calObjScore(String reportId,List<String> compareObjs,int date,String scoreRuleId) {
List<Map<String,String>> results = new ArrayList<>();
//根据单位得分评分卡id获取评分卡详情
ScoreRule scoreRule = this.getById(scoreRuleId);
if(scoreRule!=null) {
for(String compareObj : compareObjs) {
//根据对标对象,日期查找该对标对象对标的所有指标结果
List<DriveIndCalResult> calResults = driveIndCalResultRepo.
findByCompareObjAndDate(compareObj, date);
if(!calResults.isEmpty()) {
//计算类型:0 计算所有考核指标平均数
if("0".equals(scoreRule.getCalType())) {
List<String> values = new ArrayList<>();
List<String> improveValues = new ArrayList<>();
for(DriveIndCalResult calResult : calResults) {
//得分类型:0 指标值直接参与计算
if("0".equals(scoreRule.getScoreType())) {
values.add(calResult.getValue());
}else {
values.add(calResult.getScore());
if(!"No".equals(calResult.getImproveScore())) {
improveValues.add(calResult.getImproveScore());
}
}
}
//计算平均值
String averageValue = CalculateUtils.averageValue(values,0);
String averageImproveValue = "0.0";
if(!improveValues.isEmpty()) {
averageImproveValue = CalculateUtils.averageValue(improveValues,0);
}
Map<String,String> resultMap = new HashMap<>();
resultMap.put("reportId", reportId);
resultMap.put("compareObj", compareObj);
resultMap.put("scoreValue", averageValue);
resultMap.put("improveValue", averageImproveValue);
resultMap.put("date",String.valueOf(date));
results.add(resultMap);
}else {//1 根据目录类别分类算平均分后根据权重计算考核指标
if(!scoreRule.getIndTypeWeights().isEmpty()) {
double scoreValue = 0.0;
double improveValue = 0.0;
for(IndTypeWeight indTypeWeight : scoreRule.getIndTypeWeights()) {
List<String> values = new ArrayList<>();
List<String> improveValues = new ArrayList<>();
for(DriveIndCalResult calResult : calResults) {
String catalogIdPath = indTypeWeight.getCatalogIdPath();
String driveIndId = calResult.getIndId();
//根据考核指标id获取考核指标
DriveIndDef driveIndDef = driveIndDefMapper.selectByPrimaryKey(driveIndId);
if(driveIndDef!=null) {
//如果考核指标是参与单位计分的
if("1".equals(driveIndDef.getIsUnitCalScore())) {
if(indTypeWeight.getCatalogIdPath().indexOf(catalogIdPath)>=0) {
//得分类型:0 指标值直接参与计算
if("0".equals(scoreRule.getScoreType())) {
values.add(calResult.getValue());
}else {
values.add(calResult.getScore());
if(!"No".equals(calResult.getImproveScore())) {
improveValues.add(calResult.getImproveScore());
}
}
}
}
}
}
//计算平均值
String averageValue = CalculateUtils.averageValue(values,0);
String averageImproveValue = "0.0";
if(!improveValues.isEmpty()) {
averageImproveValue = CalculateUtils.averageValue(improveValues,0);
}
scoreValue = scoreValue+Double.valueOf(averageValue)*
(Double.valueOf(indTypeWeight.getWeight())/100);
improveValue = improveValue+Double.valueOf(averageImproveValue)*
(Double.valueOf(indTypeWeight.getWeight())/100);
}
Map<String,String> resultMap = new HashMap<>();
resultMap.put("reportId", reportId);
resultMap.put("compareObj", compareObj);
resultMap.put("scoreValue", String.valueOf(scoreValue));
resultMap.put("improveValue", String.valueOf(improveValue));
resultMap.put("date",String.valueOf(date));
results.add(resultMap);
}
}
}
}
}else {
logger.info("单位评分卡id:"+scoreRuleId+" 不存在");
}
System.out.println(results);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment