package com.starry.admin.modules.system.service.impl;

import com.starry.admin.common.domain.LoginUser;
import com.starry.admin.modules.system.module.entity.UserOnline;
import com.starry.admin.modules.system.service.UserOnlineService;
import com.starry.common.utils.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @author admin
 * @since 2022/10/18
 */
@Service
public class UserOnlineServiceImpl implements UserOnlineService {

    @Override
    public UserOnline selectOnlineByInfo(String ipaddr, String userName, LoginUser user) {
        if (StringUtils.equals(ipaddr, user.getIpaddr()) && StringUtils.equals(userName, user.getUsername())) {
            return setUserOnline(user);
        }
        return null;
    }

    @Override
    public UserOnline selectOnlineByIpaddr(String ipaddr, LoginUser user) {
        if (StringUtils.equals(ipaddr, user.getIpaddr())) {
            return setUserOnline(user);
        }
        return null;
    }

    @Override
    public UserOnline selectOnlineByUserName(String userName, LoginUser user) {
        if (StringUtils.equals(userName, user.getUsername())) {
            return setUserOnline(user);
        }
        return null;
    }

    @Override
    public UserOnline setUserOnline(LoginUser user) {
        if (StringUtils.isNull(user) || StringUtils.isNull(user.getUser())) {
            return null;
        }
        UserOnline userOnline = new UserOnline();
        userOnline.setTokenId(user.getToken());
        userOnline.setUserName(user.getUsername());
        userOnline.setIpaddr(user.getIpaddr());
        userOnline.setLoginLocation(user.getLoginLocation());
        userOnline.setBrowser(user.getBrowser());
        userOnline.setOs(user.getOs());
        userOnline.setLoginTime(user.getLoginTime());
        if (StringUtils.isNotNull(user.getUser().getDept())) {
            userOnline.setDeptName(user.getUser().getDept().getDeptName());
        }
        return userOnline;
    }
}
