package com.lantone.qc.dbanaly.facade.changx;

import com.google.common.collect.Lists;
import com.lantone.qc.dbanaly.facade.comsis.XmlDataAnalysisFacade;
import com.lantone.qc.dbanaly.lt.facade.ModeFacade;
import com.lantone.qc.dbanaly.lt.facade.ModelHospitalFacade;
import com.lantone.qc.pub.util.ListUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @author: rengb
 * @time: 2020/6/3 16:44
 */
@Slf4j
@Component
public class ChangxXmlDataAnalysisFacade {
    @Autowired
    private XmlDataAnalysisFacade xmlDataAnalysisFacade;
    @Autowired
    private ModeFacade modeFacade;
    @Autowired
    private ModelHospitalFacade modelHospitalFacade;

    public List<String> analyzeXml(List<Long> modeIds) {
        List<String> retList = Lists.newArrayList();
        if (ListUtil.isEmpty(modeIds)) {
            modeIds = Arrays.asList(1l, 2l, 3l, 4l, 5l, 10l, 11l, 17l, 18l, 19l, 20l, 21l, 22l, 23l, 24l, 25l, 26l, 27l, 28l, 29l, 30l, 31l, 32l, 35l);
        }
        Map<Long, String> modeMap = modeFacade.getModeMap();
        String modeName = null;
        for (Long modeId : modeIds) {
            modeName = modeMap.get(modeId);
            if (modeId == 1) {
                retList.addAll(analyzeXmlRyjl());
            } else if (modeId == 4) {
                retList.addAll(analyzeXmlCf());
            } else {
                retList.addAll(analyzeXmlOther(modeId, modeName));
            }
        }
        return retList;
    }

    private List<String> analyzeXmlRyjl() {
        List<String> retList = Lists.newArrayList();
        xmlDataAnalysisFacade.getRecTitles(1l, 1l).forEach(recTitle -> {
            try {
                xmlDataAnalysisFacade.analysisByRecTitle(1l, 1l, "入院记录", recTitle, null, "男");
                xmlDataAnalysisFacade.analysisByRecTitle(1l, 1l, "入院记录", recTitle, null, "女");
            } catch (Exception e) {
                retList.add("[长兴-入院记录-" + recTitle + "]执行失败");
                log.error("[长兴-入院记录-" + recTitle + "]执行失败--" + e.getMessage(), e);
            }
        });
        return retList;
    }

    private List<String> analyzeXmlCf() {
        String modeName = "查房记录";
        List<String> retList = Lists.newArrayList();
        List<String> excludeRecTitles = modelHospitalFacade.getRecTitles(1l, 4l);
        excludeRecTitles.forEach(recTitle -> {
            try {
                xmlDataAnalysisFacade.analysisByRecTitle(1l, 4l, modeName, recTitle, null, "");
            } catch (Exception e) {
                retList.add("[长兴-" + modeName + "-" + recTitle + "]执行失败");
                log.error("[长兴-" + modeName + "-" + recTitle + "]执行失败--" + e.getMessage(), e);
            }
        });
        try {
            xmlDataAnalysisFacade.analysisByRecTitle(1l, 4l, modeName, "", excludeRecTitles, "");
        } catch (Exception e) {
            retList.add("[长兴-" + modeName + "-" + "其余查房" + "]执行失败");
            log.error("[长兴-" + modeName + "-" + "其余查房" + "]执行失败--" + e.getMessage(), e);
        }
        return retList;
    }

    private List<String> analyzeXmlOther(long modeId, String modeName) {
        List<String> retList = Lists.newArrayList();
        xmlDataAnalysisFacade.getRecTitles(1l, modeId).forEach(recTitle -> {
            try {
                xmlDataAnalysisFacade.analysisByRecTitle(1l, modeId, modeName, recTitle, null, "");
            } catch (Exception e) {
                retList.add("[长兴-" + modeName + "-" + recTitle + "]执行失败");
                log.error("[长兴-" + modeName + "-" + recTitle + "]执行失败--" + e.getMessage(), e);
            }
        });
        return retList;
    }

}