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.Content;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.Map;


/**
 * @ClassName : BEH0414
 * @Description :  入院记录年龄填写错误
 * @Author : 楼辉荣
 * @Date: 2020-03-06 17:28
 */
@Component
public class BEH0414 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getFirstPageRecordDoc() != null && inputInfo.getFirstPageRecordDoc().getStructureMap() != null
                && inputInfo.getBeHospitalizedDoc() != null && inputInfo.getBeHospitalizedDoc().getStructureMap() != null) {
            Map<String, String> firstpageStructureMap = inputInfo.getFirstPageRecordDoc().getStructureMap();
            Map<String, String> beHospitalStructureMap = inputInfo.getBeHospitalizedDoc().getStructureMap();
            String firstAdmissionAge = firstpageStructureMap.get(Content.age);
            /* 根据病案首页新生儿出生月数和新生儿出生天数拼接年龄 */
            String newbornAgeMonths = firstpageStructureMap.get(Content.newbornAgeMonths);
            String newbornAgeDays = firstpageStructureMap.get(Content.newbornAgeDays);
            String newbornAgeStr = "";
            int newbornAge = 0;
            if (StringUtil.isNotBlank(newbornAgeMonths) && StringUtil.isNotBlank(newbornAgeDays)){
                newbornAgeStr = newbornAgeMonths + newbornAgeDays;
            }
            if (StringUtil.isNotBlank(newbornAgeStr)){
                newbornAge = removalUnit(newbornAgeStr);
            }
            String admissionAge = beHospitalStructureMap.get(Content.age);
            if (StringUtil.isBlank(firstAdmissionAge) || StringUtil.isBlank(admissionAge)) {
                status.set("0");
                return;
            }
            int firstAdmissionAgeNum = removalUnit(firstAdmissionAge);
            int admissionAgeNum = removalUnit(admissionAge);

            if (firstAdmissionAgeNum != admissionAgeNum && newbornAge != admissionAgeNum) {
                status.set("-1");
            }
        }
    }

    private int removalUnit(String admissionAge) {
        int age = 0;
        if (StringUtil.isNotBlank(admissionAge) && admissionAge.contains("岁")) {
            admissionAge = admissionAge.replaceAll("[岁天]", "");
        }
        admissionAge = admissionAge.replaceAll("[^0-9]","");
        if (CatalogueUtil.numbersOnly(admissionAge)) {
            age = Integer.parseInt(admissionAge);
        }
        return age;
    }
}