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

import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.kernel.util.CatalogueUtil;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.entity.Diag;
import com.lantone.qc.pub.model.label.DiagLabel;
import com.lantone.qc.pub.model.label.PastLabel;
import com.lantone.qc.pub.model.label.PresentLabel;
import com.lantone.qc.pub.util.ListUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName : BEH0738
 * @Description: 患者病情前后描述不一致
 * @author: 胡敬
 * @time: 2020-05-08 16:45
 */
@Component
public class BEH02887 extends QCCatalogue {

    @Override
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getBeHospitalizedDoc() == null) {
            return;
        }
        List<Diag> presentDiags = null;
        List<Diag> pastDiags = null;
        List<Diag> initialDiags = null;
        PresentLabel presentLabel = inputInfo.getBeHospitalizedDoc().getPresentLabel();
        PastLabel pastLabel = inputInfo.getBeHospitalizedDoc().getPastLabel();
        DiagLabel initialDiagLabel = inputInfo.getBeHospitalizedDoc().getInitialDiagLabel();
        if (presentLabel != null) {
            presentDiags = presentLabel.getDiags();
        }
        if (pastLabel != null) {
            pastDiags = pastLabel.getDiags();
        }
        if (initialDiagLabel != null) {
            initialDiags = initialDiagLabel.getDiags();
        }

        if (presentDiags != null && pastDiags != null){
            if (checkDiagDiff(presentDiags,pastDiags)){
                status.set("-1");
                return;
            }
        }

        if (presentDiags != null && initialDiags != null){
            if (checkDiagDiff(presentDiags,initialDiags)){
                status.set("-1");
                return;
            }
        }

        if (pastDiags != null && initialDiags != null){
            if (checkDiagDiff(pastDiags,initialDiags)){
                status.set("-1");
            }
        }
    }

    /**
     * 检查两个Diag列表是否前后描述不一致
     * @param firstDiags
     * @param secondDiags
     * @return
     */
    private boolean checkDiagDiff(List<Diag> firstDiags, List<Diag> secondDiags) {
        String firstDiagName = null, secondDiagName = null;
        for (Diag firstDiag : firstDiags) {
            firstDiagName = firstDiag.getHospitalDiagName();
            if (StringUtil.isBlank(firstDiagName)) {
                continue;
            }
            for (Diag secondDiag : secondDiags) {
                secondDiagName = secondDiag.getHospitalDiagName();
                if (StringUtil.isBlank(secondDiagName)) {
                    continue;
                }
                if (firstDiagName.equals(secondDiagName)) {
                    boolean checkNeg = checkNeg(firstDiag, secondDiag);
                    if (!checkNeg) {
                        //如果两个Diag一个为否定，一个不为否定，返回true
                        return true;
                    }
                }
            }
        }
        return false;
    }

    private boolean checkNeg(Diag firstDiag, Diag secondDiag) {
        if (firstDiag.getNegative() == null && secondDiag.getNegative() == null) {
            return true;
        } else {
            return firstDiag.getNegative() != null && secondDiag.getNegative() != null;
        }
    }


}