Authored by fangyeqing

ADD:add weight score service

... ... @@ -2,11 +2,13 @@ package com.xkl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* Spring-Boot启动类
*/
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
... ...
package com.xkl.config;
import com.xkl.domain.XklAmpReportMetaScoreStandardEntity;
import java.util.HashMap;
import java.util.Map;
/**
* 常量
*/
public class Constants {
public interface Constants {
/**
* 存储当前登录用户id的字段名
*/
public static final String CURRENT_USER_ID = "CURRENT_USER_ID";
String CURRENT_USER_ID = "CURRENT_USER_ID";
/**
* token有效期(小时)
*/
public static final int TOKEN_EXPIRES_HOUR = 72;
int TOKEN_EXPIRES_HOUR = 72;
/**
* 存放Authorization的header字段
*/
public static final String AUTHORIZATION = "authorization";
String AUTHORIZATION = "authorization";
/**
* 单项打分标准
*/
Map<Integer,XklAmpReportMetaScoreStandardEntity> scoreMap=new HashMap<>();
/**
* 综合加权标准
*/
Map<Integer,Integer> weightedScoreMap=new HashMap<Integer,Integer>(){
{
put(2,10);
put(3,6);
put(4,4);
put(5,7);
put(6,3);
put(7,10);
put(8,10);
put(9,6);
put(10,4);
put(11,2);
put(12,4);
put(13,5);
put(14,5);
put(15,6);
put(16,4);
put(17,6);
put(18,2);
put(19,6);
}
};
}
... ...
package com.xkl.config;
import com.xkl.domain.XklAmpReportMetaScoreStandardEntity;
import com.xkl.repository.XklAmpReportMetaScoreStandardRespository;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Iterator;
/**
* Created by win7 on 2016/11/21.
* 定时任务读取打分标准
*/
@Component
@CommonsLog
public class ScheduledTask {
@Autowired
private XklAmpReportMetaScoreStandardRespository scoreStandardRespository;
@Scheduled(initialDelay = 0,fixedRate = 24*60*60*1000)
public void getTableMap(){
log.info("Load ScoreStandard Table");
Iterator<XklAmpReportMetaScoreStandardEntity> it=scoreStandardRespository.findAll().iterator();
while(it.hasNext()){
XklAmpReportMetaScoreStandardEntity scoreStandard=it.next();
Constants.scoreMap.put(scoreStandard.getItemId(),scoreStandard);
}
}
}
... ...
... ... @@ -5,11 +5,13 @@ import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xkl.authorization.annotation.Authorization;
import com.xkl.authorization.annotation.CurrentUser;
import com.xkl.domain.User;
import com.xkl.domain.XklAmpReportEntity;
import com.xkl.domain.XklMemberEntity;
import com.xkl.config.Constants;
import com.xkl.domain.*;
import com.xkl.model.ResultModel;
import com.xkl.repository.XklAmpReportDetailRespository;
import com.xkl.repository.XklAmpReportHealthScoreRespository;
import com.xkl.repository.XklAmpReportRespository;
import com.xkl.service.IScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
... ... @@ -19,7 +21,8 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.*;
import java.util.function.DoubleToIntFunction;
/**
* Created by win7 on 2016/11/20.
... ... @@ -29,18 +32,41 @@ import java.util.List;
public class ReportController {
@Autowired
private XklAmpReportRespository xklAmpReportRespository;
@Autowired
private XklAmpReportDetailRespository xklAmpReportDetailRespository;
@RequestMapping(method = RequestMethod.GET)
@Authorization
private XklAmpReportHealthScoreRespository xklAmpReportHealthScoreRespository;
@Autowired
private IScoreService scoreService;
@RequestMapping(value="/list",method = RequestMethod.GET)
//@Authorization
//@Sign
@ApiOperation(value = "体检报告列表查询接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
})
public ResponseEntity<ResultModel> getUserInfo(HttpServletRequest request, @CurrentUser User user,
public ResponseEntity<ResultModel> getReportList(HttpServletRequest request, @CurrentUser User user,
@RequestParam String sign, @RequestParam long t, @RequestParam int type) {
long member_id=user.getMember_id();
List<XklAmpReportEntity> xklAmpReportEntity=xklAmpReportRespository.findByMemberId(member_id);
return new ResponseEntity<>(ResultModel.ok(xklAmpReportEntity), HttpStatus.OK);
}
@RequestMapping(value="/score",method = RequestMethod.GET)
@Authorization
//@Sign
@ApiOperation(value = "健康评分接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
})
public ResponseEntity<ResultModel> getReportScore(HttpServletRequest request, @CurrentUser User user,@RequestParam long report_id,
@RequestParam String sign, @RequestParam long t, @RequestParam int type) {
scoreService.getScore(report_id);
return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK);
}
}
... ...
... ... @@ -2,13 +2,10 @@ package com.xkl.domain;
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.*;
/**
* 用户数据的domain类
* 用户登录表
*/
@Entity
@Table(name = "xkl_local_account")
... ... @@ -24,6 +21,7 @@ public class User {
//用户id
@Id
@GeneratedValue
@Column(name = "id")
private long id;
... ...
package com.xkl.domain;
import lombok.Data;
import javax.persistence.*;
/**
* Created by win7 on 2016/11/21.
* report详情
*/
@Data
@Entity
@Table(name = "xkl_amp_report_detail", schema = "hanhe_test", catalog = "")
public class XklAmpReportDetailEntity {
@Id
@GeneratedValue
@Column(name = "id")
private long id;
@Basic
@Column(name = "report_id")
private long reportId;
@Basic
@Column(name = "item_id")
private int itemId;
@Basic
@Column(name = "item_value")
private double itemValue;
@Basic
@Column(name = "status")
private int status;
}
... ...
... ... @@ -7,6 +7,7 @@ import java.io.Serializable;
/**
* Created by win7 on 2016/11/20.
* report列表
*/
@Data
@Entity
... ...
package com.xkl.domain;
import lombok.Data;
import javax.persistence.*;
/**
* Created by win7 on 2016/11/21.
* 每个体检大类的打分
*/
@Data
@Entity
@Table(name = "xkl_amp_report_health_scroe", schema = "hanhe_test", catalog = "")
public class XklAmpReportHealthScoreEntity {
@Id
@GeneratedValue
@Column(name = "id")
private long id;
@Basic
@Column(name = "report_id")
private long reportId;
@Basic
@Column(name = "type")
private int type;
@Basic
@Column(name = "type_health_score")
private double typeHealthScore;
}
... ...
package com.xkl.domain;
import lombok.Data;
import javax.persistence.*;
/**
* Created by win7 on 2016/11/21.
* 体检结果的解释
*/
@Data
@Entity
@Table(name = "xkl_amp_report_meta_items", schema = "hanhe_test", catalog = "")
public class XklAmpReportMetaItemsEntity {
private int id;
private int itmeId;
private String type;
private String title;
private double standardLowMale;
private double standardHighMale;
private double standardLowFemale;
private double standardHighFemale;
private String explainLow;
private String explainHigh;
private String explainNormal;
@Id
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Basic
@Column(name = "itme_id")
public int getItmeId() {
return itmeId;
}
public void setItmeId(int itmeId) {
this.itmeId = itmeId;
}
@Basic
@Column(name = "type")
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Basic
@Column(name = "title")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Basic
@Column(name = "standard_low_male")
public double getStandardLowMale() {
return standardLowMale;
}
public void setStandardLowMale(double standardLowMale) {
this.standardLowMale = standardLowMale;
}
@Basic
@Column(name = "standard_high_male")
public double getStandardHighMale() {
return standardHighMale;
}
public void setStandardHighMale(double standardHighMale) {
this.standardHighMale = standardHighMale;
}
@Basic
@Column(name = "standard_low_female")
public double getStandardLowFemale() {
return standardLowFemale;
}
public void setStandardLowFemale(double standardLowFemale) {
this.standardLowFemale = standardLowFemale;
}
@Basic
@Column(name = "standard_high_female")
public double getStandardHighFemale() {
return standardHighFemale;
}
public void setStandardHighFemale(double standardHighFemale) {
this.standardHighFemale = standardHighFemale;
}
@Basic
@Column(name = "explain_low")
public String getExplainLow() {
return explainLow;
}
public void setExplainLow(String explainLow) {
this.explainLow = explainLow;
}
@Basic
@Column(name = "explain_high")
public String getExplainHigh() {
return explainHigh;
}
public void setExplainHigh(String explainHigh) {
this.explainHigh = explainHigh;
}
@Basic
@Column(name = "explain_normal")
public String getExplainNormal() {
return explainNormal;
}
public void setExplainNormal(String explainNormal) {
this.explainNormal = explainNormal;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
XklAmpReportMetaItemsEntity that = (XklAmpReportMetaItemsEntity) o;
if (id != that.id) return false;
if (itmeId != that.itmeId) return false;
if (Double.compare(that.standardLowMale, standardLowMale) != 0) return false;
if (Double.compare(that.standardHighMale, standardHighMale) != 0) return false;
if (Double.compare(that.standardLowFemale, standardLowFemale) != 0) return false;
if (Double.compare(that.standardHighFemale, standardHighFemale) != 0) return false;
if (type != null ? !type.equals(that.type) : that.type != null) return false;
if (title != null ? !title.equals(that.title) : that.title != null) return false;
if (explainLow != null ? !explainLow.equals(that.explainLow) : that.explainLow != null) return false;
if (explainHigh != null ? !explainHigh.equals(that.explainHigh) : that.explainHigh != null) return false;
if (explainNormal != null ? !explainNormal.equals(that.explainNormal) : that.explainNormal != null)
return false;
return true;
}
@Override
public int hashCode() {
int result;
long temp;
result = id;
result = 31 * result + itmeId;
result = 31 * result + (type != null ? type.hashCode() : 0);
result = 31 * result + (title != null ? title.hashCode() : 0);
temp = Double.doubleToLongBits(standardLowMale);
result = 31 * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(standardHighMale);
result = 31 * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(standardLowFemale);
result = 31 * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(standardHighFemale);
result = 31 * result + (int) (temp ^ (temp >>> 32));
result = 31 * result + (explainLow != null ? explainLow.hashCode() : 0);
result = 31 * result + (explainHigh != null ? explainHigh.hashCode() : 0);
result = 31 * result + (explainNormal != null ? explainNormal.hashCode() : 0);
return result;
}
}
... ...
package com.xkl.domain;
import lombok.Data;
import javax.persistence.*;
/**
* Created by win7 on 2016/11/21.
* 打分标准
*/
@Data
@Entity
@Table(name = "xkl_amp_report_meta_score_standard", schema = "hanhe_test", catalog = "")
public class XklAmpReportMetaScoreStandardEntity {
@Id
@GeneratedValue
@Column(name = "id")
private long id;
@Basic
@Column(name = "item_id")
private int itemId;
@Basic
@Column(name = "item_name")
private String itemName;
@Basic
@Column(name = "item_type")
private int itemType;
@Basic
@Column(name = "interval1_min")
private double interval1Min;
@Basic
@Column(name = "interval1_max")
private double interval1Max;
@Basic
@Column(name = "interval1_score")
private double interval1Score;
@Basic
@Column(name = "interval2_min")
private double interval2Min;
@Basic
@Column(name = "interval2_max")
private double interval2Max;
@Basic
@Column(name = "interval2_score")
private double interval2Score;
@Basic
@Column(name = "interval3_min")
private double interval3Min;
@Basic
@Column(name = "interval3_max")
private double interval3Max;
@Basic
@Column(name = "interval3_score")
private double interval3Score;
@Basic
@Column(name = "interval4_min")
private double interval4Min;
@Basic
@Column(name = "interval4_max")
private double interval4Max;
@Basic
@Column(name = "interval4_score")
private double interval4Score;
@Basic
@Column(name = "interval5_min")
private double interval5Min;
@Basic
@Column(name = "interval5_max")
private double interval5Max;
@Basic
@Column(name = "interval5_score")
private double interval5Score;
/**
* 由单项体检的值判断得分
* @param score
* @return
*/
public double getScore(double score){
double result = 0;
if(score>interval1Min && score<interval1Max)
result = interval1Score;
else if(score>interval2Min && score<interval2Max)
result = interval2Score;
else if(score>interval3Min && score<interval3Max)
result = interval3Score;
else if(score>interval4Min && score<interval4Max)
result = interval4Score;
else if(score>interval5Min && score<interval5Max)
result = interval5Score;
return result;
}
}
... ...
... ... @@ -6,6 +6,7 @@ import javax.persistence.*;
/**
* Created by win7 on 2016/11/20.
* 每种访问接口形式的key
*/
@Data
@Entity
... ...
... ... @@ -8,12 +8,14 @@ import java.sql.Date;
/**
* Created by win7 on 2016/11/20.
* 用户信息表
*/
@Data
@Entity
@Table(name = "xkl_member", schema = "hanhe_test", catalog = "")
public class XklMemberEntity {
@Id
@GeneratedValue
@Column(name = "id")
private long id;
@Basic
... ...
package com.xkl.repository;
import com.xkl.domain.XklAmpReportDetailEntity;
import com.xkl.domain.XklAmpReportHealthScoreEntity;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
/**
* Created by win7 on 2016/11/20.
*/
public interface XklAmpReportDetailRespository extends CrudRepository<XklAmpReportDetailEntity, Long> {
//item_id
public List<XklAmpReportDetailEntity> findByReportIdAndItemId(long report_id, long item_id);
public List<XklAmpReportDetailEntity> findAllByReportId(long report_id);
}
... ...
package com.xkl.repository;
import com.xkl.domain.XklAmpReportHealthScoreEntity;
import com.xkl.domain.XklAmpReportMetaScoreStandardEntity;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
/**
* Created by win7 on 2016/11/20.
*/
public interface XklAmpReportHealthScoreRespository extends CrudRepository<XklAmpReportHealthScoreEntity, Long> {
//item_id
public List<XklAmpReportHealthScoreEntity> findByReportIdAndType(long report_id,int type);
//public boolean existsByReportId(long report_id);
//public void saveByReportId(List<XklAmpReportHealthScoreEntity> list,long report_id);
}
... ...
package com.xkl.repository;
import com.xkl.domain.XklAmpReportEntity;
import com.xkl.domain.XklAmpReportMetaScoreStandardEntity;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
/**
* Created by win7 on 2016/11/20.
*/
public interface XklAmpReportMetaScoreStandardRespository extends CrudRepository<XklAmpReportMetaScoreStandardEntity, Long> {
//item_id
public List<XklAmpReportMetaScoreStandardEntity> findByItemId(long member_id);
}
... ...
package com.xkl.service;
/**
* Created by win7 on 2016/11/21.
*/
public interface IScoreService {
public void getScore(long report_id);
}
... ...
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 void getScore(long report_id) {
Map<Integer,Double> typeScoreMap=new HashMap<>();//记录各大项打分的Map
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);
finalScore += weightedScore*(score/100);
}
/**
* 每类得分存入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);
}
}
}
... ...