PatientGroupToLabelRunnable.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.SignPatientLabelInfo;
  6. import com.yihu.wlyy.statistics.model.patient.Patient;
  7. import org.springframework.data.domain.Page;
  8. import org.springframework.data.domain.PageRequest;
  9. import org.springframework.orm.jpa.JpaTransactionManager;
  10. import org.springframework.transaction.TransactionDefinition;
  11. import org.springframework.transaction.TransactionStatus;
  12. import org.springframework.transaction.support.DefaultTransactionDefinition;
  13. import java.util.*;
  14. /**
  15. * Created by lyr-pc on 2016/10/12.
  16. */
  17. public class PatientGroupToLabelRunnable implements Runnable {
  18. long start = 0;
  19. long end = 0;
  20. DoctorPatientGroupInfoDao groupInfoDao;
  21. SignPatientLabelInfoDao labelInfoDao;
  22. JpaTransactionManager transactionManager;
  23. public PatientGroupToLabelRunnable(Long start, Long end) {
  24. this.start = start;
  25. this.end = end;
  26. this.groupInfoDao = Application.ctx.getBean(DoctorPatientGroupInfoDao.class);
  27. this.labelInfoDao = Application.ctx.getBean(SignPatientLabelInfoDao.class);
  28. this.transactionManager = Application.ctx.getBean(JpaTransactionManager.class);
  29. }
  30. @Override
  31. public void run() {
  32. boolean flag = true;
  33. while (flag) {
  34. PageRequest pageRequest = new PageRequest(0, 100);
  35. Page<DoctorPatientGroupInfo> groups = groupInfoDao.findByIdRange(start, end, pageRequest);
  36. if (groups != null && groups.getContent().size() < 100) {
  37. flag = false;
  38. }
  39. DefaultTransactionDefinition def = new DefaultTransactionDefinition();
  40. def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); // 事物隔离级别,开启新事务
  41. TransactionStatus status = transactionManager.getTransaction(def); // 获得事务状态
  42. try {
  43. groupToLabel(groups);
  44. //事物提交
  45. transactionManager.commit(status);
  46. } catch (Exception e) {
  47. transactionManager.rollback(status);
  48. }
  49. start = start + 100;
  50. }
  51. }
  52. void groupToLabel(Page<DoctorPatientGroupInfo> groups) {
  53. Map<String, SignPatientLabelInfo> normalLabel = new HashMap<>();
  54. Map<String, SignPatientLabelInfo> manbingLabel = new HashMap<>();
  55. Map<String, SignPatientLabelInfo> sixFiveLabel = new HashMap<>();
  56. List<DoctorPatientGroupInfo> groupInfos = groups.getContent();
  57. for (DoctorPatientGroupInfo groupInfo : groupInfos) {
  58. SignPatientLabelInfo labelInfo = new SignPatientLabelInfo();
  59. labelInfo.setPatient(groupInfo.getPatient());
  60. labelInfo.setPname(groupInfo.getPname());
  61. if (groupInfo.getGroup().equals("1")) {
  62. labelInfo.setLabel("1");
  63. labelInfo.setLabelName("普通人群");
  64. } else if (groupInfo.getGroup().equals("2")) {
  65. labelInfo.setLabel("2");
  66. labelInfo.setLabelName("慢病人群");
  67. } else if (groupInfo.equals(3)) {
  68. labelInfo.setLabel("3");
  69. labelInfo.setLabelName("65岁以上人群");
  70. } else {
  71. continue;
  72. }
  73. labelInfo.setLabelType("1");
  74. labelInfo.setStatus(1);
  75. labelInfo.setCzrq(new Date());
  76. groupInfo.setStatus(2);
  77. normalLabel.put(groupInfo.getPatient(), labelInfo);
  78. }
  79. if (normalLabel.size() > 0) {
  80. labelInfoDao.save(normalLabel.values());
  81. }
  82. if (manbingLabel.size() > 0) {
  83. labelInfoDao.save(manbingLabel.values());
  84. }
  85. if (sixFiveLabel.size() > 0) {
  86. labelInfoDao.save(sixFiveLabel.values());
  87. }
  88. groupInfoDao.save(groups);
  89. }
  90. }