package com.fdkankan.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fdkankan.common.util.JwtUtil;
import com.fdkankan.redis.util.RedisUtil;
import com.fdkankan.ucenter.entity.LoginLog;
import com.fdkankan.ucenter.mapper.ILoginLogMapper;
import com.fdkankan.ucenter.service.ILoginLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.ucenter.util.DateUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2022-09-28
 */
@Service
public class LoginLogServiceImpl extends ServiceImpl<ILoginLogMapper, LoginLog> implements ILoginLogService {

    @Autowired
    RedisUtil redisUtil;

    @Override
    public synchronized void addLog(String clientIP, String token) {
        try {
            String username = JwtUtil.getUsername(token);
            LambdaQueryWrapper<LoginLog> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(LoginLog::getUserName,username);
            wrapper.between(LoginLog::getCreateTime, DateUserUtil.getZeroDate(),DateUserUtil.getLastDate());
            LoginLog one = this.getOne(wrapper);
            if(one == null){
                one = new LoginLog();
                one.setUserName(username);
                one.setIp(clientIP);
                this.save(one);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
