SignTeamAndGroupRunnable.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.signfamily.SignFamily;
  7. import com.yihu.wlyy.statistics.model.team.DoctorTeam;
  8. import com.yihu.wlyy.statistics.model.team.DoctorTeamMember;
  9. import com.yihu.wlyy.statistics.util.IdCardUtil;
  10. import com.yihu.wlyy.statistics.util.SpringUtil;
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.data.domain.Page;
  14. import org.springframework.data.domain.PageRequest;
  15. import org.springframework.orm.jpa.JpaTransactionManager;
  16. import org.springframework.stereotype.Component;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.transaction.TransactionDefinition;
  19. import org.springframework.transaction.TransactionStatus;
  20. import org.springframework.transaction.support.DefaultTransactionDefinition;
  21. import org.springframework.web.context.support.SpringBeanAutowiringSupport;
  22. import java.text.SimpleDateFormat;
  23. import java.util.*;
  24. /**
  25. * Created by lyr-pc on 2016/10/11.
  26. */
  27. public class SignTeamAndGroupRunnable implements Runnable {
  28. long start = 0;
  29. long end = 0;
  30. SignFamilyDao signFamilyDao;
  31. DoctorTeamDao teamDao;
  32. DoctorTeamMemberDao teamMemberDao;
  33. JpaTransactionManager transactionManager;
  34. SignPatientLabelInfoDao labelInfoDao;
  35. public SignTeamAndGroupRunnable(Long start, Long end){
  36. this.start = start;
  37. this.end = end;
  38. this.signFamilyDao = SpringUtil.getApplicationContext().getBean(SignFamilyDao.class);
  39. this.teamDao = SpringUtil.getApplicationContext().getBean(DoctorTeamDao.class);
  40. this.teamMemberDao = SpringUtil.getApplicationContext().getBean(DoctorTeamMemberDao.class);
  41. this.transactionManager = SpringUtil.getApplicationContext().getBean(JpaTransactionManager.class);
  42. this.labelInfoDao = SpringUtil.getApplicationContext().getBean(SignPatientLabelInfoDao.class);
  43. }
  44. public String getCode() {
  45. return UUID.randomUUID().toString().replaceAll("-", "");
  46. }
  47. @Override
  48. public void run() {
  49. boolean flag = true;
  50. while (flag) {
  51. PageRequest pageRequest = new PageRequest(0, 100);
  52. Page<SignFamily> signs = signFamilyDao.findByTypeAndSignSourceAndId(2, "1",start, end,pageRequest);
  53. if (signs != null && signs.getContent().size() < 100) {
  54. flag = false;
  55. }
  56. start = signs.getContent().get(signs.getContent().size() - 1).getId() + 1;
  57. DefaultTransactionDefinition def = new DefaultTransactionDefinition();
  58. def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); // 事物隔离级别,开启新事务
  59. TransactionStatus status = transactionManager.getTransaction(def); // 获得事务状态
  60. try {
  61. transform(signs);
  62. //事物提交
  63. transactionManager.commit(status);
  64. } catch (Exception e) {
  65. transactionManager.rollback(status);
  66. }
  67. }
  68. }
  69. public void transform(Page<SignFamily> signs){
  70. System.out.println("sign-patient-group-team:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
  71. List<DoctorTeam> teams = new ArrayList<>();
  72. List<DoctorTeamMember> teamMembers = new ArrayList<>();
  73. List<DoctorPatientGroupInfo> groupList = new ArrayList<>();
  74. Map<String,SignPatientLabelInfo> labels = new HashMap<>();
  75. List<SignFamily> signList = signs.getContent();
  76. for (SignFamily sign : signList) {
  77. try {
  78. if (StringUtils.isNotEmpty(sign.getTeamCode())) {
  79. continue;
  80. }
  81. //建立团队
  82. DoctorTeam doctorTeam = new DoctorTeam();
  83. String doctorTeamCode = getCode();
  84. doctorTeam.setCode(doctorTeamCode);
  85. doctorTeam.setCzrq(new Date());
  86. doctorTeam.setName("团队名称:" + sign.getName());
  87. doctorTeam.setSignType("2");//家庭签约
  88. doctorTeam.setDel("1");
  89. teams.add(doctorTeam);
  90. //添加团队成员
  91. if (StringUtils.isNotEmpty(sign.getDoctor())) {
  92. //添加全科
  93. DoctorTeamMember wlyyDoctorTeamDoctor = new DoctorTeamMember();
  94. wlyyDoctorTeamDoctor.setTeam(doctorTeamCode);
  95. wlyyDoctorTeamDoctor.setMemberCode(sign.getDoctor());
  96. wlyyDoctorTeamDoctor.setName(sign.getDoctorName());
  97. wlyyDoctorTeamDoctor.setDel("1");
  98. wlyyDoctorTeamDoctor.setType(2);
  99. wlyyDoctorTeamDoctor.setSignType("2");//家庭签约
  100. wlyyDoctorTeamDoctor.setCode(getCode());
  101. wlyyDoctorTeamDoctor.setCzrq(new Date());
  102. teamMembers.add(wlyyDoctorTeamDoctor);
  103. }
  104. if (StringUtils.isNotEmpty(sign.getDoctorHealth())) {
  105. //添加健康管理师
  106. DoctorTeamMember wlyyDoctorTeamDoctor = new DoctorTeamMember();
  107. wlyyDoctorTeamDoctor.setTeam(doctorTeamCode);
  108. wlyyDoctorTeamDoctor.setMemberCode(sign.getDoctorHealth());
  109. wlyyDoctorTeamDoctor.setName(sign.getDoctorHealthName());
  110. wlyyDoctorTeamDoctor.setDel("1");
  111. wlyyDoctorTeamDoctor.setType(3);
  112. wlyyDoctorTeamDoctor.setSignType("2");//家庭签约
  113. wlyyDoctorTeamDoctor.setCode(getCode());
  114. wlyyDoctorTeamDoctor.setCzrq(new Date());
  115. teamMembers.add(wlyyDoctorTeamDoctor);
  116. }
  117. //添加患者和团队的关系
  118. DoctorTeamMember wlyyDoctorTeamPatient = new DoctorTeamMember();
  119. wlyyDoctorTeamPatient.setTeam(doctorTeamCode);
  120. wlyyDoctorTeamPatient.setMemberCode(sign.getPatient());
  121. wlyyDoctorTeamPatient.setName(sign.getName());
  122. wlyyDoctorTeamPatient.setDel("1");
  123. wlyyDoctorTeamPatient.setSignType("2");//家庭签约
  124. wlyyDoctorTeamPatient.setType(5);
  125. wlyyDoctorTeamPatient.setCode(getCode());
  126. wlyyDoctorTeamPatient.setCzrq(new Date());
  127. teamMembers.add(wlyyDoctorTeamPatient);
  128. if (StringUtils.isEmpty(sign.getIdcard())) {
  129. System.out.println("error:sign-family-set-group:no-idcard:" + sign.getId());
  130. continue;
  131. }
  132. int age = IdCardUtil.getAgeForIdcard(sign.getIdcard());
  133. String groupCode = "1";
  134. String groupName = "普通人群";
  135. if (age >= 65) {
  136. groupCode = "3";
  137. groupName = "65岁以上人群";
  138. }
  139. SignPatientLabelInfo label = new SignPatientLabelInfo();
  140. SignPatientLabelInfo exLabelInfo = labelInfoDao.findByPatientAndLabelAndLabelTypeAndStatus(
  141. sign.getPatient(), groupCode, "1", 1);
  142. if (exLabelInfo != null) {
  143. continue;
  144. }
  145. label.setPatient(sign.getPatient());
  146. label.setPatient(sign.getName());
  147. label.setLabel(groupCode);
  148. label.setLabelName(groupName);
  149. label.setLabelType("1");
  150. label.setStatus(1);
  151. label.setCzrq(new Date());
  152. labels.put(sign.getPatient(),label);
  153. sign.setTeamCode(doctorTeamCode);
  154. } catch (Exception e) {
  155. e.printStackTrace();
  156. }
  157. }
  158. teamDao.save(teams);
  159. teamMemberDao.save(teamMembers);
  160. labelInfoDao.save(labels.values());
  161. signFamilyDao.save(signList);
  162. System.out.println("sign-patient-group-team:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
  163. }
  164. }