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

import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.kernel.util.KernelConstants;
import com.lantone.qc.kernel.util.RedisUtil;
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.Clinical;
import com.lantone.qc.pub.model.entity.Diag;
import com.lantone.qc.pub.model.label.ChiefLabel;
import com.lantone.qc.pub.model.label.DiagLabel;
import com.lantone.qc.pub.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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


/**
 * @ClassName : BEH0002
 * @Description : 主诊断与主诉不符
 * @Author : 楼辉荣
 * @Date: 2020-03-06 17:28
 */
@Component
public class BEH0002 extends QCCatalogue {
    @Autowired
    private RedisUtil redisUtil;

    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        BeHospitalizedDoc beHospitalizedDoc = inputInfo.getBeHospitalizedDoc();
        if (beHospitalizedDoc == null) {
            status.set("0");
            return;
        }
        boolean bool = false;
        ChiefLabel chiefLabel = beHospitalizedDoc.getChiefLabel();
        DiagLabel initialDiagLabel = beHospitalizedDoc.getInitialDiagLabel();
        if (chiefLabel == null || initialDiagLabel == null) {
            status.set("0");
            return;
        }
        List<Diag> chiefDiags = chiefLabel.getDiags();
        List<Diag> diags = initialDiagLabel.getDiags();
        if (chiefDiags.size() > 0) {
            for (Diag diag : chiefDiags) {
                String hospitalDiagName = diag.getHospitalDiagName();
                if (hospitalDiagName.contains("术后") || hospitalDiagName.contains("癌") ||
                        hospitalDiagName.contains("瘤") || hospitalDiagName.contains("复诊")) {
                    status.set("0");
                    break;
                }
                {
                    bool = true;
                }
            }
        } else {
            bool = true;
        }
        if (bool) {
            String firstDiag = diags.get(0).getName();
            String firstHDiag = diags.get(0).getHospitalDiagName();
            String mainDiag = StringUtil.isBlank(firstHDiag) ? firstDiag : firstHDiag;
            List<String> symptoms = null;
            Map<String, Map<String, Object>> diagMap = redisUtil.getJsonStringValue(KernelConstants.CONCEPT_DIAG_PROPERTY_MAP);
            Map<String, Object> map = (Map<String, Object>) diagMap.get(mainDiag);
            if (map != null && map.size() > 0) {
                symptoms = (List<String>) map.get("symptoms");
            }
            List<Clinical> clinicals = inputInfo.getBeHospitalizedDoc().getChiefLabel().getClinicals();
            if (clinicals != null && clinicals.size() > 0) {
                for (Clinical clinical : clinicals) {
                    String clinicalName = clinical.getName();
                    if (symptoms != null && !symptoms.contains(clinicalName)) {
                        if (StringUtils.isEmpty(info.get())) {
                            info.set(clinicalName);
                        } else {
                            info.set(info.get() + "," + clinicalName);
                        }
                    }
                }
            }
            if (StringUtils.isEmpty(info.get())) {
                status.set("0");
            }
        }
    }
}