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

import com.lantone.qc.dbanaly.util.KernelConstants;
import com.lantone.qc.dbanaly.util.SpecialStorageUtil;
import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.util.SpringContextUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @ClassName : FIRC0082
 * @Description : 首次病程由非执业医师书写
 * @Author : rengb
 * @Date: 2020-03-06 17:28
 */
@Component
public class FIRC0082 extends QCCatalogue {
    @Autowired
    private SpecialStorageUtil specialStorageUtil;

    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getFirstCourseRecordDoc() == null) {
            status.set("0");
            return;
        }

        String doctorName = inputInfo.getFirstCourseRecordDoc().getStructureMap().get("医师签名");
        if (StringUtil.isBlank(doctorName)) {
            status.set("0");
            return;
        }

        String occup = getOccup(doctorName);
        if (StringUtils.isNotEmpty(occup) && "1".equals(occup)) {
            status.set("0");
            return;
        }

        if (doctorName.length() > 1) {
            doctorName = doctorName.substring(0, doctorName.length() - 1);
            occup = getOccup(doctorName);
            if (StringUtils.isNotEmpty(occup) && "1".equals(occup)) {
                status.set("0");
                return;
            }
        }
    }

    private String getOccup(String doctorName) {
        String occup = "";
        if (StringUtil.isBlank(doctorName)) {
            return occup;
        }
        SpecialStorageUtil specialStorageUtil = SpringContextUtil.getBean("specialStorageUtil");
        Map<String, Map<String, String>> surgeon = specialStorageUtil.getJsonStringValue(KernelConstants.HOSPITAL_DOCTOR_MAP);
        if (surgeon != null) {
            Map<String, String> doctor = (Map) surgeon.get(doctorName);
            if (doctor != null) {
                occup = doctor.get("occup");
            }
        }
        return occup;
    }

}