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

import com.microservices.cms.common.service.ISysUserService;
import com.microservices.common.core.constant.SecurityConstants;
import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.security.utils.SecurityUtils;
import com.microservices.system.api.RemoteUserService;
import com.microservices.system.api.domain.SysUser;
import com.microservices.system.api.model.LoginUser;
import com.microservices.system.api.utils.FeignUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author otto
 */
@Service
public class SysUserServiceImpl implements ISysUserService {

    @Autowired
    private RemoteUserService remoteUserService;

    @Override
    public SysUser selectUserByUserName(String username) {
        return FeignUtils.getReturnData(remoteUserService.getSysUserByUserName(username, SecurityConstants.INNER));
    }

    @Override
    public SysUser selectUserById(Long userId) {
        SysUser sysUser = FeignUtils.getReturnData(remoteUserService.getSysUserByUserId(userId, SecurityConstants.INNER));
        if (sysUser == null) {
            throw new ServiceException("该用户不存在(用户id[" + userId + "])");
        }
        return sysUser;
    }

    @Override
    public SysUser getCurrentUser() {
        //通过安全工具类获取当前登录用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null || loginUser.getSysUser() == null) {
            throw new ServiceException("用户未登录");
        }
        return loginUser.getSysUser();
    }
}
