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

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.DeathRecordDoc;
import com.lantone.qc.pub.model.doc.LeaveHospitalDoc;
import com.lantone.qc.pub.model.entity.Diag;
import com.lantone.qc.pub.model.label.DiagLabel;
import com.lantone.qc.pub.util.ListUtil;
import org.springframework.stereotype.Component;

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

/**
 * @Description: 出院记录出院诊断与首页出院诊断不一致
 * @author: 胡敬
 * @time: 2020/3/27 15:48
 */
@Component
public class LEA0514 extends QCCatalogue {
    @Override
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        LeaveHospitalDoc leaveHospitalDoc = inputInfo.getLeaveHospitalDoc();
        if (leaveHospitalDoc == null) {
            return;
        }
        DeathRecordDoc deathRecordDoc = inputInfo.getDeathRecordDoc();
        if (deathRecordDoc == null || deathRecordDoc.getText() == null) {
            if (inputInfo.getFirstPageRecordDoc() != null) {
                DiagLabel leaveDiagLabel = leaveHospitalDoc.getLeaveDiagLabel();
                Map<String, Object> firstPageRecordStructureExtMap = inputInfo.getFirstPageRecordDoc().getStructureExtMap();
                List<Map<String, String>> dischargeDiagStructure = (List<Map<String, String>>) firstPageRecordStructureExtMap.get(Content.dischargeDiag);
                if (ListUtil.isEmpty(dischargeDiagStructure)) {
                    return;
                }
                //首页出院诊断
                List<String> dischargeDiags = new ArrayList<>();
                for (Map<String, String> dischargeDiagMap : dischargeDiagStructure) {
                    String diagnoseName = dischargeDiagMap.get(Content.diagnoseName);
                    if (CatalogueUtil.isEmpty(diagnoseName)) {
                        continue;
                    }
                    dischargeDiags.add(diagnoseName);
                }
                if (leaveDiagLabel != null) {
                    //出院记录出院诊断
                    List<Diag> leaveDiags = leaveDiagLabel.getDiags();
                    List<String> leaveDiagStrList = getDiagList(leaveDiags);
                    if (!ListUtil.equals(leaveDiagStrList, dischargeDiags)) {
                        status.set("-1");
                    }
                }
            }
        }
    }

    private List<String> getDiagList(List<Diag> diags) {
        List<String> diagList = new ArrayList<>();
        for (Diag diag : diags) {
            if (diag.getHospitalDiagName() != null) {
                diagList.add(diag.getHospitalDiagName());
            }
        }
        return diagList;
    }
}