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

import com.google.common.collect.Lists;
import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.kernel.util.RegularUtil;
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.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

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

/**
 * @ClassName : FIRP02992
 * @Description : 药物过敏与既往史不一致（结构化数据）
 * @Author : rengb
 * @Date: 2020-03-18 14:07
 */
@Component
public class FIRP02992 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getFirstPageRecordDoc() != null && inputInfo.getBeHospitalizedDoc() != null) {
            Map<String, String> fpStructureMap = inputInfo.getFirstPageRecordDoc().getStructureMap();
            if (StringUtil.isBlank(fpStructureMap.get(Content.drugAllergy))) {
                return;
            }
            String fpAllergyDrug = fpStructureMap.get(Content.allergyDrug);
            fpAllergyDrug = StringUtil.isBlank(fpAllergyDrug) ? "" : fpAllergyDrug;
            fpAllergyDrug = RegularUtil.ClearBracket(fpAllergyDrug); // 括号内容不匹配

            Map<String, String> beStructureMap = inputInfo.getBeHospitalizedDoc().getStructureMap();
            String beDrugAllergy = beStructureMap.get("过敏史");
            beDrugAllergy = StringUtil.isBlank(beDrugAllergy) ? "" : beDrugAllergy;
            beDrugAllergy = RegularUtil.ClearBracket(beDrugAllergy); // 括号内容不匹配

            if (StringUtil.isBlank(fpAllergyDrug) || StringUtil.isBlank(beDrugAllergy)) {
                return ;
            }
            // “未发现” == “否认”`
            if (("未发现".equals(fpAllergyDrug) && "否认".equals(beDrugAllergy))
                    || ("否认".equals(fpAllergyDrug) && "未发现".equals(beDrugAllergy))) {
                return ;
            }
            if (beDrugAllergy.contains(fpAllergyDrug)) {
                return ;
            }
            String regx = "；|;|,|，|、| ";
            List<String> fpList = Lists.newArrayList(fpAllergyDrug.split(regx));
            fpList = fpList.stream().filter(x -> StringUtils.isNotBlank(x) && !x.trim().equals("其它")).collect(Collectors.toList());
            // 全部包含
            for (String s : fpList) {
                if (!beDrugAllergy.contains(s)) {
                    status.set("-1");
                    return ;
                }
            }
        }
    }

}