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

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.Address;
import com.lantone.qc.pub.util.ListUtil;
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.regex.Pattern;

/**
 * @Description: 出生地未描述
 * @author: rengb
 * @time: 2020/3/10 14:02
 */
@Component
public class BEH0042 extends QCCatalogue {

    @Override
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if(inputInfo.getBeHospitalizedDoc() == null){
            status.set("0");
            return;
        }
        Map<String, String> structureMap = inputInfo.getBeHospitalizedDoc().getStructureMap();
        if (StringUtils.isNotEmpty(structureMap.get("出生地"))) {
            status.set("0");
            return;
        }

        String text = inputInfo.getBeHospitalizedDoc().getPersonalLabel().getText();
        if (StringUtil.isBlank(text)) {
            return;
        }
        List<Address> addresses = inputInfo.getBeHospitalizedDoc().getPersonalLabel().getAddresses();
        if (ListUtil.isNotEmpty(addresses)) {
            long count = addresses.stream().filter(i -> {
                boolean flag = false;
                if (i != null && StringUtil.isNotBlank(i.getName())) {
                    if (i.getName().indexOf("出生") > -1) {
                        flag = true;
                    } else {
                        Pattern pattern = Pattern.compile("[\\s\\S]*(出生|生长)[\\s\\S]{0,5}" + i.getName() + "[\\s\\S]*");
                        flag = pattern.matcher(text).matches();
                    }
                }
                return flag;
            }).count();
            if (count > 0) {
                status.set("0");
            }
        }
        if (text.contains("出生")) {
            status.set("0");
        }
    }

}