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

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : FIRP0257
 * @Description : 门诊诊断与住院诊断符合情况填写错误
 * @Author : 楼辉荣
 * @Date: 2020-03-06 17:28
 */
@Component
public class FIRP0257 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getFirstPageRecordDoc() != null && inputInfo.getFirstPageRecordDoc().getStructureExtMap() != null
                && inputInfo.getLeaveHospitalDoc() != null && inputInfo.getLeaveHospitalDoc().getLeaveDiagLabel() != null) {
            Map<String, Object> firstpageStructureMap = inputInfo.getFirstPageRecordDoc().getStructureExtMap();
            List<Map<String, String>> outpatientEmergencyDiagList = (List) firstpageStructureMap.get(Content.outpatientEmergencyDiag);
            List<Diag> dischargeDiags = inputInfo.getLeaveHospitalDoc().getLeaveDiagLabel().getDiags();
            if (ListUtil.isEmpty(outpatientEmergencyDiagList) || ListUtil.isEmpty(dischargeDiags)) {
                return;
            }
            List<String> firstpageDischargeDiag = getFirstpageDischargeDiag(outpatientEmergencyDiagList);
            List<String> dischargeDiag = getDischargeDiag(dischargeDiags);

            String situation = ""; //符合情况
            if (dischargeDiag.containsAll(firstpageDischargeDiag)){
                situation = "符合";
            } else {
                situation = "不符合";
            }
            Object o_iObj = firstpageStructureMap.get(Content.o_i);
            if (o_iObj != null){
                String o_i = o_iObj.toString();
                if (StringUtil.isNotBlank(o_i) && !CatalogueUtil.compareToken(o_i,situation)) {
                    status.set("-1");
                }
            }
        }
    }

    private List<String> getDischargeDiag(List<Diag> dischargeDiags) {
        List<String> dischargeDiagList = new ArrayList<>();
        for (Diag diag : dischargeDiags) {
            dischargeDiagList.add(diag.getHospitalDiagName());
        }
        return dischargeDiagList;
    }

    private List<String> getFirstpageDischargeDiag(List<Map<String, String>> outpatientEmergencyDiagList) {
        List<String> firstpageDischargeDiag = new ArrayList<>();
        for (Map<String, String> outpatientEmergencyDiag : outpatientEmergencyDiagList) {
            String diag = outpatientEmergencyDiag.get(Content.diagnoseName);
            if (StringUtil.isNotBlank(diag)) {
                firstpageDischargeDiag.add(diag);
            }
        }
        return firstpageDischargeDiag;
    }
}