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

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.model.doc.ThreeLevelWardDoc;
import com.lantone.qc.pub.util.ListUtil;
import com.lantone.qc.pub.util.SpringContextUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : MEDI0667
 * @Description :非执业医师书写的病历无执业医师审核签名
 * @Author : 胡敬
 * @Date: 2020-03-30 17:27
 */
@Component
public class THR02150 extends QCCatalogue {
    @Override
    protected void start(InputInfo inputInfo, OutputInfo outputInfo) throws ParseException {
        /**
         * 查房记录里【审核人】在bas_doctor_info表中的occup=1(职业医师资格0：无，1：有)，审核人的名字里可能包含字母A,F，现在的处理方式是: 先匹配全部，如果无医生信息，字母先替换成空
         */
        status.set("0");
        if (ListUtil.isEmpty(inputInfo.getThreeLevelWardDocs())) {
            return;
        }
        SpecialStorageUtil specialStorageUtil = SpringContextUtil.getBean("specialStorageUtil");
        Map<String, Object> surgeon = specialStorageUtil.getJsonStringValue(KernelConstants.HOSPITAL_DOCTOR_MAP);
        if (surgeon == null) {
            return;
        }
        List<ThreeLevelWardDoc> threeLevelWardDocList = inputInfo.getThreeLevelWardDocs().get(0).getAllDoctorWradDocs();
        for (ThreeLevelWardDoc bean : threeLevelWardDocList) {
            Map<String, String> structureMap = bean.getStructureMap();
            String doctorName = structureMap.get("审核人");
            if (StringUtil.isNotBlank(doctorName)) {
                Map<String, String> doctor = (Map) surgeon.get(doctorName);
                if (doctor == null) {
                    doctorName = doctorName.replaceAll("[a-z|A-Z]$", ""); // 将字母结尾的替换成""
                    doctor = (Map) surgeon.get(doctorName);
                }
                String occup = doctor.get("occup");
                if (StringUtils.isNotEmpty(occup) && "0".equals(occup)) {
                    status.set("-1");
                    return;
                }
            }
        }
    }
}