package com.inspect.system.service.impl;

import com.inspect.base.core.utils.StringUtils;
import com.inspect.system.base.model.LoginUser;
import com.inspect.system.domain.SysUserOnline;
import com.inspect.system.service.ISysUserOnlineService;
import org.springframework.stereotype.Service;

@Service
public class SysUserOnlineServiceImpl implements ISysUserOnlineService {
    public SysUserOnline selectOnlineByIpaddr(String ipaddr, LoginUser user) {
        return StringUtils.equals(ipaddr, user.getIpaddr()) ? this.loginUserToUserOnline(user) : null;
    }

    public SysUserOnline selectOnlineByUserName(String userName, LoginUser user) {
        return StringUtils.equals(userName, user.getUsername()) ? this.loginUserToUserOnline(user) : null;
    }

    public SysUserOnline selectOnlineByInfo(String ipaddr, String userName, LoginUser user) {
        return StringUtils.equals(ipaddr, user.getIpaddr()) && StringUtils.equals(userName, user.getUsername()) ? this.loginUserToUserOnline(user) : null;
    }

    public SysUserOnline loginUserToUserOnline(LoginUser user) {
        if (StringUtils.isNull(user)) {
            return null;
        } else {
            SysUserOnline sysUserOnline = new SysUserOnline();
            sysUserOnline.setTokenId(user.getToken());
            sysUserOnline.setUserName(user.getUsername());
            sysUserOnline.setIpaddr(user.getIpaddr());
            sysUserOnline.setLoginTime(user.getLoginTime());
            return sysUserOnline;
        }
    }
}
