package com.example.system.service.impl;

import com.example.system.mapper.UserMapper;
import com.example.system.model.RegistrationTable;
import com.example.system.model.UserTable;
import com.example.system.repository.UserRepository;
import com.example.system.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    private final UserMapper userMapper;
    private final BCryptPasswordEncoder passwordEncoder;

    //管理员更新用户信息
    @Transactional
    @Override
    public boolean updateOther(UserTable user) {
        int tmp = userMapper.updateOther(user);
        if (tmp == 1) {
            return true;
        }
        else {
            return false;
        }
    }

    //获取用户总数量
    @Override
    public Object getCount() {
        return userMapper.getCount();
    }

    //获取老师总数量
    @Override
    public Object getTeacherCount() {
        return userMapper.getTeacherCount();
    }

    //获取学生总数量
    @Override
    public Object getStudentCount() {
        return userMapper.getStudentCount();
    }

    @Autowired
    public UserServiceImpl(UserMapper userMapper, BCryptPasswordEncoder passwordEncoder) {
        this.userMapper = userMapper;
        this.passwordEncoder = passwordEncoder;
    }
    @Autowired
    private UserRepository userRepository; // 假设这是你的JPA仓库



    @Transactional(rollbackFor = Exception.class) // 添加事务管理，遇到异常时回滚
    public boolean createUser(UserTable user) {
        try {
            if (userMapper.findBySTId(user.getSTId()) != null) {
                return 0 != userMapper.updateUserStatus(user.getSTId());
            }
            userMapper.createUser(user);
            return true;
        } catch (Exception e) {
            // 异常处理，记录日志等
            System.out.println(e);
            return false;
        }
    }

    @Override
    public boolean createRegistration(RegistrationTable registration) {
        return false;
    }

    @Override
    public boolean updateUser(UserTable user) {
        return 1 == userMapper.updateUser(user);
    }

    @Override
    public UserTable validateUserLogin(String userName, String password) {
        UserTable user = findUserByUserName(userName);
        if (user != null && passwordEncoder.matches(password, user.getUserPassword())) {
            return user;
        }
        return null;
    }

    @Override
    public UserTable findUserByUserName(String userName) {
        return userMapper.findUserByUserName(userName);
    }

    @Override
    public UserTable getUserInfo(String studentId) {
        return null;
    }

    @Override
    public boolean updateStudentInfo(UserTable student) {
        return false;
    }

    @Override
    public UserTable findBySTId(String sTId) {
        return userMapper.findBySTId(sTId);
    }

    @Override
    public boolean checkPassword(String rawPassword, String storedPassword) {
        // 直接比较原始密码和数据库中存储的密码是否相同
        return rawPassword.equals(storedPassword);
    }

    @Override
    public UserTable findUserById(Long userId) {
        return userMapper.findUserById(userId);
    }

    @Override
    public List<UserTable> findAllUsers() {
        return userMapper.findAllUsers();
    }


    @Override
    public int getUserPrivileges(String username) {
        UserTable user = findUserByUserName(username);
        if (user != null) {
            return user.getUserPrivileges();
        }
        return -1; // Or other error code or handling mechanism
    }

    @Override
    public String getPhoneNumberByUserId(Integer userId) {
        return null;
    }

    @Override
    public List<Integer> getUserIdsByCompetitionId(Integer competitionId) {
        return null;
    }

    // Assuming implementation for createRegistration and other methods if needed

    @Override
    @Transactional
    public void importUsers(List<UserTable> users) {
        userRepository.saveAll(users);
    }

    @Override
    //批量删除用户
    @Transactional
    public Boolean deleteList(List<Integer> list) {
        int size = list.size();
        if (size == userMapper.deleteList(list)) {
            return true;
        }
        else{
            log.info("删除数量于前端传入数量不同");
            throw new NullPointerException();
        }

    }
}