package thyyxxk.webserver.entity.zhuyuanyisheng.emrcontrolrule;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.Data;
import thyyxxk.webserver.entity.zhuyuanyisheng.emr.EmrAuditDetail;
import thyyxxk.webserver.utils.ListUtil;

import java.math.BigDecimal;
import java.util.*;

@Data
public class QualityControlAnalysisOfMedicalRecords {
    private String patNo;
    private Integer times;
    private String inpatientNo;
    private Integer admissTimes;
    private String name;
    private String deptName;

    private String age;
    private String sex;

    /**
     * 管床医生
     */
    private String tubeBedDoctorName;

    /**
     * 主治医生
     */
    private String consultPhysicianName;

    /**
     * 主任医生
     */
    private String deptDirectorName;

    /**
     * 入院诊断
     */
    private String admissDiag;

    /**
     * 出院诊断
     */
    private String disDiag;

    /**
     * 出院科室
     */
    private String disDeptName;

    /**
     * 入院科室名称
     */
    private String admissDeptName;

    /**
     * 门急诊诊断
     */
    private String clinicDiagStr;

    private Date admissDate;
    private String diagnosisOfAdmission;
    private String medicalHistoryTyping;
    private String promptStatements;
    private Date disDate;

    private String message;

    /**
     * 转科信息，用 -》 分割
     */
    private String zkListName;

    /**
     * 病历分型
     */
    private String typing;

    /**
     * 住院天数
     */
    private String admissDay;

    /**
     * 住院费用
     */
    private BigDecimal totalCharge;

    /**
     * 是否死亡
     */
    private Integer death;

    /**
     * 法定传染病
     */
    private String fdcrb;

    /**
     * 病理诊断
     */
    private String pathologicDiagStr;

    /**
     * 手术编码
     */
    private String ssbm;

    /**
     * 手术名称
     */
    private String ssmc;

    /**
     * 术者
     */
    private String operatorName;

    /**
     * 手术级别
     */
    private Integer level;

    /**
     * 切口
     */
    private String cut;

    /**
     * 愈合等级
     */
    private String heal;

    /**
     * 麻醉
     */
    private String anaesthesiaName;

    /**
     * 抢救次数
     */
    private String qjcs;

    /**
     * 过敏药物
     */
    private String allergicMedicine;

    /**
     * 抗生素
     */
    private String kss;

    /**
     * 细菌培养
     */
    private String xjpy;


    private Map<String, String> mapMessage = new HashMap<>();

    /**
     * 轻
     */
    private Integer light = 0;

    /**
     * 中
     */
    private Integer middle = 0;

    /**
     * 重
     */
    private Integer heavy = 0;


    public void setMapMessage(String name, String message) {
        String tempMsg = this.mapMessage.get(name);
        if (tempMsg != null) {
            tempMsg += message;
            this.mapMessage.replace(name, tempMsg);
        } else {
            this.mapMessage.put(name, message);
        }
    }

    private Map<String, String> qualityControlOpinions = new HashMap<>();

    public void setQualityControlOpinions(EmrAuditDetail auditDetail) {
        String key = auditDetail.getEmrName();
        String template = "{}({})。缺陷数【{}】，【{}】，审批人：【{}】。";
        String str = StrUtil.format(template,
                auditDetail.getName(),
                auditDetail.getRemark(),
                auditDetail.getNumberOfDefects(),
                gradeName(auditDetail.getScoringCriteria()),
                auditDetail.getApproverName());
        String temp = qualityControlOpinions.get(auditDetail.getEmrName());
        if (temp != null) {
            temp += str;
            qualityControlOpinions.replace(key, temp);
        } else {
            qualityControlOpinions.put(key, str);
        }
    }

    public String getOpinion() {
        if (qualityControlOpinions.isEmpty()) {
            return "";
        }
        return JSON.toJSONString(qualityControlOpinions, SerializerFeature.PrettyFormat);
    }


    public String getMessage() {
        if (!mapMessage.isEmpty()) {
            return JSON.toJSONString(mapMessage);
        }
        return null;
    }

    public static String gradeName(int score) {
        switch (score) {
            case 1:
                return "轻";
            case 2:
                return "中";
            case 3:
                return "重";
        }
        return "";
    }

    /**
     * 设置患者的病历等级
     *
     * @param score 1-轻度 2-中度 3-重度
     * @param count 数量
     */
    public void setScore(int score, int count) {
        switch (score) {
            case 1:
                light += count;
                break;
            case 2:
                middle += count;
                break;
            case 3:
                heavy += count;
                break;
        }
    }


    /**
     * 根据时限缺陷数的数量来执行
     *
     * @param numberOfDefects 数量
     */
    public void setAccordingToTheNumberOfDefects(int numberOfDefects) {
        if (numberOfDefects > 1) {
            setScore(2, 1);
        } else if (numberOfDefects > 0) {
            setScore(1, 1);
        }
    }

    public String getSexName() {
        if (sex == null) {
            return "";
        }
        if (sex.equals("1")) {
            return "男";
        }
        if (sex.equals("2")) {
            return "女";
        }
        return "";
    }

    public String getGradingScale() {
        int b = light / 6;

        if (b > 0) {
            light = 0;
            middle += 1;
        }

        if (heavy >= 1) {
            return "Ⅴ";
        }
        if (middle >= 4) {
            return "Ⅳ";
        }
        if (middle == 2 || middle == 3) {
            return "Ⅲ";
        }
        if (middle == 1) {
            return "Ⅱ";
        }

        return "Ⅰ";
    }

    public String getDeathName() {
        if (death == null) return "否";
        if (death > 0) {
            return "是";
        }
        return "否";
    }

}
