package com.tashow.cloud.sso.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tashow.cloud.sso.mapper.SystemUserMapper;
import com.tashow.cloud.sso.model.SystemUser;
import com.tashow.cloud.sso.service.SystemUserService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;

/**
 * 系统用户服务实现类
 */
@Service
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser> implements SystemUserService {

    private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();


    @Override
    public SystemUser  getUserByUsername(String username) {
        return getOne(new LambdaQueryWrapper<SystemUser>()
                .eq(SystemUser::getUsername, username)
                .eq(SystemUser::getStatus, 0)
                .eq(SystemUser::getDeleted, 0));
    }

    @Override
    public boolean validatePassword(String rawPassword, String encodedPassword) {
        return passwordEncoder.matches(rawPassword, encodedPassword);
    }


    @Override
    public void updateLoginInfo(Long userId, String ip) {
        SystemUser user = new SystemUser();
        user.setId(userId);
        user.setLoginIp(ip);
        LocalDateTime now = LocalDateTime.now();
        user.setLoginDate(now);
        user.setUpdateTime(now);
        updateById(user);
    }


}

