PatientGroupToLabelRunnable.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package com.yihu.wlyy.statistics.task;
  2. import com.yihu.wlyy.statistics.Application;
  3. import com.yihu.wlyy.statistics.dao.*;
  4. import com.yihu.wlyy.statistics.model.doctor.DoctorPatientGroupInfo;
  5. import com.yihu.wlyy.statistics.model.label.SignPatientLabel;
  6. import com.yihu.wlyy.statistics.model.label.SignPatientLabelInfo;
  7. import com.yihu.wlyy.statistics.model.patient.Patient;
  8. import com.yihu.wlyy.statistics.util.SpringUtil;
  9. import org.springframework.data.domain.Page;
  10. import org.springframework.data.domain.PageRequest;
  11. import org.springframework.orm.jpa.JpaTransactionManager;
  12. import org.springframework.transaction.TransactionDefinition;
  13. import org.springframework.transaction.TransactionStatus;
  14. import org.springframework.transaction.support.DefaultTransactionDefinition;
  15. import java.text.SimpleDateFormat;
  16. import java.util.*;
  17. /**
  18. * Created by lyr-pc on 2016/10/12.
  19. */
  20. public class PatientGroupToLabelRunnable implements Runnable {
  21. long start = 0;
  22. long end = 0;
  23. DoctorPatientGroupInfoDao groupInfoDao;
  24. SignPatientLabelInfoDao labelInfoDao;
  25. JpaTransactionManager transactionManager;
  26. public PatientGroupToLabelRunnable(Long start, Long end) {
  27. this.start = start;
  28. this.end = end;
  29. this.groupInfoDao = SpringUtil.getApplicationContext().getBean(DoctorPatientGroupInfoDao.class);
  30. this.labelInfoDao = SpringUtil.getApplicationContext().getBean(SignPatientLabelInfoDao.class);
  31. this.transactionManager = SpringUtil.getApplicationContext().getBean(JpaTransactionManager.class);
  32. }
  33. @Override
  34. public void run() {
  35. boolean flag = true;
  36. while (flag) {
  37. PageRequest pageRequest = new PageRequest(0, 100);
  38. Page<DoctorPatientGroupInfo> groups = groupInfoDao.findByIdRange(start, end, pageRequest);
  39. if (groups != null && groups.getContent().size() < 100) {
  40. flag = false;
  41. }
  42. start = groups.getContent().get(groups.getContent().size() - 1).getId() + 1;
  43. DefaultTransactionDefinition def = new DefaultTransactionDefinition();
  44. def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); // 事物隔离级别,开启新事务
  45. TransactionStatus status = transactionManager.getTransaction(def); // 获得事务状态
  46. try {
  47. groupToLabel(groups);
  48. //事物提交
  49. transactionManager.commit(status);
  50. } catch (Exception e) {
  51. transactionManager.rollback(status);
  52. }
  53. }
  54. }
  55. void groupToLabel(Page<DoctorPatientGroupInfo> groups) {
  56. System.out.println("sign-patient-group-team:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
  57. Map<String, SignPatientLabelInfo> normalLabel = new HashMap<>();
  58. Map<String, SignPatientLabelInfo> healthMap = new HashMap<>();
  59. List<DoctorPatientGroupInfo> groupInfos = groups.getContent();
  60. for (DoctorPatientGroupInfo groupInfo : groupInfos) {
  61. SignPatientLabelInfo labelInfo = new SignPatientLabelInfo();
  62. labelInfo.setPatient(groupInfo.getPatient());
  63. labelInfo.setPname(groupInfo.getPname());
  64. if (groupInfo.getGroup().equals("1")) {
  65. labelInfo.setLabel("1");
  66. labelInfo.setLabelName("普通人群");
  67. } else if (groupInfo.getGroup().equals("2")) {
  68. labelInfo.setLabel("2");
  69. labelInfo.setLabelName("慢病人群");
  70. } else if (groupInfo.equals(3)) {
  71. labelInfo.setLabel("3");
  72. labelInfo.setLabelName("65岁以上人群");
  73. } else {
  74. continue;
  75. }
  76. labelInfo.setLabelType("1");
  77. labelInfo.setStatus(1);
  78. labelInfo.setCzrq(new Date());
  79. List<SignPatientLabelInfo> patientLabels = labelInfoDao.findByPatientAndStatus(labelInfo.getPatient(), 1);
  80. SignPatientLabelInfo healthLabel = null;
  81. SignPatientLabelInfo wjwGroup = null;
  82. String typeTwo = "1";
  83. for (SignPatientLabelInfo patientLabel : patientLabels) {
  84. if (patientLabel.getLabelType().equals("3")) {
  85. typeTwo = "2";
  86. }
  87. if (patientLabel.getLabelType().equals("1")) {
  88. wjwGroup = patientLabel;
  89. }
  90. if (patientLabel.getLabelType().equals("2")) {
  91. healthLabel = patientLabel;
  92. }
  93. }
  94. if (healthLabel == null) {
  95. healthLabel = new SignPatientLabelInfo();
  96. healthLabel.setPatient(groupInfo.getPatient());
  97. healthLabel.setPname(groupInfo.getPname());
  98. healthLabel.setLabelType("2");
  99. healthLabel.setStatus(1);
  100. healthLabel.setCzrq(new Date());
  101. }
  102. if (typeTwo.equals("1")) {
  103. healthLabel.setLabel("1");
  104. healthLabel.setLabelName("健康人群");
  105. } else {
  106. healthLabel.setLabel("2");
  107. healthLabel.setLabelName("患病人群");
  108. }
  109. healthMap.put(groupInfo.getPatient(), healthLabel);
  110. if (wjwGroup != null) {
  111. wjwGroup.setLabel(labelInfo.getLabel());
  112. wjwGroup.setLabelName(labelInfo.getLabelName());
  113. wjwGroup.setCzrq(new Date());
  114. normalLabel.put(groupInfo.getPatient(), wjwGroup);
  115. } else {
  116. normalLabel.put(groupInfo.getPatient(), labelInfo);
  117. }
  118. }
  119. if (normalLabel.size() > 0) {
  120. labelInfoDao.save(normalLabel.values());
  121. }
  122. if (healthMap.size() > 0) {
  123. labelInfoDao.save(healthMap.values());
  124. }
  125. groupInfoDao.save(groups);
  126. System.out.println("sign-patient-group-team:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
  127. }
  128. }