UserNameConverter.java 863 Bytes
package vion.config;

import cn.hutool.core.lang.Opt;
import com.github.liaochong.myexcel.core.converter.CustomWriteContext;
import com.github.liaochong.myexcel.core.converter.CustomWriteConverter;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import vion.model.User;

/**
 * @author HlQ
 * @date 2024/1/3
 */
@Component
@RequiredArgsConstructor
public class UserNameConverter implements CustomWriteConverter<Long, String> {

    private final RedisTemplate redisTemplate;

    @Override
    public String convert(Long originalData, CustomWriteContext customWriteContext) {
        return Opt.ofNullable(((User) redisTemplate.opsForValue().get("dingtalk:user:" + originalData)))
                .map(User::getUsername)
                .orElse("未知");
    }
}