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

import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.dbanaly.util.KernelConstants;
import com.lantone.qc.dbanaly.util.SpecialStorageUtil;
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.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
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 SpecialStorageUtil specialStorageUtil;

    @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) {
            DiagLabel leaveDiagLabel = leaveHospitalDoc.getLeaveDiagLabel();
            if (leaveDiagLabel != null) {
                List<Diag> diags = leaveDiagLabel.getDiags();
                Map<String, Map<String, String>> hospitalDiagMap = specialStorageUtil.getJsonStringValue(KernelConstants.HOSPITAL_DIAG_MAP);
                if (ListUtil.isNotEmpty(diags) && hospitalDiagMap != null) {
                    List<String> diagnames = new ArrayList<>();
                    for (Diag dg : diags) {
                        String hospitalDiagName = dg.getHospitalDiagName();
                        if (hospitalDiagName.contains("伴")) {
                            hospitalDiagName = hospitalDiagName.substring(0, hospitalDiagName.indexOf("伴"));
                        }
                        if ("其他的".equals(hospitalDiagName) || hospitalDiagName.length() > 15) {
                            continue;
                        }
                        if (hospitalDiagMap.get(hospitalDiagName) == null) {
                            diagnames.add(hospitalDiagName);
                        }
                    }
                    if (diagnames.size() > 0) {
                        info.set(StringUtils.join(diagnames.toArray(), ","));
                        status.set("-1");
                    }
                }
            }
        }
    }
}