package org.dromara.web.domain.vo.hospitalRecord;

import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import org.dromara.web.domain.NutritionEvaluation;
import org.dromara.web.domain.NutritionScreening;
import org.dromara.web.domain.*;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class EntityFieldRegistry {
    private static final Map<Class<?>, Function<String, ? extends SFunction<?, ?>>> FIELD_MAPPINGS = new HashMap<>();

    // 注册实体类的字段映射
    public static <T> void register(Class<T> entityClass, Function<String, SFunction<T, ?>> fieldResolver) {
        FIELD_MAPPINGS.put(entityClass, fieldResolver);
    }

    // 获取字段映射（类型安全）
    @SuppressWarnings("unchecked")
    public static <T> SFunction<T, ?> getField(Class<T> entityClass, String fieldName) {
        Function<String, SFunction<T, ?>> resolver = (Function<String, SFunction<T, ?>>) FIELD_MAPPINGS.get(entityClass);
        if (resolver == null) {
            throw new IllegalArgumentException("Unsupported entity class: " + entityClass);
        }
        return resolver.apply(fieldName);
    }

    static {
        register(NutritionDiagnosis.class, fieldName -> {
            switch (fieldName) {
                case "id": return NutritionDiagnosis::getId;
                case "type": return NutritionDiagnosis::getType;
                case "deptId": return NutritionDiagnosis::getDeptId;
                case "patientId": return NutritionDiagnosis::getTreatmentUserId;
                case "createBy": return NutritionDiagnosis::getCreateBy;
                case "outpatientNo": return NutritionDiagnosis::getOutpatientNo;
                case "createTime": return NutritionDiagnosis::getCreateTime;
                default: throw new IllegalArgumentException("Unsupported field: " + fieldName);
            }
        });
        register(HospitalMealPlan.class, fieldName -> {
            switch (fieldName) {
                case "id": return HospitalMealPlan::getId;
                case "type": return HospitalMealPlan::getType;
                case "deptId": return HospitalMealPlan::getDeptId;
                case "patientId": return HospitalMealPlan::getPatientId;
                case "createBy": return HospitalMealPlan::getCreateBy;
                case "outpatientNo": return HospitalMealPlan::getOutpatientNo;
                case "createTime": return HospitalMealPlan::getCreateTime;
                default: throw new IllegalArgumentException("Unsupported field: " + fieldName);
            }
        });
        register(DailyMealPlan.class, fieldName -> {
            switch (fieldName) {
                case "id": return DailyMealPlan::getId;
                case "type": return DailyMealPlan::getType;
                case "deptId": return DailyMealPlan::getDeptId;
                case "patientId": return DailyMealPlan::getPatientId;
                case "createBy": return DailyMealPlan::getCreateBy;
                case "outpatientNo": return DailyMealPlan::getOutpatientNo;
                case "createTime": return DailyMealPlan::getCreateTime;
                default: throw new IllegalArgumentException("Unsupported field: " + fieldName);
            }
        });
        register(NutritionScreening.class, fieldName -> {
            switch (fieldName) {
                case "id": return NutritionScreening::getId;
                case "type": return NutritionScreening::getVisitType;
//                case "deptId": return NutritionScreening::getDetpId;
                case "patientId": return NutritionScreening::getPatientId;
                case "createBy": return NutritionScreening::getCreateBy;
                case "outpatientNo": return NutritionScreening::getPatientNo;
                case "createTime": return NutritionScreening::getCreateTime;
                default: throw new IllegalArgumentException("Unsupported field: " + fieldName);
            }
        });
        register(NutritionEvaluation.class, fieldName -> {
            switch (fieldName) {
                case "id": return NutritionEvaluation::getId;
                case "type": return NutritionEvaluation::getVisitType;
//                case "deptId": return NutritionScreening::getDetpId;
                case "patientId": return NutritionEvaluation::getPatientId;
                case "createBy": return NutritionEvaluation::getCreateBy;
                case "outpatientNo": return NutritionEvaluation::getPatientNo;
                case "createTime": return NutritionEvaluation::getCreateTime;
                default: throw new IllegalArgumentException("Unsupported field: " + fieldName);
            }
        });
        register(Settlement.class, fieldName -> {
            switch (fieldName) {
                case "id": return Settlement::getId;
                case "type": return Settlement::getVisitType;
                case "deptId": return Settlement::getDoorId;
                case "patientId": return Settlement::getPatientId;
                case "createBy": return Settlement::getCreateBy;
                case "outpatientNo": return Settlement::getPatientNo;
                case "createTime": return Settlement::getCreateTime;
                default: throw new IllegalArgumentException("Unsupported field: " + fieldName);
            }
        });
    }
}
