package thyyxxk.webserver.utils;

import thyyxxk.webserver.entity.api.forpowersi.drgdip.frontsheetqualitycheck.QualityCheckBaseInfo;
import thyyxxk.webserver.entity.api.forpowersi.drgdip.frontsheetqualitycheck.QualityCheckDiseInfo;
import thyyxxk.webserver.entity.api.forpowersi.drgdip.frontsheetqualitycheck.QualityCheckOprtInfo;
import thyyxxk.webserver.entity.casefrontsheet.CaseFrontsheetDisdiag;
import thyyxxk.webserver.entity.casefrontsheet.CaseFrontsheetMain;
import thyyxxk.webserver.entity.casefrontsheet.CaseFrontsheetSurgery;
import thyyxxk.webserver.entity.dictionary.CodeName;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FrontSheetUtil {

    public static void mergeObject(Object origin, Object destination) {
        if (origin == null || destination == null) {
            return;
        }
        Field[] fields = destination.getClass().getDeclaredFields();
        for (Field field : fields) {
            try {
                field.setAccessible(true);
                Object valueD = field.get(origin);
                Object valueO = field.get(destination);
                if (null == valueO) {
                    field.set(destination, valueD);
                }
                field.setAccessible(false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static QualityCheckBaseInfo fillBaseInfoFromSheet(CaseFrontsheetMain sheet, Map<String, CodeName> countriesMap,
                                                             Map<String, CodeName> nationsMap) {
        QualityCheckBaseInfo baseInfo = new QualityCheckBaseInfo();
        baseInfo.setHospitalCode("H32132200561");
        baseInfo.setHospitalName("沭阳铭和医院");
        baseInfo.setCardNumber(sheet.getSocialNo().toUpperCase());
        baseInfo.setPatientNo(sheet.getBah() + "_" + sheet.getAdmissTimes());
        baseInfo.setVisitId(sheet.getBah() + "_" + sheet.getAdmissTimes() + "_1");
        baseInfo.setAdmNo(sheet.getBah());
        baseInfo.setHsptzdCount(sheet.getAdmissTimes());
        baseInfo.setAge(sheet.getAge());
        baseInfo.setName(sheet.getName());
        baseInfo.setSexId(sheet.getSex());
        baseInfo.setSex("1".equals(sheet.getSex()) ? "男" : "女");
        baseInfo.setBirthday(DateUtil.formatDatetime(sheet.getBirthDate(), DateUtil.DATE));
        baseInfo.setIdCard(sheet.getSocialNo().toUpperCase());
        CodeName country = countriesMap.get(sheet.getCountry());
        if (null != country) {
            baseInfo.setCountryId(country.getYbCode());
            baseInfo.setCountry(country.getName());
        }
        CodeName nation = nationsMap.get(sheet.getNation());
        if (null != nation) {
            baseInfo.setNationId(nation.getYbCode());
            baseInfo.setNation(nation.getName());
        }
        // TODO: 2022/12/9 映射职业代码
        baseInfo.setNewbornWeight(sheet.getNewBornWeight());
        baseInfo.setNewbornHsptzdWeight(sheet.getNewBornAdmissWeight());
        baseInfo.setHsptzdWaysId(sheet.getZyAdmissWay());
        baseInfo.setHsptzdWays(filterAdmissWayName(sheet.getZyAdmissWay()));
        baseInfo.setMarriageStatusId(reflectMarriage(sheet.getMarriage()));
        baseInfo.setMarriageStatus(filterMarriageName(sheet.getMarriage()));
        baseInfo.setCompanyName(sheet.getUnitName());
        baseInfo.setCompanyAddress(sheet.getUnitPlace());
        baseInfo.setCompanyTel(sheet.getUnitPhone());
        baseInfo.setCompanyPost(sheet.getUnitZipCode());
        baseInfo.setResidentAddress(sheet.getHkPlaceName());
        baseInfo.setResidentPost(sheet.getHkZipCode());
        baseInfo.setCont(sheet.getContactName());
        baseInfo.setContRelPatient(sheet.getContactRelation());
        baseInfo.setContAddress(sheet.getContactAddrName());
        baseInfo.setContTel(sheet.getContactPhone());
        baseInfo.setAsCard(sheet.getHealthCardNo());
        baseInfo.setHsptzdDate(DateUtil.formatDatetime(sheet.getAdmissDate(), DateUtil.DATE));
        baseInfo.setHsptzdDeptId(sheet.getAdmissDeptCode());
        baseInfo.setHsptzdDept(sheet.getAdmissDept());
        baseInfo.setHsptzdWard(sheet.getAdmissWard());
        baseInfo.setHospDischDate(DateUtil.formatDatetime(sheet.getDismissDate(), DateUtil.DATE));
        baseInfo.setDischDept(sheet.getDismissDept());
        baseInfo.setDischDeptId(sheet.getDismissDeptCode());
        baseInfo.setActualAdmDays(sheet.getAdmissDays());
        baseInfo.setOutemgcDagnsId(sheet.getClinicDiagCode());
        baseInfo.setOutemgcDagns(sheet.getClinicDiagStr());
        baseInfo.setPthlgyDagns(sheet.getPathologicDiagStr());
        baseInfo.setPthlgyId((sheet.getPathologicDiagCode()));
        baseInfo.setPthlgy(sheet.getBlh());
        baseInfo.setPathoDiagnCodeOne(sheet.getPathologicDiagCode());
        baseInfo.setPathoDiagnNameOne(sheet.getPathologicDiagStr());
        baseInfo.setAllergicDrugs(sheet.getAllergicMedicine());
        baseInfo.setRescueNum(sheet.getRescueTimes());
        baseInfo.setRescueSuccNum(sheet.getRescueSuccessTimes());
        baseInfo.setFeeModeId(reflectPayMethod(sheet.getPayMethod()));
        baseInfo.setFeeMode(filterPayMethodName(sheet.getPayMethod()));
        baseInfo.setQualityDate(DateUtil.formatDatetime(sheet.getQualityControlDate(), DateUtil.DATE));
        baseInfo.setTotalFee(sheet.getTotalCost());
        baseInfo.setOutHospId(reflectDismissWay(sheet.getZyDismissWay()));
        baseInfo.setOutHosp(filterDismissWayName(sheet.getZyDismissWay()));
        baseInfo.setSelfFee(sheet.getSelfPay());
        baseInfo.setInteServFee(sheet.getGeneralMedicalServiceFee());
        baseInfo.setInteOperaFee(sheet.getGeneralTreatmentOperatingFee());
        baseInfo.setInteNursFee(sheet.getCareFee());
        baseInfo.setInteOtherFee(sheet.getElseFee());
        baseInfo.setDagnsPathoFee(sheet.getPathologicDiagFee());
        baseInfo.setDagnsLabFee(sheet.getExperimentDiagFee());
        baseInfo.setDagnsImaFee(sheet.getCtDiagFee());
        baseInfo.setDagnsCliFee(sheet.getClinicalDiagFee());
        baseInfo.setTreaNooprtFee(sheet.getNotSurgicalFee());
        baseInfo.setTreaCliFee(sheet.getClinicalPhysicalFee());
        baseInfo.setTreaOprtreatFee(sheet.getSurgicalFee());
        baseInfo.setTreaAnthsaFee(sheet.getAnesthetizeFee());
        baseInfo.setTreaOprtFee(sheet.getSurgeryFee());
        baseInfo.setRecFee(sheet.getRecoverFee());
        baseInfo.setTradiMeditreaFee(sheet.getTcmTreatmentFee());
        baseInfo.setWestDrugFee(sheet.getWestMedicineFee());
        baseInfo.setChDrugFee(DecimalUtil.add(sheet.getChinesePatentMedicineFee(), sheet.getChineseHerbalMedicineFee()));
        baseInfo.setChPatentFee(sheet.getChinesePatentMedicineFee());
        baseInfo.setChHerbalFee(sheet.getChineseHerbalMedicineFee());
        baseInfo.setBlooBloodFee(sheet.getBloodFee());
        baseInfo.setBlooAlbuminFee(sheet.getAlbuminProductsFee());
        baseInfo.setBlooGlobulinFee(sheet.getGlobulinProductsFee());
        baseInfo.setBlooCoagulaFee(sheet.getCoagulationFactorProductsFee());
        baseInfo.setBlooCellFee(sheet.getCytokineProductsFee());
        baseInfo.setComsuChkdispFee(sheet.getCostOfDisposableMedicalMaterialsForExamination());
        baseInfo.setComsuTreadispFee(sheet.getCostOfDisposableMedicalMaterialsForTreatment());
        baseInfo.setComsuOprtdispFee(sheet.getCostOfDisposableMedicalMaterialsForOperation());
        baseInfo.setIsagainHsptzdId(sheet.getAdmissAgainInOneMonth());
        baseInfo.setIsagainHsptzd(filterAdmissAgainName(sheet.getAdmissAgainInOneMonth()));
        baseInfo.setAgainAim(sheet.getAdmissAgainPurpose());
        baseInfo.setInjuPoisExterRsnCode(sheet.getHurtReasonCode());
        baseInfo.setInjuPoisExterRsn(sheet.getHurtReasonName());
        return baseInfo;
    }

    public static List<QualityCheckDiseInfo> fillDiseInfoFromSheet(List<CaseFrontsheetDisdiag> diagList) {
        List<QualityCheckDiseInfo> list = new ArrayList<>();
        for (CaseFrontsheetDisdiag diag : diagList) {
            if (StringUtil.isBlank(diag.getName())) {
                break;
            }
            QualityCheckDiseInfo diseInfo = new QualityCheckDiseInfo();
            diseInfo.setOrder(String.valueOf(diag.getNo()));
            diseInfo.setDagnsType(diag.getNo() == 1 ? "1" : "2");
            diseInfo.setDiseaseName(diag.getName());
            diseInfo.setIcdCode(diag.getCode());
            diseInfo.setDagnsCondiId(String.valueOf(diag.getAdmissStatus()));
            diseInfo.setDagnsCondi(filterDiagnoseCondition(diag.getAdmissStatus()));
            list.add(diseInfo);
        }
        return list;
    }

    public static List<QualityCheckOprtInfo> fillOprtInfoFromSheet(List<CaseFrontsheetSurgery> surgeryList,
                                                                   Map<String, CodeName> anstWaysMap) {
        List<QualityCheckOprtInfo> list = new ArrayList<>();
        for (CaseFrontsheetSurgery surgery : surgeryList) {
            if (StringUtil.isBlank(surgery.getName())) {
                break;
            }
            QualityCheckOprtInfo oprtInfo = new QualityCheckOprtInfo();
            oprtInfo.setOrder(surgery.getNo());
            oprtInfo.setOprtCode(surgery.getCode());
            oprtInfo.setOprtName(surgery.getName());
            oprtInfo.setOprtType(surgery.getNo() == 1 ? "1" : "2");
            oprtInfo.setOprtDate(DateUtil.formatDatetime(surgery.getDate(), DateUtil.DATE));
            oprtInfo.setOprtCutId(reflectCutId(surgery.getCut()));
            oprtInfo.setOprtCut(filterCutName(oprtInfo.getOprtCutId()));
            oprtInfo.setHealId(reflectHealId(surgery.getHeal()));
            oprtInfo.setHeal(filterHealName(oprtInfo.getHealId()));
            oprtInfo.setOprtDoctorId(surgery.getOperator());
            oprtInfo.setOprtDoctor(surgery.getOperatorName());
            CodeName anstWay = anstWaysMap.get(surgery.getAnaesthesia());
            if (null != anstWay) {
                oprtInfo.setAnthsaModeId(anstWay.getYbCode());
                oprtInfo.setAnthsaMode(anstWay.getName());
            }
            oprtInfo.setFirstAssId(surgery.getAssistantOne());
            oprtInfo.setFirstAss(surgery.getAssistantOneName());
            oprtInfo.setSecondAssId(surgery.getAssistantTwo());
            oprtInfo.setSecondAss(surgery.getAssistantTwoName());
            oprtInfo.setAnthsaDoctorId(surgery.getAnaesthesiaor());
            oprtInfo.setAnthsaDoctor(surgery.getAnaesthesiaorName());
            oprtInfo.setOprtLevelId(surgery.getLevel());
            oprtInfo.setOprtLevel(filterOprtLevelName(surgery.getLevel()));
            oprtInfo.setIsAddPort(null);
            oprtInfo.setPlanOprtId(null);
            oprtInfo.setPlanOprt(null);
            list.add(oprtInfo);
        }
        return list;
    }

    public static String filterAdmissAgainName(String code) {
        if (StringUtil.isBlank(code)) {
            return "";
        }
        return code.equals("1") ? "无" : "有";
    }

    public static String filterAdmissWayName(String code) {
        if (StringUtil.isBlank(code)) {
            return "";
        }
        switch (code) {
            case "1":
                return "急诊";
            case "2":
                return "门诊";
            case "3":
                return "其他医疗机构转入";
            case "9":
                return "其他";
            default:
                return "";
        }
    }

    public static String reflectPayMethod(String code) {
        if (StringUtil.isBlank(code)) {
            return "";
        }
        switch (code) {
            case "1":
                return "01";
            case "2":
                return "02";
            case "3":
                return "03";
            case "4":
                return "04";
            case "5":
                return "05";
            case "6":
                return "06";
            case "7":
                return "07";
            case "8":
                return "08";
            case "9":
                return "99";
            default:
                return "";
        }
    }

    public static String filterPayMethodName(String code) {
        if (StringUtil.isBlank(code)) {
            return "";
        }
        switch (code) {
            case "1":
                return "城镇职工基本医疗保险";
            case "2":
                return "城镇居民基本医疗保险";
            case "3":
                return "新型农村合作医疗";
            case "4":
                return "贫困救助";
            case "5":
                return "商业医疗保险";
            case "6":
                return "全公费";
            case "7":
                return "全自费";
            case "8":
                return "其他社会保险";
            case "9":
                return "其他";
            default:
                return "";
        }
    }

    public static String reflectDismissWay(String code) {
        if (StringUtil.isBlank(code)) {
            return "";
        }
        switch (code) {
            case "0":
                return "1";
            case "1":
                return "2";
            case "2":
                return "3";
            case "3":
                return "4";
            case "4":
                return "5";
            case "5":
                return "9";
            default:
                return "";
        }
    }

    public static String filterDismissWayName(String code) {
        if (StringUtil.isBlank(code)) {
            return "";
        }
        switch (code) {
            case "0":
                return "医嘱离院";
            case "1":
                return "医嘱转院";
            case "2":
                return "医嘱转社区卫生服务机构/乡镇卫生院";
            case "3":
                return "非医嘱离院";
            case "4":
                return "死亡";
            case "5":
                return "其他";
            default:
                return "";
        }
    }

    public static String reflectMarriage(String code) {
        if (StringUtil.isBlank(code)) {
            return "";
        }
        switch (code) {
            case "1":
                return "10";
            case "2":
                return "20";
            case "3":
                return "30";
            case "4":
                return "40";
            case "9":
                return "90";
            default:
                return "";
        }
    }

    public static String filterMarriageName(String code) {
        if (StringUtil.isBlank(code)) {
            return "";
        }
        switch (code) {
            case "1":
                return "未婚";
            case "2":
                return "已婚";
            case "3":
                return "丧偶";
            case "4":
                return "离婚";
            case "9":
                return "其他";
            default:
                return "";
        }
    }

    public static String filterOprtLevelName(String level) {
        if (StringUtil.isBlank(level)) {
            return "";
        }
        switch (level) {
            case "1":
                return "一级手术";
            case "2":
                return "二级手术";
            case "3":
                return "三级手术";
            case "4":
                return "四级手术";
            default:
                return "";
        }
    }

    public static String reflectHealId(String heal) {
        if (StringUtil.isBlank(heal)) {
            heal = "0";
        }
        return heal.equals("0") ? "9" : heal;
    }

    public static String filterHealName(String healId) {
        if (StringUtil.isBlank(healId)) {
            return "";
        }
        switch (healId) {
            case "1":
                return "甲";
            case "2":
                return "乙";
            case "3":
                return "丙";
            case "9":
                return "未知";
        }
        return "";
    }

    public static String reflectCutId(String cut) {
        if (StringUtil.isBlank(cut)) {
            return "1";
        }
        return String.valueOf(Integer.parseInt(cut) + 1);
    }

    public static String filterCutName(String cutId) {
        if (StringUtil.isBlank(cutId)) {
            return "";
        }
        switch (cutId) {
            case "1":
                return "0";
            case "2":
                return "Ⅰ";
            case "3":
                return "Ⅱ";
            case "4":
                return "Ⅲ";
        }
        return "";
    }

    public static String filterDiagnoseCondition(Integer condition) {
        if (null == condition) {
            return "";
        }
        switch (condition) {
            case 1:
                return "有";
            case 2:
                return "临床未确定";
            case 3:
                return "情况不明";
            case 4:
                return "无";
            default:
                return "";
        }
    }
}
