package com.energy.manage.service.test;

import com.energy.manage.common.util.UUIDTools;
import com.energy.manage.service.domain.dto.analysis.AnalysisAlgorithmDto;
import com.energy.manage.service.domain.dto.analysis.AnalysisDto;

import java.lang.reflect.Field;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chy
 * @date 2024/5/31 10:26
 * @desc
 */
public class TestClass {

    public static void main(String[] args) {
        // 本地时间
//        int lastHour = LocalDateTime.now().minusHours(1).getHour();

// 带时区（中国时区）
        int lastHour = ZonedDateTime.now(ZoneId.of("Asia/Shanghai")).minusHours(1).getHour();
        System.out.println(lastHour);
    }

    /**
     * 递归处理引用类型
     * @param analysis
     * @param algorithm
     * @param analysisField
     * @param algorithmFields
     * @throws IllegalAccessException
     */
    private static void setValue(AnalysisDto analysis,AnalysisAlgorithmDto algorithm,Field analysisField,Field[] algorithmFields) throws IllegalAccessException, NoSuchFieldException {
        analysisField.setAccessible(true);
        String fieldName = analysisField.getName();
        for(Field algorithmField : algorithmFields){
            if(!fieldName.equals(algorithmField.getName()) && !isBaseType(algorithmField.getType())){
                algorithmField.setAccessible(true);
                Object algorithmObj = algorithmField.get(algorithm);
                algorithmObj = algorithmObj == null ? algorithmField.getClass().getInterfaces() : algorithmObj;
                inspectReferenceType(analysis,algorithmField,fieldName,analysisField,algorithmObj);
            }
        }
    }

    /**
     * 递归检查引用类型属性
     * @param algorithmField
     * @param fieldName
     */
    private static void inspectReferenceType(AnalysisDto analysis,Field algorithmField,String fieldName,Field analysisField,Object algorithmObj) throws IllegalAccessException, NoSuchFieldException {
        Class<?> algorithmClazz = algorithmField.getType();
        Field[] fields = algorithmClazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            Class<?> fieldType = field.getType();
            String algorithmFieldName = field.getName();
            if(fieldName.equals(field.getName())){
                Object value = analysisField.get(analysis);
//                String fieldTypeName = field.getDeclaringClass().getName();

//                setFieldValue(algorithmObj,fieldTypeName,value);
                field.set(field.get(algorithmObj),value);
            }
            if (!isBaseType(fieldType)) {
                algorithmObj = algorithmObj == null ? fieldType.getInterfaces() : algorithmObj;
                // 递归检查嵌套引用类型的属性
                 inspectReferenceType(analysis,field,fieldName,analysisField,algorithmObj);
            }
        }
    }


    public static void setFieldValue(Object obj, String fieldPath, Object value) throws NoSuchFieldException, IllegalAccessException {
        String[] fields = fieldPath.split("\\.");
        for (int i = 0; i < fields.length - 1; i++) {
            Field field = obj.getClass().getDeclaredField(fields[i]);
            field.setAccessible(true);
            obj = field.get(obj); // 移动到下一层引用
        }
        // 最后一个字段，设置值
        Field lastField = obj.getClass().getDeclaredField(fields[fields.length - 1]);
        lastField.setAccessible(true);
        lastField.set(obj, value);
    }

    private static boolean isBaseType(Class<?> clazz) {
        return clazz.isPrimitive() || clazz == String.class || clazz == Integer.class || clazz == List.class;
    }


}
