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.patient.Patient; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.DefaultTransactionDefinition; import java.util.*; /** * Created by lyr-pc on 2016/10/12. */ public class PatientGroupToLabelRunnable implements Runnable { long start = 0; long end = 0; DoctorPatientGroupInfoDao groupInfoDao; SignPatientLabelInfoDao labelInfoDao; JpaTransactionManager transactionManager; public PatientGroupToLabelRunnable(Long start, Long end) { this.start = start; this.end = end; this.groupInfoDao = Application.ctx.getBean(DoctorPatientGroupInfoDao.class); this.labelInfoDao = Application.ctx.getBean(SignPatientLabelInfoDao.class); this.transactionManager = Application.ctx.getBean(JpaTransactionManager.class); } @Override public void run() { boolean flag = true; while (flag) { PageRequest pageRequest = new PageRequest(0, 100); Page groups = groupInfoDao.findByIdRange(start, end, pageRequest); if (groups != null && groups.getContent().size() < 100) { flag = false; } DefaultTransactionDefinition def = new DefaultTransactionDefinition(); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); // 事物隔离级别,开启新事务 TransactionStatus status = transactionManager.getTransaction(def); // 获得事务状态 try { groupToLabel(groups); //事物提交 transactionManager.commit(status); } catch (Exception e) { transactionManager.rollback(status); } start = start + 100; } } void groupToLabel(Page groups) { Map normalLabel = new HashMap<>(); Map manbingLabel = new HashMap<>(); Map sixFiveLabel = new HashMap<>(); List groupInfos = groups.getContent(); for (DoctorPatientGroupInfo groupInfo : groupInfos) { SignPatientLabelInfo labelInfo = new SignPatientLabelInfo(); labelInfo.setPatient(groupInfo.getPatient()); labelInfo.setPname(groupInfo.getPname()); if (groupInfo.getGroup().equals("1")) { labelInfo.setLabel("1"); labelInfo.setLabelName("普通人群"); } else if (groupInfo.getGroup().equals("2")) { labelInfo.setLabel("2"); labelInfo.setLabelName("慢病人群"); } else if (groupInfo.equals(3)) { labelInfo.setLabel("3"); labelInfo.setLabelName("65岁以上人群"); } else { continue; } labelInfo.setLabelType("1"); labelInfo.setStatus(1); labelInfo.setCzrq(new Date()); groupInfo.setStatus(2); normalLabel.put(groupInfo.getPatient(), labelInfo); } if (normalLabel.size() > 0) { labelInfoDao.save(normalLabel.values()); } if (manbingLabel.size() > 0) { labelInfoDao.save(manbingLabel.values()); } if (sixFiveLabel.size() > 0) { labelInfoDao.save(sixFiveLabel.values()); } groupInfoDao.save(groups); } }