package com.yihu.wlyy.statistics.task;

import com.yihu.wlyy.statistics.Application;
import com.yihu.wlyy.statistics.dao.PatientDiseaseDao;
import com.yihu.wlyy.statistics.dao.SignPatientLabelInfoDao;
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 com.yihu.wlyy.statistics.model.patient.PatientDisease;
import com.yihu.wlyy.statistics.util.SpringUtil;
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.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by lyr-pc on 2016/10/13.
 */
public class PatientDiseaseToLabel implements Runnable {

    long start = 1;

    PatientDiseaseDao diseaseDao;
    SignPatientLabelInfoDao labelInfoDao;
    JpaTransactionManager transactionManager;

    public PatientDiseaseToLabel(Long start) {
        this.start = start;
        this.diseaseDao = SpringUtil.getApplicationContext().getBean(PatientDiseaseDao.class);
        this.labelInfoDao = SpringUtil.getApplicationContext().getBean(SignPatientLabelInfoDao.class);
        this.transactionManager = SpringUtil.getApplicationContext().getBean(JpaTransactionManager.class);
    }

    @Override
    public void run() {
        boolean flag = true;

        while (flag) {

            PageRequest pageRequest = new PageRequest(0, 500);
            Page<PatientDisease> diseases = diseaseDao.findByIdRange(start, pageRequest);

            if (diseases != null && diseases.getContent().size() < 500) {
                flag = false;
            }

            start = diseases.getContent().get(diseases.getContent().size() - 1).getId() + 1;

            DefaultTransactionDefinition def = new DefaultTransactionDefinition();
            def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); // 事物隔离级别，开启新事务
            TransactionStatus status = transactionManager.getTransaction(def); // 获得事务状态
            try {
                diseasesToLabel(diseases);
                //事物提交
                transactionManager.commit(status);
            } catch (Exception e) {
                transactionManager.rollback(status);
            }
        }
    }

    void diseasesToLabel(Page<PatientDisease> diseases) {
        System.out.println("sign-patient-group-team:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

        Map<String, SignPatientLabelInfo> labelInfos = new HashMap<>();

        for (PatientDisease disease : diseases.getContent()) {
            SignPatientLabelInfo labelInfo = new SignPatientLabelInfo();

            SignPatientLabelInfo exLabelInfo = labelInfoDao.findByPatientAndLabelAndLabelTypeAndStatus(
                    disease.getPatient(), disease.getDisease(), "3", 1);

            if (exLabelInfo != null) {
                continue;
            }

            labelInfo.setPatient(disease.getPatient());
            labelInfo.setLabel(disease.getDisease());
            labelInfo.setLabelName(disease.getDiseaseName());
            labelInfo.setLabelType("3");
            labelInfo.setStatus(1);
            labelInfo.setCzrq(new Date());

            labelInfos.put(disease.getPatient() + disease.getDisease(), labelInfo);
        }

        labelInfoDao.save(labelInfos.values());

        System.out.println("sign-patient-group-team:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }
}
