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

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;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName : FIRP0248
 * @Description : 抢救次数填写错误
 * @Author : 楼辉荣
 * @Date: 2020-03-06 17:28ss
 */
@Component
public class FIRP0248 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getFirstPageRecordDoc() != null) {
            Map<String, String> firstpageStructureMap = inputInfo.getFirstPageRecordDoc().getStructureMap();
            int rescueNum = inputInfo.getRescueDocs().size();
            boolean match = false;

            String fpRescueNumStr = firstpageStructureMap.get("病人抢救次数");
            if (StringUtil.isBlank(fpRescueNumStr) || "-".equals(fpRescueNumStr)) {
                return;
            }
            if (StringUtil.isNotBlank(fpRescueNumStr) && CatalogueUtil.numbersOnly(fpRescueNumStr)) {
                int fpRescueNum = Integer.parseInt(fpRescueNumStr);
                if (fpRescueNum == rescueNum) {
                    match = true;
                }
            }
            String name = firstpageStructureMap.get(Content.rescue_condition);
            if (!CatalogueUtil.isEmpty(name)) {
                String[] split = name.split("\\s+");
                Pattern compile = Pattern.compile("(?<=抢救).*(?=次)");
                Matcher matcher = compile.matcher(split[0]);
                while (matcher.find()) {
                    try {
                        String number = matcher.group(0);
                        int num = Integer.parseInt(number);
                        if (num == rescueNum) {
                            match = true;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            if (!match) {
                status.set("-1");
            }
        }
    }
}