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.model.doc.ClinicalBloodDoc;
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;

/**
 * @ClassName : FIRP0190
 * @Description :Rh填写错误
 * @Author : 胡敬
 * @Date: 2020-03-18 13:35
 */
@Component
public class FIRP0190 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getFirstPageRecordDoc() == null || inputInfo.getFirstPageRecordDoc().getStructureMap() == null
                || inputInfo.getClinicalBloodDocs().size() == 0) {
            return;
        }
        Map<String, String> firstpageStructureMap = inputInfo.getFirstPageRecordDoc().getStructureMap();
        List<ClinicalBloodDoc> clinicalBloodDocList = inputInfo.getClinicalBloodDocs();
        String rhFactor = firstpageStructureMap.get(Content.rhFactor);
        if (CatalogueUtil.isEmpty(rhFactor)) {
            return;
        }
        int matchSum = 0, noRhSum = 0;
        for (ClinicalBloodDoc clinicalBloodDoc : clinicalBloodDocList) {
            String bloodType = clinicalBloodDoc.getStructureMap().get("输注种类、血型、数量");
            if (StringUtil.isBlank(bloodType)) {
                // 台州无“输注种类、血型、数量”，匹配“Rh血型”
                bloodType = clinicalBloodDoc.getStructureMap().get("Rh血型");
            }
            if (StringUtils.isEmpty(bloodType)) {
                noRhSum++;
                continue;
            }
            if (!bloodType.contains("阴") && !bloodType.contains("阳")) {
                noRhSum++;
            }
            if (bloodType.contains("白蛋白") || bloodType.contains("凝血酶原复合物")) {
                continue;
            }
            bloodType = bloodType.toUpperCase();
            String rh = Content.rhFactor.toUpperCase();
            if (bloodType.contains(rh) ||
                    bloodType.contains(CatalogueUtil.removeSpecialChar(rhFactor))) {
                matchSum++;
                return ;
            }
        }
        /* 如果所有输血记录都没有rh记录（阴、阳）,则不报错 */
        if (noRhSum == clinicalBloodDocList.size()){
            return;
        }
        if (matchSum == 0) {
            status.set("-1");
        }
    }
}