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.label.MenstrualLabel;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @ClassName : BEH0059
 * @Description : 月经史未填写
 * @Author : 胡敬
 * @Date: 2020-03-10 10:10
 */
@Component
public class BEH0059 extends QCCatalogue {
    @Override
    protected void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getBeHospitalizedDoc() != null) {
            if (inputInfo.getBeHospitalizedDoc().getStructureMap() == null
                    || inputInfo.getBeHospitalizedDoc().getStructureMap().get("性别") == null
                    || inputInfo.getBeHospitalizedDoc().getStructureMap().get("性别").contains("男")) {
                status.set("0"); //如果性别是男,就不报错
            } else {
                String concatMenstrualText = concatMenstrualText(inputInfo);
                if (StringUtil.isNotBlank(concatMenstrualText)) {
                    status.set("0");
                    return;
                }
                MenstrualLabel menstrualLabel = inputInfo.getBeHospitalizedDoc().getMenstrualLabel();
                if (menstrualLabel == null) {
                    return;
                }
                String menstrualText = StringUtil.removeBlank(menstrualLabel.getText()).replaceAll("[月经史|：|:|null]", "");
                if (StringUtil.isBlank(menstrualText)) {
                    return;
                }
                boolean containChinese = CatalogueUtil.isContainChinese(menstrualText);
                if (containChinese) {
                    status.set("0"); //如果性别是女,不为空就不报错
                }
            }
        }else {
            status.set("0");
            return;
        }
    }

    private String concatMenstrualText(InputInfo inputInfo) {
        Map<String, String> beHospitalizedStructureMap = inputInfo.getBeHospitalizedDoc().getStructureMap();
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> bhMap : beHospitalizedStructureMap.entrySet()) {
            if (bhMap.getKey().contains("月经") || bhMap.getKey().contains("经期") || bhMap.getKey().contains("绝经")) {
                if (StringUtil.isNotBlank(bhMap.getValue())) {
                    sb.append(bhMap.getValue());
                }
            }
        }
        return sb.toString();
    }
}