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

import cn.reghao.file.api.dto.ObjectMeta;
import cn.reghao.file.api.iface.OssService;
import cn.reghao.tnb.auth.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.beans.factory.annotation.Qualifier;
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(@Qualifier("storeServiceWrapperRouter") OssService ossService) {
        this.ossService = ossService;
    }

    public String updateAvatar(AvatarUpdate avatarUpdate) throws Exception {
        String uploadId = avatarUpdate.getUploadId();
        ObjectMeta objectMeta = ossService.getObjectMeta(uploadId);
        if (objectMeta != null) {
            String avatarUrl = objectMeta.getUrl();
            long loginUser = UserContext.getUserId();
            accountQuery.updateAvatar(loginUser, avatarUrl);
            return avatarUrl;
        }

        return null;
    }
}
