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

import com.lantone.qc.dbanaly.util.KernelConstants;
import com.lantone.qc.dbanaly.util.SpecialStorageUtil;
import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.entity.Diag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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


/**
 * @ClassName : BEH0012
 * @Description :  入院诊断名称不标准
 * 取redis中hospital_concept_diag_map为key的map
 * @Author : 楼辉荣
 * @Date: 2020-03-06 17:28
 */
@Component
public class BEH0012 extends QCCatalogue {
    private static String[] strings = { "2型糖尿病" };
    @Autowired
    private SpecialStorageUtil specialStorageUtil;

    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getBeHospitalizedDoc() == null) {
            status.set("0");
            return;
        }
        List<String> diags_out = new ArrayList<>();
        Map<String, String> hostpital_standDiag = specialStorageUtil.getJsonStringValue(KernelConstants.HOSPITAL_DIAG_MAP);
        if (hostpital_standDiag == null) {
            status.set("0");
            return;
        }
        List<Diag> diags = null;
        if (inputInfo.getBeHospitalizedDoc().getInitialDiagLabel() != null) {
            diags = inputInfo.getBeHospitalizedDoc().getInitialDiagLabel().getDiags();
        }
        if (diags != null && diags.size() > 0) {
            for (Diag diag : diags) {
                List<String> disList = Arrays.asList(strings);
                for (String diagSplit : diag.getHospitalDiagName().split("、")) {
                    if ("ACT".equals(diagSplit)) {
                        continue;
                    }
                    String hospitalDiagName = diagSplit.trim().replace("待查", "").replace("?", "").replace("？", "");
                    if (hospitalDiagName.contains("伴")) {
                        hospitalDiagName = hospitalDiagName.substring(0, hospitalDiagName.indexOf("伴"));
                    }
                    if (hospitalDiagName.length() > 15) {
                        continue;
                    }
                    if (!disList.contains(hospitalDiagName)) {
                        hospitalDiagName = hospitalDiagName.replaceAll("^[0-9]", "");
                    }
                    if (!hostpital_standDiag.containsKey(hospitalDiagName)) {
                        diags_out.add(hospitalDiagName);
                    }
                }
            }
            if (diags_out.size() > 0) {
                info.set(diags_out.toString().replace("[", "").replace("]", ""));
            } else {
                status.set("0");
            }
        } else {
            status.set("0");
        }


    }
}