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

import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.kernel.util.KernelConstants;
import com.lantone.qc.kernel.util.RedisUtil;
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 com.lantone.qc.pub.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @Description: 出院诊断不标准
 * @author: rengb
 * @time: 2020/3/10 13:53
 */
@Component
public class LEA0147 extends QCCatalogue {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        LeaveHospitalDoc leaveHospitalDoc = inputInfo.getLeaveHospitalDoc();
        if (leaveHospitalDoc == null) {
            status.set("0");
            return;
        }
        DeathRecordDoc deathRecordDoc = inputInfo.getDeathRecordDoc();
        if (deathRecordDoc == null || deathRecordDoc.getText() == null) {
            DiagLabel leaveDiagLabel = leaveHospitalDoc.getLeaveDiagLabel();
            if (leaveDiagLabel != null) {
                List<Diag> diags = leaveDiagLabel.getDiags();
                Map<String, Map<String, String>> hospitalDiagMap = redisUtil.getJsonStringValue(KernelConstants.HOSPITAL_DIAG_MAP);
                if (ListUtil.isNotEmpty(diags) && hospitalDiagMap != null) {
                    long count = diags.stream().filter(i -> {
                        if (i != null && StringUtil.isNotBlank(i.getHospitalDiagName())) {
                            return hospitalDiagMap.get(i.getHospitalDiagName()) == null;
                        }
                        return false;
                    }).count();
                    if (count > 0) {
                        status.set("-1");
                    }
                }
            }
        }
    }
}