SignTeamAndGroupRunnable.java 8.0 KB

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