package com.ruoyi.interceptor;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.cache.DictCache;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.*;

import java.lang.reflect.Field;
import java.sql.Statement;
import java.util.List;
import java.util.Properties;

/**
 * Mybatis字典拦截器
 *
 * @author 戴鹏涛
 * @since 2023-09-14 9:16
 */
@Intercepts({@Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class})})
public class DictionaryInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object result = invocation.proceed();

        if (result instanceof List) {
            List<?> resultList = (List<?>) result;

            // 遍历查询结果集
            for (Object obj : resultList) {
                // 获取实体类类型
                Class<?> entityClass = obj.getClass();

                // 遍历实体类的字段
                for (Field field : entityClass.getDeclaredFields()) {
                    // 判断字段是否为Integer类型
                    if (field.getType().equals(Integer.class)) {
                        field.setAccessible(true);
                        //将字段的值进行翻译，并回显至对应的Cn字段
                        String fieldName = field.getName();
                        Object dictValue = field.get(obj);
                        String dictType = StrUtil.toUnderlineCase(fieldName);
                        String dictLabel = DictCache.getDictLabel(StrUtil.format("{}-{}", dictType, dictValue));
                        try {
                            ReflectUtil.setFieldValue(obj, fieldName + "Cn", dictLabel);
                        } catch (Exception ignored) {
                        }
                    }
                    // 判断字段名是否为passTime
                    if (StrUtil.equals(field.getName(), "passTime")) {
                        field.setAccessible(true);
                        Long passTime = (Long) field.get(obj);
                        try {
                            ReflectUtil.setFieldValue(obj, field.getName() + "Cn", DateUtil.format(DateUtil.date(passTime * 1000), DatePattern.NORM_DATETIME_PATTERN));
                        } catch (Exception ignored) {
                        }

                    }

                    if (StrUtil.endWith(field.getName(), "Time") && field.getType().equals(Long.class)) {
                        field.setAccessible(true);
                        field.setAccessible(true);
                        //将字段的值进行翻译，并回显至对应的Cn字段
                        Object value = field.get(obj);
                        try {
                            String label = DateUtil.date((Long) value * 1000).toString(DatePattern.NORM_DATETIME_PATTERN);
                            ReflectUtil.setFieldValue(obj, field.getName() + "Cn", label);
                        } catch (Exception ignored) {
                        }
                    }

                }
            }
        }
        return result;
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
    }

}
