PortraitService.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package com.yihu.figure.service;
  2. import com.yihu.figure.dao.patient.PatientInfoDao;
  3. import com.yihu.figure.dao.patient.portrait.UserPortraitDao;
  4. import com.yihu.figure.model.disease.Visit;
  5. import com.yihu.figure.model.patient.PatientInfo;
  6. import com.yihu.figure.model.patient.portrait.UserPortrait;
  7. import com.yihu.figure.util.DateUtil;
  8. import com.yihu.figure.util.IdCardUtil;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  11. import org.springframework.jdbc.core.JdbcTemplate;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.transaction.annotation.Transactional;
  14. import org.springframework.util.StringUtils;
  15. import java.util.*;
  16. /**
  17. * Created by chenweida on 2017/3/8.
  18. */
  19. @Service
  20. public class PortraitService {
  21. @Autowired
  22. private UserPortraitDao userPortraitDao;
  23. @Autowired
  24. private JdbcTemplate jdbcTemplate;
  25. @Autowired
  26. private PatientInfoDao patientDao;
  27. public List<UserPortrait> getPatientInfo(String category, String subCategory, String value) {
  28. StringBuffer sql = new StringBuffer("select * from figure_user_portrait f where 1=1");
  29. List params = new ArrayList<>();
  30. if (!StringUtils.isEmpty(category)) {
  31. sql.append(" and f.category = ? ");
  32. params.add(category);
  33. }
  34. if (!StringUtils.isEmpty(subCategory)) {
  35. sql.append(" and f.sub_category = ? ");
  36. params.add(subCategory);
  37. }
  38. if (!StringUtils.isEmpty(value)) {
  39. sql.append(" and f.value like ? ");
  40. params.add("%" + value + "%");
  41. }
  42. List<UserPortrait> userPortraits = jdbcTemplate.query(sql.toString(), params.toArray(), new BeanPropertyRowMapper(UserPortrait.class));
  43. return userPortraits;
  44. }
  45. @Transactional
  46. public void newPortrait(String category, String subCategory) {
  47. List<PatientInfo> patient = patientDao.findAllPatient();
  48. List<UserPortrait> ups = new ArrayList<>();
  49. patient.stream().forEach(p -> {
  50. UserPortrait u = new UserPortrait();
  51. u.setCategory(category);
  52. u.setSubCategory(subCategory);
  53. u.setCzrq(new Date());
  54. u.setUserCode(p.getCode());
  55. //年龄
  56. //ups.add(age(DateUtil.getAgeByBirthday(p.getBirthday()),u));
  57. //市
  58. //ups.add(city("厦门市",u));
  59. //区
  60. //ups.add(town(p.getTownName(),u));
  61. //性别标签
  62. //ups.add(sex(p.getSex(),u));
  63. //疾病标签
  64. // ups.add(jibing(p,u));
  65. //健康分布
  66. ups.add(jkfb(p, u));
  67. });
  68. userPortraitDao.save(ups);
  69. }
  70. private UserPortrait jkfb(PatientInfo p, UserPortrait u) {
  71. Integer age = DateUtil.getAgeByBirthday(p.getBirthday());
  72. if (age > 25)
  73. u.setValue("患病人群");
  74. return u;
  75. }
  76. private UserPortrait jibing(PatientInfo p, UserPortrait u) {
  77. Integer age = DateUtil.getAgeByBirthday(p.getBirthday());
  78. if (age > 25)
  79. u.setValue("糖尿病");
  80. return u;
  81. }
  82. private UserPortrait city(String townName, UserPortrait u) {
  83. u.setValue(townName);
  84. return u;
  85. }
  86. private UserPortrait sex(Integer sex, UserPortrait u) {
  87. if (sex != null && sex == 2) {
  88. u.setValue("女");
  89. } else {
  90. u.setValue("男");
  91. }
  92. return u;
  93. }
  94. private UserPortrait town(String townName, UserPortrait u) {
  95. u.setValue(townName);
  96. return u;
  97. }
  98. private UserPortrait age(int age, UserPortrait u) {
  99. if (age <= 20) {
  100. u.setValue("20岁以下");
  101. } else if (age > 20 && age <= 40) {
  102. u.setValue("20-40岁");
  103. } else if (age > 40 && age <= 65) {
  104. u.setValue("40-65岁");
  105. } else {
  106. u.setValue("65岁以上");
  107. }
  108. return u;
  109. }
  110. /**
  111. * 根据疾病获取病人区域分布
  112. *
  113. * @param subcategory 二级指标的维度
  114. * @param value 疾病名称 糖尿病 高血压
  115. * @return
  116. */
  117. public Map<String, Integer> getPortraits(String subcategory, String value) {
  118. //查找除子类别是1002001中的其他类别 例如 查找高血压下的各个年龄段的患者
  119. String sql = " select * from " +
  120. " (select DISTINCT(u.user_code) user_code from figure_user_portrait u where u.sub_category='1002001' and u.`value` like ? ) jb," +
  121. " (select DISTINCT(u.user_code) user_code,u.`value` from figure_user_portrait u where u.sub_category= ? ) q " +
  122. " where jb.user_code=q.user_code ";
  123. List params = new ArrayList<>();
  124. params.add("%" + value + "%");
  125. params.add(subcategory);
  126. List<UserPortrait> userPortraits = jdbcTemplate.query(sql, params.toArray(), new BeanPropertyRowMapper(UserPortrait.class));
  127. Map<String, Integer> data = new HashMap<>();
  128. userPortraits.stream().forEach(u -> {
  129. Integer sum = 1;
  130. if (data.containsKey(u.getValue())) {
  131. sum = data.get(u.getValue()) + 1;
  132. }
  133. data.put(u.getValue(), sum);
  134. });
  135. //总数 用于控制层计算
  136. data.put("allSize", userPortraits.size());
  137. return data;
  138. }
  139. /**
  140. * @param key 1 疾病 2 健康分布
  141. * @return
  142. */
  143. public Map<String, Integer> getPortraitsLevel1(String key) {
  144. //找出对应的子类别代码
  145. String subCategory = getSubCategory(key);
  146. //根据子类别找出所有的用户标签
  147. List<UserPortrait> ups = userPortraitDao.findBySubCategory(subCategory);
  148. Map<String, Integer> returnMap = new HashMap<>();
  149. ups.stream().forEach(up -> {
  150. Integer sum = 1;
  151. if (returnMap.containsKey(up.getValue())) {
  152. sum = returnMap.get(up.getValue()) + 1;
  153. }
  154. returnMap.put(up.getValue(), sum);
  155. });
  156. //总数 用于控制层计算
  157. returnMap.put("allSize", ups.size());
  158. return returnMap;
  159. }
  160. private String getSubCategory(String key) {
  161. switch (key) {
  162. case "1": {
  163. return "1002001";
  164. }
  165. case "2": {
  166. return "1002006";
  167. }
  168. }
  169. return "";
  170. }
  171. public UserPortrait findPortraitById(Long id) {
  172. return userPortraitDao.findOne(id);
  173. }
  174. }