package org.ta.authserver.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.ta.authserver.mapper.UserMapper;
import org.ta.authserver.service.UserService;
import org.ta.commoncore.model.User;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    PasswordEncoder passwordEncoder;

    @Override
    public User getByUsername(String username) {
        List<User> users = baseMapper.selectByUsername(username);
        if (!users.isEmpty()) return users.get(0);
        return null;
    }

    @Override
    public int addUser(User user) {
        String encode = passwordEncoder.encode(user.getPassword());
        user.setPassword(encode);
        user.setEnabled(true);
        return baseMapper.insert(user);
    }
}
