package com.xkl.service; import com.xkl.config.Constants; import com.xkl.domain.XklAmpReportDetailEntity; import com.xkl.domain.XklAmpReportEntity; import com.xkl.domain.XklAmpReportHealthScoreEntity; import com.xkl.domain.XklAmpReportMetaScoreStandardEntity; import com.xkl.repository.XklAmpReportDetailRespository; import com.xkl.repository.XklAmpReportHealthScoreRespository; import com.xkl.repository.XklAmpReportRespository; import lombok.extern.apachecommons.CommonsLog; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.*; /** * Created by win7 on 2016/11/21. */ @Service @CommonsLog public class ScoreServiceImpl implements IScoreService,Constants{ @Autowired private XklAmpReportRespository xklAmpReportRespository; @Autowired private XklAmpReportDetailRespository xklAmpReportDetailRespository; @Autowired private XklAmpReportHealthScoreRespository xklAmpReportHealthScoreRespository; @Override public double getScore(long report_id) { Map<Integer,Double> typeScoreMap=new HashMap<>();//记录各大项打分的Map /** * report_detail表中获取每项体检的值,按大类存入typeScoreMap */ List<XklAmpReportDetailEntity> reportDetailList=xklAmpReportDetailRespository.findAllByReportId(report_id); for(XklAmpReportDetailEntity detail:reportDetailList){ int item_id=detail.getItemId(); XklAmpReportMetaScoreStandardEntity scoreStandard= scoreMap.get(item_id);//从定时加载的打分map中取出该体检小项 if(scoreStandard!=null) { double score = scoreStandard.getScore(detail.getItemValue()); int item_type = scoreStandard.getItemType(); if(typeScoreMap.containsKey(item_type)){//将分类中的每个大项中分数相加 double type_score=typeScoreMap.get(item_type)+score; typeScoreMap.put(item_type,type_score); }else{ typeScoreMap.put(item_type,score); } } } /** * 循环每类得分的typeScoreMap,计算总得分 */ double finalScore=0; List<XklAmpReportHealthScoreEntity> saveScoreList=new ArrayList<>(); Iterator<Map.Entry<Integer,Double>> it=typeScoreMap.entrySet().iterator(); while(it.hasNext()){ Map.Entry<Integer, Double> entry = it.next(); int item_type = entry.getKey(); double score = entry.getValue(); XklAmpReportHealthScoreEntity scoreEntity=new XklAmpReportHealthScoreEntity(); scoreEntity.setReportId(report_id); scoreEntity.setType(item_type); scoreEntity.setTypeHealthScore(score); saveScoreList.add(scoreEntity); double weightedScore=weightedScoreMap.get(item_type).getScoreWeight(); finalScore += weightedScore*score; } /** * 每类得分存入xkl_amp_report_health_scroe * 总得分存入xkl_amp_report */ if(saveScoreList!=null&&saveScoreList.size()>0) xklAmpReportHealthScoreRespository.save(saveScoreList); XklAmpReportEntity report=xklAmpReportRespository.findOne(report_id); if(report!=null){ report.setScore(finalScore); xklAmpReportRespository.save(report); } return finalScore; } }