package cn.reghao.tnb.file.app.service;

import cn.reghao.file.api.iface.OssService;
import cn.reghao.oss.sdk.model.dto.media.ImageInfo;
import cn.reghao.tnb.account.api.iface.AccountQuery;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.file.app.model.dto.AvatarUpdate;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * @author reghao
 * @date 2024-12-06 10:55:13
 */
@Service
public class AvatarService {
    @DubboReference(check = false, timeout = 60_000)
    private AccountQuery accountQuery;
    private final OssService ossService;

    public AvatarService(OssService ossService) {
        this.ossService = ossService;
    }

    public String updateAvatar(AvatarUpdate avatarUpdate) throws Exception {
        int channelId = avatarUpdate.getChannelId();
        String uploadId = avatarUpdate.getUploadId();
        ImageInfo imageInfo = ossService.getImageInfo(channelId, uploadId);
        if (imageInfo != null) {
            String avatarUrl = imageInfo.getUrl();
            long loginUser = UserContext.getUserId();
            accountQuery.updateAvatar(loginUser, avatarUrl);
            return avatarUrl;
        }

        return null;
    }
}
