123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- package com.yihu.wlyy.statistics.task;
- import com.yihu.wlyy.statistics.Application;
- import com.yihu.wlyy.statistics.dao.*;
- import com.yihu.wlyy.statistics.model.doctor.DoctorPatientGroupInfo;
- import com.yihu.wlyy.statistics.model.label.SignPatientLabelInfo;
- import com.yihu.wlyy.statistics.model.signfamily.SignFamily;
- import com.yihu.wlyy.statistics.model.team.DoctorTeam;
- import com.yihu.wlyy.statistics.model.team.DoctorTeamMember;
- import com.yihu.wlyy.statistics.util.IdCardUtil;
- import com.yihu.wlyy.statistics.util.SpringUtil;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.PageRequest;
- import org.springframework.orm.jpa.JpaTransactionManager;
- import org.springframework.stereotype.Component;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.TransactionDefinition;
- import org.springframework.transaction.TransactionStatus;
- import org.springframework.transaction.support.DefaultTransactionDefinition;
- import org.springframework.web.context.support.SpringBeanAutowiringSupport;
- import java.text.SimpleDateFormat;
- import java.util.*;
- /**
- * Created by lyr-pc on 2016/10/11.
- */
- public class SignTeamAndGroupRunnable implements Runnable {
- long start = 0;
- long end = 0;
- SignFamilyDao signFamilyDao;
- DoctorTeamDao teamDao;
- DoctorTeamMemberDao teamMemberDao;
- JpaTransactionManager transactionManager;
- SignPatientLabelInfoDao labelInfoDao;
- public SignTeamAndGroupRunnable(Long start, Long end){
- this.start = start;
- this.end = end;
- this.signFamilyDao = SpringUtil.getApplicationContext().getBean(SignFamilyDao.class);
- this.teamDao = SpringUtil.getApplicationContext().getBean(DoctorTeamDao.class);
- this.teamMemberDao = SpringUtil.getApplicationContext().getBean(DoctorTeamMemberDao.class);
- this.transactionManager = SpringUtil.getApplicationContext().getBean(JpaTransactionManager.class);
- this.labelInfoDao = SpringUtil.getApplicationContext().getBean(SignPatientLabelInfoDao.class);
- }
- public String getCode() {
- return UUID.randomUUID().toString().replaceAll("-", "");
- }
- @Override
- public void run() {
- boolean flag = true;
- while (flag) {
- PageRequest pageRequest = new PageRequest(0, 100);
- Page<SignFamily> signs = signFamilyDao.findByTypeAndSignSourceAndId(2, "1",start, end,pageRequest);
- if (signs != null && signs.getContent().size() < 100) {
- flag = false;
- }
- start = signs.getContent().get(signs.getContent().size() - 1).getId() + 1;
- DefaultTransactionDefinition def = new DefaultTransactionDefinition();
- def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); // 事物隔离级别,开启新事务
- TransactionStatus status = transactionManager.getTransaction(def); // 获得事务状态
- try {
- transform(signs);
- //事物提交
- transactionManager.commit(status);
- } catch (Exception e) {
- transactionManager.rollback(status);
- }
- }
- }
- public void transform(Page<SignFamily> signs){
- System.out.println("sign-patient-group-team:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
- List<DoctorTeam> teams = new ArrayList<>();
- List<DoctorTeamMember> teamMembers = new ArrayList<>();
- List<DoctorPatientGroupInfo> groupList = new ArrayList<>();
- Map<String,SignPatientLabelInfo> labels = new HashMap<>();
- List<SignFamily> signList = signs.getContent();
- for (SignFamily sign : signList) {
- try {
- if (StringUtils.isNotEmpty(sign.getTeamCode())) {
- continue;
- }
- //建立团队
- DoctorTeam doctorTeam = new DoctorTeam();
- String doctorTeamCode = getCode();
- doctorTeam.setCode(doctorTeamCode);
- doctorTeam.setCzrq(new Date());
- doctorTeam.setName("团队名称:" + sign.getName());
- doctorTeam.setSignType("2");//家庭签约
- doctorTeam.setDel("1");
- teams.add(doctorTeam);
- //添加团队成员
- if (StringUtils.isNotEmpty(sign.getDoctor())) {
- //添加全科
- DoctorTeamMember wlyyDoctorTeamDoctor = new DoctorTeamMember();
- wlyyDoctorTeamDoctor.setTeam(doctorTeamCode);
- wlyyDoctorTeamDoctor.setMemberCode(sign.getDoctor());
- wlyyDoctorTeamDoctor.setName(sign.getDoctorName());
- wlyyDoctorTeamDoctor.setDel("1");
- wlyyDoctorTeamDoctor.setType(2);
- wlyyDoctorTeamDoctor.setSignType("2");//家庭签约
- wlyyDoctorTeamDoctor.setCode(getCode());
- wlyyDoctorTeamDoctor.setCzrq(new Date());
- teamMembers.add(wlyyDoctorTeamDoctor);
- }
- if (StringUtils.isNotEmpty(sign.getDoctorHealth())) {
- //添加健康管理师
- DoctorTeamMember wlyyDoctorTeamDoctor = new DoctorTeamMember();
- wlyyDoctorTeamDoctor.setTeam(doctorTeamCode);
- wlyyDoctorTeamDoctor.setMemberCode(sign.getDoctorHealth());
- wlyyDoctorTeamDoctor.setName(sign.getDoctorHealthName());
- wlyyDoctorTeamDoctor.setDel("1");
- wlyyDoctorTeamDoctor.setType(3);
- wlyyDoctorTeamDoctor.setSignType("2");//家庭签约
- wlyyDoctorTeamDoctor.setCode(getCode());
- wlyyDoctorTeamDoctor.setCzrq(new Date());
- teamMembers.add(wlyyDoctorTeamDoctor);
- }
- //添加患者和团队的关系
- DoctorTeamMember wlyyDoctorTeamPatient = new DoctorTeamMember();
- wlyyDoctorTeamPatient.setTeam(doctorTeamCode);
- wlyyDoctorTeamPatient.setMemberCode(sign.getPatient());
- wlyyDoctorTeamPatient.setName(sign.getName());
- wlyyDoctorTeamPatient.setDel("1");
- wlyyDoctorTeamPatient.setSignType("2");//家庭签约
- wlyyDoctorTeamPatient.setType(5);
- wlyyDoctorTeamPatient.setCode(getCode());
- wlyyDoctorTeamPatient.setCzrq(new Date());
- teamMembers.add(wlyyDoctorTeamPatient);
- if (StringUtils.isEmpty(sign.getIdcard())) {
- System.out.println("error:sign-family-set-group:no-idcard:" + sign.getId());
- continue;
- }
- int age = IdCardUtil.getAgeForIdcard(sign.getIdcard());
- String groupCode = "1";
- String groupName = "普通人群";
- if (age >= 65) {
- groupCode = "3";
- groupName = "65岁以上人群";
- }
- SignPatientLabelInfo label = new SignPatientLabelInfo();
- SignPatientLabelInfo exLabelInfo = labelInfoDao.findByPatientAndLabelAndLabelTypeAndStatus(
- sign.getPatient(), groupCode, "1", 1);
- if (exLabelInfo != null) {
- continue;
- }
- label.setPatient(sign.getPatient());
- label.setPatient(sign.getName());
- label.setLabel(groupCode);
- label.setLabelName(groupName);
- label.setLabelType("1");
- label.setStatus(1);
- label.setCzrq(new Date());
- labels.put(sign.getPatient(),label);
- sign.setTeamCode(doctorTeamCode);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- teamDao.save(teams);
- teamMemberDao.save(teamMembers);
- labelInfoDao.save(labels.values());
- signFamilyDao.save(signList);
- System.out.println("sign-patient-group-team:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
- }
- }
|