package com.microservices.zone.common.service.impl;

import com.microservices.common.core.constant.SecurityConstants;
import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.httpClient.util.GitLinkRequestHelper;
import com.microservices.system.api.RemoteUserService;
import com.microservices.system.api.domain.SimpleSysUser;
import com.microservices.system.api.domain.SysUser;
import com.microservices.system.api.utils.FeignUtils;
import com.microservices.zone.common.service.IZoneCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author OTTO
 */
@Service
public class ZoneCommonServiceImpl implements IZoneCommonService {
    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private GitLinkRequestHelper gitLinkRequestHelper;

    @Override
    public SimpleSysUser getSimpleSysUserByUserId(Long userId) {
        SysUser sysUser = getSysUserByUserId(userId);
        return sysUser.toSimpleSysUser(gitLinkRequestHelper.gitLinkUrl);
    }

    private SysUser getSysUserByUserId(Long userId) {
        if (userId == null) {
            throw new ServiceException("用户Id不能为空");
        }
        SysUser sysUser = FeignUtils.getReturnData(
                remoteUserService.getSysUserByUserId(userId, SecurityConstants.INNER)
        );
        if (sysUser == null) {
            throw new ServiceException("该用户不存在(用户Id[%d])", userId);
        }
        return sysUser;
    }
}
