package com.diagbot.facade.data;


import com.alibaba.fastjson.JSON;
import com.diagbot.entity.*;
import com.diagbot.facade.BehospitalInfoFacade;
import com.diagbot.util.TZDBConn;
import com.diagbot.vo.AnalyzeRunVO;
import com.diagbot.vo.AnalyzeVO;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class SendDataDealFacade {

    @Autowired
    private ADoctorAdviceFacade aDoctorAdviceFacade;
    @Autowired
    private AHomePageFacade aHomePageFacade;
    @Autowired
    private AHomeOperationInfoFacade aHomeOperationInfoFacade;
    @Autowired
    private AHomeDiagnoseInfoFacade aHomeDiagnoseInfoFacade;
    @Autowired
    private ABehospitalInfoFacade aBehospitalInfoFacade;
    @Autowired
    private AMedicalRecordContentFacade aMedicalRecordContentFacade;
    @Autowired
    private AMedicalRecordFacade aMedicalRecordFacade;
    @Autowired
    private AMedAbnormalInfoFacade aMedAbnormalInfoFacade;
    @Autowired
    BehospitalInfoFacade behospitalInfoFacade;

    private TZDBConn tzDBConn = new TZDBConn();




    public void sendDataDeal(String startDate,String stopDate){

//        list.forEach(s->{
//            sendDataDealABehospitalInfo(s);
//            sendDataDealADoctorAdvice(s);
//            sendDataDealAMedicalRecord(s);
//        });

        List<BehospitalInfo> behospitalInfoList=sendDataDealABehospitalInfo(startDate,stopDate);
//        根据的到的病人登记数据来获取病人的其他信息
        if (behospitalInfoList.size()>0){
            behospitalInfoList.forEach(s->{
                sendDataDealADoctorAdvice(s.getBehospitalCode());
                sendDataDealAMedicalRecord(s.getBehospitalCode());
            });
        }else {
            System.out.println("没有该时间点的病人登记信息");
        }

    }

    public void sendDateDealOne(String code){
        sendInfo(code);
        sendDataDealADoctorAdvice(code);
        sendDataDealAMedicalRecord(code);
        try {
            AnalyzeRunVO analyzeRunVO = new AnalyzeRunVO();
            analyzeRunVO.setHospitalId(Long.valueOf("14"));
            analyzeRunVO.setBehospitalCode(code);
            behospitalInfoFacade.analyzeApi(analyzeRunVO);
        }catch (Exception e){
            aMedAbnormalInfoFacade.saveAbnormalInfo("补传自动评分失败", code, "", "", e.getMessage());

        }
    }

    /**
     * 获取病人住院登记历史数据
     */
    public List<BehospitalInfo> sendInfo(String code) {
        List<BehospitalInfo> behospitalInfos =new ArrayList<>();
        try {
            String sql = "select * from   v_jkcdss_zy_bingrenjbxx where behospitalCode = '"+code+"'";
            behospitalInfos = tzDBConn.getBehospitalInfo(sql);
            aBehospitalInfoFacade.execute(behospitalInfos);
        } catch (Exception e) {
            aMedAbnormalInfoFacade.saveAbnormalInfo("获取病人住院登记历史数据", code, JSON.toJSONString(behospitalInfos), "", e.getMessage());
        }
        return behospitalInfos;
    }


    /**
     * 获取病人住院登记历史数据
     */
    public List<BehospitalInfo> sendDataDealABehospitalInfo(String startDate,String stopDate) {
        List<BehospitalInfo> behospitalInfos =new ArrayList<>();
        try {
            String sql = "select * from   v_jkcdss_zy_bingrenjbxx t where to_char(t.behospitalDate,'yyyymmdd')>= '"+startDate+" ' " +
                    " and to_char(t.behospitalDate,'yyyymmdd')<= '"+stopDate+" '";
            behospitalInfos = tzDBConn.getBehospitalInfo(sql);
            aBehospitalInfoFacade.execute(behospitalInfos);
        } catch (Exception e) {
            aMedAbnormalInfoFacade.saveAbnormalInfo("获取病人住院登记历史数据", startDate+"--"+stopDate, JSON.toJSONString(behospitalInfos), "", e.getMessage());
        }
        return behospitalInfos;
    }
    /**
     * 获取病人住院登记历史数据
     */
    public List<BehospitalInfo> sendDataDealABehospitalInfo(String behospitalCode) {
        List<BehospitalInfo> behospitalInfos =new ArrayList<>();
        try {
            String sql = "select * from   v_jkcdss_zy_bingrenjbxx t where behospitalCode = '"+behospitalCode+" '";

            behospitalInfos = tzDBConn.getBehospitalInfo(sql);
            aBehospitalInfoFacade.execute(behospitalInfos);
        } catch (Exception e) {
            aMedAbnormalInfoFacade.saveAbnormalInfo("获取病人住院登记历史数据", behospitalCode, JSON.toJSONString(behospitalInfos), "", e.getMessage());
        }
        return behospitalInfos;
    }

    /**
     * 获取医嘱历史数据
     */
    public void sendDataDealADoctorAdvice(String behospitalCode) {
        try {
            String sql = "select * from   v_jkcdss_yz_order  where behospitalCode= '"+behospitalCode+" '";
            List<DoctorAdvice> doctorAdviceList = tzDBConn.getDoctorAdvice(sql);
            aDoctorAdviceFacade.execute(doctorAdviceList);
        } catch (Exception e) {
            aMedAbnormalInfoFacade.saveAbnormalInfo("获取医嘱历史数据", behospitalCode, "", "", e.getMessage());
        }

    }

    /**
     * 获取文书历史数据
     */
    public void sendDataDealAMedicalRecord(String behospitalCode) {
        try {
            String sql = "select * from  v_jkcdss_bl_bingli   where behospitalCode= '"+behospitalCode+" '";
            List<MedicalRecord> medicalRecords = tzDBConn.getMedicalRecord(sql);
            aMedicalRecordFacade.execute(medicalRecords);
            medicalRecords.forEach(medicalRecord -> {
                String tableName =medicalRecord.getRecId().substring(0,2);
                String recId=medicalRecord.getRecId().substring(3);
                sendDataDealAMedicalRecordContent(recId,tableName);
            });
        } catch (Exception e) {
            aMedAbnormalInfoFacade.saveAbnormalInfo("获取文书历史数据", behospitalCode, "", "", e.getMessage());
        }
    }

    /**
     * 获取文书详情历史数据
     */
    public void sendDataDealAMedicalRecordContent(String recId,String tableName) {
        try {
            String sql = "select * from v_jkcdss_bl_binglijlnr where RECID= '"+recId+" ' and tableName = '"+tableName+"'";
            List<MedicalRecordContent> medicalRecordContents = tzDBConn.getMedicalRecordContent(sql);
            aMedicalRecordContentFacade.execute(medicalRecordContents);
        } catch (Exception e) {
            aMedAbnormalInfoFacade.saveAbnormalInfo("获取文书详情历史数据", recId, "", "", e.getMessage());
        }
    }

    /**
     * 获取病案首页历史数据
     */
    public void sendDataDealAHomePage(String behospitalCode) {
        try {
            String sql = "";
            List<HomePage> homePages = tzDBConn.getHomePage(sql);
            aHomePageFacade.execute(homePages);
        } catch (Exception e) {
            aMedAbnormalInfoFacade.saveAbnormalInfo("获取病案首页历史数据", behospitalCode, "", "", e.getMessage());
        }
    }

    /**
     * 获取病案首页医嘱历史数据
     */
    public void sendDataDealAHomeDiagnoseInfo(String behospitalCode) {
        try {
            String sql = "";
            List<HomeDiagnoseInfo> homeDiagnoseInfoFacades = tzDBConn.getHomeDiagnose(sql);
            aHomeDiagnoseInfoFacade.execute(homeDiagnoseInfoFacades);
        } catch (Exception e) {
            aMedAbnormalInfoFacade.saveAbnormalInfo("获取病案首页医嘱历史数据", behospitalCode, "", "", e.getMessage());
        }
    }

    /**
     * 获取病案首页手术及操作历史数据
     */
    public void sendDataDealAHomeOperationInfo(String behospitalCode) {
        try {
            String sql = "";
            List<HomeOperationInfo> homeOperationInfos = tzDBConn.getHomeOperation(sql);
            aHomeOperationInfoFacade.execute(homeOperationInfos);
        } catch (Exception e) {
            aMedAbnormalInfoFacade.saveAbnormalInfo("获取病案首页手术及操作历史数据", behospitalCode, "", "", e.getMessage());
        }
    }


}
