package com.yonge.cooleshow.biz.dal.service.impl;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.service.SysUserService;
import com.yonge.toolset.base.exception.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Override
    public Long getUserId() {
        return Optional.ofNullable(sysUserFeignService.queryUserInfo()).
                map(SysUser::getId).
                orElseThrow(()-> new BizException("请登录"));
    }

    @Override
    public SysUser getUser() {
        Optional<SysUser> optional = Optional.ofNullable(sysUserFeignService.queryUserInfo());
        optional.map(SysUser::getId).
                orElseThrow(()-> new BizException("请登录"));
        return optional.get();
    }

    @Override
    public SysUser findUserById(Long userId) {
        Optional<SysUser> optional = Optional.ofNullable(sysUserFeignService.queryUserById(userId));
        optional.map(SysUser::getId).
                orElseThrow(()-> new BizException("请登录"));
        return optional.get();
    }
}