package com.ym.mec.biz.service.impl;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.service.SysUserService;
import com.ym.mec.common.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 Integer getUserId(){
        return Optional.ofNullable(sysUserFeignService.queryUserInfo())
                .map(SysUser::getId)
                .orElseThrow(() -> new BizException("请登录"));
    }

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

    @Override
    public SysUser queryUserById(Integer userId) {
        SysUser user = sysUserFeignService.queryUserById(userId);
        Optional.ofNullable(user)
                .map(SysUser::getId)
                .orElseThrow(() -> new BizException("用户信息获取失败"));
        return user;
    }

    @Override
    public Boolean updateSysUser(SysUser user) {
        return sysUserFeignService.updateSysUser(user);
    }
}