package com.lantone.qc.kernel.catalogue.firstpagerecord;

import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.pub.Content;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.doc.BeHospitalizedDoc;
import com.lantone.qc.pub.model.entity.Diag;
import com.lantone.qc.pub.util.ListUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

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

/**
 * @ClassName : FIRP0178
 * @Description :出院其他诊断不完全
 * @Author : 胡敬
 * @Date: 2020-03-16 14:01
 */
@Component
public class FIRP0178 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getFirstPageRecordDoc() != null && inputInfo.getFirstPageRecordDoc().getStructureMap() != null
                && inputInfo.getBeHospitalizedDoc() != null) {
            Map<String, Object> firstpageStructureMap = inputInfo.getFirstPageRecordDoc().getStructureExtMap();
            List<Map<String, String>> dischargeDiag = (List) firstpageStructureMap.get(Content.dischargeDiag);
            if (ListUtil.isEmpty(dischargeDiag)){
                return;
            }

            BeHospitalizedDoc beHospitalizedDoc = inputInfo.getBeHospitalizedDoc();
            List<String> beHospitalDiag = new ArrayList<>();//收集入院记录中初步诊断、修正诊断、其他诊断所有诊断
            if (beHospitalizedDoc.getInitialDiagLabel() != null) {
                putDiagToList(beHospitalizedDoc.getInitialDiagLabel().getDiags(),beHospitalDiag); //初步诊断
            }
            if (beHospitalizedDoc.getRevisedDiagLabel() != null){
                putDiagToList(beHospitalizedDoc.getRevisedDiagLabel().getDiags(),beHospitalDiag); //修正诊断
            }
            if (beHospitalizedDoc.getSuppleDiagLabel() != null){
                putDiagToList(beHospitalizedDoc.getSuppleDiagLabel().getDiags(), beHospitalDiag); //其他诊断
            }

            if (beHospitalDiag.size() > 0 && dischargeDiag.size() < 2) {
                status.set("-1");
                return;
            }

            List<String> firstpageDiag = new ArrayList<>();
            for (int i = 1; i < dischargeDiag.size(); i++) {
                String diagnoseName = dischargeDiag.get(i).get(Content.diagnoseName);
                if (StringUtil.isBlank(diagnoseName)) {
                    continue;
                }
                firstpageDiag.add(diagnoseName);
            }

            if (!ListUtil.equals(firstpageDiag,beHospitalDiag)){
                status.set("-1");
            }
        }
    }

    private void putDiagToList(List<Diag> diagList, List<String> beHospitalDiag){
        for (Diag diag:diagList) {
            beHospitalDiag.add(diag.getName());
        }
    }
}